openfire muc 移除成员

时间:2023-03-09 05:33:23
openfire muc 移除成员

muc添加成员到数据库可参考 将Openfire中的MUC改造成类似QQ群一样的永久群

插件

插件是一位大神参考第一篇文章改进后编写的插件,进测试可以直接使用。

----------------------------------------分割线------------------------------------------------------------

本文主要介绍如何移除成员

基本思路:

1、openfire控制台的muc-room-occupants.jsp页面有踢出成员功能

jsp源码

// Kick nick specified
if (kick != null) {
MUCRole role = room.getOccupant(nickName);
if (role != null) {
try {
room.kickOccupant(role.getUserAddress(), XMPPServer.getInstance().createJID(webManager.getUser().getUsername(), null), null, "");
// Log the event
webManager.logEvent("kicked MUC occupant "+nickName+" from "+roomName, null);
// Done, so redirect
response.sendRedirect("muc-room-occupants.jsp?roomJID="+URLEncoder.encode(room.getJID().toBareJID(), "UTF-8")+"&nickName="+URLEncoder.encode(role.getNickname(), "UTF-8")+"&deletesuccess=true");
return;
}
catch (NotAllowedException e) {
// Done, so redirect
response.sendRedirect("muc-room-occupants.jsp?roomJID="+URLEncoder.encode(room.getJID().toBareJID(), "UTF-8")+"&nickName="+URLEncoder.encode(role.getNickname(), "UTF-8")+"&deletefailed=true");
return;
}
}
}

2、可以看到核心的kickOccupant方法,我们来看实现类LocalMUCRoom中的具体实现方法

public Presence kickOccupant(JID jid, JID actorJID, String actorNickname, String reason)
throws NotAllowedException {
System.out.println("jid="+jid.toString()+" ==== actorJID="+actorJID);
// Update the presence with the new role and inform all occupants
Presence updatedPresence = changeOccupantRole(jid, MUCRole.Role.none);//改变成员在房间的角色
if (updatedPresence != null) {
Element frag = updatedPresence.getChildElement(
"x", "http://jabber.org/protocol/muc#user"); // Add the status code 307 that indicates that the user was kicked
frag.addElement("status").addAttribute("code", "307");
// Add the reason why the user was kicked
if (reason != null && reason.trim().length() > 0) {
frag.element("item").addElement("reason").setText(reason);
} // Effectively kick the occupant from the room
kickPresence(updatedPresence, actorJID, actorNickname);//移除成员 //Inform the other occupants that user has been kicked
broadcastPresence(updatedPresence, false);//广播
}
return updatedPresence;
}

主要在上述方法添加删除数据库方法就可以了。

但是发现如果房间设置只允许成员加入,但是明明移除了成员,数据库也删除了,但是在服务器未重启的情况下用户还是可以进入房间。

研究进入房间的源码LocalMucRoom的joinRoom方法,发现有判断 members.includesKey(bareJID)。

所以我们可以在删除数据库后把成员remove。

3、还需要判断一点,用户是否在房间内,如果不在只需删除数据库。

------------------------以下是部分源码截图------------------------------------------------------------------

1、插件

openfire muc 移除成员

2、方法

openfire muc 移除成员

3、kickMember方法

openfire muc 移除成员

操作数据库的代码就不在贴出了。

ps:第一次写博客,写的不好,请多多指教