powered by Jive Software

Need help for my first plugin

Hello,

I’'m writing my first Openfire plugin. My plugin should block packets send between users who have an username that start by “cabine”.

My class is the following :


public class IntercepteurPlugin implements Plugin, PacketInterceptor

{

PacketRejectedException rejected;

public void destroyPlugin() {

// TODO Raccord de méthode auto-généré

}

public void initializePlugin(PluginManager manager, File pluginDirectory) {

// TODO Raccord de méthode auto-généré

rejected = new PacketRejectedException(“Packet rejected with disallowed content!”);

}

public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) throws PacketRejectedException {

// TODO Raccord de méthode auto-généré

if ( (packet.getFrom().toString().startsWith(“cabine”)) && (packet.getTo().toString().startsWith(“cabine”)) )

{

rejected.setRejectionMessage(“Message refusé”);

throw rejected;

}

}

}


Any idea why it doesn’'t work ? No packets are rejected.

Thanks.

Hi there,

You left one thing out. You did create your plugin to be a PacketInterceptor. What you need to do next is to register your new PacketInterceptor to the server.

Openfire keeps a list of PacketInterceptors, to which you need to add your plugin (and of course, remove it from that list if you unload your plugin). Openfire uses the InterceptorManager class to manage PacketInterceptors.

I would suggest modifying your plugin something like this:

public class IntercepteurPlugin implements Plugin, PacketInterceptor {      private static final PacketRejectedException REJECTED = new PacketRejectedException(
     "Message refusé");      public void destroyPlugin() {
          // remove this PacketInterceptor from the server.
          final InterceptorManager interceptorManager = InterceptorManager.getInstance();
          interceptorManager.removeInterceptor(this);
     }      public void initializePlugin(PluginManager manager, File pluginDirectory) {
          // add this PacketInterceptor to the server.
          final InterceptorManager interceptorManager = InterceptorManager.getInstance();
          interceptorManager.addInterceptor(this);
     }      public void interceptPacket(Packet packet, Session session,
               boolean incoming, boolean processed) throws PacketRejectedException {           if ((packet.getFrom().getNode().startsWith("cabine"))
                    && (packet.getTo().getNode().startsWith("cabine"))) {                throw REJECTED;
          }
     }
}

By the way: not that’'s really important, but if you want to check the username of packet, you should probably be using the JID.getNode() result (which typically returns the username), instead of JID.toString() (which returns the entire String). But hey, the end result is the same.

You should check (packet.getFrom() == null) and (packet.getFrom().getNode() == null) before you do anything with it. The same with .getTo().

Coolcat