参考:http://myopenfire.com/article/getarticle/9

package com.myopenfire.plugin;

import java.io.File;

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.Message;
import org.xmpp.packet.Packet; /**
* 过滤插件:当body中有fuck时,将消息截断,不转发消息。
*
*/
public class MessageFilterPlugin implements Plugin ,PacketInterceptor{
// A:生成一个日志实例,用于打印日志,日志被打印在openfire_src\target\openfire\logs目录中
private static final Logger Log = LoggerFactory.getLogger(MessageFilterPlugin.class);
//B: 消息拦截器
private InterceptorManager interceptorManager; //C: 插件初始化函数
@Override
public void initializePlugin(PluginManager manager, File pluginDirectory) { Log.info("MessageFilter init");
// 将当前插件加入到消息拦截管理器(interceptorManager )中,当消息到来或者发送出去的时候,会触发本插件的interceptPacket方法。
interceptorManager = InterceptorManager.getInstance();
interceptorManager.addInterceptor(this);
} //D: 插件销毁函数
@Override
public void destroyPlugin() {
Log.info("MessageFilter destory");
// 当插件被卸载的时候,主要通过openfire管理控制台卸载插件时,被调用。注意interceptorManager的addInterceptor和removeInterceptor需要成对调用。
interceptorManager.removeInterceptor(this);
} //E 插件拦截处理函数
@Override
public void interceptPacket(Packet packet, Session session,
boolean incoming, boolean processed)
throws PacketRejectedException {
// incoming表示本条消息刚进入openfire。processed为false,表示本条消息没有被openfire处理过。这说明这是一条处女消息,也就是没有被处理过的消息。
if (incoming && processed == false) {
// packet可能是IQ、Presence、Message,这里当packet是message的时候,进行处理。
if (packet instanceof Message) {
// 将packet强制转换为Message
Message msg = (Message)packet;
// 取得message中的body内容,就是消息正文
String body = msg.getBody();
// 如果内容中包含fuck,则拒绝处理消息
if(body != null && body.contains("fuck")){
// F: 这里通过抛出异常的方式,来阻止程序流程继续执行下去。
PacketRejectedException rejectedException = new PacketRejectedException(); rejectedException.setRejectionMessage("fuck is error"); throw rejectedException;
} }
} } }

openfire过滤脏话插件,控制消息是否发送的更多相关文章

  1. openfire广播broadcast插件怎么发送消息给所有用户(包括在线和离线)

    openfire广播broadcast插件怎么发送消息给所有用户(包括在线和离线): 打开openfire管理界面,找到服务器系统属性,添加一个属性(属性名:plugin.broadcast.all2 ...

  2. 开源一个Java Class实现Openfire登陆、推出、消息发送,方便其他系统集成IM功能了

    开源一个Java Class实现Openfire登陆.推出.消息发送 N年前写的,希望对Openfire开发新手有帮助哦 import java.util.*; import java.io.*;   ...

  3. Openfire 是怎么存离线消息

    原文:http://myopenfire.com/article/getarticle/26 1.openfire默认怎么存离线消息   在默认情况下,不添加任何插件的情况下,当用户不在线,对于发送给 ...

  4. openfire :openfire 不同类型插件的开发示例

    新建一个自己的Javaproject工程,添加的jar包如下: 将jasper-compiler.jar.jasper-runtime.jar.servlet.jar添加到新建的工程中.如果没有jar ...

  5. 2.RabbitMQ 的可靠性消息的发送

      本篇包含 1. RabbitMQ 的可靠性消息的发送 2. RabbitMQ 集群的原理与高可用架构的搭建 3. RabbitMQ 的实践经验   上篇包含 1.MQ 的本质,MQ 的作用 2.R ...

  6. RabbitMQ,RocketMQ,Kafka 事务性,消息丢失和消息重复发送的处理策略

    消息队列常见问题处理 分布式事务 什么是分布式事务 常见的分布式事务解决方案 基于 MQ 实现的分布式事务 本地消息表-最终一致性 MQ事务-最终一致性 RocketMQ中如何处理事务 Kafka中如 ...

  7. [转]PhoneGap使用PushPlugin插件实现消息推送

    本文转自:http://my.oschina.net/u/1270482/blog/217661 http://devgirl.org/2013/07/17/tutorial-implement-pu ...

  8. TreeView控制消息

    控制消息的作用 通过发送消息到Treeview控件, 就能够控机Treeview控件.常用的控制有: 获取被点击的节点 获取节点的文本 设置节点的文本 获取节点的父节点 获取节点的子节点 TVM_GE ...

  9. 如何在优雅地Spring 中实现消息的发送和消费

    本文将对rocktmq-spring-boot的设计实现做一个简单的介绍,读者可以通过本文了解将RocketMQ Client端集成为spring-boot-starter框架的开发细节,然后通过一个 ...

随机推荐

  1. Case of the Zeros and Ones 分类: CF 2015-07-24 11:05 15人阅读 评论(0) 收藏

    A. Case of the Zeros and Ones time limit per test 1 second memory limit per test 256 megabytes input ...

  2. Eclipse编译器及一些jdk + notepad

    Eclipse32位系统 http://pan.baidu.com/s/1i3eU8V7 Eclipse64位系统 http://pan.baidu.com/s/1i36ERCp jdk64位 htt ...

  3. Python学习笔记-Day2-Python基础之元组操作

    元组的常用操作包括但不限于以下操作: 元组的索引,计数等 这里将对列表的内置操作方法进行总结归纳,重点是以示例的方式进行展示. 使用type获取创建对象的类 type(tuple) 使用dir获取类的 ...

  4. Java中的String为什么是不可变的?

    转载:http://blog.csdn.net/zhangjg_blog/article/details/18319521 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那 ...

  5. springmvc配置文件-1

    项目1: web.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?& ...

  6. Less/Sass编译工具

    less中午网站有详细的说明:http://www.1024i.com/demo/less/ 那里我使用的koala   全平台支持,国产.下载地址:http://koala-app.com/inde ...

  7. Radar之字节流加载图片

    获取GUITexture GameObject _obj = GameObject.Find("Tex1"); GUITexture _tex = _obj.GetComponen ...

  8. IOSView显示特性设置

    一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等 ...

  9. Infragistics UltraGrid的使用

    OL SDK:http://help.infragistics.com/ 资料参考:http://blog.csdn.net/andy_212/article/details/4019895 http ...

  10. spring容器启动过程

    1.AbstractApplicationContext的prepareRefresh()方法调用. 2.XmlBeanDefinitionReader的loadBeanDifinitions(Bea ...