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').
|
|
|
|
|
|
|
|
-export([start/0,
|
|
|
|
store_packet/3,
|
|
|
|
resend_offline_messages/1]).
|
|
|
|
|
|
|
|
-include("namespaces.hrl").
|
|
|
|
|
2003-01-11 21:25:11 +01:00
|
|
|
-record(offline_msg, {user, timestamp, from, to, packet}).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
start() ->
|
|
|
|
mnesia:create_table(offline_msg,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{attributes, record_info(fields, offline_msg)}]).
|
|
|
|
|
|
|
|
|
|
|
|
store_packet(From, To, Packet) ->
|
|
|
|
case check_event(From, To, Packet) of
|
|
|
|
true ->
|
|
|
|
{User, Server, Resource} = To,
|
|
|
|
LUser = jlib:tolower(User),
|
2003-01-11 21:25:11 +01:00
|
|
|
TimeStamp = now(),
|
2003-01-05 21:24:59 +01:00
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(#offline_msg{user = LUser,
|
|
|
|
timestamp = TimeStamp,
|
|
|
|
from = From,
|
|
|
|
to = To,
|
|
|
|
packet = Packet})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F);
|
|
|
|
_ ->
|
|
|
|
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;
|
|
|
|
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) ->
|
|
|
|
LUser = jlib:tolower(User),
|
|
|
|
F = fun() ->
|
|
|
|
Rs = mnesia:read({offline_msg, LUser}),
|
|
|
|
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.
|