Make mod_post_log working with ejabberd 16.12+
This commit is contained in:
parent
da878bd75c
commit
3dc4494ee1
|
@ -14,11 +14,14 @@
|
||||||
|
|
||||||
-export([start/2,
|
-export([start/2,
|
||||||
stop/1,
|
stop/1,
|
||||||
|
depends/2,
|
||||||
|
mod_opt_type/1,
|
||||||
|
log_user_send/1,
|
||||||
log_user_send/4,
|
log_user_send/4,
|
||||||
post_result/1]).
|
post_result/1]).
|
||||||
|
|
||||||
-include("ejabberd.hrl").
|
-include("ejabberd.hrl").
|
||||||
-include("jlib.hrl").
|
-include("xmpp.hrl").
|
||||||
|
|
||||||
start(Host, _Opts) ->
|
start(Host, _Opts) ->
|
||||||
ok = case inets:start() of
|
ok = case inets:start() of
|
||||||
|
@ -36,54 +39,57 @@ stop(Host) ->
|
||||||
?MODULE, log_user_send, 50),
|
?MODULE, log_user_send, 50),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
log_user_send(Packet, _C2SState, From, To) ->
|
depends(_Host, _Opts) ->
|
||||||
ok = log_packet(From, To, Packet),
|
[].
|
||||||
|
|
||||||
|
mod_opt_type(url) -> fun iolist_to_binary/1;
|
||||||
|
mod_opt_type(ts_header) -> fun iolist_to_binary/1;
|
||||||
|
mod_opt_type(from_header) -> fun iolist_to_binary/1;
|
||||||
|
mod_opt_type(to_header) -> fun iolist_to_binary/1;
|
||||||
|
mod_opt_type(headers) -> fun(L) when is_list(L) -> L end;
|
||||||
|
mod_opt_type(content_type) -> fun iolist_to_binary/1;
|
||||||
|
mod_opt_type(req_options) -> fun(L) when is_list(L) -> L end;
|
||||||
|
mod_opt_type(_) ->
|
||||||
|
[url, ts_header, from_header, to_header, headers,
|
||||||
|
content_type, req_options].
|
||||||
|
|
||||||
|
%% TODO: remove log_user_send/4 after 17.02 is released
|
||||||
|
log_user_send(Packet, C2SState, From, To) ->
|
||||||
|
log_user_send({xmpp:set_from_to(Packet, From, To), C2SState}),
|
||||||
Packet.
|
Packet.
|
||||||
|
|
||||||
log_packet(From, To, #xmlel{name = <<"message">>} = Packet) ->
|
log_user_send({#message{type = T} = Packet, _C2SState} = Acc)
|
||||||
ok = log_message(From, To, Packet);
|
when T == chat; T == groupchat ->
|
||||||
|
ok = log_message(Packet),
|
||||||
|
Acc;
|
||||||
|
log_user_send(Acc) ->
|
||||||
|
Acc.
|
||||||
|
|
||||||
log_packet(_From, _To, _Packet) ->
|
log_message(#message{from = From, to = To, body = Body} = Msg) ->
|
||||||
ok.
|
case xmpp:get_text(Body) of
|
||||||
|
<<"">> ->
|
||||||
log_message(From, To, #xmlel{attrs = Attrs} = Packet) ->
|
|
||||||
Type = lists:keyfind(<<"type">>, 1, Attrs),
|
|
||||||
log_message_filter(Type, From, To, Packet).
|
|
||||||
|
|
||||||
log_message_filter({<<"type">>, Type}, From, To, Packet)
|
|
||||||
when Type =:= <<"chat">>;
|
|
||||||
Type =:= <<"groupchat">> ->
|
|
||||||
log_chat(From, To, Packet);
|
|
||||||
log_message_filter(_Other, _From, _To, _Packet) ->
|
|
||||||
ok.
|
|
||||||
|
|
||||||
log_chat(From, To, #xmlel{children = Els} = Packet) ->
|
|
||||||
case get_body(Els) of
|
|
||||||
no_body ->
|
|
||||||
ok;
|
ok;
|
||||||
{ok, _Body} ->
|
_ ->
|
||||||
log_chat_with_body(From, To, Packet)
|
XML = fxml:element_to_binary(xmpp:encode(Msg)),
|
||||||
|
post_xml(From, To, XML)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
log_chat_with_body(From, To, Packet) ->
|
post_xml(#jid{lserver = LServer} = From, To, Xml) ->
|
||||||
post_xml(From, To, fxml:element_to_binary(Packet)).
|
|
||||||
|
|
||||||
post_xml(From, To, Xml) ->
|
|
||||||
Ts = to_iso_8601_date(os:timestamp()),
|
Ts = to_iso_8601_date(os:timestamp()),
|
||||||
|
|
||||||
Body = Xml,
|
Body = Xml,
|
||||||
|
|
||||||
Url = get_opt(url),
|
Url = get_opt(LServer, url),
|
||||||
TsHeader = get_opt(ts_header, "X-Message-Timestamp"),
|
TsHeader = get_opt(LServer, ts_header, "X-Message-Timestamp"),
|
||||||
FromHeader = get_opt(from_header, "X-Message-From"),
|
FromHeader = get_opt(LServer, from_header, "X-Message-From"),
|
||||||
ToHeader = get_opt(to_header, "X-Message-To"),
|
ToHeader = get_opt(LServer, to_header, "X-Message-To"),
|
||||||
Headers = [ {TsHeader, Ts},
|
Headers = [ {TsHeader, Ts},
|
||||||
{FromHeader, format_jid(From)},
|
{FromHeader, format_jid(From)},
|
||||||
{ToHeader, format_jid(To)}
|
{ToHeader, format_jid(To)}
|
||||||
| get_opt(headers, []) ],
|
| get_opt(LServer, headers, []) ],
|
||||||
ContentType = get_opt(content_type, "text/xml"),
|
ContentType = get_opt(LServer, content_type, "text/xml"),
|
||||||
HttpOptions = get_opt(http_options, []),
|
HttpOptions = get_opt(LServer, http_options, []),
|
||||||
ReqOptions = get_opt(req_options, []),
|
ReqOptions = get_opt(LServer, req_options, []),
|
||||||
|
|
||||||
{ok, _ReqId} = httpc:request(post,
|
{ok, _ReqId} = httpc:request(post,
|
||||||
{Url, Headers, ContentType, Body},
|
{Url, Headers, ContentType, Body},
|
||||||
|
@ -109,41 +115,20 @@ post_result({_ReqId, Result}) ->
|
||||||
ok
|
ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
get_body(Els) ->
|
get_opt(LServer, Opt) ->
|
||||||
XmlEls = [ El || El <- Els, is_record(El, xmlel) ],
|
get_opt(LServer, Opt, undefined).
|
||||||
case lists:keyfind(<<"body">>, #xmlel.name, XmlEls) of
|
|
||||||
false ->
|
|
||||||
no_body;
|
|
||||||
#xmlel{children = InnerEls} ->
|
|
||||||
case lists:keyfind(xmlcdata, 1, InnerEls) of
|
|
||||||
false ->
|
|
||||||
no_body;
|
|
||||||
{xmlcdata, Body} ->
|
|
||||||
{ok, Body}
|
|
||||||
end
|
|
||||||
end.
|
|
||||||
|
|
||||||
get_opt(Opt) ->
|
get_opt(LServer, Opt, Default) ->
|
||||||
get_opt(Opt, undefined).
|
|
||||||
|
|
||||||
get_opt(Opt, Default) ->
|
|
||||||
F = fun(Val) when is_binary(Val) -> binary_to_list(Val);
|
F = fun(Val) when is_binary(Val) -> binary_to_list(Val);
|
||||||
(Val) -> Val
|
(Val) -> Val
|
||||||
end,
|
end,
|
||||||
gen_mod:get_module_opt(global, ?MODULE, Opt, F, Default).
|
gen_mod:get_module_opt(LServer, ?MODULE, Opt, F, Default).
|
||||||
|
|
||||||
report_error(ReportArgs) ->
|
report_error(ReportArgs) ->
|
||||||
ok = error_logger:error_report([ mod_post_log_cannot_post | ReportArgs ]).
|
ok = error_logger:error_report([ mod_post_log_cannot_post | ReportArgs ]).
|
||||||
|
|
||||||
format_jid(#jid{luser = User, lserver = Server, lresource = Resource})
|
format_jid(JID) ->
|
||||||
when Resource =:= undefined;
|
binary_to_list(jid:to_string(JID)).
|
||||||
Resource =:= "";
|
|
||||||
Resource =:= <<"">> ->
|
|
||||||
%% The guard above feels defensive, but I don't yet know the full
|
|
||||||
%% set of ways that ejabberd will represent an empty resource
|
|
||||||
io_lib:format("~s@~s", [User, Server]);
|
|
||||||
format_jid(#jid{luser = User, lserver = Server, lresource = Resource}) ->
|
|
||||||
io_lib:format("~s@~s/~s", [User, Server, Resource]).
|
|
||||||
|
|
||||||
%% Erlang now()-style timestamps are in UTC by definition, and we are
|
%% Erlang now()-style timestamps are in UTC by definition, and we are
|
||||||
%% assuming ISO 8601 dates should be printed in UTC as well, so no
|
%% assuming ISO 8601 dates should be printed in UTC as well, so no
|
||||||
|
|
Loading…
Reference in New Issue