您的位置:首页 > 运维架构

openfire插件最佳实践(三) 用户加入指定群组功能

2014-01-14 11:14 417 查看
忽然要实现这样一个功能,完全没头绪还是看一下openfire的源码吧,发现room部分的服务器端可以直接将用户加入群组,只要发送请求

http://jabber.org/protocol/muc


那么我们可以利用这个请求:

依旧是使用一个自定义IQ来获取用户请求

假设我们指定iq如下格式:

<span style="font-size:18px;"><iq id="2uF5u-4" type="set" from="test@888/Smack">
<query xmlns="hoo.iq.roommember">
<roomJID>1388453982252@conference.888</roomJID>
<jID>wtf111@888</jID>
<affiliation>member</affiliation>
</query>
</iq></span>


iq 包set请求,包含roomJID、jID、affiliation三个参数,
roomJID是房间的jid,jID是想要操作的用户的jID,affiliation是用户被操作后的类型,

affiliation 设置为owner :拥有者权限
affiliation 设置为admin :管理员权限
affiliation 设置为member:房间成员权限
affiliation 设置为outcast:被排除者权限

通过发送上面的数据将会将wtf111这个人设置为1388453982252的房间成员
用户可通过这个接口重复设置,也就是说:
如果用户开始是拥有者,再次设置affiliation 设置为member 则此用户成灰普通房间成员,
但是当拥有者=1时,不允许继续减少拥有者

好的,我们定义好了消息格式,可以开始对消息进行处理了!

重写 handleIQ 方法

@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException {
System.out.println(packet.toXML());
//iqpackage:(因为麻烦所以,当前只提供单个用户对某房间的某权限添加)。iq包格式如下
//<iq ><query>
//<roomJID></><jID></><affiliation></>
//</iq><query>
IQ reply = IQ.createResultIQ(packet);
reply.setChildElement(packet.getChildElement().createCopy());
try {

ClientSession session = sessionManager.getSession(packet.getFrom());
if (session == null) {
Log.error("Error during userInfo. Session not found in " +
sessionManager.getPreAuthenticatedKeys() +
" for key " +
packet.getFrom());
// This error packet will probably won't make it through
reply.setError(PacketError.Condition.internal_server_error);
return reply;
}
if (IQ.Type.get.equals(packet.getType())) {
throw new RuntimeException("no interface to get it ");
} else if (IQ.Type.set.equals(packet.getType())) {
Element iq = packet.getElement();
Element query = iq.element("query");
Element roomJIDEle = query.element("roomJID");
Element jID = query.element("jID");
Element affiliation = query.element("affiliation");
if (roomJIDEle == null || jID == null || affiliation == null) {
reply.setError(PacketError.Condition.item_not_found);
return reply;
}

WebManager webManager = new WebManager();
JID roomJID = new JID(roomJIDEle.getText());
String roomName = roomJID.getNode();
MUCRoom room = webManager.getMultiUserChatManager().getMultiUserChatService(roomJID).getChatRoom(roomName);
IQ muciq = new IQ(IQ.Type.set);
//下面都是对内部发送IQ请求来功能

if ("owner".equals(affiliation.getText())|| "admin".equals(affiliation.getText())) {
Element frag = muciq.setChildElement("query","http://jabber.org/protocol/muc#owner");
Element item = frag.addElement("item");
item.addAttribute("affiliation", affiliation.getText());
item.addAttribute("jid", jID.getText());
// Send the IQ packet that will modify the room's
// configuration
room.getIQOwnerHandler().handleIQ(muciq, room.getRole());
} else if ("member".equals(affiliation.getText())|| "outcast".equals(affiliation.getText())) {
Element frag = muciq.setChildElement("query","http://jabber.org/protocol/muc#admin");
Element item = frag.addElement("item");
item.addAttribute("affiliation", affiliation.getText());
item.addAttribute("jid", jID.getText());
// Send the IQ packet that will modify the room's
// configuration
room.getIQAdminHandler().handleIQ(muciq, room.getRole());
}
}

} catch (Exception e) {
reply.setError(PacketError.Condition.internal_server_error);
return reply;
}
return reply;
}


这样就完成了处理,记得把它加到你的插件中啊~
server.getIQRouter().addHandler(new RoomMemberIQHander());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: