1 11 12 package org.jivesoftware.messenger.muc.spi; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import org.dom4j.Element; 18 import org.jivesoftware.messenger.PacketRouter; 19 import org.jivesoftware.messenger.muc.ConflictException; 20 import org.jivesoftware.messenger.muc.ForbiddenException; 21 import org.jivesoftware.messenger.muc.MUCRole; 22 import org.jivesoftware.messenger.muc.NotAllowedException; 23 import org.jivesoftware.messenger.user.UserNotFoundException; 24 import org.xmpp.packet.IQ; 25 import org.xmpp.packet.JID; 26 import org.xmpp.packet.PacketError; 27 import org.xmpp.packet.Presence; 28 29 37 public class IQAdminHandler { 38 private MUCRoomImpl room; 39 40 private PacketRouter router; 41 42 public IQAdminHandler(MUCRoomImpl chatroom, PacketRouter packetRouter) { 43 this.room = chatroom; 44 this.router = packetRouter; 45 } 46 47 70 public void handleIQ(IQ packet, MUCRole role) throws ForbiddenException, ConflictException, 71 NotAllowedException { 72 IQ reply = IQ.createResultIQ(packet); 73 Element element = packet.getChildElement(); 74 75 List itemsList = element.elements("item"); 77 if (!itemsList.isEmpty()) { 78 handleItemsElement(role, itemsList, reply); 79 } 80 else { 81 reply.setChildElement(packet.getChildElement().createCopy()); 84 reply.setError(PacketError.Condition.bad_request); 85 } 86 if (reply.getTo() != null) { 87 router.route(reply); 90 } 91 } 92 93 108 private void handleItemsElement(MUCRole senderRole, List itemsList, IQ reply) 109 throws ForbiddenException, ConflictException, NotAllowedException { 110 Element item; 111 String affiliation = null; 112 String roleAttribute = null; 113 boolean hasJID = ((Element)itemsList.get(0)).attributeValue("jid") != null; 114 boolean hasNick = ((Element)itemsList.get(0)).attributeValue("nick") != null; 115 if (!hasJID && !hasNick) { 117 for (Iterator items = itemsList.iterator(); items.hasNext();) { 119 item = (Element)items.next(); 120 affiliation = item.attributeValue("affiliation"); 121 roleAttribute = item.attributeValue("role"); 122 Element result = reply.setChildElement("query", "http://jabber.org/protocol/muc#admin"); 125 126 Element metaData; 127 if ("outcast".equals(affiliation)) { 128 if (MUCRole.Affiliation.admin != senderRole.getAffiliation() 130 && MUCRole.Affiliation.owner != senderRole.getAffiliation()) { 131 throw new ForbiddenException(); 132 } 133 for (String jid : room.getOutcasts()) { 134 metaData = result.addElement("item", "http://jabber.org/protocol/muc#admin"); 135 metaData.addAttribute("affiliation", "outcast"); 136 metaData.addAttribute("jid", jid); 137 } 138 139 } 140 else if ("member".equals(affiliation)) { 141 if (!room.isMembersOnly() 144 && MUCRole.Affiliation.admin != senderRole.getAffiliation() 145 && MUCRole.Affiliation.owner != senderRole.getAffiliation()) { 146 throw new ForbiddenException(); 147 } 148 for (String jid : room.getMembers()) { 149 metaData = result.addElement("item", "http://jabber.org/protocol/muc#admin"); 150 metaData.addAttribute("affiliation", "member"); 151 metaData.addAttribute("jid", jid); 152 try { 153 List <MUCRole> roles = room.getOccupantsByBareJID(jid); 154 MUCRole role = roles.get(0); 155 metaData.addAttribute("role", role.getRole().toString()); 156 metaData.addAttribute("nick", role.getNickname()); 157 } 158 catch (UserNotFoundException e) { 159 } 161 } 162 } 163 else if ("moderator".equals(roleAttribute)) { 164 if (MUCRole.Affiliation.admin != senderRole.getAffiliation() 166 && MUCRole.Affiliation.owner != senderRole.getAffiliation()) { 167 throw new ForbiddenException(); 168 } 169 for (MUCRole role : room.getModerators()) { 170 metaData = result.addElement("item", "http://jabber.org/protocol/muc#admin"); 171 metaData.addAttribute("role", "moderator"); 172 metaData.addAttribute("jid", role.getChatUser().getAddress().toString()); 173 metaData.addAttribute("nick", role.getNickname()); 174 metaData.addAttribute("affiliation", role.getAffiliation().toString()); 175 } 176 } 177 else if ("participant".equals(roleAttribute)) { 178 if (MUCRole.Role.moderator != senderRole.getRole()) { 180 throw new ForbiddenException(); 181 } 182 for (MUCRole role : room.getParticipants()) { 183 metaData = result.addElement("item", "http://jabber.org/protocol/muc#admin"); 184 metaData.addAttribute("role", "participant"); 185 metaData.addAttribute("jid", role.getChatUser().getAddress().toString()); 186 metaData.addAttribute("nick", role.getNickname()); 187 metaData.addAttribute("affiliation", role.getAffiliation().toString()); 188 } 189 } 190 else { 191 reply.setError(PacketError.Condition.bad_request); 192 } 193 } 194 } 195 else { 196 JID jid = null; 198 String nick; 199 String target = null; 200 boolean hasAffiliation = ((Element) itemsList.get(0)).attributeValue("affiliation") != 201 null; 202 203 List <Presence> presences = new ArrayList <Presence>(itemsList.size()); 205 206 for (Iterator items = itemsList.iterator(); items.hasNext();) { 208 try { 209 item = (Element)items.next(); 210 target = (hasAffiliation ? item.attributeValue("affiliation") : item 211 .attributeValue("role")); 212 if (hasJID) { 215 jid = new JID(item.attributeValue("jid")); 216 nick = null; 217 } 218 else { 219 nick = item.attributeValue("nick"); 221 jid = room.getOccupant(nick).getChatUser().getAddress(); 222 } 223 224 room.lock.writeLock().lock(); 225 try { 226 if ("moderator".equals(target)) { 227 presences.add(room.addModerator(jid, senderRole)); 229 } 230 else if ("participant".equals(target)) { 231 presences.add(room.addParticipant(jid, 233 item.elementTextTrim("reason"), 234 senderRole)); 235 } 236 else if ("visitor".equals(target)) { 237 presences.add(room.addVisitor(jid, senderRole)); 239 } 240 else if ("member".equals(target)) { 241 boolean hadAffiliation = room.getAffiliation(jid.toBareJID()) != MUCRole.Affiliation.none; 243 presences.addAll(room.addMember(jid.toBareJID(), nick, senderRole)); 244 if (!hadAffiliation && room.isMembersOnly()) { 247 room.sendInvitation(jid, null, senderRole, null); 248 } 249 } 250 else if ("outcast".equals(target)) { 251 presences.addAll(room.addOutcast(jid.toBareJID(), item.elementTextTrim("reason"), senderRole)); 253 } 254 else if ("none".equals(target)) { 255 if (hasAffiliation) { 256 presences.addAll(room.addNone(jid.toBareJID(), senderRole)); 258 } 259 else { 260 if (MUCRole.Role.moderator != senderRole.getRole()) { 262 throw new ForbiddenException(); 263 } 264 presences.add(room.kickOccupant(jid, 265 senderRole.getChatUser().getAddress(), 266 item.elementTextTrim("reason"))); 267 } 268 } 269 else { 270 reply.setError(PacketError.Condition.bad_request); 271 } 272 } 273 finally { 274 room.lock.writeLock().unlock(); 275 } 276 } 277 catch (UserNotFoundException e) { 278 } 280 } 281 282 for (Presence presence : presences) { 284 room.send(presence); 285 } 286 } 287 } 288 } | Popular Tags |