openfire插件开发-聊天记录插件

时间:2022-07-12 14:44:11

MessageFilter

package org.jivesoftware.openfire.plugin;

import java.io.File;
import java.util.List;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;

public class MessageFilterPlugin implements Plugin, PacketInterceptor {

private static final Logger log = LoggerFactory
.getLogger(MessageFilterPlugin.class);

private static PluginManager pluginManager;

private InterceptorManager interceptorManager;

public MessageFilterPlugin() {
interceptorManager = InterceptorManager.getInstance();
}

@Override
public void interceptPacket(Packet packet, Session session,
boolean incoming, boolean processed) throws PacketRejectedException {
this.doAction(packet, incoming, processed, session);
}

/**
* <b>function:</b> 执行保存/分析聊天记录动作
*
* @author hoojo
* @createDate 2013-3-24 下午12:20:56
* @param packet
* 数据包
* @param incoming
* true表示发送方
* @param session
* 当前用户session
*/
private void doAction(Packet packet, boolean incoming, boolean processed,
Session session) {
Packet copyPacket = packet.createCopy();
if (packet instanceof Message) {
Message message = (Message) copyPacket;

// 一对一聊天,单人模式
if (message.getType() == Message.Type.chat) {
log.info("单人聊天信息:{}", message.toXML());
debug("单人聊天信息:" + message.toXML());
try {
System.out.println("信息体 = " + message.getBody());
System.out.println("信息from = " + message.getFrom());
System.out.println("信息to = " + message.getTo());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

}

// 程序执行中;是否为结束或返回状态(是否是当前session用户发送消息)
if (processed || !incoming) {
return;
}
// logsManager.add(this.get(packet, incoming, session));

// 群聊天,多人模式
} else if (message.getType() == Message.Type.groupchat) {
List<?> els = message.getElement().elements("x");
if (els != null && !els.isEmpty()) {
log.info("群聊天信息:{}", message.toXML());
debug("群聊天信息:" + message.toXML());
} else {
log.info("群系统信息:{}", message.toXML());
debug("群系统信息:" + message.toXML());
}

// 其他信息
} else {
log.info("其他信息:{}", message.toXML());
debug("其他信息:" + message.toXML());
log.info("单人聊天信息:{}", message.toXML());
debug("单人聊天信息:" + message.toXML());
System.out.println("信息体 = " + message.getBody());
System.out.println("信息from = " + message.getFrom());
System.out.println("信息to = " + message.getTo());
}
} else if (packet instanceof IQ) {
IQ iq = (IQ) copyPacket;
if (iq.getType() == IQ.Type.set && iq.getChildElement() != null
&& "session".equals(iq.getChildElement().getName())) {
log.info("用户登录成功:{}", iq.toXML());
debug("用户登录成功:" + iq.toXML());
}
} else if (packet instanceof Presence) {
Presence presence = (Presence) copyPacket;
if (presence.getType() == Presence.Type.unavailable) {
log.info("用户退出服务器成功:{}", presence.toXML());
debug("用户退出服务器成功:" + presence.toXML());
}
}
}

private void debug(Object message) {
if (true) {
System.out.println(message);
}
}

@Override
public void destroyPlugin() {
interceptorManager.removeInterceptor(this);
debug("销毁聊天记录插件成功!");
}

@Override
public void initializePlugin(PluginManager manager, File pluginDirectory) {
// TODO Auto-generated method stub
interceptorManager.addInterceptor(this);
pluginManager = manager;
debug("添加聊天记录插件成功!");
}

}

Plugin.xml


<?xml version="1.0" encoding="UTF-8"?>

<!--
Plugin configuration for the broadcast plugin.
-->
<plugin>
<class>org.jivesoftware.openfire.plugin.MessageFilterPlugin</class>
<name>MessageFilter</name>
<description>MessageFilter messages to users.</description>
<author>Jive Software</author>
<version>1.9.0</version>
<date>9/13/2013</date>
<url>http://www.igniterealtime.org</url>
<minServerVersion>3.9.0</minServerVersion>


</plugin>

openfire插件开发-聊天记录插件

下面截取的时候试了很多方法 都是一个信息打印好几次,后来只能判断信息的来源和内容以及发送的对象是不是一样的来判断了。