%%%-------------------------------------------------------------------
%%% File    : mod_admin_extra_last.erl
  Author   :    > ,   <  >
%%% Purpose : Contributed administrative functions and commands
  Created : 10 Aug 2008 by    >
%%%
%%%
  ejabberd , Copyright ( C ) 2002 - 2008    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. , 59 Temple Place , Suite 330 , Boston , MA
  02111 - 1307 USA
%%%
%%%-------------------------------------------------------------------

-module(mod_admin_extra_last).
-author('').

-export([
      commands/0,

      set_last/4,
      get_lastactivity_fun/1,
      get_lastactivity_module/1]).

-include("ejabberd.hrl").
-include("ejabberd_commands.hrl").
-include("mod_roster.hrl").
-include("jlib.hrl").
-include_lib("exml/include/exml.hrl").

%%%
%%% Register commands
%%%

-spec commands() -> [ejabberd_commands:cmd(),...].
commands() ->
    [
     #ejabberd_commands{name = set_last, tags = [last],
                        desc = "Set last activity information",
                        longdesc = "Timestamp is the seconds since"
                        "1970-01-01 00:00:00 UTC, for example: date +%s",
                        module = ?MODULE, function = set_last,
                        args = [{user, binary}, {host, binary}, {timestamp, integer}, {status, binary}],
                        result = {res, restuple}}
    ].

%%%
%%% Last Activity
%%%

-spec set_last(ejabberd:user(), ejabberd:server(), _, _) -> {Res, string()} when
    Res :: ok | user_does_not_exist.
set_last(User, Server, Timestamp, Status) ->
    case ejabberd_auth:is_user_exists(User, Server) of
        true ->
            Fun = get_lastactivity_fun(Server),
            Module = get_lastactivity_module(Server),
            Module:Fun(jid:nodeprep(User), jid:nameprep(Server), Timestamp, Status),
            {ok, io_lib:format("Last activity for user ~s@~s is set as ~B with status ~s", [User, Server, Timestamp, Status])};
        false ->
            String = io_lib:format("User ~s@~s does not exist", [User, Server]),
            {user_does_not_exist, String}
    end.


%% Could just return fun mod_last:store_last_info/4 but its easier when called through rpc in tests
-spec get_lastactivity_fun(ejabberd:server()) -> 'store_last_info' | 'set_last_info'.
get_lastactivity_fun(Server) ->
    case lists:member(mod_last, gen_mod:loaded_modules(Server)) of
        true -> store_last_info;
        _ -> set_last_info
    end.

-spec get_lastactivity_module(ejabberd:server()) -> 'mod_last' | 'mod_last_odbc'.
get_lastactivity_module(Server) ->
    case lists:member(mod_last, gen_mod:loaded_modules(Server)) of
        true -> mod_last;
        _ -> mod_last_odbc
    end.

-------------------------------------------------------------------
 File    : mod_admin_extra_last.erl
 Purpose : Contributed administrative functions and commands



 This program is free software; you can redistribute it and/or
 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.

 along with this program; if not, write to the Free Software

-------------------------------------------------------------------

 Register commands


 Last Activity

 Could just return fun mod_last:store_last_info/4 but its easier when called through rpc in tests
  Author   :    > ,   <  >
  Created : 10 Aug 2008 by    >
  ejabberd , Copyright ( C ) 2002 - 2008    ProcessOne
  modify it under the terms of the GNU General Public License as
  published by the Free Software Foundation ; either version 2 of the
  You should have received a copy of the GNU General Public License
  Foundation , Inc. , 59 Temple Place , Suite 330 , Boston , MA
  02111 - 1307 USA

-module(mod_admin_extra_last).
-author('').

-export([
      commands/0,

      set_last/4,
      get_lastactivity_fun/1,
      get_lastactivity_module/1]).

-include("ejabberd.hrl").
-include("ejabberd_commands.hrl").
-include("mod_roster.hrl").
-include("jlib.hrl").
-include_lib("exml/include/exml.hrl").


-spec commands() -> [ejabberd_commands:cmd(),...].
commands() ->
    [
     #ejabberd_commands{name = set_last, tags = [last],
                        desc = "Set last activity information",
                        longdesc = "Timestamp is the seconds since"
                        "1970-01-01 00:00:00 UTC, for example: date +%s",
                        module = ?MODULE, function = set_last,
                        args = [{user, binary}, {host, binary}, {timestamp, integer}, {status, binary}],
                        result = {res, restuple}}
    ].


-spec set_last(ejabberd:user(), ejabberd:server(), _, _) -> {Res, string()} when
    Res :: ok | user_does_not_exist.
set_last(User, Server, Timestamp, Status) ->
    case ejabberd_auth:is_user_exists(User, Server) of
        true ->
            Fun = get_lastactivity_fun(Server),
            Module = get_lastactivity_module(Server),
            Module:Fun(jid:nodeprep(User), jid:nameprep(Server), Timestamp, Status),
            {ok, io_lib:format("Last activity for user ~s@~s is set as ~B with status ~s", [User, Server, Timestamp, Status])};
        false ->
            String = io_lib:format("User ~s@~s does not exist", [User, Server]),
            {user_does_not_exist, String}
    end.


-spec get_lastactivity_fun(ejabberd:server()) -> 'store_last_info' | 'set_last_info'.
get_lastactivity_fun(Server) ->
    case lists:member(mod_last, gen_mod:loaded_modules(Server)) of
        true -> store_last_info;
        _ -> set_last_info
    end.

-spec get_lastactivity_module(ejabberd:server()) -> 'mod_last' | 'mod_last_odbc'.
get_lastactivity_module(Server) ->
    case lists:member(mod_last, gen_mod:loaded_modules(Server)) of
        true -> mod_last;
        _ -> mod_last_odbc
    end.
