[置顶] openfire删除永久群成员操作

来源:互联网 发布:sql语句exists insert 编辑:程序博客网 时间:2024/06/14 20:41
 

[置顶] openfire删除永久群成员操作

标签: openfire博客源码数据库
 62人阅读 评论(0) 收藏 举报
 分类:

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) {        // TODO Auto-generated method stub        MUCPersistenceManager.deleMember(jid.toString(), roomID);    }
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

拦截包处理:在客户端发布一个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>
  • 1
  • 1

第二个c子元素是系统加上去的,可能是客户端处理不正确,但在这个presence包中包含要删除的群成员以及群信息,删除操作在LocalMUCUser的process(Presence packet) 方法中:

        // Ignore presences of type ERROR sent to a room        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 we're not already in a room (role == null), we either are joining it or it's not                // properly addressed and we drop it silently                // Alternative is that mucInfo is not null, in which case the client thinks it isn't in the room, so we should join anyway.                if (recipient.getResource() != null                        && recipient.getResource().trim().length() > 0) {                    if (packet.isAvailable()) {                        try {                            // Get or create the room                            MUCRoom room = server.getChatRoom(group, packet.getFrom());                            // User must support MUC in order to create a room                            HistoryRequest historyRequest = null;                            String password = null;                            // Check for password & requested history if client supports MUC                            if (mucInfo != null) {                                password = mucInfo.elementTextTrim("password");                                if (mucInfo.element("history") != null) {                                    historyRequest = new HistoryRequest(mucInfo);                                }                                //刪除群用戶的jid                          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){                            // The user joins the room                            role = room.joinRoom(recipient.getResource().trim(),                                    password,                                    historyRequest,                                    this,                                    packet.createCopy());}                            // If the client that created the room is non-MUC compliant then                            // unlock the room thus creating an "instant" room                            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) {                            // If a user attempts to enter a room while it is "locked" (i.e., before the room creator provides an initial configuration and therefore before the room officially exists), the service MUST refuse entry and return an <item-not-found/> error to the user                            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 {                        // TODO: send error message to user (can't send presence to group you                        // haven't joined)                    }                }                else {                    if (packet.isAvailable()) {                        // A resource is required in order to join a room                        // http://xmpp.org/extensions/xep-0045.html#enter                        // If the user does not specify a room nickname (note the bare JID on the 'from' address in the following example), the service MUST return a <jid-malformed/> error                        sendErrorPacket(packet, PacketError.Condition.jid_malformed);                    }                    // TODO: send error message to user (can't send packets to group you haven't                    // joined)                }            }            else {                // Check and reject conflicting packets with conflicting roles                // In other words, another user already has this nickname                if (!role.getUserAddress().equals(packet.getFrom())) {                    sendErrorPacket(packet, PacketError.Condition.conflict);                }                else {                    if (Presence.Type.unavailable == packet.getType()) {                        try {                            // TODO Consider that different nodes can be creating and processing this presence at the same time (when remote node went down)                            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)) {                                // Occupant has changed his availability status                                role.getChatRoom().presenceUpdated(role, packet);                            }                            else {                                // Occupant has changed his nickname. Send two presences                                // to each room occupant                                // Check if occupants are allowed to change their nicknames                                if (!role.getChatRoom().canChangeNickname()) {                                    sendErrorPacket(packet, PacketError.Condition.not_acceptable);                                }                                // Answer a conflic error if the new nickname is taken                                else if (role.getChatRoom().hasOccupant(resource)) {                                    sendErrorPacket(packet, PacketError.Condition.conflict);                                }                                else {                                    // Send "unavailable" presence for the old nickname                                    Presence presence = role.getPresence().createCopy();                                    // Switch the presence to OFFLINE                                    presence.setType(Presence.Type.unavailable);                                    presence.setStatus(null);                                    // Add the new nickname and status 303 as properties                                    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);                                    // Send availability presence for the new nickname                                    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是否为空的,如果为空,就不加入房间了(之前是判断密码,后发现用密码判断不能创建群)。

原创粉丝点击