1 /**
2 * 模似用户进群(含呢称、含Precense、初始化角色LocalMUCRole)
3 * @param roomId
4 * @param packet
5 * @return
6 */ 7 public MUCRole getRolesByRoomId(
long roomId, Packet packet)
8 {
9 MUCRole role =
null;
10 try {
11 // Get or create the room 获取该群
12 MUCRoom room = server.getChatRoom(roomId, packet.getFrom());
13 14 //从数据库中查询他的姓名作为昵称(得自己实现)
15 String nickname =
new MUCRoomServiceDao().getUserNickname(packet.getFrom().getNode());
16 if(nickname ==
null)
17 {
18 if(packet.getFrom().getResource() !=
null)
19 {
20 nickname = packet.getFrom().getResource();
21 }
22 else 23 {
24 nickname = packet.getFrom().getNode();
25 }
26 }
27 28 HistoryRequest historyRequest =
null;
29 String password =
null;
30 31 //构建成员进入群的Presence
32 Presence presence =
new Presence();
33 presence.setTo(room.getJID().toBareJID() + "/" + nickname);
34 presence.setFrom(packet.getFrom());
35 PacketExtension extension =
new PacketExtension("x",
http://jabber.org/protocol/muc);
36 presence.addExtension(extension);
37 38 // The user joins the room 用户进入群
39 role = room.joinRoom(nickname,
40 password,
41 historyRequest,
42 this,
43 presence);
44 45 // If the client that created the room is non-MUC compliant then
46 // unlock the room thus creating an "instant" room
47 //if (mucInfo == null && room.isLocked() && !room.isManuallyLocked()) {
48 if (room.isLocked() && !room.isManuallyLocked()) {
49 room.unlock(role);
50 //server.chatRoomAdded((LocalMUCRoom)room);
51 }
52 53 addRole(roomId, (LocalMUCRole)role);
//添加“用户在某个群中的角色”
54 55 }
56 catch (UnauthorizedException e) {
57 sendErrorPacket(packet, PacketError.Condition.not_authorized);
58 }
59 catch (ServiceUnavailableException e) {
60 sendErrorPacket(packet, PacketError.Condition.service_unavailable);
61 }
62 catch (UserAlreadyExistsException e) {
63 sendErrorPacket(packet, PacketError.Condition.conflict);
64 }
65 catch (RoomLockedException e) {
66 sendErrorPacket(packet, PacketError.Condition.recipient_unavailable);
67 }
68 catch (ForbiddenException e) {
69 sendErrorPacket(packet, PacketError.Condition.forbidden);
70 }
71 catch (RegistrationRequiredException e) {
72 sendErrorPacket(packet, PacketError.Condition.registration_required);
73 }
74 catch (ConflictException e) {
75 sendErrorPacket(packet, PacketError.Condition.conflict);
76 }
77 catch (NotAcceptableException e) {
78 sendErrorPacket(packet, PacketError.Condition.not_acceptable);
79 }
80 catch (NotAllowedException e) {
81 sendErrorPacket(packet, PacketError.Condition.not_allowed);
82 }
83 return role;
84 }