openfire插件最佳实践(三) 用户加入指定群组功能
2014-01-14 11:14
417 查看
忽然要实现这样一个功能,完全没头绪还是看一下openfire的源码吧,发现room部分的服务器端可以直接将用户加入群组,只要发送请求
那么我们可以利用这个请求:
依旧是使用一个自定义IQ来获取用户请求
假设我们指定iq如下格式:
iq 包set请求,包含roomJID、jID、affiliation三个参数,
roomJID是房间的jid,jID是想要操作的用户的jID,affiliation是用户被操作后的类型,
affiliation 设置为owner :拥有者权限
affiliation 设置为admin :管理员权限
affiliation 设置为member:房间成员权限
affiliation 设置为outcast:被排除者权限
通过发送上面的数据将会将wtf111这个人设置为1388453982252的房间成员
用户可通过这个接口重复设置,也就是说:
如果用户开始是拥有者,再次设置affiliation 设置为member 则此用户成灰普通房间成员,
但是当拥有者=1时,不允许继续减少拥有者
好的,我们定义好了消息格式,可以开始对消息进行处理了!
重写 handleIQ 方法
这样就完成了处理,记得把它加到你的插件中啊~
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());
相关文章推荐
- 解决VMware虚拟机CentOS linux联网问题
- SUSE Linux 多路径软件+LVM+裸设备的配置
- Linux测试硬盘读写速度
- linux新增用户和密码
- Linux du
- linux 下大量TIME_WAIT 的处理
- Linux 下的两个特殊的文件 -- /dev/null 和 /dev/zero 简介及对比
- 基于Apache的Tomcat负载均衡和集群
- linux下的SVN server的POST-COMMIT钩子自动部署代码
- 自动备份(同步更新)linux环境上的tomcat服务器到windows系统
- centos缺少共享库:libstdc++.so.6
- Linux学习小结(文件与文件系统的压缩与打包)
- linux 命令 losetup
- photoshop怎么抠图
- Linux学习笔记24——进程管道
- 各种 shell 的区别
- 我的Linux书架
- nstimer,需要注册到 runloop里面去的,
- linux下中文乱码问题解决
- 利用Linux守护进程机制完成一个简单系统监控demo