openfire删除永久群成员操作

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);
        }
    }

在MultiUserChatService中添加删除群成员接口方法
在MultiUserChatServiceImpl添加实现方法

public void deleteMember(JID jid, long roomID) {
        // TODO Auto-generated method stub
        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) 方法中:


        // 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);
                        }
                    }
                }
            }
        }

由于删除群成员和加入房间的操作均通过此方法,为了避免我自传的presence包引起openfire加入房间操作抛异常,就判断了mucInfo是否为空的,如果为空,就不加入房间了(之前是判断密码,后发现用密码判断不能创建群)。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值