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

xiaoxiao2021-02-28  91

 

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

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

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

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

public void deleteMember(JID jid, long roomID) { // TODO Auto-generated method stub MUCPersistenceManager.deleMember(jid.toString(), roomID); } 1234 1234

拦截包处理:在客户端发布一个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); } } } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162

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

转载请注明原文地址: https://www.6miu.com/read-32790.html

最新回复(0)