powered by Jive Software

Creating a Chat Room via Plugin

Hi everyone, I’m new to OpenFire developement and am attempting to create a chatroom via a plugin. After getting lost in the OpenFire API, I searched the Developer forums to no avail. Could someone please provide me with a code sample for how I would go about doing this? I would certainly appreciate it!

Also, I’d like to invite users to join the chatroom. Tips on that would be appreciated.

Thank you!

acm

Hi acm,

You’ll want to take a look at the MultiUserChatServiceImpl part of the Openfire javadocs. In order to create a room and send invitation you’d do something like:

XMPPServer server = XMPPServer.getInstance();
MUCRoom room = server.getMultiUserChatServer().getChatRoom("hill", server.createJID("jack", null));
room.sendInvitation(server.createJID("jill", null), null, room.getRole(), null);

Hope that helps,

Ryan

Thanks Ryan, I certainly appreciated your quick response. This method server.getMultiUserChatServer() must be new (or old) to the codebase, because th 3.6.0 source code has a MultiUserChatManager which manages multiple muc servers.

In any event, I was able to get something going. The only problem now is that my chat client (Spark) says “**null **is inviting you to join a group.” I don’t know if this is a problem with the client or if somehow I can set the invititing jid that I just can’t find. If I can’t figure this out, I think how it works as-is is acceptable for what I’m using it for.

The code I came up with is below:

public void createChatroom(String chatroomName, Collection<JID> invitees, String inviteReason) {
         try {
              XMPPServer server = XMPPServer.getInstance();               if(server.getMultiUserChatManager().getMultiUserChatServicesCount() == 0) {
                   server.getMultiUserChatManager().createMultiUserChatService("conference", null, false);
              }
              MultiUserChatService service = server.getMultiUserChatManager().getMultiUserChatServices().get(0);
              LocalMUCRoom room = (LocalMUCRoom)service.getChatRoom(chatroomName, server.createJID("autoChatroomCreator", null));
              room.unlock(room.getRole());               for(JID j : invitees) {
                   room.sendInvitation(j, inviteReason, room.getRole(), null);
              }
         }
         catch(Exception e) {
              componentManager.getLog().debug("*** Exception in createChatroom()");
              componentManager.getLog().error(e);
         }
    }

Thanks again,

acm

Hi,

I think this is a bug in openfire, since in the LocalMUCRoom is

    **public** **void** sendInvitation(JID to, String reason, MUCRole senderRole, List extensions)
            **throws** ForbiddenException, CannotBeInvitedException {
        ...
        // ChatUser will be null if the room itself (ie. via admin console) made the request
        **if** (senderRole.getUserAddress() != ****null****) {
            frag.addElement("invite").addAttribute("from", senderRole.getUserAddress().toBareJID());
        }
        ...
    }

and in XEP-0045.html says “The room@service itself MUST then add a ‘from’ address to the element…” but on the other hand it’s difficult to add the invitor to the from attribute if there is none. I tested your code and my pidgin client just ignore the invitation. Maybe you should specify an invator or add a bot in the room and then call

room.sendInvitation(j, inviteReason, role, **null**);

with the MUCRole of the invitor.

Thanks for testing my code! I’d love to set the invitor, but I haven’t figured out how to do that yet.

After reading your comments about pidgin, I installed pidgin 2.5.1 for Windows (pidgin-2.5.1-no-gtk.exe) and tested the code. I got this message from Pidgin:

Accept chat invitation?

room@conference.chatserver has invited acm@chatserver/Home to the chat room room@conference.chatserver

Better than null! I don’t know why we’d have different results. I’m running the client and server on the same box, but that shouldn’t matter.

Thanks,

acm

Hi Master;

I am creating ChatRoom plugin. The user will chat via private message, but this message will be broadcasted to all room members.
How can detect the message and decide it is MUC message via plugin on that case? I will not use the conference window in the IM client.

Thank you;
Edui