1 11 12 package org.jivesoftware.messenger.muc.spi; 13 14 import java.util.*; 15 import java.util.concurrent.ConcurrentHashMap ; 16 17 import org.dom4j.Element; 18 19 import org.jivesoftware.messenger.muc.*; 20 import org.jivesoftware.util.*; 21 import org.jivesoftware.messenger.*; 22 import org.jivesoftware.messenger.auth.UnauthorizedException; 23 import org.jivesoftware.messenger.user.UserAlreadyExistsException; 24 import org.jivesoftware.messenger.user.UserNotFoundException; 25 import org.xmpp.packet.*; 26 27 33 public class MUCUserImpl implements MUCUser { 34 35 36 private MultiUserChatServer server; 37 38 39 private JID realjid; 40 41 42 private Map <String , MUCRole> roles = new ConcurrentHashMap <String , MUCRole>(); 43 44 45 private PacketRouter router; 46 47 50 private long lastPacketTime; 51 52 59 MUCUserImpl(MultiUserChatServerImpl chatserver, PacketRouter packetRouter, JID jid) { 60 this.realjid = jid; 61 this.router = packetRouter; 62 this.server = chatserver; 63 } 64 65 public long getID() { 66 return -1; 67 } 68 69 public MUCRole getRole(String roomName) throws NotFoundException { 70 MUCRole role = roles.get(roomName.toLowerCase()); 71 if (role == null) { 72 throw new NotFoundException(roomName); 73 } 74 return role; 75 } 76 77 public Iterator<MUCRole> getRoles() { 78 return Collections.unmodifiableCollection(roles.values()).iterator(); 79 } 80 81 public void removeRole(String roomName) { 82 roles.remove(roomName.toLowerCase()); 83 } 84 85 public long getLastPacketTime() { 86 return lastPacketTime; 87 } 88 89 95 private void sendErrorPacket(Packet packet, PacketError.Condition error) { 96 if (packet instanceof IQ) { 97 IQ reply = IQ.createResultIQ((IQ) packet); 98 reply.setChildElement(((IQ) packet).getChildElement().createCopy()); 99 reply.setError(error); 100 router.route(reply); 101 } 102 else { 103 Packet reply = packet.createCopy(); 104 reply.setError(error); 105 reply.setFrom(packet.getTo()); 106 reply.setTo(packet.getFrom()); 107 router.route(reply); 108 } 109 } 110 111 public JID getAddress() { 112 return realjid; 113 } 114 115 public void process(Packet packet) throws UnauthorizedException, PacketException { 116 if (packet instanceof IQ) { 117 process((IQ)packet); 118 } 119 else if (packet instanceof Message) { 120 process((Message)packet); 121 } 122 else if (packet instanceof Presence) { 123 process((Presence)packet); 124 } 125 } 126 127 141 public void process(Message packet) { 142 if (Message.Type.error == packet.getType()) { 144 return; 145 } 146 lastPacketTime = System.currentTimeMillis(); 147 JID recipient = packet.getTo(); 148 String group = recipient.getNode(); 149 if (group == null) { 150 Log.info(LocaleUtils.getLocalizedString("muc.error.not-supported") + " " 153 + packet.toString()); 154 } 155 else { 156 MUCRole role = roles.get(group.toLowerCase()); 157 if (role == null) { 158 if (server.hasChatRoom(group)) { 159 boolean declinedInvitation = false; 160 Element userInfo = null; 161 if (Message.Type.normal == packet.getType()) { 162 userInfo = packet.getChildElement( 164 "x", "http://jabber.org/protocol/muc#user"); 165 if (userInfo != null 166 && userInfo.element("decline") != null) { 167 declinedInvitation = true; 171 } 172 } 173 if (declinedInvitation) { 174 Element info = userInfo.element("decline"); 175 server.getChatRoom(group).sendInvitationRejection( 176 new JID(info.attributeValue("to")), 177 info.elementTextTrim("reason"), 178 packet.getFrom()); 179 } 180 else { 181 sendErrorPacket(packet, PacketError.Condition.not_acceptable); 183 } 184 } 185 else { 186 sendErrorPacket(packet, PacketError.Condition.recipient_unavailable); 188 } 189 } 190 else { 191 if (!role.getChatUser().getAddress().equals(packet.getFrom())) { 194 sendErrorPacket(packet, PacketError.Condition.conflict); 195 } 196 else { 197 try { 198 if (packet.getSubject() != null && packet.getSubject().trim().length() > 0 199 && Message.Type.groupchat == packet.getType()) { 200 role.getChatRoom().changeSubject(packet, role); 202 203 } 204 else { 205 Message.Type type = packet.getType(); 208 String resource = packet.getTo().getResource(); 209 if (resource == null || resource.trim().length() == 0) { 210 resource = null; 211 } 212 if (resource == null && Message.Type.groupchat == type) { 213 role.getChatRoom().sendPublicMessage(packet, role); 215 } 216 else if (resource != null 217 && (Message.Type.chat == type || Message.Type.normal == type)) { 218 role.getChatRoom().sendPrivateMessage(packet, role); 220 } 221 else if (resource == null && Message.Type.normal == type) { 222 Element userInfo = packet.getChildElement( 225 "x", 226 "http://jabber.org/protocol/muc#user"); 227 MUCRoomImpl room = (MUCRoomImpl) role.getChatRoom(); 230 if (userInfo != null && userInfo.element("invite") != null) { 231 Element info = userInfo.element("invite"); 233 234 if (room.isMembersOnly()) { 237 room.lock.writeLock().lock(); 238 try { 239 room.addMember(info.attributeValue("to"), null, role); 240 } 241 finally { 242 room.lock.writeLock().unlock(); 243 } 244 } 245 List<Element> extensions = new ArrayList<Element>(packet 249 .getElement().elements()); 250 extensions.remove(userInfo); 251 252 room.sendInvitation(new JID(info.attributeValue("to")), 254 info.elementTextTrim("reason"), role, extensions); 255 } 256 else if (userInfo != null 257 && userInfo.element("decline") != null) { 258 Element info = userInfo.element("decline"); 260 room.sendInvitationRejection(new JID(info.attributeValue("to")), 261 info.elementTextTrim("reason"), packet.getFrom()); 262 } 263 else { 264 sendErrorPacket(packet, PacketError.Condition.bad_request); 265 } 266 } 267 else { 268 sendErrorPacket(packet, PacketError.Condition.bad_request); 269 } 270 } 271 } 272 catch (ForbiddenException e) { 273 sendErrorPacket(packet, PacketError.Condition.forbidden); 274 } 275 catch (NotFoundException e) { 276 sendErrorPacket(packet, PacketError.Condition.recipient_unavailable); 277 } 278 catch (ConflictException e) { 279 sendErrorPacket(packet, PacketError.Condition.conflict); 280 } 281 } 282 } 283 } 284 } 285 286 public void process(IQ packet) { 287 if (IQ.Type.error == packet.getType()) { 289 return; 290 } 291 lastPacketTime = System.currentTimeMillis(); 292 JID recipient = packet.getTo(); 293 String group = recipient.getNode(); 294 if (group == null) { 295 Log.info(LocaleUtils.getLocalizedString("muc.error.not-supported") + " " 298 + packet.toString()); 299 } 300 else { 301 MUCRole role = roles.get(group.toLowerCase()); 302 if (role == null) { 303 } 306 else { 307 if (!role.getChatUser().getAddress().equals(packet.getFrom())) { 310 sendErrorPacket(packet, PacketError.Condition.conflict); 311 } 312 else { 313 try { 314 Element query = packet.getElement().element("query"); 315 if (query != null && 316 "http://jabber.org/protocol/muc#owner".equals(query.getNamespaceURI())) { 317 role.getChatRoom().getIQOwnerHandler().handleIQ(packet, role); 318 } 319 else if (query != null && 320 "http://jabber.org/protocol/muc#admin".equals(query.getNamespaceURI())) { 321 role.getChatRoom().getIQAdminHandler().handleIQ(packet, role); 322 } 323 else { 324 sendErrorPacket(packet, PacketError.Condition.bad_request); 325 } 326 } 327 catch (ForbiddenException e) { 328 sendErrorPacket(packet, PacketError.Condition.forbidden); 329 } 330 catch (ConflictException e) { 331 sendErrorPacket(packet, PacketError.Condition.conflict); 332 } 333 catch (NotAllowedException e) { 334 sendErrorPacket(packet, PacketError.Condition.not_allowed); 335 } 336 } 337 } 338 } 339 } 340 341 public void process(Presence packet) { 342 if (Presence.Type.error == packet.getType()) { 344 return; 345 } 346 lastPacketTime = System.currentTimeMillis(); 347 JID recipient = packet.getTo(); 348 String group = recipient.getNode(); 349 if (group == null) { 350 if (Presence.Type.unavailable == packet.getType()) { 351 server.removeUser(packet.getFrom()); 352 } 353 } 354 else { 355 MUCRole role = roles.get(group.toLowerCase()); 356 if (role == null) { 357 if (recipient.getResource() != null 360 && recipient.getResource().trim().length() > 0) { 361 if (packet.isAvailable()) { 362 try { 363 MUCRoom room = server.getChatRoom(group, packet.getFrom()); 365 Element mucInfo = packet.getChildElement("x", 367 "http://jabber.org/protocol/muc"); 368 HistoryRequest historyRequest = null; 369 String password = null; 370 if (mucInfo != null) { 372 password = mucInfo.elementTextTrim("password"); 373 if (mucInfo.element("history") != null) { 374 historyRequest = new HistoryRequest(mucInfo); 375 } 376 } 377 role = room.joinRoom(recipient.getResource().trim(), 379 password, 380 historyRequest, 381 this, 382 packet.createCopy()); 383 roles.put(group.toLowerCase(), role); 384 if (mucInfo == null && room.isLocked() && !room.isManuallyLocked()) { 387 room.unlock(role); 388 } 389 } 390 catch (UnauthorizedException e) { 391 sendErrorPacket(packet, PacketError.Condition.not_authorized); 392 } 393 catch (ServiceUnavailableException e) { 394 sendErrorPacket(packet, PacketError.Condition.service_unavailable); 395 } 396 catch (UserAlreadyExistsException e) { 397 sendErrorPacket(packet, PacketError.Condition.conflict); 398 } 399 catch (RoomLockedException e) { 400 sendErrorPacket(packet, PacketError.Condition.recipient_unavailable); 401 } 402 catch (ForbiddenException e) { 403 sendErrorPacket(packet, PacketError.Condition.forbidden); 404 } 405 catch (RegistrationRequiredException e) { 406 sendErrorPacket(packet, PacketError.Condition.registration_required); 407 } 408 catch (ConflictException e) { 409 sendErrorPacket(packet, PacketError.Condition.conflict); 410 } 411 catch (NotAcceptableException e) { 412 sendErrorPacket(packet, PacketError.Condition.not_acceptable); 413 } 414 } 415 else { 416 } 419 } 420 else { 421 if (packet.isAvailable()) { 422 sendErrorPacket(packet, PacketError.Condition.bad_request); 424 } 425 } 428 } 429 else { 430 if (!role.getChatUser().getAddress().equals(packet.getFrom())) { 433 sendErrorPacket(packet, PacketError.Condition.conflict); 434 } 435 else { 436 if (Presence.Type.unavailable == packet.getType()) { 437 try { 438 roles.remove(group.toLowerCase()); 439 role.getChatRoom().leaveRoom(role.getNickname()); 440 } 441 catch (UserNotFoundException e) { 442 } 444 catch (Exception e) { 445 Log.error(e); 446 } 447 } 448 else { 449 try { 450 String resource = (recipient.getResource() == null 451 || recipient.getResource().trim().length() == 0 ? null 452 : recipient.getResource().trim()); 453 if (resource == null 454 || role.getNickname().equalsIgnoreCase(resource)) { 455 role.setPresence(packet.createCopy()); 457 role.getChatRoom().send(role.getPresence().createCopy()); 458 } 459 else { 460 463 if (!role.getChatRoom().canChangeNickname()) { 465 sendErrorPacket(packet, PacketError.Condition.not_acceptable); 466 } 467 else if (role.getChatRoom().hasOccupant(resource)) { 469 sendErrorPacket(packet, PacketError.Condition.conflict); 470 } 471 else { 472 Presence presence = role.getPresence().createCopy(); 474 presence.setType(Presence.Type.unavailable); 476 presence.setStatus(null); 477 Element frag = presence.getChildElement("x", 479 "http://jabber.org/protocol/muc#user"); 480 frag.element("item").addAttribute("nick", resource); 481 frag.addElement("status").addAttribute("code", "303"); 482 role.getChatRoom().send(presence); 483 484 String oldNick = role.getNickname(); 486 role.setPresence(packet.createCopy()); 487 role.changeNickname(resource); 488 role.getChatRoom().nicknameChanged(oldNick, resource); 489 role.getChatRoom().send(role.getPresence().createCopy()); 490 } 491 } 492 } 493 catch (Exception e) { 494 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 495 } 496 } 497 } 498 } 499 } 500 } 501 } | Popular Tags |