2002-12-11 21:57:45 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_roster.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
2004-09-10 22:57:00 +02:00
|
|
|
%%% Purpose : Roster management
|
2002-12-11 21:57:45 +01:00
|
|
|
%%% Created : 11 Dec 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_roster).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2, stop/1,
|
2002-12-17 21:49:45 +01:00
|
|
|
process_iq/3,
|
2003-01-22 21:40:40 +01:00
|
|
|
process_local_iq/3,
|
2005-04-17 20:08:34 +02:00
|
|
|
get_user_roster/2,
|
|
|
|
get_subscription_lists/3,
|
|
|
|
in_subscription/5,
|
|
|
|
out_subscription/4,
|
|
|
|
set_items/3,
|
|
|
|
remove_user/2,
|
|
|
|
get_jid_info/4]).
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2005-04-17 20:08:34 +02:00
|
|
|
-include("mod_roster.hrl").
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2002-12-11 21:57:45 +01:00
|
|
|
mnesia:create_table(roster,[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, roster)}]),
|
2005-04-17 20:08:34 +02:00
|
|
|
update_table(),
|
|
|
|
mnesia:add_table_index(roster, us),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(roster_get, Host,
|
2005-04-17 20:08:34 +02:00
|
|
|
?MODULE, get_user_roster, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(roster_in_subscription, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, in_subscription, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(roster_out_subscription, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, out_subscription, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(roster_get_subscription_lists, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, get_subscription_lists, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(roster_get_jid_info, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, get_jid_info, 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
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_ROSTER,
|
2003-07-21 22:01:22 +02:00
|
|
|
?MODULE, process_iq, IQDisc).
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
|
|
|
ejabberd_hooks:delete(roster_get, Host,
|
2005-04-17 20:08:34 +02:00
|
|
|
?MODULE, get_user_roster, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(roster_in_subscription, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, in_subscription, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(roster_out_subscription, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, out_subscription, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(roster_get_subscription_lists, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, get_subscription_lists, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(roster_get_jid_info, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, get_jid_info, 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
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_ROSTER).
|
2004-07-10 00:34:26 +02:00
|
|
|
|
|
|
|
|
2003-10-27 20:35:03 +01:00
|
|
|
-define(PSI_ROSTER_WORKAROUND, true).
|
|
|
|
|
|
|
|
-ifdef(PSI_ROSTER_WORKAROUND).
|
|
|
|
|
|
|
|
process_iq(From, To, IQ) ->
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{sub_el = SubEl} = IQ,
|
2003-10-27 20:35:03 +01:00
|
|
|
#jid{lserver = LServer} = From,
|
2005-04-17 20:08:34 +02:00
|
|
|
case lists:member(LServer, ?MYHOSTS) of
|
|
|
|
true ->
|
2003-10-27 20:35:03 +01:00
|
|
|
ResIQ = process_local_iq(From, To, IQ),
|
|
|
|
ejabberd_router:route(From, From,
|
|
|
|
jlib:iq_to_xml(ResIQ)),
|
|
|
|
ignore;
|
|
|
|
_ ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_ITEM_NOT_FOUND]}
|
2003-10-27 20:35:03 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
-else.
|
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
process_iq(From, To, IQ) ->
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{sub_el = SubEl} = IQ,
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{lserver = LServer} = From,
|
2005-04-17 20:08:34 +02:00
|
|
|
case lists:member(LServer, ?MYHOSTS) of
|
|
|
|
true ->
|
2003-07-21 22:01:22 +02:00
|
|
|
process_local_iq(From, To, IQ);
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_ITEM_NOT_FOUND]}
|
2002-12-11 21:57:45 +01:00
|
|
|
end.
|
|
|
|
|
2003-10-27 20:35:03 +01:00
|
|
|
-endif.
|
2003-07-21 22:01:22 +02:00
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
process_local_iq(From, To, #iq{type = Type} = IQ) ->
|
2003-07-21 22:01:22 +02:00
|
|
|
case Type of
|
|
|
|
set ->
|
|
|
|
process_iq_set(From, To, IQ);
|
|
|
|
get ->
|
|
|
|
process_iq_get(From, To, IQ)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
process_iq_get(From, To, #iq{sub_el = SubEl} = IQ) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
LUser = From#jid.luser,
|
|
|
|
LServer = From#jid.lserver,
|
|
|
|
US = {LUser, LServer},
|
2005-06-20 05:18:13 +02:00
|
|
|
case catch ejabberd_hooks:run_fold(roster_get, To#jid.lserver, [], [US]) of
|
2004-09-10 22:57:00 +02:00
|
|
|
Items when is_list(Items) ->
|
2002-12-13 21:58:27 +01:00
|
|
|
XItems = lists:map(fun item_to_xml/1, Items),
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_ROSTER}],
|
|
|
|
XItems}]};
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_INTERNAL_SERVER_ERROR]}
|
2002-12-13 21:58:27 +01:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_user_roster(Acc, US) ->
|
|
|
|
case catch mnesia:dirty_index_read(roster, US, #roster.us) of
|
|
|
|
Items when is_list(Items) ->
|
|
|
|
Items ++ Acc;
|
|
|
|
_ ->
|
|
|
|
Acc
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
item_to_xml(Item) ->
|
|
|
|
Attrs1 = [{"jid", jlib:jid_to_string(Item#roster.jid)}],
|
|
|
|
Attrs2 = case Item#roster.name of
|
|
|
|
"" ->
|
|
|
|
Attrs1;
|
|
|
|
Name ->
|
|
|
|
[{"name", Name} | Attrs1]
|
|
|
|
end,
|
|
|
|
Attrs3 = case Item#roster.subscription of
|
|
|
|
none ->
|
|
|
|
[{"subscription", "none"} | Attrs2];
|
2002-12-20 21:42:08 +01:00
|
|
|
from ->
|
|
|
|
[{"subscription", "from"} | Attrs2];
|
|
|
|
to ->
|
|
|
|
[{"subscription", "to"} | Attrs2];
|
2002-12-17 21:49:45 +01:00
|
|
|
both ->
|
|
|
|
[{"subscription", "both"} | Attrs2];
|
2002-12-14 21:07:26 +01:00
|
|
|
remove ->
|
2002-12-20 21:42:08 +01:00
|
|
|
[{"subscription", "remove"} | Attrs2]
|
2002-12-13 21:58:27 +01:00
|
|
|
end,
|
2003-12-11 21:31:40 +01:00
|
|
|
Attrs4 = case ask_to_pending(Item#roster.ask) of
|
|
|
|
out ->
|
|
|
|
[{"ask", "subscribe"} | Attrs3];
|
|
|
|
both ->
|
2002-12-20 21:42:08 +01:00
|
|
|
[{"ask", "subscribe"} | Attrs3];
|
2003-12-11 21:31:40 +01:00
|
|
|
_ ->
|
|
|
|
Attrs3
|
2002-12-20 21:42:08 +01:00
|
|
|
end,
|
|
|
|
Attrs = Attrs4 ++ Item#roster.xattrs,
|
2002-12-13 21:58:27 +01:00
|
|
|
SubEls1 = lists:map(fun(G) ->
|
|
|
|
{xmlelement, "group", [], [{xmlcdata, G}]}
|
|
|
|
end, Item#roster.groups),
|
|
|
|
SubEls = SubEls1 ++ Item#roster.xs,
|
|
|
|
{xmlelement, "item", Attrs, SubEls}.
|
|
|
|
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
process_iq_set(From, To, #iq{sub_el = SubEl} = IQ) ->
|
2003-10-27 20:35:03 +01:00
|
|
|
{xmlelement, _Name, _Attrs, Els} = SubEl,
|
2003-10-07 22:31:44 +02:00
|
|
|
lists:foreach(fun(El) -> process_item_set(From, To, El) end, Els),
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result, sub_el = []}.
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2003-10-27 20:35:03 +01:00
|
|
|
process_item_set(From, To, {xmlelement, _Name, Attrs, Els}) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
JID1 = jlib:string_to_jid(xml:get_attr_s("jid", Attrs)),
|
2005-04-17 20:08:34 +02:00
|
|
|
#jid{user = User, luser = LUser, lserver = LServer} = From,
|
2003-10-07 22:31:44 +02:00
|
|
|
case JID1 of
|
2002-12-13 21:58:27 +01:00
|
|
|
error ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
2003-10-07 22:31:44 +02:00
|
|
|
JID = {JID1#jid.user, JID1#jid.server, JID1#jid.resource},
|
|
|
|
LJID = jlib:jid_tolower(JID1),
|
2002-12-13 21:58:27 +01:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
Res = mnesia:read({roster, {LUser, LServer, LJID}}),
|
2002-12-13 21:58:27 +01:00
|
|
|
Item = case Res of
|
|
|
|
[] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
#roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer},
|
2002-12-20 21:42:08 +01:00
|
|
|
jid = JID};
|
2002-12-13 21:58:27 +01:00
|
|
|
[I] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
I#roster{jid = JID,
|
2002-12-20 21:42:08 +01:00
|
|
|
name = "",
|
2002-12-17 21:49:45 +01:00
|
|
|
groups = [],
|
2002-12-14 21:07:26 +01:00
|
|
|
xattrs = [],
|
|
|
|
xs = []}
|
2002-12-13 21:58:27 +01:00
|
|
|
end,
|
|
|
|
Item1 = process_item_attrs(Item, Attrs),
|
|
|
|
Item2 = process_item_els(Item1, Els),
|
2002-12-14 21:07:26 +01:00
|
|
|
case Item2#roster.subscription of
|
|
|
|
remove ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:delete({roster, {LUser, LServer, LJID}});
|
2002-12-14 21:07:26 +01:00
|
|
|
_ ->
|
|
|
|
mnesia:write(Item2)
|
|
|
|
end,
|
2003-01-17 20:58:42 +01:00
|
|
|
{Item, Item2}
|
2002-12-13 21:58:27 +01:00
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
2003-01-17 20:58:42 +01:00
|
|
|
{atomic, {OldItem, Item}} ->
|
2005-04-17 20:08:34 +02:00
|
|
|
push_item(User, LServer, To, Item),
|
2003-01-17 20:58:42 +01:00
|
|
|
case Item#roster.subscription of
|
|
|
|
remove ->
|
|
|
|
IsTo = case OldItem#roster.subscription of
|
|
|
|
both -> true;
|
|
|
|
to -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
IsFrom = case OldItem#roster.subscription of
|
|
|
|
both -> true;
|
|
|
|
from -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
if IsTo ->
|
|
|
|
ejabberd_router:route(
|
2003-12-24 22:05:45 +01:00
|
|
|
jlib:jid_remove_resource(From),
|
|
|
|
jlib:make_jid(OldItem#roster.jid),
|
2003-01-17 20:58:42 +01:00
|
|
|
{xmlelement, "presence",
|
|
|
|
[{"type", "unsubscribe"}],
|
|
|
|
[]});
|
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
if IsFrom ->
|
|
|
|
ejabberd_router:route(
|
2003-12-24 22:05:45 +01:00
|
|
|
jlib:jid_remove_resource(From),
|
|
|
|
jlib:make_jid(OldItem#roster.jid),
|
2003-01-17 20:58:42 +01:00
|
|
|
{xmlelement, "presence",
|
|
|
|
[{"type", "unsubscribed"}],
|
|
|
|
[]});
|
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
2002-12-14 21:07:26 +01:00
|
|
|
E ->
|
|
|
|
?DEBUG("ROSTER: roster item set error: ~p~n", [E]),
|
2002-12-13 21:58:27 +01:00
|
|
|
ok
|
|
|
|
end
|
2003-02-20 18:12:03 +01:00
|
|
|
end;
|
2003-10-27 20:35:03 +01:00
|
|
|
process_item_set(_From, _To, _) ->
|
2003-02-20 18:12:03 +01:00
|
|
|
ok.
|
2002-12-13 21:58:27 +01:00
|
|
|
|
|
|
|
process_item_attrs(Item, [{Attr, Val} | Attrs]) ->
|
|
|
|
case Attr of
|
|
|
|
"jid" ->
|
|
|
|
case jlib:string_to_jid(Val) of
|
|
|
|
error ->
|
2002-12-14 21:07:26 +01:00
|
|
|
process_item_attrs(Item, Attrs);
|
2003-10-07 22:31:44 +02:00
|
|
|
JID1 ->
|
|
|
|
JID = {JID1#jid.user, JID1#jid.server, JID1#jid.resource},
|
2002-12-14 21:07:26 +01:00
|
|
|
process_item_attrs(Item#roster{jid = JID}, Attrs)
|
2002-12-13 21:58:27 +01:00
|
|
|
end;
|
|
|
|
"name" ->
|
2002-12-14 21:07:26 +01:00
|
|
|
process_item_attrs(Item#roster{name = Val}, Attrs);
|
2002-12-13 21:58:27 +01:00
|
|
|
"subscription" ->
|
|
|
|
case Val of
|
|
|
|
"remove" ->
|
|
|
|
process_item_attrs(Item#roster{subscription = remove},
|
2002-12-14 21:07:26 +01:00
|
|
|
Attrs);
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
2002-12-14 21:07:26 +01:00
|
|
|
process_item_attrs(Item, Attrs)
|
2002-12-13 21:58:27 +01:00
|
|
|
end;
|
|
|
|
"ask" ->
|
2002-12-14 21:07:26 +01:00
|
|
|
process_item_attrs(Item, Attrs);
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
|
|
|
XAttrs = Item#roster.xattrs,
|
|
|
|
process_item_attrs(Item#roster{xattrs = [{Attr, Val} | XAttrs]},
|
2002-12-14 21:07:26 +01:00
|
|
|
Attrs)
|
2002-12-13 21:58:27 +01:00
|
|
|
end;
|
|
|
|
process_item_attrs(Item, []) ->
|
|
|
|
Item.
|
|
|
|
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
process_item_els(Item, [{xmlelement, Name, Attrs, SEls} | Els]) ->
|
|
|
|
case Name of
|
|
|
|
"group" ->
|
|
|
|
Groups = [xml:get_cdata(SEls) | Item#roster.groups],
|
|
|
|
process_item_els(Item#roster{groups = Groups}, Els);
|
|
|
|
_ ->
|
|
|
|
case xml:get_attr_s("xmlns", Attrs) of
|
|
|
|
"" ->
|
|
|
|
process_item_els(Item, Els);
|
|
|
|
_ ->
|
|
|
|
XEls = [{xmlelement, Name, Attrs, SEls} | Item#roster.xs],
|
|
|
|
process_item_els(Item#roster{xs = XEls}, Els)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
process_item_els(Item, [{xmlcdata, _} | Els]) ->
|
|
|
|
process_item_els(Item, Els);
|
|
|
|
process_item_els(Item, []) ->
|
|
|
|
Item.
|
2002-12-14 21:07:26 +01:00
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
push_item(User, Server, From, Item) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
ejabberd_sm:route(jlib:make_jid("", "", ""),
|
2005-04-17 20:08:34 +02:00
|
|
|
jlib:make_jid(User, Server, ""),
|
2004-12-19 21:47:35 +01:00
|
|
|
{xmlelement, "broadcast", [],
|
|
|
|
[{item,
|
|
|
|
Item#roster.jid,
|
|
|
|
Item#roster.subscription}]}),
|
2002-12-14 21:07:26 +01:00
|
|
|
lists:foreach(fun(Resource) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
push_item(User, Server, Resource, From, Item)
|
|
|
|
end, ejabberd_sm:get_user_resources(User, Server)).
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2005-11-22 19:43:06 +01:00
|
|
|
% TODO: don't push to those who didn't load roster
|
2003-10-27 20:35:03 +01:00
|
|
|
-ifdef(PSI_ROSTER_WORKAROUND).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
push_item(User, Server, Resource, _From, Item) ->
|
2003-12-17 21:13:21 +01:00
|
|
|
ResIQ = #iq{type = set, xmlns = ?NS_ROSTER,
|
2005-11-22 19:43:06 +01:00
|
|
|
id = "push",
|
2003-12-17 21:13:21 +01:00
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_ROSTER}],
|
|
|
|
[item_to_xml(Item)]}]},
|
|
|
|
ejabberd_router:route(
|
2005-04-17 20:08:34 +02:00
|
|
|
jlib:make_jid(User, Server, Resource),
|
|
|
|
jlib:make_jid(User, Server, Resource),
|
2003-12-17 21:13:21 +01:00
|
|
|
jlib:iq_to_xml(ResIQ)).
|
2003-10-27 20:35:03 +01:00
|
|
|
|
|
|
|
-else.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
push_item(User, Server, Resource, From, Item) ->
|
2003-12-17 21:13:21 +01:00
|
|
|
ResIQ = #iq{type = set, xmlns = ?NS_ROSTER,
|
2005-11-22 19:43:06 +01:00
|
|
|
id = "push",
|
2003-12-17 21:13:21 +01:00
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_ROSTER}],
|
|
|
|
[item_to_xml(Item)]}]},
|
|
|
|
ejabberd_router:route(
|
|
|
|
From,
|
2005-04-17 20:08:34 +02:00
|
|
|
jlib:make_jid(User, Server, Resource),
|
2003-12-17 21:13:21 +01:00
|
|
|
jlib:iq_to_xml(ResIQ)).
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2003-10-27 20:35:03 +01:00
|
|
|
-endif.
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_subscription_lists(_, 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},
|
|
|
|
case mnesia:dirty_index_read(roster, US, #roster.us) of
|
2004-09-10 22:57:00 +02:00
|
|
|
Items when is_list(Items) ->
|
2002-12-17 21:49:45 +01:00
|
|
|
fill_subscription_lists(Items, [], []);
|
|
|
|
_ ->
|
|
|
|
{[], []}
|
|
|
|
end.
|
|
|
|
|
|
|
|
fill_subscription_lists([I | Is], F, T) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
J = element(3, I#roster.usj),
|
2002-12-17 21:49:45 +01:00
|
|
|
case I#roster.subscription of
|
|
|
|
both ->
|
|
|
|
fill_subscription_lists(Is, [J | F], [J | T]);
|
|
|
|
from ->
|
|
|
|
fill_subscription_lists(Is, [J | F], T);
|
|
|
|
to ->
|
|
|
|
fill_subscription_lists(Is, F, [J | T]);
|
|
|
|
_ ->
|
|
|
|
fill_subscription_lists(Is, F, T)
|
|
|
|
end;
|
|
|
|
fill_subscription_lists([], F, T) ->
|
|
|
|
{F, T}.
|
|
|
|
|
2003-12-11 21:31:40 +01:00
|
|
|
ask_to_pending(subscribe) -> out;
|
|
|
|
ask_to_pending(unsubscribe) -> none;
|
|
|
|
ask_to_pending(Ask) -> Ask.
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2002-12-20 21:42:08 +01:00
|
|
|
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
in_subscription(_, User, Server, JID, Type) ->
|
|
|
|
process_subscription(in, User, Server, JID, Type).
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
out_subscription(User, Server, JID, Type) ->
|
|
|
|
process_subscription(out, User, Server, JID, Type).
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_subscription(Direction, User, Server, JID1, Type) ->
|
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-10-07 22:31:44 +02:00
|
|
|
LJID = jlib:jid_tolower(JID1),
|
2002-12-20 21:42:08 +01:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
Item = case mnesia:read({roster, {LUser, LServer, LJID}}) of
|
2002-12-20 21:42:08 +01:00
|
|
|
[] ->
|
2003-12-11 21:31:40 +01:00
|
|
|
JID = {JID1#jid.user,
|
|
|
|
JID1#jid.server,
|
|
|
|
JID1#jid.resource},
|
2005-04-17 20:08:34 +02:00
|
|
|
#roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = US,
|
2003-12-11 21:31:40 +01:00
|
|
|
jid = JID};
|
2002-12-20 21:42:08 +01:00
|
|
|
[I] ->
|
|
|
|
I
|
|
|
|
end,
|
2003-12-11 21:31:40 +01:00
|
|
|
NewState = case Direction of
|
|
|
|
out ->
|
|
|
|
out_state_change(Item#roster.subscription,
|
|
|
|
Item#roster.ask,
|
|
|
|
Type);
|
|
|
|
in ->
|
|
|
|
in_state_change(Item#roster.subscription,
|
|
|
|
Item#roster.ask,
|
|
|
|
Type)
|
|
|
|
end,
|
2004-01-18 21:42:09 +01:00
|
|
|
AutoReply = case Direction of
|
|
|
|
out ->
|
|
|
|
none;
|
|
|
|
in ->
|
|
|
|
in_auto_reply(Item#roster.subscription,
|
|
|
|
Item#roster.ask,
|
|
|
|
Type)
|
|
|
|
end,
|
2003-12-11 21:31:40 +01:00
|
|
|
case NewState of
|
|
|
|
none ->
|
2004-01-18 21:42:09 +01:00
|
|
|
{none, AutoReply};
|
2003-12-11 21:31:40 +01:00
|
|
|
{Subscription, Pending} ->
|
|
|
|
NewItem = Item#roster{subscription = Subscription,
|
|
|
|
ask = Pending},
|
2002-12-20 21:42:08 +01:00
|
|
|
mnesia:write(NewItem),
|
2004-01-18 21:42:09 +01:00
|
|
|
{{push, NewItem}, AutoReply}
|
2002-12-20 21:42:08 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
2004-01-18 21:42:09 +01:00
|
|
|
{atomic, {Push, AutoReply}} ->
|
|
|
|
case AutoReply of
|
|
|
|
none ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
T = case AutoReply of
|
|
|
|
subscribed -> "subscribed";
|
|
|
|
unsubscribed -> "unsubscribed"
|
|
|
|
end,
|
|
|
|
ejabberd_router:route(
|
2005-04-17 20:08:34 +02:00
|
|
|
jlib:make_jid(User, Server, ""), JID1,
|
2004-01-18 21:42:09 +01:00
|
|
|
{xmlelement, "presence", [{"type", T}], []})
|
|
|
|
end,
|
|
|
|
case Push of
|
|
|
|
{push, Item} ->
|
2005-04-17 20:08:34 +02:00
|
|
|
push_item(User, Server,
|
|
|
|
jlib:make_jid("", Server, ""), Item),
|
2004-01-18 21:42:09 +01:00
|
|
|
true;
|
|
|
|
none ->
|
|
|
|
false
|
|
|
|
end;
|
2002-12-18 21:26:08 +01:00
|
|
|
_ ->
|
2002-12-20 21:42:08 +01:00
|
|
|
false
|
2002-12-18 21:26:08 +01:00
|
|
|
end.
|
|
|
|
|
2003-12-11 21:31:40 +01:00
|
|
|
%% in_state_change(Subscription, Pending, Type) -> NewState
|
|
|
|
%% NewState = none | {NewSubscription, NewPending}
|
2005-12-11 20:48:31 +01:00
|
|
|
-ifdef(ROSTER_GATEWAY_WORKAROUND).
|
|
|
|
-define(NNSD, {to, none}).
|
|
|
|
-define(NISD, {to, in}).
|
|
|
|
-else.
|
|
|
|
-define(NNSD, none).
|
|
|
|
-define(NISD, none).
|
|
|
|
-endif.
|
2003-12-11 21:31:40 +01:00
|
|
|
|
|
|
|
in_state_change(none, none, subscribe) -> {none, in};
|
2005-12-11 20:48:31 +01:00
|
|
|
in_state_change(none, none, subscribed) -> ?NNSD;
|
2003-12-11 21:31:40 +01:00
|
|
|
in_state_change(none, none, unsubscribe) -> none;
|
|
|
|
in_state_change(none, none, unsubscribed) -> none;
|
|
|
|
in_state_change(none, out, subscribe) -> {none, both};
|
|
|
|
in_state_change(none, out, subscribed) -> {to, none};
|
|
|
|
in_state_change(none, out, unsubscribe) -> none;
|
|
|
|
in_state_change(none, out, unsubscribed) -> {none, none};
|
|
|
|
in_state_change(none, in, subscribe) -> none;
|
2005-12-11 20:48:31 +01:00
|
|
|
in_state_change(none, in, subscribed) -> ?NISD;
|
2003-12-11 21:31:40 +01:00
|
|
|
in_state_change(none, in, unsubscribe) -> {none, none};
|
|
|
|
in_state_change(none, in, unsubscribed) -> none;
|
|
|
|
in_state_change(none, both, subscribe) -> none;
|
|
|
|
in_state_change(none, both, subscribed) -> {to, in};
|
|
|
|
in_state_change(none, both, unsubscribe) -> {none, out};
|
|
|
|
in_state_change(none, both, unsubscribed) -> {none, in};
|
|
|
|
in_state_change(to, none, subscribe) -> {to, in};
|
|
|
|
in_state_change(to, none, subscribed) -> none;
|
|
|
|
in_state_change(to, none, unsubscribe) -> none;
|
|
|
|
in_state_change(to, none, unsubscribed) -> {none, none};
|
|
|
|
in_state_change(to, in, subscribe) -> none;
|
|
|
|
in_state_change(to, in, subscribed) -> none;
|
|
|
|
in_state_change(to, in, unsubscribe) -> {to, none};
|
|
|
|
in_state_change(to, in, unsubscribed) -> {none, in};
|
|
|
|
in_state_change(from, none, subscribe) -> none;
|
|
|
|
in_state_change(from, none, subscribed) -> none;
|
|
|
|
in_state_change(from, none, unsubscribe) -> {none, none};
|
|
|
|
in_state_change(from, none, unsubscribed) -> none;
|
|
|
|
in_state_change(from, out, subscribe) -> none;
|
|
|
|
in_state_change(from, out, subscribed) -> {both, none};
|
|
|
|
in_state_change(from, out, unsubscribe) -> {none, out};
|
|
|
|
in_state_change(from, out, unsubscribed) -> {from, none};
|
|
|
|
in_state_change(both, none, subscribe) -> none;
|
|
|
|
in_state_change(both, none, subscribed) -> none;
|
|
|
|
in_state_change(both, none, unsubscribe) -> {to, none};
|
|
|
|
in_state_change(both, none, unsubscribed) -> {from, none}.
|
|
|
|
|
|
|
|
out_state_change(none, none, subscribe) -> {none, out};
|
|
|
|
out_state_change(none, none, subscribed) -> none;
|
|
|
|
out_state_change(none, none, unsubscribe) -> none;
|
|
|
|
out_state_change(none, none, unsubscribed) -> none;
|
|
|
|
out_state_change(none, out, subscribe) -> none;
|
|
|
|
out_state_change(none, out, subscribed) -> none;
|
|
|
|
out_state_change(none, out, unsubscribe) -> {none, none};
|
|
|
|
out_state_change(none, out, unsubscribed) -> none;
|
|
|
|
out_state_change(none, in, subscribe) -> {none, both};
|
|
|
|
out_state_change(none, in, subscribed) -> {from, none};
|
|
|
|
out_state_change(none, in, unsubscribe) -> none;
|
|
|
|
out_state_change(none, in, unsubscribed) -> {none, none};
|
|
|
|
out_state_change(none, both, subscribe) -> none;
|
|
|
|
out_state_change(none, both, subscribed) -> {from, out};
|
|
|
|
out_state_change(none, both, unsubscribe) -> {none, in};
|
|
|
|
out_state_change(none, both, unsubscribed) -> {none, out};
|
|
|
|
out_state_change(to, none, subscribe) -> none;
|
|
|
|
out_state_change(to, none, subscribed) -> none;
|
|
|
|
out_state_change(to, none, unsubscribe) -> {none, none};
|
|
|
|
out_state_change(to, none, unsubscribed) -> none;
|
|
|
|
out_state_change(to, in, subscribe) -> none;
|
|
|
|
out_state_change(to, in, subscribed) -> {both, none};
|
|
|
|
out_state_change(to, in, unsubscribe) -> {none, in};
|
|
|
|
out_state_change(to, in, unsubscribed) -> {to, none};
|
|
|
|
out_state_change(from, none, subscribe) -> {from, out};
|
|
|
|
out_state_change(from, none, subscribed) -> none;
|
|
|
|
out_state_change(from, none, unsubscribe) -> none;
|
|
|
|
out_state_change(from, none, unsubscribed) -> {none, none};
|
|
|
|
out_state_change(from, out, subscribe) -> none;
|
|
|
|
out_state_change(from, out, subscribed) -> none;
|
|
|
|
out_state_change(from, out, unsubscribe) -> {from, none};
|
|
|
|
out_state_change(from, out, unsubscribed) -> {none, out};
|
|
|
|
out_state_change(both, none, subscribe) -> none;
|
|
|
|
out_state_change(both, none, subscribed) -> none;
|
|
|
|
out_state_change(both, none, unsubscribe) -> {from, none};
|
|
|
|
out_state_change(both, none, unsubscribed) -> {to, none}.
|
|
|
|
|
2004-01-18 21:42:09 +01:00
|
|
|
in_auto_reply(from, none, subscribe) -> subscribed;
|
|
|
|
in_auto_reply(from, out, subscribe) -> subscribed;
|
|
|
|
in_auto_reply(both, none, subscribe) -> subscribed;
|
|
|
|
in_auto_reply(none, in, unsubscribe) -> unsubscribed;
|
|
|
|
in_auto_reply(none, both, unsubscribe) -> unsubscribed;
|
|
|
|
in_auto_reply(to, in, unsubscribe) -> unsubscribed;
|
|
|
|
in_auto_reply(from, none, unsubscribe) -> unsubscribed;
|
|
|
|
in_auto_reply(from, out, unsubscribe) -> unsubscribed;
|
|
|
|
in_auto_reply(both, none, unsubscribe) -> unsubscribed;
|
|
|
|
in_auto_reply(_, _, _) -> none.
|
|
|
|
|
2003-12-11 21:31:40 +01:00
|
|
|
|
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() ->
|
|
|
|
lists:foreach(fun(R) ->
|
|
|
|
mnesia:delete_object(R)
|
|
|
|
end,
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:index_read(roster, US, #roster.us))
|
2003-01-26 21:16:53 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2003-02-02 20:49:19 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
set_items(User, Server, SubEl) ->
|
2003-10-27 20:35:03 +01:00
|
|
|
{xmlelement, _Name, _Attrs, Els} = SubEl,
|
2003-10-17 21:15:38 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2003-02-02 20:49:19 +01:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
lists:foreach(fun(El) ->
|
|
|
|
process_item_set_t(LUser, LServer, El)
|
|
|
|
end, Els)
|
2003-02-02 20:49:19 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
process_item_set_t(LUser, LServer, {xmlelement, _Name, Attrs, Els}) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
JID1 = jlib:string_to_jid(xml:get_attr_s("jid", Attrs)),
|
|
|
|
case JID1 of
|
2003-02-02 20:49:19 +01:00
|
|
|
error ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
2003-10-07 22:31:44 +02:00
|
|
|
JID = {JID1#jid.user, JID1#jid.server, JID1#jid.resource},
|
2003-10-17 21:15:38 +02:00
|
|
|
LJID = {JID1#jid.luser, JID1#jid.lserver, JID1#jid.lresource},
|
2005-04-17 20:08:34 +02:00
|
|
|
Item = #roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer},
|
2003-02-02 20:49:19 +01:00
|
|
|
jid = JID},
|
|
|
|
Item1 = process_item_attrs_ws(Item, Attrs),
|
|
|
|
Item2 = process_item_els(Item1, Els),
|
|
|
|
case Item2#roster.subscription of
|
|
|
|
remove ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:delete({roster, {LUser, LServer, LJID}});
|
2003-02-02 20:49:19 +01:00
|
|
|
_ ->
|
|
|
|
mnesia:write(Item2)
|
|
|
|
end
|
2003-02-20 18:12:03 +01:00
|
|
|
end;
|
2005-04-17 20:08:34 +02:00
|
|
|
process_item_set_t(_LUser, _LServer, _) ->
|
2003-02-20 18:12:03 +01:00
|
|
|
ok.
|
2003-02-02 20:49:19 +01:00
|
|
|
|
|
|
|
process_item_attrs_ws(Item, [{Attr, Val} | Attrs]) ->
|
|
|
|
case Attr of
|
|
|
|
"jid" ->
|
|
|
|
case jlib:string_to_jid(Val) of
|
|
|
|
error ->
|
|
|
|
process_item_attrs_ws(Item, Attrs);
|
2003-10-07 22:31:44 +02:00
|
|
|
JID1 ->
|
|
|
|
JID = {JID1#jid.user, JID1#jid.server, JID1#jid.resource},
|
2003-02-02 20:49:19 +01:00
|
|
|
process_item_attrs_ws(Item#roster{jid = JID}, Attrs)
|
|
|
|
end;
|
|
|
|
"name" ->
|
|
|
|
process_item_attrs_ws(Item#roster{name = Val}, Attrs);
|
|
|
|
"subscription" ->
|
|
|
|
case Val of
|
|
|
|
"remove" ->
|
|
|
|
process_item_attrs_ws(Item#roster{subscription = remove},
|
|
|
|
Attrs);
|
|
|
|
"none" ->
|
|
|
|
process_item_attrs_ws(Item#roster{subscription = none},
|
|
|
|
Attrs);
|
|
|
|
"both" ->
|
|
|
|
process_item_attrs_ws(Item#roster{subscription = both},
|
|
|
|
Attrs);
|
|
|
|
"from" ->
|
|
|
|
process_item_attrs_ws(Item#roster{subscription = from},
|
|
|
|
Attrs);
|
|
|
|
"to" ->
|
|
|
|
process_item_attrs_ws(Item#roster{subscription = to},
|
|
|
|
Attrs);
|
|
|
|
_ ->
|
|
|
|
process_item_attrs_ws(Item, Attrs)
|
|
|
|
end;
|
|
|
|
"ask" ->
|
|
|
|
process_item_attrs_ws(Item, Attrs);
|
|
|
|
_ ->
|
|
|
|
XAttrs = Item#roster.xattrs,
|
|
|
|
process_item_attrs_ws(Item#roster{xattrs = [{Attr, Val} | XAttrs]},
|
|
|
|
Attrs)
|
|
|
|
end;
|
|
|
|
process_item_attrs_ws(Item, []) ->
|
|
|
|
Item.
|
2003-08-03 21:09:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_jid_info(_, User, Server, JID) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2003-08-03 21:09:40 +02:00
|
|
|
LJID = jlib:jid_tolower(JID),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
case catch mnesia:dirty_read(roster, {LUser, LServer, LJID}) of
|
2003-08-03 21:09:40 +02:00
|
|
|
[#roster{subscription = Subscription, groups = Groups}] ->
|
|
|
|
{Subscription, Groups};
|
|
|
|
_ ->
|
2003-10-10 21:01:11 +02:00
|
|
|
LRJID = jlib:jid_tolower(jlib:jid_remove_resource(JID)),
|
|
|
|
if
|
|
|
|
LRJID == LJID ->
|
|
|
|
{none, []};
|
|
|
|
true ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case catch mnesia:dirty_read(
|
|
|
|
roster, {LUser, LServer, LRJID}) of
|
2003-10-10 21:01:11 +02:00
|
|
|
[#roster{subscription = Subscription,
|
|
|
|
groups = Groups}] ->
|
|
|
|
{Subscription, Groups};
|
|
|
|
_ ->
|
|
|
|
{none, []}
|
|
|
|
end
|
|
|
|
end
|
2003-08-03 21:09:40 +02:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, roster),
|
|
|
|
case mnesia:table_info(roster, attributes) of
|
|
|
|
Fields ->
|
|
|
|
ok;
|
|
|
|
[uj, user, jid, name, subscription, ask, groups, xattrs, xs] ->
|
|
|
|
?INFO_MSG("Converting roster table from "
|
|
|
|
"{uj, user, jid, name, subscription, ask, groups, xattrs, xs} format", []),
|
|
|
|
Host = ?MYNAME,
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_roster_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, roster},
|
|
|
|
{attributes, record_info(fields, roster)}]),
|
|
|
|
mnesia:del_table_index(roster, user),
|
|
|
|
mnesia:transform_table(roster, ignore, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_roster_tmp_table),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(#roster{usj = {U, JID}, us = U} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_roster_tmp_table,
|
|
|
|
R#roster{usj = {U, Host, JID},
|
|
|
|
us = {U, Host}})
|
|
|
|
end, ok, roster)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(roster),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(roster),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_roster_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_roster_tmp_table);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating roster table", []),
|
|
|
|
mnesia:transform_table(roster, ignore, Fields)
|
|
|
|
end.
|
|
|
|
|
2003-08-03 21:09:40 +02:00
|
|
|
|