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).
|
|
|
|
|
|
|
|
-export([start/1,
|
2003-02-13 20:39:13 +01:00
|
|
|
init/0,
|
2003-01-29 18:12:23 +01:00
|
|
|
stop/0,
|
2003-01-05 21:24:59 +01:00
|
|
|
store_packet/3,
|
2003-01-26 21:16:53 +01:00
|
|
|
resend_offline_messages/1,
|
2004-08-08 21:07:55 +02:00
|
|
|
pop_offline_messages/2,
|
2003-10-24 21:21:07 +02:00
|
|
|
remove_old_messages/1,
|
2003-01-26 21:16:53 +01:00
|
|
|
remove_user/1]).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2003-01-11 21:25:11 +01:00
|
|
|
-record(offline_msg, {user, timestamp, 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
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
start(_) ->
|
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-08-08 21:07:55 +02:00
|
|
|
ejabberd_hooks:add(offline_message_hook,
|
|
|
|
?MODULE, store_packet, 50),
|
2004-08-22 23:54:14 +02:00
|
|
|
ejabberd_hooks:add(offline_subscription_hook,
|
|
|
|
?MODULE, store_packet, 50),
|
2004-08-08 21:07:55 +02:00
|
|
|
ejabberd_hooks:add(resend_offline_messages_hook,
|
|
|
|
?MODULE, pop_offline_messages, 50),
|
2003-02-13 20:39:13 +01:00
|
|
|
register(?PROCNAME, spawn(?MODULE, init, [])).
|
|
|
|
|
|
|
|
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
|
|
|
|
2003-01-29 18:12:23 +01:00
|
|
|
stop() ->
|
2004-08-08 21:07:55 +02:00
|
|
|
ejabberd_hooks:delete(offline_message_hook,
|
|
|
|
?MODULE, store_packet, 50),
|
|
|
|
ejabberd_hooks:delete(resend_offline_messages_hook,
|
|
|
|
?MODULE, pop_offline_messages, 50),
|
2003-02-21 20:52:15 +01:00
|
|
|
exit(whereis(?PROCNAME), stop),
|
2003-01-29 18:12:23 +01:00
|
|
|
ok.
|
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 ->
|
|
|
|
#jid{luser = LUser} = To,
|
|
|
|
TimeStamp = now(),
|
|
|
|
?PROCNAME ! #offline_msg{user = LUser,
|
|
|
|
timestamp = TimeStamp,
|
|
|
|
from = From,
|
|
|
|
to = To,
|
|
|
|
packet = Packet},
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
resend_offline_messages(User) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2003-01-05 21:24:59 +01:00
|
|
|
F = fun() ->
|
2004-01-18 21:42:09 +01:00
|
|
|
Rs = mnesia:wread({offline_msg, LUser}),
|
2003-01-05 21:24:59 +01:00
|
|
|
mnesia:delete({offline_msg, LUser}),
|
|
|
|
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
|
|
|
|
2004-08-08 21:07:55 +02:00
|
|
|
pop_offline_messages(Ls, User) ->
|
2004-01-18 21:42:09 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
F = fun() ->
|
|
|
|
Rs = mnesia:wread({offline_msg, LUser}),
|
|
|
|
mnesia:delete({offline_msg, LUser}),
|
|
|
|
Rs
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Rs} ->
|
|
|
|
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,
|
2004-08-08 21:07:55 +02:00
|
|
|
Ls ++ 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.
|
|
|
|
|
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).
|
|
|
|
|
2003-01-26 21:16:53 +01:00
|
|
|
remove_user(User) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2003-01-26 21:16:53 +01:00
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({offline_msg, LUser})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|