2003-01-05 21:24:59 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_offline.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 5 Jan 2003 by Alexey Shchepin <alexey@sevcom.net>
|
2003-01-06 20:57:05 +01:00
|
|
|
%%% Id : $Id$
|
2003-01-05 21:24:59 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_offline).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
2003-02-13 20:39:13 +01:00
|
|
|
init/0,
|
2005-06-20 05:18:13 +02:00
|
|
|
stop/1,
|
2003-01-05 21:24:59 +01:00
|
|
|
store_packet/3,
|
2005-04-17 20:08:34 +02:00
|
|
|
resend_offline_messages/2,
|
|
|
|
pop_offline_messages/3,
|
2004-09-10 22:57:00 +02:00
|
|
|
remove_expired_messages/0,
|
2003-10-24 21:21:07 +02:00
|
|
|
remove_old_messages/1,
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user/2]).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-record(offline_msg, {us, timestamp, expire, from, to, packet}).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2003-02-13 20:39:13 +01:00
|
|
|
-define(PROCNAME, ejabberd_offline).
|
2004-08-22 23:54:14 +02:00
|
|
|
-define(OFFLINE_TABLE_LOCK_THRESHOLD, 1000).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, _Opts) ->
|
2003-01-05 21:24:59 +01:00
|
|
|
mnesia:create_table(offline_msg,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
2003-02-13 20:39:13 +01:00
|
|
|
{attributes, record_info(fields, offline_msg)}]),
|
2004-09-10 22:57:00 +02:00
|
|
|
update_table(),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(offline_message_hook, Host,
|
2004-08-08 21:07:55 +02:00
|
|
|
?MODULE, store_packet, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(offline_subscription_hook, Host,
|
2004-08-22 23:54:14 +02:00
|
|
|
?MODULE, store_packet, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(resend_offline_messages_hook, Host,
|
2004-08-08 21:07:55 +02:00
|
|
|
?MODULE, pop_offline_messages, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(remove_user, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
register(gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
spawn(?MODULE, init, [])).
|
2003-02-13 20:39:13 +01:00
|
|
|
|
|
|
|
init() ->
|
|
|
|
loop().
|
|
|
|
|
|
|
|
loop() ->
|
|
|
|
receive
|
|
|
|
#offline_msg{} = Msg ->
|
|
|
|
Msgs = receive_all([Msg]),
|
2004-08-22 23:54:14 +02:00
|
|
|
Len = length(Msgs),
|
2003-02-13 20:39:13 +01:00
|
|
|
F = fun() ->
|
2004-08-22 23:54:14 +02:00
|
|
|
if
|
|
|
|
Len >= ?OFFLINE_TABLE_LOCK_THRESHOLD ->
|
|
|
|
mnesia:write_lock_table(offline_msg);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2003-02-13 20:39:13 +01:00
|
|
|
lists:foreach(fun(M) ->
|
|
|
|
mnesia:write(M)
|
|
|
|
end, Msgs)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
|
|
|
loop();
|
|
|
|
_ ->
|
|
|
|
loop()
|
|
|
|
end.
|
|
|
|
|
|
|
|
receive_all(Msgs) ->
|
|
|
|
receive
|
|
|
|
#offline_msg{} = Msg ->
|
|
|
|
receive_all([Msg | Msgs])
|
|
|
|
after 0 ->
|
|
|
|
Msgs
|
|
|
|
end.
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
|
|
|
ejabberd_hooks:delete(offline_message_hook, Host,
|
2004-08-08 21:07:55 +02:00
|
|
|
?MODULE, store_packet, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(offline_subscription_hook, Host,
|
2004-08-23 22:47:15 +02:00
|
|
|
?MODULE, store_packet, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(resend_offline_messages_hook, Host,
|
2004-08-08 21:07:55 +02:00
|
|
|
?MODULE, pop_offline_messages, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(remove_user, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
exit(whereis(Proc), stop),
|
|
|
|
{wait, Proc}.
|
2003-01-05 21:24:59 +01:00
|
|
|
|
|
|
|
store_packet(From, To, Packet) ->
|
2003-10-28 21:26:43 +01:00
|
|
|
Type = xml:get_tag_attr_s("type", Packet),
|
2004-08-08 21:07:55 +02:00
|
|
|
if
|
|
|
|
(Type /= "error") and (Type /= "groupchat") ->
|
|
|
|
case check_event(From, To, Packet) of
|
|
|
|
true ->
|
2005-04-17 20:08:34 +02:00
|
|
|
#jid{luser = LUser, lserver = LServer} = To,
|
2004-08-08 21:07:55 +02:00
|
|
|
TimeStamp = now(),
|
2004-09-10 22:57:00 +02:00
|
|
|
{xmlelement, _Name, _Attrs, Els} = Packet,
|
|
|
|
Expire = find_x_expire(TimeStamp, Els),
|
2005-06-30 13:29:04 +02:00
|
|
|
gen_mod:get_module_proc(To#jid.lserver, ?PROCNAME) !
|
|
|
|
#offline_msg{us = {LUser, LServer},
|
|
|
|
timestamp = TimeStamp,
|
|
|
|
expire = Expire,
|
|
|
|
from = From,
|
|
|
|
to = To,
|
|
|
|
packet = Packet},
|
2004-08-08 21:07:55 +02:00
|
|
|
stop;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
2003-01-05 21:24:59 +01:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
check_event(From, To, Packet) ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = Packet,
|
|
|
|
case find_x_event(Els) of
|
|
|
|
false ->
|
|
|
|
true;
|
|
|
|
El ->
|
|
|
|
case xml:get_subtag(El, "id") of
|
|
|
|
false ->
|
|
|
|
case xml:get_subtag(El, "offline") of
|
|
|
|
false ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
ID = case xml:get_tag_attr_s("id", Packet) of
|
|
|
|
"" ->
|
|
|
|
{xmlelement, "id", [], []};
|
|
|
|
S ->
|
|
|
|
{xmlelement, "id", [],
|
|
|
|
[{xmlcdata, S}]}
|
|
|
|
end,
|
|
|
|
ejabberd_router:route(
|
|
|
|
To, From, {xmlelement, Name, Attrs,
|
|
|
|
[{xmlelement, "x",
|
|
|
|
[{"xmlns", ?NS_EVENT}],
|
|
|
|
[ID,
|
|
|
|
{xmlelement, "offline", [], []}]}]
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
find_x_event([]) ->
|
|
|
|
false;
|
2003-02-05 21:28:49 +01:00
|
|
|
find_x_event([{xmlcdata, _} | Els]) ->
|
|
|
|
find_x_event(Els);
|
2003-01-05 21:24:59 +01:00
|
|
|
find_x_event([El | Els]) ->
|
|
|
|
case xml:get_tag_attr_s("xmlns", El) of
|
|
|
|
?NS_EVENT ->
|
|
|
|
El;
|
|
|
|
_ ->
|
|
|
|
find_x_event(Els)
|
|
|
|
end.
|
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
find_x_expire(_, []) ->
|
|
|
|
never;
|
|
|
|
find_x_expire(TimeStamp, [{xmlcdata, _} | Els]) ->
|
|
|
|
find_x_expire(TimeStamp, Els);
|
|
|
|
find_x_expire(TimeStamp, [El | Els]) ->
|
|
|
|
case xml:get_tag_attr_s("xmlns", El) of
|
|
|
|
?NS_EXPIRE ->
|
|
|
|
case xml:get_tag_attr_s("seconds", El) of
|
|
|
|
Val ->
|
|
|
|
case catch list_to_integer(Val) of
|
|
|
|
{'EXIT', _} ->
|
|
|
|
never;
|
|
|
|
Int when Int > 0 ->
|
|
|
|
{MegaSecs, Secs, MicroSecs} = TimeStamp,
|
|
|
|
S = MegaSecs * 1000000 + Secs + Int,
|
|
|
|
MegaSecs1 = S div 1000000,
|
|
|
|
Secs1 = S rem 1000000,
|
|
|
|
{MegaSecs1, Secs1, MicroSecs};
|
|
|
|
_ ->
|
|
|
|
never
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
never
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
find_x_expire(TimeStamp, Els)
|
|
|
|
end.
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
resend_offline_messages(User, Server) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
2003-01-05 21:24:59 +01:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
Rs = mnesia:wread({offline_msg, US}),
|
|
|
|
mnesia:delete({offline_msg, US}),
|
2003-01-05 21:24:59 +01:00
|
|
|
Rs
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Rs} ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = R#offline_msg.packet,
|
|
|
|
ejabberd_sm !
|
|
|
|
{route,
|
|
|
|
R#offline_msg.from,
|
|
|
|
R#offline_msg.to,
|
|
|
|
{xmlelement, Name, Attrs,
|
|
|
|
Els ++
|
2003-01-11 21:25:11 +01:00
|
|
|
[jlib:timestamp_to_xml(
|
|
|
|
calendar:now_to_universal_time(
|
|
|
|
R#offline_msg.timestamp))]}}
|
2003-01-05 21:24:59 +01:00
|
|
|
end,
|
|
|
|
lists:keysort(#offline_msg.timestamp, Rs));
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2003-01-26 21:16:53 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
pop_offline_messages(Ls, User, Server) ->
|
2004-01-18 21:42:09 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
2004-01-18 21:42:09 +01:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
Rs = mnesia:wread({offline_msg, US}),
|
|
|
|
mnesia:delete({offline_msg, US}),
|
2004-01-18 21:42:09 +01:00
|
|
|
Rs
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Rs} ->
|
2004-09-10 22:57:00 +02:00
|
|
|
TS = now(),
|
|
|
|
Ls ++ lists:map(
|
|
|
|
fun(R) ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = R#offline_msg.packet,
|
|
|
|
{route,
|
|
|
|
R#offline_msg.from,
|
|
|
|
R#offline_msg.to,
|
|
|
|
{xmlelement, Name, Attrs,
|
|
|
|
Els ++
|
|
|
|
[jlib:timestamp_to_xml(
|
|
|
|
calendar:now_to_universal_time(
|
|
|
|
R#offline_msg.timestamp))]}}
|
|
|
|
end,
|
|
|
|
lists:filter(
|
|
|
|
fun(R) ->
|
|
|
|
case R#offline_msg.expire of
|
|
|
|
never ->
|
|
|
|
true;
|
|
|
|
TimeStamp ->
|
|
|
|
TS < TimeStamp
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
lists:keysort(#offline_msg.timestamp, Rs)));
|
2004-01-18 21:42:09 +01:00
|
|
|
_ ->
|
2004-08-08 21:07:55 +02:00
|
|
|
Ls
|
2004-01-18 21:42:09 +01:00
|
|
|
end.
|
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
remove_expired_messages() ->
|
|
|
|
TimeStamp = now(),
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write_lock_table(offline_msg),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(Rec, _Acc) ->
|
|
|
|
case Rec#offline_msg.expire of
|
|
|
|
never ->
|
|
|
|
ok;
|
|
|
|
TS ->
|
|
|
|
if
|
|
|
|
TS < TimeStamp ->
|
|
|
|
mnesia:delete_object(Rec);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end, ok, offline_msg)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2003-10-24 21:21:07 +02:00
|
|
|
remove_old_messages(Days) ->
|
|
|
|
{MegaSecs, Secs, _MicroSecs} = now(),
|
|
|
|
S = MegaSecs * 1000000 + Secs - 60 * 60 * 24 * Days,
|
|
|
|
MegaSecs1 = S div 1000000,
|
|
|
|
Secs1 = S rem 1000000,
|
|
|
|
TimeStamp = {MegaSecs1, Secs1, 0},
|
|
|
|
F = fun() ->
|
2004-08-08 21:07:55 +02:00
|
|
|
mnesia:write_lock_table(offline_msg),
|
2003-10-24 21:21:07 +02:00
|
|
|
mnesia:foldl(
|
|
|
|
fun(#offline_msg{timestamp = TS} = Rec, _Acc)
|
|
|
|
when TS < TimeStamp ->
|
|
|
|
mnesia:delete_object(Rec);
|
|
|
|
(_Rec, _Acc) -> ok
|
|
|
|
end, ok, offline_msg)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
2003-01-26 21:16:53 +01:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:delete({offline_msg, US})
|
2003-01-26 21:16:53 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
2004-09-10 22:57:00 +02:00
|
|
|
|
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, offline_msg),
|
|
|
|
case mnesia:table_info(offline_msg, attributes) of
|
|
|
|
Fields ->
|
|
|
|
ok;
|
2005-04-17 20:08:34 +02:00
|
|
|
[user, timestamp, expire, from, to, packet] ->
|
|
|
|
?INFO_MSG("Converting offline_msg table from "
|
|
|
|
"{user, timestamp, expire, from, to, packet} format", []),
|
|
|
|
Host = ?MYNAME,
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_offline_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, offline_msg},
|
|
|
|
{attributes, record_info(fields, offline_msg)}]),
|
|
|
|
mnesia:transform_table(offline_msg, ignore, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_offline_tmp_table),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(#offline_msg{us = U} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_offline_tmp_table,
|
|
|
|
R#offline_msg{us = {U, Host}})
|
|
|
|
end, ok, offline_msg)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(offline_msg),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(offline_msg),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_offline_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_offline_tmp_table);
|
2004-09-10 22:57:00 +02:00
|
|
|
[user, timestamp, from, to, packet] ->
|
|
|
|
?INFO_MSG("Converting offline_msg table from "
|
|
|
|
"{user, timestamp, from, to, packet} format", []),
|
2005-04-17 20:08:34 +02:00
|
|
|
Host = ?MYNAME,
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_offline_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, offline_msg},
|
|
|
|
{attributes, record_info(fields, offline_msg)}]),
|
2004-09-10 22:57:00 +02:00
|
|
|
mnesia:transform_table(
|
|
|
|
offline_msg,
|
|
|
|
fun({_, U, TS, F, T, P}) ->
|
|
|
|
{xmlelement, _Name, _Attrs, Els} = P,
|
|
|
|
Expire = find_x_expire(TS, Els),
|
2005-04-17 20:08:34 +02:00
|
|
|
#offline_msg{us = U,
|
2004-09-10 22:57:00 +02:00
|
|
|
timestamp = TS,
|
|
|
|
expire = Expire,
|
|
|
|
from = F,
|
|
|
|
to = T,
|
|
|
|
packet = P}
|
2005-04-17 20:08:34 +02:00
|
|
|
end, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_offline_tmp_table),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(#offline_msg{us = U} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_offline_tmp_table,
|
|
|
|
R#offline_msg{us = {U, Host}})
|
|
|
|
end, ok, offline_msg)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(offline_msg),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(offline_msg),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_offline_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_offline_tmp_table);
|
2004-09-10 22:57:00 +02:00
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating offline_msg table", []),
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:transform_table(offline_msg, ignore, Fields)
|
2004-09-10 22:57:00 +02:00
|
|
|
end.
|
|
|
|
|