Fix use of old jlib in mod_rest, and forget timezone option (#268)

This commit is contained in:
Badlop 2019-02-08 17:31:11 +01:00
parent eb39a4f3af
commit ebfaf70df4
3 changed files with 24 additions and 47 deletions

View File

@ -33,13 +33,6 @@ orientation:
logdir: logdir:
Base filename, including absolute path Base filename, including absolute path
Default value: "/tmp/jabberlogs/" Default value: "/tmp/jabberlogs/"
timezone:
The time zone for the logs is configurable with this option.
Allowed values are 'local' and 'universal'.
With the first value, the local time,
as reported to Erlang by the operating system, will be used.
With the latter, GMT/UTC time will be used.
Default value: local
show_ip: show_ip:
If the IP address of the local user should be logged to file. If the IP address of the local user should be logged to file.
Default value: false Default value: false
@ -76,7 +69,6 @@ modules:
- send - send
- recv - recv
logdir: "/tmp/logs/" logdir: "/tmp/logs/"
timezone: universal
show_ip: false show_ip: false
rotate_days: 1 rotate_days: 1
rotate_megs: 100 rotate_megs: 100

View File

@ -9,7 +9,6 @@ modules:
- send - send
- recv - recv
logdir: "/tmp/logs/" logdir: "/tmp/logs/"
timezone: universal
show_ip: false show_ip: false
rotate_days: 1 rotate_days: 1
rotate_megs: 100 rotate_megs: 100

View File

@ -11,7 +11,7 @@
-behaviour(gen_mod). -behaviour(gen_mod).
-export([start/2, init/7, stop/1, -export([start/2, init/6, stop/1,
send_packet/1, receive_packet/1]). send_packet/1, receive_packet/1]).
-include("xmpp.hrl"). -include("xmpp.hrl").
@ -36,9 +36,6 @@ start(Host, Opts) ->
end, end,
RotateO = {Rd, Rf, Rp}, RotateO = {Rd, Rf, Rp},
CheckRKP = gen_mod:get_opt(check_rotate_kpackets, Opts, fun(A) -> A end, 1), CheckRKP = gen_mod:get_opt(check_rotate_kpackets, Opts, fun(A) -> A end, 1),
Timezone = gen_mod:get_opt(timezone, Opts, fun(A) -> A end, local),
Orientation = gen_mod:get_opt(orientation, Opts, fun(A) -> A end, [send, recv]), Orientation = gen_mod:get_opt(orientation, Opts, fun(A) -> A end, [send, recv]),
Stanza = gen_mod:get_opt(stanza, Opts, fun(A) -> A end, [iq, message, presence, other]), Stanza = gen_mod:get_opt(stanza, Opts, fun(A) -> A end, [iq, message, presence, other]),
Direction = gen_mod:get_opt(direction, Opts, fun(A) -> A end, [internal, vhosts, external]), Direction = gen_mod:get_opt(direction, Opts, fun(A) -> A end, [internal, vhosts, external]),
@ -52,7 +49,7 @@ start(Host, Opts) ->
ejabberd_hooks:add(user_receive_packet, Host, ?MODULE, receive_packet, 90), ejabberd_hooks:add(user_receive_packet, Host, ?MODULE, receive_packet, 90),
register(gen_mod:get_module_proc(Host, ?PROCNAME), register(gen_mod:get_module_proc(Host, ?PROCNAME),
spawn(?MODULE, init, [binary_to_list(Host), Logdir, RotateO, CheckRKP, spawn(?MODULE, init, [binary_to_list(Host), Logdir, RotateO, CheckRKP,
Timezone, ShowIP, FilterO])), ShowIP, FilterO])),
ok. ok.
stop(Host) -> stop(Host) ->
@ -62,17 +59,17 @@ stop(Host) ->
Proc ! stop, Proc ! stop,
{wait, Proc}. {wait, Proc}.
init(Host, Logdir, RotateO, CheckRKP, Timezone, ShowIP, FilterO) -> init(Host, Logdir, RotateO, CheckRKP, ShowIP, FilterO) ->
{IoDevice, Filename, Gregorian_day} = open_file(Logdir, Host, Timezone), {IoDevice, Filename, Gregorian_day} = open_file(Logdir, Host),
loop(Host, IoDevice, Filename, Logdir, CheckRKP, RotateO, 0, Gregorian_day, loop(Host, IoDevice, Filename, Logdir, CheckRKP, RotateO, 0, Gregorian_day,
Timezone, ShowIP, FilterO). ShowIP, FilterO).
%% ------------------- %% -------------------
%% Main %% Main
%% ------------------- %% -------------------
manage_rotate(Host, IoDevice, Filename, Logdir, RotateO, PacketC, manage_rotate(Host, IoDevice, Filename, Logdir, RotateO, PacketC,
Gregorian_day_log, Timezone) -> Gregorian_day_log) ->
{RO_days, RO_size, RO_packets} = RotateO, {RO_days, RO_size, RO_packets} = RotateO,
Rotate1 = case RO_packets of Rotate1 = case RO_packets of
@ -99,7 +96,7 @@ manage_rotate(Host, IoDevice, Filename, Logdir, RotateO, PacketC,
case lists:any(fun(E) -> E end, [Rotate1, Rotate2, Rotate3]) of case lists:any(fun(E) -> E end, [Rotate1, Rotate2, Rotate3]) of
true -> true ->
{IoDevice2, Filename2, Gregorian_day2} = {IoDevice2, Filename2, Gregorian_day2} =
rotate_log(IoDevice, Logdir, Host, Timezone), rotate_log(IoDevice, Logdir, Host),
{IoDevice2, Filename2, Gregorian_day2, 0}; {IoDevice2, Filename2, Gregorian_day2, 0};
false -> false ->
{IoDevice, Filename, Gregorian_day_log, PacketC+1} {IoDevice, Filename, Gregorian_day_log, PacketC+1}
@ -131,7 +128,7 @@ filter(FilterO, E) ->
{Orientation, Stanza, Direction}}. {Orientation, Stanza, Direction}}.
loop(Host, IoDevice, Filename, Logdir, CheckRKP, RotateO, PacketC, loop(Host, IoDevice, Filename, Logdir, CheckRKP, RotateO, PacketC,
Gregorian_day, Timezone, ShowIP, FilterO) -> Gregorian_day, ShowIP, FilterO) ->
receive receive
{addlog, E} -> {addlog, E} ->
{IoDevice3, Filename3, Gregorian_day3, PacketC3} = {IoDevice3, Filename3, Gregorian_day3, PacketC3} =
@ -143,24 +140,24 @@ loop(Host, IoDevice, Filename, Logdir, CheckRKP, RotateO, PacketC,
true -> true ->
manage_rotate(Host, IoDevice, Filename, manage_rotate(Host, IoDevice, Filename,
Logdir, RotateO, PacketC, Logdir, RotateO, PacketC,
Gregorian_day, Timezone); Gregorian_day);
false -> false ->
{IoDevice, Filename, Gregorian_day, {IoDevice, Filename, Gregorian_day,
PacketC+1} PacketC+1}
end, end,
add_log(IoDevice2, Timezone, ShowIP, E, OSD), add_log(IoDevice2, ShowIP, E, OSD),
{IoDevice2, Filename2, Gregorian_day2, PacketC2}; {IoDevice2, Filename2, Gregorian_day2, PacketC2};
_ -> _ ->
{IoDevice, Filename, Gregorian_day, PacketC} {IoDevice, Filename, Gregorian_day, PacketC}
end, end,
loop(Host, IoDevice3, Filename3, Logdir, CheckRKP, RotateO, loop(Host, IoDevice3, Filename3, Logdir, CheckRKP, RotateO,
PacketC3, Gregorian_day3, Timezone, ShowIP, FilterO); PacketC3, Gregorian_day3, ShowIP, FilterO);
stop -> stop ->
close_file(IoDevice), close_file(IoDevice),
ok; ok;
_ -> _ ->
loop(Host, IoDevice, Filename, Logdir, CheckRKP, RotateO, PacketC, loop(Host, IoDevice, Filename, Logdir, CheckRKP, RotateO, PacketC,
Gregorian_day, Timezone, ShowIP, FilterO) Gregorian_day, ShowIP, FilterO)
end. end.
send_packet({P, State}) -> send_packet({P, State}) ->
@ -184,7 +181,7 @@ get_from_to(Packet) ->
#presence{from = F, to = T} -> {F, T} #presence{from = F, to = T} -> {F, T}
end. end.
add_log(Io, Timezone, ShowIP, {Orientation, From, To, Packet}, _OSD) -> add_log(Io, ShowIP, {Orientation, From, To, Packet}, _OSD) ->
%%{Orientation, Stanza, Direction} = OSD, %%{Orientation, Stanza, Direction} = OSD,
LocalJID = case Orientation of LocalJID = case Orientation of
send -> From; send -> From;
@ -202,26 +199,19 @@ add_log(Io, Timezone, ShowIP, {Orientation, From, To, Packet}, _OSD) ->
end; end;
false -> "" false -> ""
end, end,
TimestampISO = get_now_iso(Timezone), TimestampISO = get_now_iso(),
io:fwrite(Io, "<packet or=\"~p\" ljid=\"~s\" ~sts=\"~s\">~s</packet>~n", io:fwrite(Io, "<packet or=\"~p\" ljid=\"~s\" ~sts=\"~s\">~s</packet>~n",
[Orientation, jlib:jid_to_string(LocalJID), LocalIPS, [Orientation, binary_to_list(jid:encode(LocalJID)), LocalIPS,
TimestampISO, binary_to_list(fxml:element_to_binary(xmpp:encode(Packet)))]). binary_to_list(TimestampISO), binary_to_list(fxml:element_to_binary(xmpp:encode(Packet)))]).
%% ------------------- %% -------------------
%% File %% File
%% ------------------- %% -------------------
open_file(Logdir, Host, Timezone) -> open_file(Logdir, Host) ->
TimeStamp = get_now_iso(Timezone), {{Year, Month, Day}, {Hour, Min, Sec}} = calendar:universal_time(),
Year = string:substr(TimeStamp, 1, 4), Logname = str:format("~s-~4..0B~2..0B~2..0BT~2..0B:~2..0B:~2..0B.xml",
Month = string:substr(TimeStamp, 5, 2), [Host, Year, Month, Day, Hour, Min, Sec]),
Day = string:substr(TimeStamp, 7, 2),
Hour = string:substr(TimeStamp, 10, 2),
Min = string:substr(TimeStamp, 13, 2),
Sec = string:substr(TimeStamp, 16, 2),
S = "-",
Logname = lists:flatten([Host,S,Year,S,Month,S,Day,S,Hour,S,Min,S,Sec,
".xml"]),
Filename = filename:join([Logdir, Logname]), Filename = filename:join([Logdir, Logname]),
Gregorian_day = get_gregorian_day(), Gregorian_day = get_gregorian_day(),
@ -243,9 +233,9 @@ close_file(IoDevice) ->
io:fwrite(IoDevice, "~s~n", ["</log>"]), io:fwrite(IoDevice, "~s~n", ["</log>"]),
file:close(IoDevice). file:close(IoDevice).
rotate_log(IoDevice, Logdir, Host, Timezone) -> rotate_log(IoDevice, Logdir, Host) ->
close_file(IoDevice), close_file(IoDevice),
open_file(Logdir, Host, Timezone). open_file(Logdir, Host).
make_dir_rec(Dir) -> make_dir_rec(Dir) ->
case file:read_file_info(Dir) of case file:read_file_info(Dir) of
@ -264,12 +254,8 @@ make_dir_rec(Dir) ->
get_gregorian_day() -> calendar:date_to_gregorian_days(date()). get_gregorian_day() -> calendar:date_to_gregorian_days(date()).
get_now_iso(Timezone) -> get_now_iso() ->
TimeStamp = case Timezone of xmpp_util:encode_timestamp(now()).
local -> calendar:local_time();
universal -> calendar:universal_time()
end,
binary_to_list(jlib:timestamp_to_legacy(TimeStamp)).
calc_div(A, B) when is_integer(A) and is_integer(B) and (B /= 0) -> calc_div(A, B) when is_integer(A) and is_integer(B) and (B /= 0) ->
A/B; A/B;