1 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 31 public class MUCRoleImpl implements MUCRole { 32 33 36 private MUCRoomImpl room; 37 38 41 private MUCUserImpl user; 42 43 46 private String nick; 47 48 51 private Presence presence; 52 53 56 private MultiUserChatServer server; 57 58 61 private MUCRole.Role role; 62 63 66 private MUCRole.Affiliation affiliation; 67 68 71 private PacketRouter router; 72 73 76 private JID rJID; 77 78 81 private Element extendedInformation; 82 83 95 public MUCRoleImpl(MultiUserChatServer chatserver, MUCRoomImpl chatroom, String 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 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 if (MUCRole.Affiliation.owner == affiliation || MUCRole.Affiliation.admin == affiliation) { 139 if (MUCRole.Role.moderator != newRole) { 140 throw new NotAllowedException(); 141 } 142 } 143 if (MUCRole.Role.moderator == role && MUCRole.Role.none == newRole) { 145 throw new NotAllowedException(); 146 } 147 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 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 calculateExtendedInformation(); 172 } 173 174 public MUCRole.Affiliation getAffiliation() { 175 return affiliation; 176 } 177 178 public String getNickname() { 179 return nick; 180 } 181 182 public void changeNickname(String 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 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 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 |