I am using Ejabberd server for chatting application. It works to save messages in arhieve table, but I want to save additional field in the table when message is sent. This field will be per message.
-
Do you use mod_mam module? – Mani Kandan Oct 15 '16 at 12:19
-
Yes I use mod_mam to log message in database(mysql) – user1820017 Oct 16 '16 at 19:08
2 Answers
There are some ways to achieve this. The first and the simplest way (but it only affects 'xml' field in the 'archive' table) is an implementation of 'store_mam_message' hook in your custom module. You can modify Packet inside that hook and return new packet which should be saved in the DB. This hook is available since v16.09
If you modified 'archive' table (a new column has been added to it), then the second way helps:
Create your custom module and call it "mod_mam_sql2"
Copy the content of "src/mod_mam_sql.erl" into your custom module
Find the function store/7.
store(Pkt, LServer, {LUser, LHost}, Type, Peer, Nick, _Dir) -> TSinteger = p1_time_compat:system_time(micro_seconds), ID = jlib:integer_to_binary(TSinteger), SUser = case Type of chat -> LUser; groupchat -> jid:to_string({LUser, LHost, >}) end, BarePeer = jid:to_string( jid:tolower( jid:remove_resource(Peer))), LPeer = jid:to_string( jid:tolower(Peer)), XML = fxml:element_to_binary(Pkt), Body = fxml:get_subtag_cdata(Pkt, >), SType = jlib:atom_to_binary(Type), case ejabberd_sql:sql_query( LServer, ?SQL("insert into archive (username, timestamp," " peer, bare_peer, xml, txt, kind, nick) values (" "%(SUser)s, " "%(TSinteger)d, " "%(LPeer)s, " "%(BarePeer)s, " "%(XML)s, " "%(Body)s, " "%(SType)s, " "%(Nick)s)")) of {updated, _} -> {ok, ID}; Err -> Err end.
Change the SQL-query as you need
Compile your custom module: ejabberdctl module_install mod_mam_sql2
Update ejabberd.yml config file
mod_mam: db_type: sql2
Restart ejabberd server: ejabberdctl restart
I hope it will help you to solve your issue.

- 69
- 2
- 2
Igor, Thanks for your answer, looks promising, but I did something else which I want to share here. I have installed ejabberd from source code with help of this link http://www.blikoon.com/networking/how-to-install-ejabberd-on-linux-ubuntu then I modified mod_mam_sql.erl file in src folder. Code I modified is as follow. I recompiled module and it worked.
Body = fxml:get_subtag_cdata(Pkt, <<"body">>),
Resid = fxml:get_subtag_cdata(Pkt, <<"resid">>),
Ownuid = fxml:get_subtag_cdata(Pkt, <<"ownuid">>),
SType = jlib:atom_to_binary(Type),
case ejabberd_sql:sql_query(
LServer,
?SQL("insert into archive (username, timestamp,"
" peer, bare_peer, xml, txt, kind, nick,resid,Ownuid) values ("
"%(SUser)s, "
"%(TSinteger)d, "
"%(LPeer)s, "
"%(BarePeer)s, "
"%(XML)s, "
"%(Body)s, "
"%(SType)s, "
"%(Nick)s, "
"%(Resid)s, "
"%(Ownuid)s)")) of
{updated, _} ->
{ok, ID};

- 61
- 1
- 2
- 10