aboutsummaryrefslogtreecommitdiff
path: root/src/org/jivesoftware/smackx/muc/DeafOccupantInterceptor.java
blob: 24570fd525675d35f00d8fbfef10682db68083fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * $RCSfile$
 * $Revision:  $
 * $Date$
 *
 * Copyright 2003-2006 Jive Software.
 *
 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jivesoftware.smackx.muc;

import org.jivesoftware.smack.PacketInterceptor;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;

/**
 * Packet interceptor that will intercept presence packets sent to the MUC service to indicate
 * that the user wants to be a deaf occupant. A user can only indicate that he wants to be a
 * deaf occupant while joining the room. It is not possible to become deaf or stop being deaf
 * after the user joined the room.<p>
 *
 * Deaf occupants will not get messages broadcasted to all room occupants. However, they will
 * be able to get private messages, presences, IQ packets or room history. To use this
 * functionality you will need to send the message
 * {@link MultiUserChat#addPresenceInterceptor(org.jivesoftware.smack.PacketInterceptor)} and
 * pass this interceptor as the parameter.<p>
 *
 * Note that this is a custom extension to the MUC service so it may not work with other servers
 * than Wildfire.
 *
 * @author Gaston Dombiak
 */
public class DeafOccupantInterceptor implements PacketInterceptor {

    public void interceptPacket(Packet packet) {
        Presence presence = (Presence) packet;
        // Check if user is joining a room
        if (Presence.Type.available == presence.getType() &&
                presence.getExtension("x", "http://jabber.org/protocol/muc") != null) {
            // Add extension that indicates that user wants to be a deaf occupant
            packet.addExtension(new DeafExtension());
        }
    }

    private static class DeafExtension implements PacketExtension {

        public String getElementName() {
            return "x";
        }

        public String getNamespace() {
            return "http://jivesoftware.org/protocol/muc";
        }

        public String toXML() {
            StringBuilder buf = new StringBuilder();
            buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace())
                    .append("\">");
            buf.append("<deaf-occupant/>");
            buf.append("</").append(getElementName()).append(">");
            return buf.toString();
        }
    }
}