KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > muc > spi > MUCRoleImpl


1 /**
2  * $RCSfile: MUCRoleImpl.java,v $
3  * $Revision: 1.14 $
4  * $Date: 2005/05/31 22:06:25 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.muc.spi;
13
14 import org.dom4j.Element;
15 import org.dom4j.DocumentHelper;
16 import org.dom4j.QName;
17 import org.jivesoftware.messenger.PacketRouter;
18 import org.jivesoftware.messenger.muc.MUCRole;
19 import org.jivesoftware.messenger.muc.MUCRoom;
20 import org.jivesoftware.messenger.muc.MUCUser;
21 import org.jivesoftware.messenger.muc.MultiUserChatServer;
22 import org.jivesoftware.messenger.muc.NotAllowedException;
23 import org.jivesoftware.util.ElementUtil;
24 import org.xmpp.packet.*;
25
26 /**
27  * Simple in-memory implementation of a role in a chatroom
28  *
29  * @author Gaston Dombiak
30  */

31 public class MUCRoleImpl implements MUCRole {
32
33     /**
34      * The room this role is valid in.
35      */

36     private MUCRoomImpl room;
37
38     /**
39      * The user of the role.
40      */

41     private MUCUserImpl user;
42
43     /**
44      * The user's nickname in the room.
45      */

46     private String JavaDoc nick;
47
48     /**
49      * The user's presence in the room.
50      */

51     private Presence presence;
52
53     /**
54      * The chatserver that hosts this role.
55      */

56     private MultiUserChatServer server;
57
58     /**
59      * The role.
60      */

61     private MUCRole.Role role;
62
63     /**
64      * The affiliation.
65      */

66     private MUCRole.Affiliation affiliation;
67
68     /**
69      * The router used to send packets from this role.
70      */

71     private PacketRouter router;
72
73     /**
74      * The address of the person masquerading in this role.
75      */

76     private JID rJID;
77
78     /**
79      * A fragment containing the x-extension for non-anonymous rooms.
80      */

81     private Element extendedInformation;
82
83     /**
84      * Create a new role.
85      *
86      * @param chatserver the server hosting the role.
87      * @param chatroom the room the role is valid in.
88      * @param nickname the nickname of the user in the role.
89      * @param role the role of the user in the room.
90      * @param affiliation the affiliation of the user in the room.
91      * @param chatuser the user on the chat server.
92      * @param presence the presence sent by the user to join the room.
93      * @param packetRouter the packet router for sending messages from this role.
94      */

95     public MUCRoleImpl(MultiUserChatServer chatserver, MUCRoomImpl chatroom, String JavaDoc nickname,
96             MUCRole.Role role, MUCRole.Affiliation affiliation, MUCUserImpl chatuser, Presence presence,
97             PacketRouter packetRouter)
98     {
99         this.room = chatroom;
100         this.nick = nickname;
101         this.user = chatuser;
102         this.server = chatserver;
103         this.router = packetRouter;
104         this.role = role;
105         this.affiliation = affiliation;
106         extendedInformation =
107                 DocumentHelper.createElement(QName.get("x", "http://jabber.org/protocol/muc#user"));
108         calculateExtendedInformation();
109         rJID = new JID(room.getName(), server.getServiceDomain(), nick);
110         setPresence(presence);
111     }
112
113     public Presence getPresence() {
114         return presence;
115     }
116
117     public Element getExtendedPresenceInformation() {
118         return extendedInformation;
119     }
120
121     public void setPresence(Presence newPresence) {
122         // Try to remove the element whose namespace is "http://jabber.org/protocol/muc" since we
123
// don't need to include that element in future presence broadcasts
124
Element element = newPresence.getElement().element(QName.get("x", "http://jabber.org/protocol/muc"));
125         if (element != null) {
126             newPresence.getElement().remove(element);
127         }
128         this.presence = newPresence;
129         this.presence.setFrom(getRoleAddress());
130         if (extendedInformation != null) {
131             extendedInformation.setParent(null);
132             presence.getElement().add(extendedInformation);
133         }
134     }
135
136     public void setRole(MUCRole.Role newRole) throws NotAllowedException {
137         // Don't allow to change the role to an owner or admin unless the new role is moderator
138
if (MUCRole.Affiliation.owner == affiliation || MUCRole.Affiliation.admin == affiliation) {
139             if (MUCRole.Role.moderator != newRole) {
140                 throw new NotAllowedException();
141             }
142         }
143         // A moderator cannot be kicked from a room
144
if (MUCRole.Role.moderator == role && MUCRole.Role.none == newRole) {
145             throw new NotAllowedException();
146         }
147         // TODO A moderator MUST NOT be able to revoke voice from a user whose affiliation is at or
148
// TODO above the moderator's level.
149

150         role = newRole;
151         if (MUCRole.Role.none == role) {
152             presence.setType(Presence.Type.unavailable);
153             presence.setStatus(null);
154         }
155         calculateExtendedInformation();
156     }
157
158     public MUCRole.Role getRole() {
159         return role;
160     }
161
162     public void setAffiliation(MUCRole.Affiliation newAffiliation) throws NotAllowedException {
163         // Don't allow to ban an owner or an admin
164
if (MUCRole.Affiliation.owner == affiliation || MUCRole.Affiliation.admin== affiliation) {
165             if (MUCRole.Affiliation.outcast == newAffiliation) {
166                 throw new NotAllowedException();
167             }
168         }
169         affiliation = newAffiliation;
170         // TODO The fragment is being calculated twice (1. setting the role & 2. setting the aff)
171
calculateExtendedInformation();
172     }
173
174     public MUCRole.Affiliation getAffiliation() {
175         return affiliation;
176     }
177
178     public String JavaDoc getNickname() {
179         return nick;
180     }
181
182     public void changeNickname(String JavaDoc nickname) {
183         this.nick = nickname;
184         setRoleAddress(new JID(room.getName(), server.getServiceDomain(), nick));
185     }
186
187     public MUCUser getChatUser() {
188         return user;
189     }
190
191     public MUCRoom getChatRoom() {
192         return room;
193     }
194
195     public JID getRoleAddress() {
196         return rJID;
197     }
198
199     private void setRoleAddress(JID jid) {
200         rJID = jid;
201         // Set the new sender of the user presence in the room
202
presence.setFrom(jid);
203     }
204
205     public void send(Packet packet) {
206         if (packet == null) {
207             return;
208         }
209         packet.setTo(user.getAddress());
210         router.route(packet);
211     }
212
213     /**
214      * Calculates and sets the extended presence information to add to the presence.
215      * The information to add contains the user's jid, affiliation and role.
216      */

217     private void calculateExtendedInformation() {
218         ElementUtil.setProperty(extendedInformation, "x.item:jid", user.getAddress().toString());
219         ElementUtil.setProperty(extendedInformation, "x.item:affiliation", affiliation.toString());
220         ElementUtil.setProperty(extendedInformation, "x.item:role", role.toString());
221     }
222 }
Popular Tags