001/**
002 *
003 * Copyright 2003-2006 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.muc;
019
020import org.jivesoftware.smack.PacketInterceptor;
021import org.jivesoftware.smack.packet.Packet;
022import org.jivesoftware.smack.packet.PacketExtension;
023import org.jivesoftware.smack.packet.Presence;
024
025/**
026 * Packet interceptor that will intercept presence packets sent to the MUC service to indicate
027 * that the user wants to be a deaf occupant. A user can only indicate that he wants to be a
028 * deaf occupant while joining the room. It is not possible to become deaf or stop being deaf
029 * after the user joined the room.<p>
030 *
031 * Deaf occupants will not get messages broadcasted to all room occupants. However, they will
032 * be able to get private messages, presences, IQ packets or room history. To use this
033 * functionality you will need to send the message
034 * {@link MultiUserChat#addPresenceInterceptor(org.jivesoftware.smack.PacketInterceptor)} and
035 * pass this interceptor as the parameter.<p>
036 *
037 * Note that this is a custom extension to the MUC service so it may not work with other servers
038 * than Wildfire.
039 *
040 * @author Gaston Dombiak
041 */
042public class DeafOccupantInterceptor implements PacketInterceptor {
043
044    public void interceptPacket(Packet packet) {
045        Presence presence = (Presence) packet;
046        // Check if user is joining a room
047        if (Presence.Type.available == presence.getType() &&
048                presence.getExtension("x", "http://jabber.org/protocol/muc") != null) {
049            // Add extension that indicates that user wants to be a deaf occupant
050            packet.addExtension(new DeafExtension());
051        }
052    }
053
054    private static class DeafExtension implements PacketExtension {
055
056        public String getElementName() {
057            return "x";
058        }
059
060        public String getNamespace() {
061            return "http://jivesoftware.org/protocol/muc";
062        }
063
064        public String toXML() {
065            StringBuilder buf = new StringBuilder();
066            buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace())
067                    .append("\">");
068            buf.append("<deaf-occupant/>");
069            buf.append("</").append(getElementName()).append(">");
070            return buf.toString();
071        }
072    }
073}