Update to work with ejabberd 17.06

This commit is contained in:
Badlop 2017-06-07 16:40:34 +02:00
parent 00e76aec2c
commit ce89412feb
1 changed files with 36 additions and 23 deletions

View File

@ -13,17 +13,17 @@
-export([start/2, -export([start/2,
init/1, init/1,
stop/1, stop/1,
log_packet_send/4, depends/2,
log_packet_receive/5, log_packet_send/1,
log_packet_receive/1,
mod_opt_type/1]). mod_opt_type/1]).
-ifndef(LAGER). -ifndef(LAGER).
-define(LAGER, 1). -define(LAGER, 1).
-endif. -endif.
-include("ejabberd.hrl").
-include("logger.hrl"). -include("logger.hrl").
-include("jlib.hrl"). -include("xmpp.hrl").
-define(PROCNAME, ?MODULE). -define(PROCNAME, ?MODULE).
-define(DEFAULT_PATH, "."). -define(DEFAULT_PATH, ".").
@ -33,12 +33,13 @@
start(Host, Opts) -> start(Host, Opts) ->
?DEBUG(" ~p ~p~n", [Host, Opts]), ?DEBUG(" ~p ~p~n", [Host, Opts]),
case gen_mod:get_opt(host_config, Opts, fun(L) when is_list(L) -> L end, []) of case gen_mod:get_opt(host_config, Opts, []) of
[] -> [] ->
start_vh(Host, Opts); start_vh(Host, Opts);
HostConfig -> HostConfig ->
start_vhs(Host, HostConfig) start_vhs(Host, HostConfig)
end. end,
ok.
start_vhs(_, []) -> start_vhs(_, []) ->
ok; ok;
@ -50,8 +51,8 @@ start_vhs(Host, [{_VHost, _Opts}| Tail]) ->
?DEBUG("start_vhs ~p ~p~n", [Host, [{_VHost, _Opts}| Tail]]), ?DEBUG("start_vhs ~p ~p~n", [Host, [{_VHost, _Opts}| Tail]]),
start_vhs(Host, Tail). start_vhs(Host, Tail).
start_vh(Host, Opts) -> start_vh(Host, Opts) ->
Path = gen_mod:get_opt(path, Opts, fun iolist_to_binary/1, ?DEFAULT_PATH), Path = gen_mod:get_opt(path, Opts, ?DEFAULT_PATH),
Format = gen_mod:get_opt(format, Opts, fun(A) when is_atom(A) -> A end, ?DEFAULT_FORMAT), Format = gen_mod:get_opt(format, Opts, ?DEFAULT_FORMAT),
ejabberd_hooks:add(user_send_packet, Host, ?MODULE, log_packet_send, 55), ejabberd_hooks:add(user_send_packet, Host, ?MODULE, log_packet_send, 55),
ejabberd_hooks:add(user_receive_packet, Host, ?MODULE, log_packet_receive, 55), ejabberd_hooks:add(user_receive_packet, Host, ?MODULE, log_packet_receive, 55),
register(gen_mod:get_module_proc(Host, ?PROCNAME), register(gen_mod:get_module_proc(Host, ?PROCNAME),
@ -78,18 +79,30 @@ stop(Host) ->
gen_mod:get_module_proc(Host, ?PROCNAME) ! stop, gen_mod:get_module_proc(Host, ?PROCNAME) ! stop,
ok. ok.
log_packet_send(Packet, _C2SState, From, To) -> -spec depends(binary(), gen_mod:opts()) -> [{module(), hard | soft}].
depends(_Host, _Opts) ->
[].
log_packet_send({Packet, C2SState}) ->
From = xmpp:get_from(Packet),
To = xmpp:get_to(Packet),
log_packet(From, To, Packet, From#jid.lserver), log_packet(From, To, Packet, From#jid.lserver),
Packet. {Packet, C2SState}.
log_packet_receive(Packet, _C2SState, _JID, From, To) when From#jid.lserver == To#jid.lserver-> log_packet_receive({Packet, C2SState}) ->
Packet; % only log at send time if the message is local to the server From = xmpp:get_from(Packet),
log_packet_receive(Packet, _C2SState, _JID, From, To) -> To = xmpp:get_to(Packet),
log_packet(From, To, Packet, To#jid.lserver), %% only log at send time if the message is local to the server
Packet. case From#jid.lserver == To#jid.lserver of
true ->
ok;
false ->
log_packet(From, To, Packet, To#jid.lserver)
end,
{Packet, C2SState}.
log_packet(From, To, Packet = #xmlel{name = <<"message">>, attrs = Attrs}, Host) -> log_packet(From, To, #message{type = Type} = Packet, Host) ->
case fxml:get_attr_s(<<"type">>, Attrs) of case Type of
<<"groupchat">> -> %% mod_muc_log already does it <<"groupchat">> -> %% mod_muc_log already does it
?DEBUG("dropping groupchat: ~s", [fxml:element_to_binary(Packet)]), ?DEBUG("dropping groupchat: ~s", [fxml:element_to_binary(Packet)]),
ok; ok;
@ -109,21 +122,21 @@ write_packet(From, To, Packet, Host) ->
Result Result
end, end,
Format = Config#config.format, Format = Config#config.format,
{Subject, Body} = {case fxml:get_subtag(Packet, <<"subject">>) of {Subject, Body} = {case Packet#message.subject of
false -> false ->
""; "";
SubjEl -> SubjEl ->
escape(Format, fxml:get_tag_cdata(SubjEl)) escape(Format, xmpp:get_text(SubjEl))
end, end,
escape(Format, fxml:get_path_s(Packet, [{elem, <<"body">>}, cdata]))}, escape(Format, xmpp:get_text(Packet#message.body))},
case Subject == [] andalso Body == [] of case Subject == <<>> andalso Body == <<>> of
true -> %% don't log empty messages true -> %% don't log empty messages
?DEBUG("not logging empty message from ~s",[jlib:jid_to_string(From)]), ?DEBUG("not logging empty message from ~s",[jlib:jid_to_string(From)]),
ok; ok;
false -> false ->
Path = Config#config.path, Path = Config#config.path,
FromJid = [From#jid.luser, "@", From#jid.lserver], FromJid = jid:encode(jid:make(From#jid.luser, From#jid.lserver)),
ToJid = [To#jid.luser, "@", To#jid.lserver], ToJid = jid:encode(jid:make(To#jid.luser, To#jid.lserver)),
{FilenameTemplate, DateString, Header, MessageType} = {FilenameTemplate, DateString, Header, MessageType} =
case calendar:local_time() of case calendar:local_time() of
{{Y, M, D}, {H, Min, S}} -> {{Y, M, D}, {H, Min, S}} ->