标签: openfire博客源码数据库
2017-01-05 15:41 62人阅读 收藏 举报
openfire删除永久群成员操作
第一次写博客,主要是近期在openfire源码学习中遇见了很多问题,为防止忘记,写篇博客记录一下:
openfire是不支持group删除群成员的,在MUCPersistenceManager中可以发现仅支持在删除房间时,删除成员。
修改openfire源码删除群成员需要添加删除成员的数据库操作方法,然后拦截包,做处理,逻辑很简单,但在哪儿拦截找了很久。
添加删除群成员方法
在MUCPersistenceManager中添加如下方法:
public static boolean deleMember(String jid,long roomID){ Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(DELETE_MEMBER); pstmt.setLong(1, roomID); pstmt.setString(2,jid); pstmt.executeUpdate(); return true; } catch (SQLException sqle) { Log.error("Error saving conversation log entry", sqle); return false; } finally { DbConnectionManager.closeConnection(pstmt, con); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
在MultiUserChatService中添加删除群成员接口方法
在MultiUserChatServiceImpl添加实现方法
public void deleteMember(JID jid, long roomID) { MUCPersistenceManager.deleMember(jid.toString(), roomID); }
拦截包处理:在客户端发布一个presence包,命名空间必须为http://jabber.org/protocol/muc
presence包中包含删除的用户参数:delete=“jid”,我的presence包如下所示
<presence id="SIFwG-138" to="群名@conference.域名/用户名@域名/Smack" from="用户名@域名/Smack"><x xmlns="http://jabber.org/protocol/muc" delete="用户名@域名"/><c xmlns="http://jabber.org/protocol/caps" hash="sha-1" node="http://www.igniterealtime.org/projects/smack/" ver="o2Iy/apHXxp9Qa9FmzglAS9JXJA="/></presence>
第二个c子元素是系统加上去的,可能是客户端处理不正确,但在这个presence包中包含要删除的群成员以及群信息,删除操作在LocalMUCUser的process(Presence packet) 方法中:
if (Presence.Type.error == packet.getType()) { return; } lastPacketTime = System.currentTimeMillis(); JID recipient = packet.getTo(); String group = recipient.getNode(); if (group != null) { MUCRole role = roles.get(group); Element mucInfo = packet.getChildElement("x", "http://jabber.org/protocol/muc"); if (role == null || mucInfo != null) { if (recipient.getResource() != null && recipient.getResource().trim().length() > 0) { if (packet.isAvailable()) { try { MUCRoom room = server.getChatRoom(group, packet.getFrom()); HistoryRequest historyRequest = null; String password = null; if (mucInfo != null) { password = mucInfo.elementTextTrim("password"); if (mucInfo.element("history") != null) { historyRequest = new HistoryRequest(mucInfo); } String deletejid = mucInfo.attributeValue("delete"); if(deletejid!=null){ JID deleteJid=new JID(deletejid); server.deleteMember(deleteJid, room.getID()); server.refreshChatRoom(room.getName()); mucInfo=null; } } if(mucInfo!=null){ role = room.joinRoom(recipient.getResource().trim(), password, historyRequest, this, packet.createCopy());} if (mucInfo == null && room.isLocked() && !room.isManuallyLocked()) { room.unlock(role); } } catch (UnauthorizedException e) { sendErrorPacket(packet, PacketError.Condition.not_authorized); } catch (ServiceUnavailableException e) { sendErrorPacket(packet, PacketError.Condition.service_unavailable); } catch (UserAlreadyExistsException | ConflictException e) { sendErrorPacket(packet, PacketError.Condition.conflict); } catch (RoomLockedException e) { sendErrorPacket(packet, PacketError.Condition.item_not_found); } catch (ForbiddenException e) { sendErrorPacket(packet, PacketError.Condition.forbidden); } catch (RegistrationRequiredException e) { sendErrorPacket(packet, PacketError.Condition.registration_required); } catch (NotAcceptableException e) { sendErrorPacket(packet, PacketError.Condition.not_acceptable); } catch (NotAllowedException e) { sendErrorPacket(packet, PacketError.Condition.not_allowed); } } else { } } else { if (packet.isAvailable()) { sendErrorPacket(packet, PacketError.Condition.jid_malformed); } } } else { if (!role.getUserAddress().equals(packet.getFrom())) { sendErrorPacket(packet, PacketError.Condition.conflict); } else { if (Presence.Type.unavailable == packet.getType()) { try { removeRole(group); role.getChatRoom().leaveRoom(role); } catch (Exception e) { Log.error(e.getMessage(), e); } } else { try { String resource = (recipient.getResource() == null || recipient.getResource().trim().length() == 0 ? null : recipient.getResource().trim()); if (resource == null || role.getNickname().equalsIgnoreCase(resource)) { role.getChatRoom().presenceUpdated(role, packet); } else { if (!role.getChatRoom().canChangeNickname()) { sendErrorPacket(packet, PacketError.Condition.not_acceptable); } else if (role.getChatRoom().hasOccupant(resource)) { sendErrorPacket(packet, PacketError.Condition.conflict); } else { Presence presence = role.getPresence().createCopy(); presence.setType(Presence.Type.unavailable); presence.setStatus(null); Element frag = presence.getChildElement("x", "http://jabber.org/protocol/muc#user"); frag.element("item").addAttribute("nick", resource); frag.addElement("status").addAttribute("code", "303"); role.getChatRoom().send(presence); String oldNick = role.getNickname(); role.getChatRoom().nicknameChanged(role, packet, oldNick, resource); } } } catch (Exception e) { Log.error(LocaleUtils.getLocalizedString("admin.error"), e); } } } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
由于删除群成员和加入房间的操作均通过此方法,为了避免我自传的presence包引起openfire加入房间操作抛异常,就判断了mucInfo是否为空的,如果为空,就不加入房间了(之前是判断密码,后发现用密码判断不能创建群)。