Home | History | Annotate | Download | only in muc
      1 /**
      2  * $RCSfile$
      3  * $Revision:  $
      4  * $Date$
      5  *
      6  * Copyright 2003-2006 Jive Software.
      7  *
      8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      9  * you may not use this file except in compliance with the License.
     10  * You may obtain a copy of the License at
     11  *
     12  *     http://www.apache.org/licenses/LICENSE-2.0
     13  *
     14  * Unless required by applicable law or agreed to in writing, software
     15  * distributed under the License is distributed on an "AS IS" BASIS,
     16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     17  * See the License for the specific language governing permissions and
     18  * limitations under the License.
     19  */
     20 
     21 package org.jivesoftware.smackx.muc;
     22 
     23 import org.jivesoftware.smack.PacketInterceptor;
     24 import org.jivesoftware.smack.packet.Packet;
     25 import org.jivesoftware.smack.packet.PacketExtension;
     26 import org.jivesoftware.smack.packet.Presence;
     27 
     28 /**
     29  * Packet interceptor that will intercept presence packets sent to the MUC service to indicate
     30  * that the user wants to be a deaf occupant. A user can only indicate that he wants to be a
     31  * deaf occupant while joining the room. It is not possible to become deaf or stop being deaf
     32  * after the user joined the room.<p>
     33  *
     34  * Deaf occupants will not get messages broadcasted to all room occupants. However, they will
     35  * be able to get private messages, presences, IQ packets or room history. To use this
     36  * functionality you will need to send the message
     37  * {@link MultiUserChat#addPresenceInterceptor(org.jivesoftware.smack.PacketInterceptor)} and
     38  * pass this interceptor as the parameter.<p>
     39  *
     40  * Note that this is a custom extension to the MUC service so it may not work with other servers
     41  * than Wildfire.
     42  *
     43  * @author Gaston Dombiak
     44  */
     45 public class DeafOccupantInterceptor implements PacketInterceptor {
     46 
     47     public void interceptPacket(Packet packet) {
     48         Presence presence = (Presence) packet;
     49         // Check if user is joining a room
     50         if (Presence.Type.available == presence.getType() &&
     51                 presence.getExtension("x", "http://jabber.org/protocol/muc") != null) {
     52             // Add extension that indicates that user wants to be a deaf occupant
     53             packet.addExtension(new DeafExtension());
     54         }
     55     }
     56 
     57     private static class DeafExtension implements PacketExtension {
     58 
     59         public String getElementName() {
     60             return "x";
     61         }
     62 
     63         public String getNamespace() {
     64             return "http://jivesoftware.org/protocol/muc";
     65         }
     66 
     67         public String toXML() {
     68             StringBuilder buf = new StringBuilder();
     69             buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace())
     70                     .append("\">");
     71             buf.append("<deaf-occupant/>");
     72             buf.append("</").append(getElementName()).append(">");
     73             return buf.toString();
     74         }
     75     }
     76 }
     77