processone / ejabberd

Robust, Ubiquitous and Massively Scalable Messaging Platform (XMPP, MQTT, SIP Server)
https://www.process-one.net/en/ejabberd/
Other
6.07k stars 1.51k forks source link

How to route a MUC message ? (question) #2000

Closed andreabenini closed 7 years ago

andreabenini commented 7 years ago

What version of ejabberd are you using?

16.04

What operating system (version) are you using?

linux x86, AMD64

How did you install ejabberd (source, package, distribution)?

package

What did not work as expected? Are there error messages in the log? What was the unexpected behavior? What was the expected result?

I have created a module of mine, after some checks and when I need it I'd like to route a generated message from my module to a MUC user. Target user is in the room in that moment. I'm trying to execute this code:

    ejabberd_router:route({jid,<<"redroom">>,<<"domain">>,<<>>,<<"redroom">>,<<"domain">>,<<>>},
                          {jid,<<"benand">>,<<"domain">>,<<>>,<<"benand">>,<<"domain">>,<<>>},
                          {xmlel,<<"message">>,[{<<"from">>,<<"redroom@domain">>},
                                                {<<"to">>,<<"benand@domain">>},
                                                {<<"type">>,<<"groupchat">>},
                                                {<<"id">>,<<"purple534aa427">>}],
                                               [{xmlel,<<"body">>,[],[{xmlcdata,<<"Payload with something [purple534aa427]">>}] }]
                          }).

Even when executed on the debug console I can always receive an 'ok' but no message is routed to the user. But, when in debug mode I route a message like this one:

    ejabberd_router:route({jid,<<"begann">>,<<"domain">>,<<>>,<<"begann">>,<<"domain">>,<<>>},
                          {jid,<<"benand">>,<<"domain">>,<<>>,<<"benand">>,<<"domain">>,<<>>},
                          {xmlel,<<"message">>,[{<<"from">>,<<"begann@domain">>},
                                                {<<"to">>,<<"benand@domain">>},
                                                {<<"type">>,<<"chat">>},
                                                {<<"id">>,<<"purple534aa427">>}],
                                               [{xmlel,<<"body">>,[],[{xmlcdata,<<"Payload with something [purple534aa427]">>}] }]
                          }).

I don't have problems at all. Can you please tell me what am I doing wrong ? Thanks in advance for your reply

Ben

zinid commented 7 years ago

You're advised not to write code for 16.x, because the internal AP has completely changed in 17.x, you'll have to rewrite a lot of code if you want to migrate. Regarding 17.x, routing is quite simple, your example will look like:

ejabberd_router:route(
  #message{
     type = groupchat,
     from = jid:make(<<"begann">>, <<"domain">>),
     to = jid:make(<<"room">>, <<"conference.domain">>, <<"nick">>),
     body = xmpp:mk_text(<<"Payload with something [purple534aa427]">>)}).

Note that the message sender should be joined to the room. You're also encouraged to read the Developer Guide and the xmpp library documentation

andreabenini commented 7 years ago

Yes I know about 17.x but I already have that 16.4 server (created last year) with a lot of custom modules and my manager still wants to add stuff there. Destination user is online and has joined the room but unfortunately I cannot see messages coming from console command but only from a real message from a client.

zinid commented 7 years ago

Well, from your example it's unclear where you send a message to. You're saying about MUC, but it's not reflected in your example, seems like you're just trying to send a 1-to-1 chat message.

zinid commented 7 years ago

If you want to send a message to all participants, you need to send a message as described in https://xmpp.org/extensions/xep-0045.html#message If you want to send a message to a single user in the room, you should proceed as described in https://xmpp.org/extensions/xep-0045.html#privatemessage

andreabenini commented 7 years ago

Thanks, I got it. I'm trying to send a message in the debug console first and then I'll port it to a module later, I need to inform just one user about a specific event. Source point is/(should be) the room: 'redroom@conference.domain.xxx', destination target is the user: 'benand@domain.xxx'. After reading that XEP (basic one but I forgot it) I guess I'll try to send a private message (it should work even if user is not in that room) or some sort of message/event (like "Receiving Events" on https://docs.ejabberd.im/developer/xmpp-clients-bots/proposed-extensions/muc-sub/ ).

andreabenini commented 7 years ago

I have solved it with a dirty hack, I'm planning to move to latest version (when my boss agrees) but I have to stick with this version for a while so here's a workaround. MucSub is an interesting feature because it can deliver messages even to offline (or mobile) users or to someone that's not necessarily inside the MUC, I have created a message with <event/> tag inside it just like the "receiving events" sample provided in the MucSub documentation and it works. Now my module can send special notifications to someone inside/outside the room without breaking things.

<message from="coven@muc.shakespeare.example" to="hag66@shakespeare.example/pda">
  <event xmlns="http://jabber.org/protocol/pubsub#event">
    <items node="urn:xmpp:mucsub:nodes:messages">
      <item id="18277869892147515942">
        <message from="coven@muc.shakespeare.example/secondwitch" to="hag66@shakespeare.example/pda" type="groupchat" xmlns="jabber:client">
          <archived xmlns="urn:xmpp:mam:tmp" by="muc.shakespeare.example" id="1467896732929849" />
          <stanza-id xmlns="urn:xmpp:sid:0" by="muc.shakespeare.example" id="1467896732929849" />
          <body>Hello from the MUC room !</body>
        </message>
      </item>
    </items>
  </event>
</message>

IMHO, MUCs are a real mess and problems might rise when you've a lot of them (memory for example), probably pubsub is the way to go and mucsub might be used only when strictly necessary

zinid commented 7 years ago

This depends on MUC implementation. Correct and fast pubsub implementation is much harder to do actually.

lock[bot] commented 5 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.