From cebdb87387948cf21ff81b22e12728461af226fe Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Thu, 2 Mar 2017 15:30:34 +0300 Subject: [PATCH] Revert "Remove relict mod_service_log" This reverts commit 0d4e1abbd11155a1cddbe809ff09183af1427af8. However, stanzas are now encapsulated into forwarded element (as per XEP-0297) instead of nonza. --- src/mod_service_log.erl | 105 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/mod_service_log.erl diff --git a/src/mod_service_log.erl b/src/mod_service_log.erl new file mode 100644 index 000000000..d29cd1329 --- /dev/null +++ b/src/mod_service_log.erl @@ -0,0 +1,105 @@ +%%%---------------------------------------------------------------------- +%%% File : mod_service_log.erl +%%% Author : Alexey Shchepin +%%% Purpose : Copy user messages to logger service +%%% Created : 24 Aug 2003 by Alexey Shchepin +%%% +%%% +%%% ejabberd, Copyright (C) 2002-2017 ProcessOne +%%% +%%% This program is free software; you can redistribute it and/or +%%% modify it under the terms of the GNU General Public License as +%%% published by the Free Software Foundation; either version 2 of the +%%% License, or (at your option) any later version. +%%% +%%% This program is distributed in the hope that it will be useful, +%%% but WITHOUT ANY WARRANTY; without even the implied warranty of +%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +%%% General Public License for more details. +%%% +%%% You should have received a copy of the GNU General Public License along +%%% with this program; if not, write to the Free Software Foundation, Inc., +%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +%%% +%%%---------------------------------------------------------------------- + +-module(mod_service_log). + +-author('alexey@process-one.net'). + +-behaviour(gen_mod). + +-export([start/2, stop/1, log_user_send/1, + log_user_receive/1, mod_opt_type/1, depends/2]). + +-include("ejabberd.hrl"). +-include("logger.hrl"). + +-include("xmpp.hrl"). + +start(Host, _Opts) -> + ejabberd_hooks:add(user_send_packet, Host, ?MODULE, + log_user_send, 50), + ejabberd_hooks:add(user_receive_packet, Host, ?MODULE, + log_user_receive, 50), + ok. + +stop(Host) -> + ejabberd_hooks:delete(user_send_packet, Host, ?MODULE, + log_user_send, 50), + ejabberd_hooks:delete(user_receive_packet, Host, + ?MODULE, log_user_receive, 50), + ok. + +depends(_Host, _Opts) -> + []. + +-spec log_user_send({stanza(), ejabberd_c2s:state()}) -> {stanza(), ejabberd_c2s:state()}. +log_user_send({Packet, C2SState}) -> + From = xmpp:get_from(Packet), + To = xmpp:get_to(Packet), + log_packet(From, To, Packet, From#jid.lserver), + {Packet, C2SState}. + +-spec log_user_receive({stanza(), ejabberd_c2s:state()}) -> {stanza(), ejabberd_c2s:state()}. +log_user_receive({Packet, C2SState}) -> + From = xmpp:get_from(Packet), + To = xmpp:get_to(Packet), + log_packet(From, To, Packet, To#jid.lserver), + {Packet, C2SState}. + +-spec log_packet(jid(), jid(), stanza(), binary()) -> ok. +log_packet(From, To, Packet, Host) -> + Loggers = gen_mod:get_module_opt(Host, ?MODULE, loggers, + fun(L) -> + lists:map( + fun(S) -> + B = iolist_to_binary(S), + N = jid:nameprep(B), + if N /= error -> + N + end + end, L) + end, []), + ServerJID = jid:make(Host), + FixedPacket = xmpp:set_from_to(Packet, From, To), + lists:foreach(fun (Logger) -> + ejabberd_router:route(ServerJID, + jid:make(Logger), + #xmlel{name = <<"route">>, + attrs = [], + children = + [FixedPacket]}) + end, + Loggers). + +mod_opt_type(loggers) -> + fun (L) -> + lists:map(fun (S) -> + B = iolist_to_binary(S), + N = jid:nameprep(B), + if N /= error -> N end + end, + L) + end; +mod_opt_type(_) -> [loggers]. -- 2.40.0