Can I send messages on behalf of a user?

You could write a Openfire plugin that does the job. Take Presence Service or Registration Plugin as example to create such an HTTP interface. Once you have the data inside your plugin, sending the message is simple:

XMPPServer xmppServer = XMPPServer.getInstance();
PacketRouter packetRouter = xmppServer.getPacketRouter();
String serverName = xmppServer.getServerInfo().getXMPPDomain();
public void sendMessage(JID to, JID from, String message) {     
     Message packet = new Message();
     packet.setType(Message.Type.chat);      packet.setFrom(from);
     
     if (to.getResource() != null
        && sessionManager.isActiveRoute(to.getNode(), to.getResource())) {
          packet.setTo(to);
     }
     else {
          // resource is offline => send to bare JID
          packet.setTo(new JID(to.getNode(), serverName, null, true));
     }      packet.setBody(message);
     packetRouter.route(packet);
}

You will have to ensure, that only your application can use this interface, otherwise it would be a security risk!

1 Like