Retrieve chat room history from a module

Hi Guys,
I'm building a module of mine for getting few data from a running ejabberd server. I'd like to get few data from chat history (user or chatroom) and publish it on a web app. I'm currently using mod_mam (xep 0313) with a v16.04 and a PostgreSQL db as backend. mod_mam works fine, history_size is properly set on ejabberd.yml, messages are correctly stored in the db (I can see them as records) and I'm now trying to retrieve them from a module.

I'm aware I can get a chat history with a client, I can:
- use an existing account and enter in a chatroom with it
- issue a command like (PSI here...):

<presence to="test@conference.myserver/test1">
    <priority>5</priority>
    <x xmlns="http://jabber.org/protocol/muc"/>
</presence>

or use something like:

<iq type='set' id='112233' to='test@conference.myserver'>
    <query xmlns='urn:xmpp:mam:1' queryid='f27' />
</iq>

this works when I'm a client or I enter inside that specific room.
In my case I just want to get these msgs from a server module, I'm not an user (client) so I guess I need to use some internal FUNs instead of issuing xmpp commands.
I have also seen topics like this or modules like mod_muc_log_http but I really don't know what should I call.
How can I get messages (already stored) for my module ?

Thanks in advance
Ben

mod_mam has the function

mod_mam has the function select_and_send that searches in the database the matching messages and directly routes them to the corresponding user. Check what arguments are used inside it, and what is returned (you can add debug lines that print arguments and variables, then trigger that function using a Jabber client, and so you get internal details.

First of all, export the select/5 function adding this line, recompile, reinstall and restart:

diff --git a/src/mod_mam.erl b/src/mod_mam.erl
index f9ef104..cbb8ca6 100644
--- a/src/mod_mam.erl
+++ b/src/mod_mam.erl
@@ -33,6 +33,7 @@
 %% API
 -export([start/2, stop/1, depends/2]).
 
+-export([select/5]).
 -export([user_send_packet/4, user_send_packet_strip_tag/4, user_receive_packet/5,
         process_iq_v0_2/1, process_iq_v0_3/1, disco_sm_features/5,
         remove_user/2, remove_room/3, mod_opt_type/1, muc_process_iq/2,

Then I can run this call:

mod_mam:select(
  <<"localhost">>,
  {jid,<<"user1">>,<<"localhost">>,<<"tka1">>,<<"user1">>,<<"localhost">>,<<"tka1">>},
  {jid,<<"user1">>,<<"localhost">>,<<"tka1">>,<<"user1">>,<<"localhost">>,<<"tka1">>},
  {mam_query,<<"urn:xmpp:mam:1">>,<<"f27">>,undefined,undefined,undefined,undefined,
        {rsm_set,undefined,undefined,undefined,undefined,undefined,undefined,50},
           undefined},chat).

and I get this result:

{[{<<"1479721806878830">>,1479721806878830,
   {forwarded,{delay,{1479,721806,878830},
                     {jid,<<>>,<<"localhost">>,<<>>,<<>>,<<"localhost">>,<<>>},
                     <<>>},
              [{xmlel,<<"message">>,
                      [{<<"xml:lang">>,<<"es">>},
                       {<<"to">>,<<"user2@localhost">>},
                       {<<"from">>,<<"user1@localhost/tka1">>},
                       {<<"type">>,<<"chat">>},
                       {<<"id">>,<<"255:457805">>},
                       {<<"xmlns">>,<<"jabber:client">>}],
                      [{xmlel,<<"body">>,[],[{xmlcdata,<<"aaa">>}]}]}]}},
  {<<"1479721808024066">>,1479721808024066,
   {forwarded,{delay,{1479,721808,24066},
                     {jid,<<>>,<<"localhost">>,<<>>,<<>>,<<"localhost">>,<<>>},
                     <<>>},
              [{xmlel,<<"message">>,
                      [{<<"xml:lang">>,<<"es">>},
                       {<<"to">>,<<"user2@localhost">>},
                       {<<"from">>,<<"user1@localhost/tka1">>},
                       {<<"type">>,<<"chat">>},
                       {<<"id">>,<<"256:336062">>},
                       {<<"xmlns">>,<<"jabber:client">>}],
                      [{xmlel,<<"body">>,[],[{xmlcdata,<<"sss">>}]}]}]}}],
 true,2}

Solved and glad to have a

Solved and glad to have a working solution now.
I have used your hints reported above and I have figured how can I use SELECT without altering the official tree or exporting other functions from these modules.
I have detected the inner mod_mam_* used to deal with the db (mod_mam_sql in my case) and used it direcly from my code, I have reported details on this topic in the issues available on github as well (here) and closed the problem.
Code is helpful, thanks

Why the select/6 function was

Why the select/6 function was not export by default in mod_mam module? Is there any benefits in use this function instead of access directly the database?

Syndicate content