From a0908ba393ec6ba4e42db7684ea6c251e21be932 Mon Sep 17 00:00:00 2001 From: Lamtei W Date: Sun, 7 May 2017 18:28:11 +0530 Subject: [PATCH] Added export function for mam module --- src/ejd2sql.erl | 1 + src/mod_mam.erl | 6 +++++- src/mod_mam_sql.erl | 43 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/ejd2sql.erl b/src/ejd2sql.erl index c775cd7bf..343c3518a 100644 --- a/src/ejd2sql.erl +++ b/src/ejd2sql.erl @@ -53,6 +53,7 @@ modules() -> mod_caps, mod_irc, mod_last, + mod_mam, mod_muc, mod_offline, mod_privacy, diff --git a/src/mod_mam.erl b/src/mod_mam.erl index 947fb5fbe..c84cb907b 100644 --- a/src/mod_mam.erl +++ b/src/mod_mam.erl @@ -37,7 +37,7 @@ remove_user/2, remove_room/3, mod_opt_type/1, muc_process_iq/2, muc_filter_message/3, message_is_archived/3, delete_old_messages/2, get_commands_spec/0, msg_to_el/4, get_room_config/4, set_room_option/3, - offline_message/1]). + offline_message/1, export/1]). -include("xmpp.hrl"). -include("logger.hrl"). @@ -457,6 +457,10 @@ delete_old_messages(TypeBin, Days) when TypeBin == <<"chat">>; delete_old_messages(_TypeBin, _Days) -> unsupported_type. +export(LServer) -> + Mod = gen_mod:db_mod(LServer, ?MODULE), + Mod:export(LServer). + %%%=================================================================== %%% Internal functions %%%=================================================================== diff --git a/src/mod_mam_sql.erl b/src/mod_mam_sql.erl index d2fd48a9f..7ae3bb87f 100644 --- a/src/mod_mam_sql.erl +++ b/src/mod_mam_sql.erl @@ -30,7 +30,7 @@ %% API -export([init/2, remove_user/2, remove_room/3, delete_old_messages/3, - extended_fields/0, store/7, write_prefs/4, get_prefs/2, select/6]). + extended_fields/0, store/7, write_prefs/4, get_prefs/2, select/6, export/1]). -include_lib("stdlib/include/ms_transform.hrl"). -include("xmpp.hrl"). @@ -181,6 +181,47 @@ select(LServer, JidRequestor, #jid{luser = LUser} = JidArchive, {[], false, 0} end. +export(_Server) -> + [{archive_prefs, + fun(Host, #archive_prefs{us = + {LUser, LServer}, + default = Default, + always = Always, + never = Never}) + when LServer == Host -> + SDefault = erlang:atom_to_binary(Default, utf8), + SAlways = misc:term_to_expr(Always), + SNever = misc:term_to_expr(Never), + [?SQL("insert into archive_prefs (username, def, always, never) values" + "(%(LUser)s, %(SDefault)s, %(SAlways)s, %(SNever)s)")]; + (_Host, _R) -> + [] + end}, + {archive_msg, + fun(Host, #archive_msg{us ={_LUser, LServer}, + id = _ID, timestamp = TS, peer = Peer, + bare_peer = {PUser, PServer, <<>>}, + type = Type, nick = Nick, packet = Pkt}) + when LServer == Host -> + TStmp = now_to_usec(TS), + SUser = case Type of + chat -> PUser; + groupchat -> jid:to_string({PUser, PServer, <<>>}) + 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, <<"body">>), + SType = jlib:atom_to_binary(Type), + [?SQL("insert into archive (username, timestamp, " + "peer, bare_peer, xml, txt, kind, nick) " + "values (%(SUser)s, %(TStmp)d, %(LPeer)s, " + "%(BarePeer)s, %(XML)s, %(Body)s, %(SType)s, " + "%(Nick)s)")]; + (_Host, _R) -> + [] + end}]. + %%%=================================================================== %%% Internal functions %%%=================================================================== -- 2.40.0