2002-12-11 21:57:45 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_roster.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% 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
|
|
|
|
2003-01-22 21:40:40 +01:00
|
|
|
-export([start/1,
|
2002-12-17 21:49:45 +01:00
|
|
|
process_iq/3,
|
2003-01-22 21:40:40 +01:00
|
|
|
process_local_iq/3,
|
2002-12-18 21:26:08 +01:00
|
|
|
get_subscription_lists/1,
|
2002-12-20 21:42:08 +01:00
|
|
|
in_subscription/3,
|
2003-01-26 21:16:53 +01:00
|
|
|
out_subscription/3,
|
2003-02-02 20:49:19 +01:00
|
|
|
set_items/2,
|
2003-08-03 21:09:40 +02:00
|
|
|
remove_user/1,
|
|
|
|
get_jid_info/2]).
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
-include_lib("mnemosyne/include/mnemosyne.hrl").
|
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2002-12-20 21:42:08 +01:00
|
|
|
-record(roster, {uj,
|
|
|
|
user,
|
2002-12-13 21:58:27 +01:00
|
|
|
jid,
|
|
|
|
name = "",
|
|
|
|
subscription = none,
|
2002-12-20 21:42:08 +01:00
|
|
|
ask = none,
|
2002-12-13 21:58:27 +01:00
|
|
|
groups = [],
|
|
|
|
xattrs = [],
|
|
|
|
xs = []}).
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
start(Opts) ->
|
|
|
|
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)}]),
|
2002-12-20 21:42:08 +01:00
|
|
|
mnesia:add_table_index(roster, user),
|
2003-07-20 22:35:35 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, ?NS_ROSTER,
|
2003-07-21 22:01:22 +02:00
|
|
|
?MODULE, process_iq, IQDisc).
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2003-10-27 20:35:03 +01:00
|
|
|
-define(PSI_ROSTER_WORKAROUND, true).
|
|
|
|
|
|
|
|
-ifdef(PSI_ROSTER_WORKAROUND).
|
|
|
|
|
|
|
|
process_iq(From, To, IQ) ->
|
|
|
|
{iq, ID, _Type, XMLNS, SubEl} = IQ,
|
|
|
|
#jid{lserver = LServer} = From,
|
|
|
|
case ?MYNAME of
|
|
|
|
LServer ->
|
|
|
|
ResIQ = process_local_iq(From, To, IQ),
|
|
|
|
ejabberd_router:route(From, From,
|
|
|
|
jlib:iq_to_xml(ResIQ)),
|
|
|
|
ignore;
|
|
|
|
_ ->
|
|
|
|
{iq, ID, error, XMLNS,
|
|
|
|
[SubEl, ?ERR_ITEM_NOT_FOUND]}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-else.
|
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
process_iq(From, To, IQ) ->
|
2003-10-27 20:35:03 +01:00
|
|
|
{iq, ID, _Type, XMLNS, SubEl} = IQ,
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{lserver = LServer} = From,
|
2002-12-13 21:58:27 +01:00
|
|
|
case ?MYNAME of
|
2003-10-07 22:31:44 +02:00
|
|
|
LServer ->
|
2003-07-21 22:01:22 +02:00
|
|
|
process_local_iq(From, To, IQ);
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
|
|
|
{iq, ID, error, XMLNS,
|
2003-06-29 20:03:07 +02:00
|
|
|
[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
|
|
|
|
|
|
|
process_local_iq(From, To, {iq, _, Type, _, _} = IQ) ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
|
|
|
process_iq_set(From, To, IQ);
|
|
|
|
get ->
|
|
|
|
process_iq_get(From, To, IQ)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-10-27 20:35:03 +01:00
|
|
|
process_iq_get(From, _To, {iq, ID, _Type, XMLNS, SubEl}) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{luser = LUser} = From,
|
2002-12-13 21:58:27 +01:00
|
|
|
F = fun() ->
|
2002-12-20 21:42:08 +01:00
|
|
|
mnesia:index_read(roster, LUser, #roster.user)
|
2002-12-13 21:58:27 +01:00
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Items} ->
|
|
|
|
XItems = lists:map(fun item_to_xml/1, Items),
|
2002-12-14 21:07:26 +01:00
|
|
|
{iq, ID, result, XMLNS, [{xmlelement, "query",
|
2003-01-09 20:59:16 +01:00
|
|
|
[{"xmlns", ?NS_ROSTER}],
|
2002-12-14 21:07:26 +01:00
|
|
|
XItems}]};
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
|
|
|
{iq, ID, error, XMLNS,
|
2003-05-09 21:32:59 +02:00
|
|
|
[SubEl, ?ERR_INTERNAL_SERVER_ERROR]}
|
2002-12-13 21:58:27 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
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,
|
2002-12-20 21:42:08 +01:00
|
|
|
Attrs4 = case Item#roster.ask of
|
|
|
|
none ->
|
|
|
|
Attrs3;
|
|
|
|
subscribe ->
|
|
|
|
[{"ask", "subscribe"} | Attrs3];
|
|
|
|
unsubscribe ->
|
|
|
|
[{"ask", "unsubscribe"} | Attrs3]
|
|
|
|
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-10-27 20:35:03 +01:00
|
|
|
process_iq_set(From, To, {iq, ID, _Type, XMLNS, SubEl}) ->
|
|
|
|
{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),
|
2002-12-13 21:58:27 +01:00
|
|
|
{iq, ID, result, XMLNS, []}.
|
|
|
|
|
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)),
|
|
|
|
#jid{user = User, luser = LUser} = From,
|
|
|
|
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() ->
|
2002-12-20 21:42:08 +01:00
|
|
|
Res = mnesia:read({roster, {LUser, LJID}}),
|
2002-12-13 21:58:27 +01:00
|
|
|
Item = case Res of
|
|
|
|
[] ->
|
2002-12-20 21:42:08 +01:00
|
|
|
#roster{uj = {LUser, LJID},
|
|
|
|
user = LUser,
|
|
|
|
jid = JID};
|
2002-12-13 21:58:27 +01:00
|
|
|
[I] ->
|
2002-12-20 21:42:08 +01:00
|
|
|
I#roster{user = LUser,
|
|
|
|
jid = JID,
|
|
|
|
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 ->
|
2002-12-20 21:42:08 +01:00
|
|
|
mnesia:delete({roster, {LUser, 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}} ->
|
2002-12-14 21:07:26 +01:00
|
|
|
push_item(User, 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-10-07 22:31:44 +02:00
|
|
|
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-10-07 22:31:44 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
push_item(User, From, Item) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
ejabberd_sm ! {route,
|
|
|
|
jlib:make_jid("", "", ""),
|
|
|
|
jlib:make_jid(User, "", ""),
|
2003-01-10 20:44:35 +01:00
|
|
|
{xmlelement, "broadcast", [],
|
|
|
|
[{item,
|
|
|
|
Item#roster.jid,
|
|
|
|
Item#roster.subscription}]}},
|
2002-12-14 21:07:26 +01:00
|
|
|
lists:foreach(fun(Resource) ->
|
|
|
|
push_item(User, Resource, From, Item)
|
|
|
|
end, ejabberd_sm:get_user_resources(User)).
|
|
|
|
|
|
|
|
% TODO: don't push to those who not load roster
|
2003-10-27 20:35:03 +01:00
|
|
|
-ifdef(PSI_ROSTER_WORKAROUND).
|
|
|
|
|
|
|
|
push_item(User, Resource, From, Item) ->
|
|
|
|
ResIQ = {iq, "", set, ?NS_ROSTER,
|
|
|
|
[{xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_ROSTER}],
|
|
|
|
[item_to_xml(Item)]}]},
|
|
|
|
ejabberd_router ! {route,
|
|
|
|
jlib:make_jid(User, ?MYNAME, Resource),
|
|
|
|
jlib:make_jid(User, ?MYNAME, Resource),
|
|
|
|
jlib:iq_to_xml(ResIQ)}.
|
|
|
|
|
|
|
|
-else.
|
|
|
|
|
2002-12-14 21:07:26 +01:00
|
|
|
push_item(User, Resource, From, Item) ->
|
2003-01-09 20:59:16 +01:00
|
|
|
ResIQ = {iq, "", set, ?NS_ROSTER,
|
2002-12-14 21:07:26 +01:00
|
|
|
[{xmlelement, "query",
|
2003-01-09 20:59:16 +01:00
|
|
|
[{"xmlns", ?NS_ROSTER}],
|
2002-12-14 21:07:26 +01:00
|
|
|
[item_to_xml(Item)]}]},
|
|
|
|
ejabberd_router ! {route,
|
|
|
|
From,
|
2003-10-07 22:31:44 +02:00
|
|
|
jlib:make_jid(User, ?MYNAME, Resource),
|
2002-12-14 21:07:26 +01:00
|
|
|
jlib:iq_to_xml(ResIQ)}.
|
|
|
|
|
2003-10-27 20:35:03 +01:00
|
|
|
-endif.
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
get_subscription_lists(User) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2002-12-17 21:49:45 +01:00
|
|
|
F = fun() ->
|
2002-12-20 21:42:08 +01:00
|
|
|
mnesia:index_read(roster, LUser, #roster.user)
|
2002-12-17 21:49:45 +01:00
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Items} ->
|
|
|
|
fill_subscription_lists(Items, [], []);
|
|
|
|
_ ->
|
|
|
|
{[], []}
|
|
|
|
end.
|
|
|
|
|
|
|
|
fill_subscription_lists([I | Is], F, T) ->
|
2002-12-20 21:42:08 +01:00
|
|
|
J = element(2, I#roster.uj),
|
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}.
|
|
|
|
|
|
|
|
|
2002-12-18 21:26:08 +01:00
|
|
|
in_subscription(User, From, Type) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2002-12-20 21:42:08 +01:00
|
|
|
LFrom = jlib:jid_tolower(From),
|
2002-12-18 21:26:08 +01:00
|
|
|
F = fun() ->
|
2002-12-20 21:42:08 +01:00
|
|
|
case mnesia:read({roster, {LUser, LFrom}}) of
|
|
|
|
[] ->
|
|
|
|
case Type of
|
|
|
|
subscribe ->
|
|
|
|
true;
|
|
|
|
unsubscribe ->
|
|
|
|
true;
|
|
|
|
unsubscribed ->
|
|
|
|
false;
|
|
|
|
subscribed ->
|
2003-10-07 22:31:44 +02:00
|
|
|
JID = {From#jid.user,
|
|
|
|
From#jid.server,
|
|
|
|
From#jid.resource},
|
2002-12-20 21:42:08 +01:00
|
|
|
NewItem = #roster{uj = {LUser, LFrom},
|
|
|
|
user = LUser,
|
2003-10-27 20:35:03 +01:00
|
|
|
jid = JID},
|
2002-12-20 21:42:08 +01:00
|
|
|
mnesia:write(NewItem),
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
[I] ->
|
|
|
|
case Type of
|
|
|
|
subscribe ->
|
|
|
|
S = I#roster.subscription,
|
|
|
|
if
|
|
|
|
(S == both) or (S == from) ->
|
2002-12-22 21:56:29 +01:00
|
|
|
{update,
|
|
|
|
{xmlelement, "presence",
|
|
|
|
[{"type", "subscribed"}], []},
|
|
|
|
I};
|
2002-12-20 21:42:08 +01:00
|
|
|
true ->
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
unsubscribe ->
|
|
|
|
S = I#roster.subscription,
|
|
|
|
if
|
|
|
|
(S == none) or (S == to) ->
|
2002-12-22 21:56:29 +01:00
|
|
|
{update,
|
|
|
|
{xmlelement, "presence",
|
|
|
|
[{"type", "unsubscribed"}], []},
|
|
|
|
I};
|
2002-12-20 21:42:08 +01:00
|
|
|
true ->
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
S = I#roster.subscription,
|
|
|
|
NS = case Type of
|
|
|
|
subscribed ->
|
|
|
|
case S of
|
|
|
|
from -> both;
|
|
|
|
none -> to;
|
|
|
|
_ -> S
|
|
|
|
end;
|
|
|
|
unsubscribed ->
|
|
|
|
case S of
|
|
|
|
both -> from;
|
|
|
|
to -> none;
|
|
|
|
_ -> S
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
NewItem = I#roster{subscription = NS,
|
|
|
|
ask = none},
|
|
|
|
mnesia:write(NewItem),
|
|
|
|
{push, NewItem}
|
|
|
|
end
|
|
|
|
end
|
2002-12-18 21:26:08 +01:00
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
2002-12-20 21:42:08 +01:00
|
|
|
{atomic, true} ->
|
|
|
|
true;
|
|
|
|
{atomic, false} ->
|
|
|
|
false;
|
2002-12-22 21:56:29 +01:00
|
|
|
{atomic, {update, Presence, Item}} ->
|
2003-10-30 15:38:38 +01:00
|
|
|
ejabberd_router:route(jlib:make_jid(User, ?MYNAME, ""),
|
2003-10-07 22:31:44 +02:00
|
|
|
jlib:jid_replace_resource(From, ""),
|
|
|
|
Presence),
|
|
|
|
ejabberd_sm ! {route,
|
|
|
|
jlib:make_jid("", "", ""),
|
|
|
|
jlib:make_jid(User, "", ""),
|
2002-12-22 21:56:29 +01:00
|
|
|
{xmlelement, "broadcast", [],
|
|
|
|
[{item,
|
|
|
|
Item#roster.jid,
|
|
|
|
Item#roster.subscription}]}},
|
|
|
|
false;
|
2002-12-20 21:42:08 +01:00
|
|
|
{atomic, {push, Item}} ->
|
|
|
|
push_item(User, {"", ?MYNAME, ""}, Item),
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
out_subscription(User, JID1, Type) ->
|
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LJID = jlib:jid_tolower(JID1),
|
2002-12-20 21:42:08 +01:00
|
|
|
F = fun() ->
|
|
|
|
Item = case mnesia:read({roster, {LUser, LJID}}) of
|
|
|
|
[] ->
|
|
|
|
if (Type == unsubscribe) or
|
|
|
|
(Type == unsubscribed) ->
|
|
|
|
false;
|
|
|
|
true ->
|
2003-10-07 22:31:44 +02:00
|
|
|
JID = {JID1#jid.user,
|
|
|
|
JID1#jid.server,
|
|
|
|
JID1#jid.resource},
|
2002-12-20 21:42:08 +01:00
|
|
|
#roster{uj = {LUser, LJID},
|
|
|
|
user = LUser,
|
|
|
|
jid = JID}
|
|
|
|
end;
|
|
|
|
[I] ->
|
|
|
|
I
|
|
|
|
end,
|
|
|
|
if Item == false ->
|
|
|
|
ok;
|
|
|
|
true ->
|
2002-12-22 21:56:29 +01:00
|
|
|
{NewItem, Update} =
|
2002-12-20 21:42:08 +01:00
|
|
|
case Type of
|
|
|
|
subscribe ->
|
2002-12-22 21:56:29 +01:00
|
|
|
{Item#roster{ask = subscribe}, false};
|
2002-12-20 21:42:08 +01:00
|
|
|
unsubscribe ->
|
2002-12-22 21:56:29 +01:00
|
|
|
{Item#roster{ask = unsubscribe}, false};
|
2002-12-20 21:42:08 +01:00
|
|
|
subscribed ->
|
|
|
|
S = Item#roster.subscription,
|
|
|
|
NS = case S of
|
|
|
|
to -> both;
|
|
|
|
none -> from;
|
|
|
|
_ -> S
|
|
|
|
end,
|
2002-12-22 21:56:29 +01:00
|
|
|
{Item#roster{subscription = NS,
|
|
|
|
ask = none},
|
|
|
|
true};
|
2002-12-20 21:42:08 +01:00
|
|
|
unsubscribed ->
|
|
|
|
S = Item#roster.subscription,
|
|
|
|
NS = case S of
|
|
|
|
both -> to;
|
|
|
|
from -> none;
|
|
|
|
_ -> S
|
|
|
|
end,
|
2002-12-22 21:56:29 +01:00
|
|
|
{Item#roster{subscription = NS,
|
|
|
|
ask = none},
|
|
|
|
true}
|
2002-12-20 21:42:08 +01:00
|
|
|
end,
|
|
|
|
mnesia:write(NewItem),
|
2002-12-22 21:56:29 +01:00
|
|
|
{push, NewItem, Update}
|
2002-12-20 21:42:08 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, ok} ->
|
2002-12-18 21:26:08 +01:00
|
|
|
ok;
|
2002-12-22 21:56:29 +01:00
|
|
|
{atomic, {push, Item, Update}} ->
|
2002-12-20 21:42:08 +01:00
|
|
|
push_item(User, {"", ?MYNAME, ""}, Item),
|
2002-12-22 21:56:29 +01:00
|
|
|
if
|
|
|
|
Update ->
|
2003-10-07 22:31:44 +02:00
|
|
|
ejabberd_sm ! {route,
|
|
|
|
jlib:make_jid("", "", ""),
|
|
|
|
jlib:make_jid(User, "", ""),
|
2002-12-22 21:56:29 +01:00
|
|
|
{xmlelement, "broadcast", [],
|
|
|
|
[{item,
|
|
|
|
Item#roster.jid,
|
|
|
|
Item#roster.subscription}]}};
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
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-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() ->
|
|
|
|
lists:foreach(fun(R) ->
|
|
|
|
mnesia:delete_object(R)
|
|
|
|
end,
|
|
|
|
mnesia:index_read(roster, LUser, #roster.user))
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2003-02-02 20:49:19 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
set_items(User, 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),
|
2003-02-02 20:49:19 +01:00
|
|
|
F = fun() ->
|
2003-10-17 21:15:38 +02:00
|
|
|
lists:foreach(fun(El) -> process_item_set_t(LUser, El) end, Els)
|
2003-02-02 20:49:19 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2003-10-27 20:35:03 +01:00
|
|
|
process_item_set_t(LUser, {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},
|
2003-02-02 20:49:19 +01:00
|
|
|
Item = #roster{uj = {LUser, LJID},
|
|
|
|
user = LUser,
|
|
|
|
jid = JID},
|
|
|
|
Item1 = process_item_attrs_ws(Item, Attrs),
|
|
|
|
Item2 = process_item_els(Item1, Els),
|
|
|
|
case Item2#roster.subscription of
|
|
|
|
remove ->
|
|
|
|
mnesia:delete({roster, {LUser, LJID}});
|
|
|
|
_ ->
|
|
|
|
mnesia:write(Item2)
|
|
|
|
end
|
2003-02-20 18:12:03 +01:00
|
|
|
end;
|
2003-10-27 20:35:03 +01:00
|
|
|
process_item_set_t(_LUser, _) ->
|
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
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
get_jid_info(User, 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),
|
|
|
|
case catch mnesia:dirty_read(roster, {LUser, LJID}) of
|
|
|
|
[#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 ->
|
|
|
|
case catch mnesia:dirty_read(roster, {LUser, LRJID}) of
|
|
|
|
[#roster{subscription = Subscription,
|
|
|
|
groups = Groups}] ->
|
|
|
|
{Subscription, Groups};
|
|
|
|
_ ->
|
|
|
|
{none, []}
|
|
|
|
end
|
|
|
|
end
|
2003-08-03 21:09:40 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|