aboutsummaryrefslogtreecommitdiff
path: root/src/mod_last.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/mod_last.erl')
-rw-r--r--src/mod_last.erl70
1 files changed, 33 insertions, 37 deletions
diff --git a/src/mod_last.erl b/src/mod_last.erl
index 1cb747060..28b66be08 100644
--- a/src/mod_last.erl
+++ b/src/mod_last.erl
@@ -38,14 +38,15 @@
register_user/2, depends/2, privacy_check_packet/4]).
-include("logger.hrl").
-
-include("xmpp.hrl").
-
-include("mod_privacy.hrl").
-include("mod_last.hrl").
+-include("translate.hrl").
-define(LAST_CACHE, last_activity_cache).
+-type c2s_state() :: ejabberd_c2s:state().
+
-callback init(binary(), gen_mod:opts()) -> any().
-callback import(binary(), #last_activity{}) -> ok | pass.
-callback get_last(binary(), binary()) ->
@@ -58,7 +59,7 @@
-optional_callbacks([use_cache/1, cache_nodes/1]).
start(Host, Opts) ->
- Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
+ Mod = gen_mod:db_mod(Opts, ?MODULE),
Mod:init(Host, Opts),
init_cache(Mod, Host, Opts),
gen_iq_handler:add_iq_handler(ejabberd_local, Host,
@@ -89,8 +90,8 @@ stop(Host) ->
?NS_LAST).
reload(Host, NewOpts, OldOpts) ->
- NewMod = gen_mod:db_mod(Host, NewOpts, ?MODULE),
- OldMod = gen_mod:db_mod(Host, OldOpts, ?MODULE),
+ NewMod = gen_mod:db_mod(NewOpts, ?MODULE),
+ OldMod = gen_mod:db_mod(OldOpts, ?MODULE),
if NewMod /= OldMod ->
NewMod:init(Host, NewOpts);
true ->
@@ -104,7 +105,7 @@ reload(Host, NewOpts, OldOpts) ->
-spec process_local_iq(iq()) -> iq().
process_local_iq(#iq{type = set, lang = Lang} = IQ) ->
- Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
+ Txt = ?T("Value 'set' of 'type' attribute is not allowed"),
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
process_local_iq(#iq{type = get} = IQ) ->
xmpp:make_iq_result(IQ, #last{seconds = get_node_uptime()}).
@@ -113,12 +114,8 @@ process_local_iq(#iq{type = get} = IQ) ->
%% @doc Get the uptime of the ejabberd node, expressed in seconds.
%% When ejabberd is starting, ejabberd_config:start/0 stores the datetime.
get_node_uptime() ->
- case ejabberd_config:get_option(node_start) of
- undefined ->
- trunc(element(1, erlang:statistics(wall_clock)) / 1000);
- Now ->
- erlang:system_time(second) - Now
- end.
+ NodeStart = ejabberd_config:get_node_start(),
+ erlang:monotonic_time(second) - NodeStart.
%%%
%%% Serve queries about user last online
@@ -126,7 +123,7 @@ get_node_uptime() ->
-spec process_sm_iq(iq()) -> iq().
process_sm_iq(#iq{type = set, lang = Lang} = IQ) ->
- Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
+ Txt = ?T("Value 'set' of 'type' attribute is not allowed"),
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
process_sm_iq(#iq{from = From, to = To, lang = Lang} = IQ) ->
User = To#jid.luser,
@@ -145,10 +142,11 @@ process_sm_iq(#iq{from = From, to = To, lang = Lang} = IQ) ->
deny -> xmpp:make_error(IQ, xmpp:err_forbidden())
end;
true ->
- Txt = <<"Not subscribed">>,
+ Txt = ?T("Not subscribed"),
xmpp:make_error(IQ, xmpp:err_subscription_required(Txt, Lang))
end.
+-spec privacy_check_packet(allow | deny, c2s_state(), stanza(), in | out) -> allow | deny | {stop, deny}.
privacy_check_packet(allow, C2SState,
#iq{from = From, to = To, type = T} = IQ, in)
when T == get; T == set ->
@@ -187,9 +185,7 @@ get_last(LUser, LServer) ->
?LAST_CACHE, {LUser, LServer},
fun() -> Mod:get_last(LUser, LServer) end);
false ->
- Mod:get_last(LUser, LServer);
- undefined ->
- error
+ Mod:get_last(LUser, LServer)
end,
case Res of
{ok, {TimeStamp, Status}} -> {ok, TimeStamp, Status};
@@ -203,10 +199,10 @@ get_last_iq(#iq{lang = Lang} = IQ, LUser, LServer) ->
[] ->
case get_last(LUser, LServer) of
{error, _Reason} ->
- Txt = <<"Database failure">>,
+ Txt = ?T("Database failure"),
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang));
not_found ->
- Txt = <<"No info about last activity found">>,
+ Txt = ?T("No info about last activity found"),
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang));
{ok, TimeStamp, Status} ->
TimeStamp2 = erlang:system_time(second),
@@ -274,19 +270,16 @@ init_cache(Mod, Host, Opts) ->
-spec cache_opts(gen_mod:opts()) -> [proplists:property()].
cache_opts(Opts) ->
- MaxSize = gen_mod:get_opt(cache_size, Opts),
- CacheMissed = gen_mod:get_opt(cache_missed, Opts),
- LifeTime = case gen_mod:get_opt(cache_life_time, Opts) of
- infinity -> infinity;
- I -> timer:seconds(I)
- end,
+ MaxSize = mod_last_opt:cache_size(Opts),
+ CacheMissed = mod_last_opt:cache_missed(Opts),
+ LifeTime = mod_last_opt:cache_life_time(Opts),
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
-spec use_cache(module(), binary()) -> boolean().
use_cache(Mod, Host) ->
case erlang:function_exported(Mod, use_cache, 1) of
true -> Mod:use_cache(Host);
- false -> gen_mod:get_module_opt(Host, ?MODULE, use_cache)
+ false -> mod_last_opt:use_cache(Host)
end.
-spec cache_nodes(module(), binary()) -> [node()].
@@ -321,17 +314,20 @@ export(LServer) ->
depends(_Host, _Opts) ->
[].
-mod_opt_type(db_type) -> fun(T) -> ejabberd_config:v_db(?MODULE, T) end;
-mod_opt_type(O) when O == cache_life_time; O == cache_size ->
- fun (I) when is_integer(I), I > 0 -> I;
- (infinity) -> infinity
- end;
-mod_opt_type(O) when O == use_cache; O == cache_missed ->
- fun (B) when is_boolean(B) -> B end.
+mod_opt_type(db_type) ->
+ econf:db_type(?MODULE);
+mod_opt_type(use_cache) ->
+ econf:bool();
+mod_opt_type(cache_size) ->
+ econf:pos_int(infinity);
+mod_opt_type(cache_missed) ->
+ econf:bool();
+mod_opt_type(cache_life_time) ->
+ econf:timeout(second, infinity).
mod_options(Host) ->
[{db_type, ejabberd_config:default_db(Host, ?MODULE)},
- {use_cache, ejabberd_config:use_cache(Host)},
- {cache_size, ejabberd_config:cache_size(Host)},
- {cache_missed, ejabberd_config:cache_missed(Host)},
- {cache_life_time, ejabberd_config:cache_life_time(Host)}].
+ {use_cache, ejabberd_option:use_cache(Host)},
+ {cache_size, ejabberd_option:cache_size(Host)},
+ {cache_missed, ejabberd_option:cache_missed(Host)},
+ {cache_life_time, ejabberd_option:cache_life_time(Host)}].