2004-12-19 21:47:35 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_roster_odbc.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2004-12-19 21:47:35 +01:00
|
|
|
%%% Purpose : Roster management
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 15 Dec 2004 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2008-07-31 13:18:49 +02:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2008 ProcessOne
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2008-10-07 14:34:23 +02:00
|
|
|
%%%
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2004-12-19 21:47:35 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_roster_odbc).
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-12-19 21:47:35 +01:00
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2, stop/1,
|
2004-12-19 21:47:35 +01:00
|
|
|
process_iq/3,
|
|
|
|
process_local_iq/3,
|
2005-07-13 05:24:13 +02:00
|
|
|
get_user_roster/2,
|
|
|
|
get_subscription_lists/3,
|
2006-05-23 22:19:37 +02:00
|
|
|
get_in_pending_subscriptions/3,
|
|
|
|
in_subscription/6,
|
2005-07-13 05:24:13 +02:00
|
|
|
out_subscription/4,
|
|
|
|
set_items/3,
|
|
|
|
remove_user/2,
|
2007-08-24 18:15:05 +02:00
|
|
|
get_jid_info/4,
|
|
|
|
webadmin_page/3,
|
|
|
|
webadmin_user/4]).
|
2004-12-19 21:47:35 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("jlib.hrl").
|
2006-02-12 19:40:03 +01:00
|
|
|
-include("mod_roster.hrl").
|
2007-08-24 18:15:05 +02:00
|
|
|
-include("web/ejabberd_http.hrl").
|
|
|
|
-include("web/ejabberd_web_admin.hrl").
|
|
|
|
|
2004-12-19 21:47:35 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2005-07-13 05:24:13 +02:00
|
|
|
ejabberd_hooks:add(roster_get, Host,
|
|
|
|
?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),
|
2007-05-12 18:28:34 +02:00
|
|
|
ejabberd_hooks:add(anonymous_purge_hook, Host,
|
|
|
|
?MODULE, remove_user, 50),
|
2006-05-23 22:19:37 +02:00
|
|
|
ejabberd_hooks:add(resend_subscription_requests_hook, Host,
|
|
|
|
?MODULE, get_in_pending_subscriptions, 50),
|
2007-08-24 18:15:05 +02:00
|
|
|
ejabberd_hooks:add(webadmin_page_host, Host,
|
|
|
|
?MODULE, webadmin_page, 50),
|
|
|
|
ejabberd_hooks:add(webadmin_user, Host,
|
|
|
|
?MODULE, webadmin_user, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_ROSTER,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, process_iq, IQDisc).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2005-07-13 05:24:13 +02:00
|
|
|
ejabberd_hooks:delete(roster_get, Host,
|
|
|
|
?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),
|
2007-05-12 18:28:34 +02:00
|
|
|
ejabberd_hooks:delete(anonymous_purge_hook, Host,
|
|
|
|
?MODULE, remove_user, 50),
|
2006-05-23 22:19:37 +02:00
|
|
|
ejabberd_hooks:delete(resend_subscription_requests_hook, Host,
|
|
|
|
?MODULE, get_in_pending_subscriptions, 50),
|
2007-08-24 18:15:05 +02:00
|
|
|
ejabberd_hooks:delete(webadmin_page_host, Host,
|
|
|
|
?MODULE, webadmin_page, 50),
|
|
|
|
ejabberd_hooks:delete(webadmin_user, Host,
|
|
|
|
?MODULE, webadmin_user, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_ROSTER).
|
2004-12-19 21:47:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
-define(PSI_ROSTER_WORKAROUND, true).
|
|
|
|
|
|
|
|
-ifdef(PSI_ROSTER_WORKAROUND).
|
|
|
|
|
|
|
|
process_iq(From, To, IQ) ->
|
|
|
|
#iq{sub_el = SubEl} = IQ,
|
|
|
|
#jid{lserver = LServer} = From,
|
2006-02-12 19:40:03 +01:00
|
|
|
case lists:member(LServer, ?MYHOSTS) of
|
|
|
|
true ->
|
2004-12-19 21:47:35 +01:00
|
|
|
ResIQ = process_local_iq(From, To, IQ),
|
|
|
|
ejabberd_router:route(From, From,
|
|
|
|
jlib:iq_to_xml(ResIQ)),
|
|
|
|
ignore;
|
|
|
|
_ ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_ITEM_NOT_FOUND]}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-else.
|
|
|
|
|
|
|
|
process_iq(From, To, IQ) ->
|
|
|
|
#iq{sub_el = SubEl} = IQ,
|
|
|
|
#jid{lserver = LServer} = From,
|
2006-02-12 19:40:03 +01:00
|
|
|
case lists:member(LServer, ?MYHOSTS) of
|
|
|
|
true ->
|
2004-12-19 21:47:35 +01:00
|
|
|
process_local_iq(From, To, IQ);
|
|
|
|
_ ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_ITEM_NOT_FOUND]}
|
|
|
|
end.
|
|
|
|
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
process_local_iq(From, To, #iq{type = Type} = IQ) ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
|
|
|
process_iq_set(From, To, IQ);
|
|
|
|
get ->
|
|
|
|
process_iq_get(From, To, IQ)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-01-24 22:47:53 +01:00
|
|
|
process_iq_get(From, To, #iq{sub_el = SubEl} = IQ) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
LUser = From#jid.luser,
|
2005-07-13 05:24:13 +02:00
|
|
|
LServer = From#jid.lserver,
|
2006-01-24 22:47:53 +01:00
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch ejabberd_hooks:run_fold(roster_get, To#jid.lserver, [], [US]) of
|
|
|
|
Items when is_list(Items) ->
|
|
|
|
XItems = lists:map(fun item_to_xml/1, Items),
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_ROSTER}],
|
|
|
|
XItems}]};
|
|
|
|
_ ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_INTERNAL_SERVER_ERROR]}
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_user_roster(Acc, {LUser, LServer}) ->
|
2007-08-24 18:15:05 +02:00
|
|
|
Items = get_roster(LUser, LServer),
|
|
|
|
lists:filter(fun(#roster{subscription = none, ask = in}) ->
|
|
|
|
false;
|
|
|
|
(_) ->
|
|
|
|
true
|
|
|
|
end, Items) ++ Acc.
|
|
|
|
|
|
|
|
get_roster(LUser, LServer) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2006-09-03 17:15:46 +02:00
|
|
|
case catch odbc_queries:get_roster(LServer, Username) of
|
2004-12-19 21:47:35 +01:00
|
|
|
{selected, ["username", "jid", "nick", "subscription", "ask",
|
2006-05-26 02:00:32 +02:00
|
|
|
"askmessage", "server", "subscribe", "type"],
|
2004-12-19 21:47:35 +01:00
|
|
|
Items} when is_list(Items) ->
|
2006-09-03 17:15:46 +02:00
|
|
|
JIDGroups = case catch odbc_queries:get_roster_jid_groups(LServer, Username) of
|
|
|
|
{selected, ["jid","grp"], JGrps}
|
|
|
|
when is_list(JGrps) ->
|
2005-07-26 05:04:26 +02:00
|
|
|
JGrps;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2006-01-24 22:47:53 +01:00
|
|
|
RItems = lists:flatmap(
|
2004-12-19 21:47:35 +01:00
|
|
|
fun(I) ->
|
2006-02-12 19:40:03 +01:00
|
|
|
case raw_to_record(LServer, I) of
|
2007-07-28 11:36:37 +02:00
|
|
|
%% Bad JID in database:
|
2004-12-19 21:47:35 +01:00
|
|
|
error ->
|
|
|
|
[];
|
|
|
|
R ->
|
2005-07-26 05:04:26 +02:00
|
|
|
SJID = jlib:jid_to_string(R#roster.jid),
|
|
|
|
Groups = lists:flatmap(
|
2006-09-05 06:26:28 +02:00
|
|
|
fun({S, G}) when S == SJID ->
|
|
|
|
[G];
|
|
|
|
(_) ->
|
|
|
|
[]
|
2005-07-26 05:04:26 +02:00
|
|
|
end, JIDGroups),
|
2006-02-12 00:55:21 +01:00
|
|
|
[R#roster{groups = Groups}]
|
2004-12-19 21:47:35 +01:00
|
|
|
end
|
|
|
|
end, Items),
|
2007-08-24 18:15:05 +02:00
|
|
|
RItems;
|
2005-07-13 05:24:13 +02:00
|
|
|
_ ->
|
2007-08-24 18:15:05 +02:00
|
|
|
[]
|
2005-07-13 05:24:13 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2004-12-19 21:47:35 +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];
|
|
|
|
from ->
|
|
|
|
[{"subscription", "from"} | Attrs2];
|
|
|
|
to ->
|
|
|
|
[{"subscription", "to"} | Attrs2];
|
|
|
|
both ->
|
|
|
|
[{"subscription", "both"} | Attrs2];
|
|
|
|
remove ->
|
|
|
|
[{"subscription", "remove"} | Attrs2]
|
|
|
|
end,
|
|
|
|
Attrs = case ask_to_pending(Item#roster.ask) of
|
|
|
|
out ->
|
|
|
|
[{"ask", "subscribe"} | Attrs3];
|
|
|
|
both ->
|
|
|
|
[{"ask", "subscribe"} | Attrs3];
|
|
|
|
_ ->
|
|
|
|
Attrs3
|
|
|
|
end,
|
2005-07-26 05:04:26 +02:00
|
|
|
SubEls = lists:map(fun(G) ->
|
|
|
|
{xmlelement, "group", [], [{xmlcdata, G}]}
|
|
|
|
end, Item#roster.groups),
|
2004-12-19 21:47:35 +01:00
|
|
|
{xmlelement, "item", Attrs, SubEls}.
|
|
|
|
|
|
|
|
|
|
|
|
process_iq_set(From, To, #iq{sub_el = SubEl} = IQ) ->
|
|
|
|
{xmlelement, _Name, _Attrs, Els} = SubEl,
|
|
|
|
lists:foreach(fun(El) -> process_item_set(From, To, El) end, Els),
|
|
|
|
IQ#iq{type = result, sub_el = []}.
|
|
|
|
|
|
|
|
process_item_set(From, To, {xmlelement, _Name, Attrs, Els}) ->
|
|
|
|
JID1 = jlib:string_to_jid(xml:get_attr_s("jid", Attrs)),
|
2005-07-13 05:24:13 +02:00
|
|
|
#jid{user = User, luser = LUser, lserver = LServer} = From,
|
2004-12-19 21:47:35 +01:00
|
|
|
case JID1 of
|
|
|
|
error ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
LJID = jlib:jid_tolower(JID1),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
SJID = ejabberd_odbc:escape(jlib:jid_to_string(LJID)),
|
2005-11-16 03:59:05 +01:00
|
|
|
F = fun() ->
|
|
|
|
{selected,
|
|
|
|
["username", "jid", "nick", "subscription",
|
2006-05-26 02:00:32 +02:00
|
|
|
"ask", "askmessage", "server", "subscribe", "type"],
|
2006-09-03 17:15:46 +02:00
|
|
|
Res} = odbc_queries:get_roster_by_jid(LServer, Username, SJID),
|
2005-11-16 03:59:05 +01:00
|
|
|
Item = case Res of
|
|
|
|
[] ->
|
2006-02-12 19:40:03 +01:00
|
|
|
#roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer},
|
2005-11-16 03:59:05 +01:00
|
|
|
jid = LJID};
|
|
|
|
[I] ->
|
2007-07-28 11:36:37 +02:00
|
|
|
R = raw_to_record(LServer, I),
|
|
|
|
case R of
|
|
|
|
%% Bad JID in database:
|
|
|
|
error ->
|
|
|
|
#roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer},
|
|
|
|
jid = LJID};
|
|
|
|
_ ->
|
|
|
|
R#roster{
|
|
|
|
usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer},
|
|
|
|
jid = LJID,
|
|
|
|
name = ""}
|
|
|
|
end
|
2005-11-16 03:59:05 +01:00
|
|
|
end,
|
|
|
|
Item1 = process_item_attrs(Item, Attrs),
|
|
|
|
Item2 = process_item_els(Item1, Els),
|
|
|
|
case Item2#roster.subscription of
|
|
|
|
remove ->
|
2006-09-03 17:15:46 +02:00
|
|
|
odbc_queries:del_roster(LServer, Username, SJID);
|
2005-11-16 03:59:05 +01:00
|
|
|
_ ->
|
|
|
|
ItemVals = record_to_string(Item2),
|
|
|
|
ItemGroups = groups_to_string(Item2),
|
2006-09-03 17:15:46 +02:00
|
|
|
odbc_queries:update_roster(LServer, Username, SJID, ItemVals, ItemGroups)
|
2005-11-16 03:59:05 +01:00
|
|
|
end,
|
2006-07-06 16:08:09 +02:00
|
|
|
%% If the item exist in shared roster, take the
|
|
|
|
%% subscription information from there:
|
|
|
|
Item3 = ejabberd_hooks:run_fold(roster_process_item,
|
|
|
|
LServer, Item2, [LServer]),
|
|
|
|
{Item, Item3}
|
2005-11-16 03:59:05 +01:00
|
|
|
end,
|
2006-09-03 17:15:46 +02:00
|
|
|
case odbc_queries:sql_transaction(LServer, F) of
|
2005-11-16 03:59:05 +01:00
|
|
|
{atomic, {OldItem, Item}} ->
|
|
|
|
push_item(User, LServer, To, Item),
|
|
|
|
case Item#roster.subscription of
|
2004-12-19 21:47:35 +01:00
|
|
|
remove ->
|
2005-11-16 03:59:05 +01:00
|
|
|
IsTo = case OldItem#roster.subscription of
|
2004-12-19 21:47:35 +01:00
|
|
|
both -> true;
|
|
|
|
to -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
2005-11-16 03:59:05 +01:00
|
|
|
IsFrom = case OldItem#roster.subscription of
|
2004-12-19 21:47:35 +01:00
|
|
|
both -> true;
|
|
|
|
from -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
if IsTo ->
|
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:jid_remove_resource(From),
|
2005-11-16 03:59:05 +01:00
|
|
|
jlib:make_jid(OldItem#roster.jid),
|
2004-12-19 21:47:35 +01:00
|
|
|
{xmlelement, "presence",
|
|
|
|
[{"type", "unsubscribe"}],
|
|
|
|
[]});
|
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
if IsFrom ->
|
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:jid_remove_resource(From),
|
2005-11-16 03:59:05 +01:00
|
|
|
jlib:make_jid(OldItem#roster.jid),
|
2004-12-19 21:47:35 +01:00
|
|
|
{xmlelement, "presence",
|
|
|
|
[{"type", "unsubscribed"}],
|
|
|
|
[]});
|
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
E ->
|
|
|
|
?DEBUG("ROSTER: roster item set error: ~p~n", [E]),
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
process_item_set(_From, _To, _) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
process_item_attrs(Item, [{Attr, Val} | Attrs]) ->
|
|
|
|
case Attr of
|
|
|
|
"jid" ->
|
|
|
|
case jlib:string_to_jid(Val) of
|
|
|
|
error ->
|
|
|
|
process_item_attrs(Item, Attrs);
|
|
|
|
JID1 ->
|
2006-02-12 19:40:03 +01:00
|
|
|
JID = {JID1#jid.luser, JID1#jid.lserver, JID1#jid.lresource},
|
2004-12-19 21:47:35 +01:00
|
|
|
process_item_attrs(Item#roster{jid = JID}, Attrs)
|
|
|
|
end;
|
|
|
|
"name" ->
|
|
|
|
process_item_attrs(Item#roster{name = Val}, Attrs);
|
|
|
|
"subscription" ->
|
|
|
|
case Val of
|
|
|
|
"remove" ->
|
|
|
|
process_item_attrs(Item#roster{subscription = remove},
|
|
|
|
Attrs);
|
|
|
|
_ ->
|
|
|
|
process_item_attrs(Item, Attrs)
|
|
|
|
end;
|
|
|
|
"ask" ->
|
|
|
|
process_item_attrs(Item, Attrs);
|
|
|
|
_ ->
|
|
|
|
process_item_attrs(Item, Attrs)
|
|
|
|
end;
|
|
|
|
process_item_attrs(Item, []) ->
|
|
|
|
Item.
|
|
|
|
|
|
|
|
|
2007-08-20 20:57:11 +02:00
|
|
|
process_item_els(Item, [{xmlelement, Name, _Attrs, SEls} | Els]) ->
|
2005-07-26 05:04:26 +02:00
|
|
|
case Name of
|
|
|
|
"group" ->
|
|
|
|
Groups = [xml:get_cdata(SEls) | Item#roster.groups],
|
|
|
|
process_item_els(Item#roster{groups = Groups}, Els);
|
|
|
|
_ ->
|
|
|
|
process_item_els(Item, Els)
|
|
|
|
end;
|
2004-12-19 21:47:35 +01:00
|
|
|
process_item_els(Item, [{xmlcdata, _} | Els]) ->
|
|
|
|
process_item_els(Item, Els);
|
|
|
|
process_item_els(Item, []) ->
|
|
|
|
Item.
|
|
|
|
|
|
|
|
|
2005-07-26 05:04:26 +02:00
|
|
|
push_item(User, Server, From, Item) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
ejabberd_sm:route(jlib:make_jid("", "", ""),
|
2005-07-26 05:04:26 +02:00
|
|
|
jlib:make_jid(User, Server, ""),
|
2004-12-19 21:47:35 +01:00
|
|
|
{xmlelement, "broadcast", [],
|
|
|
|
[{item,
|
|
|
|
Item#roster.jid,
|
|
|
|
Item#roster.subscription}]}),
|
|
|
|
lists:foreach(fun(Resource) ->
|
2005-07-26 05:04:26 +02:00
|
|
|
push_item(User, Server, Resource, From, Item)
|
|
|
|
end, ejabberd_sm:get_user_resources(User, Server)).
|
2004-12-19 21:47:35 +01:00
|
|
|
|
|
|
|
% TODO: don't push to those who not load roster
|
|
|
|
-ifdef(PSI_ROSTER_WORKAROUND).
|
|
|
|
|
2005-07-26 05:04:26 +02:00
|
|
|
push_item(User, Server, Resource, _From, Item) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
ResIQ = #iq{type = set, xmlns = ?NS_ROSTER,
|
2005-11-22 19:43:06 +01:00
|
|
|
id = "push",
|
2004-12-19 21:47:35 +01:00
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_ROSTER}],
|
|
|
|
[item_to_xml(Item)]}]},
|
|
|
|
ejabberd_router:route(
|
2005-07-26 05:04:26 +02:00
|
|
|
jlib:make_jid(User, Server, Resource),
|
|
|
|
jlib:make_jid(User, Server, Resource),
|
2004-12-19 21:47:35 +01:00
|
|
|
jlib:iq_to_xml(ResIQ)).
|
|
|
|
|
|
|
|
-else.
|
|
|
|
|
|
|
|
push_item(User, Resource, From, Item) ->
|
|
|
|
ResIQ = #iq{type = set, xmlns = ?NS_ROSTER,
|
2005-11-22 19:43:06 +01:00
|
|
|
id = "push",
|
2004-12-19 21:47:35 +01:00
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_ROSTER}],
|
|
|
|
[item_to_xml(Item)]}]},
|
|
|
|
ejabberd_router:route(
|
|
|
|
From,
|
2005-07-26 05:04:26 +02:00
|
|
|
jlib:make_jid(User, Server, Resource),
|
2004-12-19 21:47:35 +01:00
|
|
|
jlib:iq_to_xml(ResIQ)).
|
|
|
|
|
|
|
|
-endif.
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
get_subscription_lists(_, User, Server) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-07-13 05:24:13 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2004-12-19 21:47:35 +01:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2006-09-03 17:15:46 +02:00
|
|
|
case catch odbc_queries:get_roster(LServer, Username) of
|
2004-12-19 21:47:35 +01:00
|
|
|
{selected, ["username", "jid", "nick", "subscription", "ask",
|
2006-05-26 02:00:32 +02:00
|
|
|
"askmessage", "server", "subscribe", "type"],
|
2004-12-19 21:47:35 +01:00
|
|
|
Items} when is_list(Items) ->
|
2006-02-12 19:40:03 +01:00
|
|
|
fill_subscription_lists(LServer, Items, [], []);
|
2004-12-19 21:47:35 +01:00
|
|
|
_ ->
|
|
|
|
{[], []}
|
|
|
|
end.
|
|
|
|
|
2006-02-12 19:40:03 +01:00
|
|
|
fill_subscription_lists(LServer, [RawI | Is], F, T) ->
|
|
|
|
I = raw_to_record(LServer, RawI),
|
2007-07-28 11:36:37 +02:00
|
|
|
case I of
|
|
|
|
%% Bad JID in database:
|
|
|
|
error ->
|
|
|
|
fill_subscription_lists(LServer, Is, F, T);
|
2004-12-19 21:47:35 +01:00
|
|
|
_ ->
|
2007-07-28 11:36:37 +02:00
|
|
|
J = I#roster.jid,
|
|
|
|
case I#roster.subscription of
|
|
|
|
both ->
|
|
|
|
fill_subscription_lists(LServer, Is, [J | F], [J | T]);
|
|
|
|
from ->
|
|
|
|
fill_subscription_lists(LServer, Is, [J | F], T);
|
|
|
|
to ->
|
|
|
|
fill_subscription_lists(LServer, Is, F, [J | T]);
|
|
|
|
_ ->
|
|
|
|
fill_subscription_lists(LServer, Is, F, T)
|
|
|
|
end
|
2004-12-19 21:47:35 +01:00
|
|
|
end;
|
2006-02-12 19:40:03 +01:00
|
|
|
fill_subscription_lists(_LServer, [], F, T) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
{F, T}.
|
|
|
|
|
|
|
|
ask_to_pending(subscribe) -> out;
|
|
|
|
ask_to_pending(unsubscribe) -> none;
|
|
|
|
ask_to_pending(Ask) -> Ask.
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-05-23 22:19:37 +02:00
|
|
|
in_subscription(_, User, Server, JID, Type, Reason) ->
|
|
|
|
process_subscription(in, User, Server, JID, Type, Reason).
|
2004-12-19 21:47:35 +01:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
out_subscription(User, Server, JID, Type) ->
|
2006-05-23 22:19:37 +02:00
|
|
|
process_subscription(out, User, Server, JID, Type, []).
|
2004-12-19 21:47:35 +01:00
|
|
|
|
2006-05-23 22:19:37 +02:00
|
|
|
process_subscription(Direction, User, Server, JID1, Type, Reason) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-07-13 05:24:13 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2004-12-19 21:47:35 +01:00
|
|
|
LJID = jlib:jid_tolower(JID1),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
SJID = ejabberd_odbc:escape(jlib:jid_to_string(LJID)),
|
2005-11-16 03:59:05 +01:00
|
|
|
F = fun() ->
|
|
|
|
Item =
|
2006-09-03 17:15:46 +02:00
|
|
|
case odbc_queries:get_roster_by_jid(LServer, Username, SJID) of
|
2005-11-16 03:59:05 +01:00
|
|
|
{selected,
|
|
|
|
["username", "jid", "nick", "subscription", "ask",
|
2006-05-26 02:00:32 +02:00
|
|
|
"askmessage", "server", "subscribe", "type"],
|
2005-11-16 03:59:05 +01:00
|
|
|
[I]} ->
|
2007-07-28 11:36:37 +02:00
|
|
|
%% raw_to_record can return error, but
|
|
|
|
%% jlib_to_string would fail before this point
|
2006-02-12 19:40:03 +01:00
|
|
|
R = raw_to_record(LServer, I),
|
2005-11-16 03:59:05 +01:00
|
|
|
Groups =
|
2006-09-03 17:15:46 +02:00
|
|
|
case odbc_queries:get_roster_groups(LServer, Username, SJID) of
|
2005-11-16 03:59:05 +01:00
|
|
|
{selected, ["grp"], JGrps} when is_list(JGrps) ->
|
|
|
|
[JGrp || {JGrp} <- JGrps];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
R#roster{groups = Groups};
|
|
|
|
{selected,
|
|
|
|
["username", "jid", "nick", "subscription", "ask",
|
2006-08-02 01:32:56 +02:00
|
|
|
"askmessage", "server", "subscribe", "type"],
|
2005-11-16 03:59:05 +01:00
|
|
|
[]} ->
|
2006-02-12 19:40:03 +01:00
|
|
|
#roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer},
|
2005-11-16 03:59:05 +01:00
|
|
|
jid = LJID}
|
|
|
|
end,
|
|
|
|
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,
|
|
|
|
AutoReply = case Direction of
|
|
|
|
out ->
|
|
|
|
none;
|
|
|
|
in ->
|
|
|
|
in_auto_reply(Item#roster.subscription,
|
|
|
|
Item#roster.ask,
|
|
|
|
Type)
|
2005-10-29 22:14:39 +02:00
|
|
|
end,
|
2006-05-23 22:19:37 +02:00
|
|
|
AskMessage = case NewState of
|
|
|
|
{_, both} -> Reason;
|
|
|
|
{_, in} -> Reason;
|
2006-05-26 02:00:32 +02:00
|
|
|
_ -> ""
|
2006-05-23 22:19:37 +02:00
|
|
|
end,
|
2005-11-16 03:59:05 +01:00
|
|
|
case NewState of
|
|
|
|
none ->
|
|
|
|
{none, AutoReply};
|
2007-08-20 20:57:11 +02:00
|
|
|
{none, none} when Item#roster.subscription == none,
|
|
|
|
Item#roster.ask == in ->
|
|
|
|
odbc_queries:del_roster(LServer, Username, SJID),
|
|
|
|
{none, AutoReply};
|
2005-11-16 03:59:05 +01:00
|
|
|
{Subscription, Pending} ->
|
|
|
|
NewItem = Item#roster{subscription = Subscription,
|
2006-05-23 22:19:37 +02:00
|
|
|
ask = Pending,
|
|
|
|
askmessage = AskMessage},
|
2005-11-16 03:59:05 +01:00
|
|
|
ItemVals = record_to_string(NewItem),
|
2006-09-03 17:15:46 +02:00
|
|
|
odbc_queries:roster_subscribe(LServer, Username, SJID, ItemVals),
|
2005-11-16 03:59:05 +01:00
|
|
|
{{push, NewItem}, AutoReply}
|
|
|
|
end
|
|
|
|
end,
|
2006-09-03 17:15:46 +02:00
|
|
|
case odbc_queries:sql_transaction(LServer, F) of
|
2005-11-16 03:59:05 +01:00
|
|
|
{atomic, {Push, AutoReply}} ->
|
|
|
|
case AutoReply of
|
|
|
|
none ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
T = case AutoReply of
|
|
|
|
subscribed -> "subscribed";
|
|
|
|
unsubscribed -> "unsubscribed"
|
|
|
|
end,
|
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid(User, Server, ""), JID1,
|
|
|
|
{xmlelement, "presence", [{"type", T}], []})
|
|
|
|
end,
|
|
|
|
case Push of
|
|
|
|
{push, Item} ->
|
2007-04-26 16:45:16 +02:00
|
|
|
if
|
|
|
|
Item#roster.subscription == none,
|
|
|
|
Item#roster.ask == in ->
|
|
|
|
ok;
|
|
|
|
true ->
|
|
|
|
push_item(User, Server,
|
2008-02-29 08:54:24 +01:00
|
|
|
jlib:make_jid(User, Server, ""), Item)
|
2007-04-26 16:45:16 +02:00
|
|
|
end,
|
2005-11-16 03:59:05 +01:00
|
|
|
true;
|
|
|
|
none ->
|
|
|
|
false
|
|
|
|
end;
|
2004-12-19 21:47:35 +01:00
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
%% 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.
|
2004-12-19 21:47:35 +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;
|
2004-12-19 21:47:35 +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;
|
2004-12-19 21:47:35 +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;
|
2006-05-29 18:48:20 +02:00
|
|
|
out_state_change(none, out, subscribe) -> {none, out}; %% We need to resend query (RFC3921, section 9.2)
|
2004-12-19 21:47:35 +01:00
|
|
|
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}.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
remove_user(User, Server) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-07-13 05:24:13 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2004-12-19 21:47:35 +01:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2006-09-03 17:15:46 +02:00
|
|
|
odbc_queries:del_user_roster_t(LServer, Username),
|
2004-12-19 21:47:35 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
set_items(User, Server, SubEl) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
{xmlelement, _Name, _Attrs, Els} = SubEl,
|
|
|
|
LUser = jlib:nodeprep(User),
|
2005-07-13 05:24:13 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2007-07-26 10:30:25 +02:00
|
|
|
catch odbc_queries:sql_transaction(
|
2005-07-26 05:04:26 +02:00
|
|
|
LServer,
|
2008-10-07 14:34:23 +02:00
|
|
|
lists:flatmap(fun(El) ->
|
2005-07-26 05:04:26 +02:00
|
|
|
process_item_set_t(LUser, LServer, El)
|
2005-12-21 17:10:56 +01:00
|
|
|
end, Els)).
|
2004-12-19 21:47:35 +01:00
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
process_item_set_t(LUser, LServer, {xmlelement, _Name, Attrs, Els}) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
JID1 = jlib:string_to_jid(xml:get_attr_s("jid", Attrs)),
|
|
|
|
case JID1 of
|
|
|
|
error ->
|
2005-07-26 05:04:26 +02:00
|
|
|
[];
|
2004-12-19 21:47:35 +01:00
|
|
|
_ ->
|
|
|
|
LJID = {JID1#jid.luser, JID1#jid.lserver, JID1#jid.lresource},
|
2005-07-26 05:04:26 +02:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
SJID = ejabberd_odbc:escape(jlib:jid_to_string(LJID)),
|
2006-02-12 19:40:03 +01:00
|
|
|
Item = #roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer},
|
2004-12-19 21:47:35 +01:00
|
|
|
jid = LJID},
|
|
|
|
Item1 = process_item_attrs_ws(Item, Attrs),
|
|
|
|
Item2 = process_item_els(Item1, Els),
|
|
|
|
case Item2#roster.subscription of
|
2005-07-26 05:04:26 +02:00
|
|
|
remove ->
|
2006-09-03 17:15:46 +02:00
|
|
|
odbc_queries:del_roster_sql(Username, SJID);
|
2005-07-26 05:04:26 +02:00
|
|
|
_ ->
|
|
|
|
ItemVals = record_to_string(Item1),
|
|
|
|
ItemGroups = groups_to_string(Item2),
|
2006-09-03 17:15:46 +02:00
|
|
|
odbc_queries:update_roster_sql(Username, SJID, ItemVals, ItemGroups)
|
2004-12-19 21:47:35 +01:00
|
|
|
end
|
|
|
|
end;
|
2005-07-13 05:24:13 +02:00
|
|
|
process_item_set_t(_LUser, _LServer, _) ->
|
2005-07-26 05:04:26 +02:00
|
|
|
[].
|
2004-12-19 21:47:35 +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);
|
|
|
|
JID1 ->
|
2006-02-12 19:40:03 +01:00
|
|
|
JID = {JID1#jid.luser, JID1#jid.lserver, JID1#jid.lresource},
|
2004-12-19 21:47:35 +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);
|
|
|
|
_ ->
|
|
|
|
process_item_attrs_ws(Item, Attrs)
|
|
|
|
end;
|
|
|
|
process_item_attrs_ws(Item, []) ->
|
|
|
|
Item.
|
|
|
|
|
2006-05-23 22:19:37 +02:00
|
|
|
get_in_pending_subscriptions(Ls, User, Server) ->
|
2006-05-26 02:00:32 +02:00
|
|
|
JID = jlib:make_jid(User, Server, ""),
|
|
|
|
LUser = JID#jid.luser,
|
|
|
|
LServer = JID#jid.lserver,
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2006-09-03 17:15:46 +02:00
|
|
|
case catch odbc_queries:get_roster(LServer, Username) of
|
2006-05-26 02:00:32 +02:00
|
|
|
{selected, ["username", "jid", "nick", "subscription", "ask",
|
|
|
|
"askmessage", "server", "subscribe", "type"],
|
|
|
|
Items} when is_list(Items) ->
|
2006-05-23 22:19:37 +02:00
|
|
|
Ls ++ lists:map(
|
2006-05-26 02:00:32 +02:00
|
|
|
fun(R) ->
|
|
|
|
Message = R#roster.askmessage,
|
|
|
|
{xmlelement, "presence",
|
|
|
|
[{"from", jlib:jid_to_string(R#roster.jid)},
|
|
|
|
{"to", jlib:jid_to_string(JID)},
|
|
|
|
{"type", "subscribe"}],
|
|
|
|
[{xmlelement, "status", [],
|
2006-09-03 17:15:46 +02:00
|
|
|
[{xmlcdata, Message}]}]}
|
2006-05-26 02:00:32 +02:00
|
|
|
end,
|
|
|
|
lists:flatmap(
|
|
|
|
fun(I) ->
|
2006-12-07 03:56:14 +01:00
|
|
|
case raw_to_record(LServer, I) of
|
2007-07-28 11:36:37 +02:00
|
|
|
%% Bad JID in database:
|
2006-12-07 03:56:14 +01:00
|
|
|
error ->
|
|
|
|
[];
|
|
|
|
R ->
|
|
|
|
case R#roster.ask of
|
|
|
|
in -> [R];
|
|
|
|
both -> [R];
|
|
|
|
_ -> []
|
|
|
|
end
|
2006-05-26 02:00:32 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
Items));
|
|
|
|
_ ->
|
|
|
|
Ls
|
2006-05-23 22:19:37 +02:00
|
|
|
end.
|
2006-05-26 02:00:32 +02:00
|
|
|
|
2006-05-23 22:19:37 +02:00
|
|
|
|
2004-12-19 21:47:35 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2005-07-13 05:24:13 +02:00
|
|
|
get_jid_info(_, User, Server, JID) ->
|
2005-07-26 05:04:26 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
LJID = jlib:jid_tolower(JID),
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
SJID = ejabberd_odbc:escape(jlib:jid_to_string(LJID)),
|
2006-09-03 17:15:46 +02:00
|
|
|
case catch odbc_queries:get_subscription(LServer, Username, SJID) of
|
2005-07-26 05:04:26 +02:00
|
|
|
{selected, ["subscription"], [{SSubscription}]} ->
|
|
|
|
Subscription = case SSubscription of
|
|
|
|
"B" -> both;
|
|
|
|
"T" -> to;
|
|
|
|
"F" -> from;
|
|
|
|
_ -> none
|
|
|
|
end,
|
2007-06-12 18:44:15 +02:00
|
|
|
Groups = case catch odbc_queries:get_rostergroup_by_jid(LServer, Username, SJID) of
|
2005-07-26 05:04:26 +02:00
|
|
|
{selected, ["grp"], JGrps} when is_list(JGrps) ->
|
2005-10-18 22:13:33 +02:00
|
|
|
[JGrp || {JGrp} <- JGrps];
|
2005-07-26 05:04:26 +02:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
{Subscription, Groups};
|
|
|
|
_ ->
|
|
|
|
LRJID = jlib:jid_tolower(jlib:jid_remove_resource(JID)),
|
|
|
|
if
|
|
|
|
LRJID == LJID ->
|
|
|
|
{none, []};
|
|
|
|
true ->
|
|
|
|
SRJID = ejabberd_odbc:escape(jlib:jid_to_string(LRJID)),
|
2006-09-03 17:15:46 +02:00
|
|
|
case catch odbc_queries:get_subscription(LServer, Username, SRJID) of
|
2005-07-26 05:04:26 +02:00
|
|
|
{selected, ["subscription"], [{SSubscription}]} ->
|
|
|
|
Subscription = case SSubscription of
|
|
|
|
"B" -> both;
|
|
|
|
"T" -> to;
|
|
|
|
"F" -> from;
|
|
|
|
_ -> none
|
|
|
|
end,
|
2006-09-03 17:15:46 +02:00
|
|
|
Groups = case catch odbc_queries:get_rostergroup_by_jid(LServer, Username, SRJID) of
|
2005-07-26 05:04:26 +02:00
|
|
|
{selected, ["grp"], JGrps} when is_list(JGrps) ->
|
2005-10-18 22:13:33 +02:00
|
|
|
[JGrp || {JGrp} <- JGrps];
|
2005-07-26 05:04:26 +02:00
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
{Subscription, Groups};
|
|
|
|
_ ->
|
|
|
|
{none, []}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end.
|
2004-12-19 21:47:35 +01:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2006-05-26 02:00:32 +02:00
|
|
|
raw_to_record(LServer, {User, SJID, Nick, SSubscription, SAsk, SAskMessage,
|
2006-02-12 19:40:03 +01:00
|
|
|
_SServer, _SSubscribe, _SType}) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
case jlib:string_to_jid(SJID) of
|
|
|
|
error ->
|
|
|
|
error;
|
|
|
|
JID ->
|
|
|
|
LJID = jlib:jid_tolower(JID),
|
|
|
|
Subscription = case SSubscription of
|
|
|
|
"B" -> both;
|
|
|
|
"T" -> to;
|
|
|
|
"F" -> from;
|
|
|
|
_ -> none
|
|
|
|
end,
|
|
|
|
Ask = case SAsk of
|
|
|
|
"S" -> subscribe;
|
|
|
|
"U" -> unsubscribe;
|
|
|
|
"B" -> both;
|
|
|
|
"O" -> out;
|
|
|
|
"I" -> in;
|
|
|
|
_ -> none
|
|
|
|
end,
|
2006-02-12 19:40:03 +01:00
|
|
|
#roster{usj = {User, LServer, LJID},
|
|
|
|
us = {User, LServer},
|
2004-12-19 21:47:35 +01:00
|
|
|
jid = LJID,
|
|
|
|
name = Nick,
|
|
|
|
subscription = Subscription,
|
2006-05-26 02:00:32 +02:00
|
|
|
ask = Ask,
|
|
|
|
askmessage = SAskMessage}
|
2004-12-19 21:47:35 +01:00
|
|
|
end.
|
|
|
|
|
2006-02-12 19:40:03 +01:00
|
|
|
record_to_string(#roster{us = {User, _Server},
|
2004-12-19 21:47:35 +01:00
|
|
|
jid = JID,
|
|
|
|
name = Name,
|
|
|
|
subscription = Subscription,
|
2006-05-23 22:19:37 +02:00
|
|
|
ask = Ask,
|
|
|
|
askmessage = AskMessage}) ->
|
2004-12-19 21:47:35 +01:00
|
|
|
Username = ejabberd_odbc:escape(User),
|
2005-10-15 16:41:48 +02:00
|
|
|
SJID = ejabberd_odbc:escape(jlib:jid_to_string(jlib:jid_tolower(JID))),
|
2004-12-19 21:47:35 +01:00
|
|
|
Nick = ejabberd_odbc:escape(Name),
|
|
|
|
SSubscription = case Subscription of
|
|
|
|
both -> "B";
|
|
|
|
to -> "T";
|
|
|
|
from -> "F";
|
|
|
|
none -> "N"
|
|
|
|
end,
|
|
|
|
SAsk = case Ask of
|
|
|
|
subscribe -> "S";
|
|
|
|
unsubscribe -> "U";
|
|
|
|
both -> "B";
|
|
|
|
out -> "O";
|
|
|
|
in -> "I";
|
|
|
|
none -> "N"
|
|
|
|
end,
|
2007-01-30 16:23:48 +01:00
|
|
|
SAskMessage = ejabberd_odbc:escape(AskMessage),
|
2006-09-03 17:15:46 +02:00
|
|
|
["'", Username, "',"
|
2004-12-19 21:47:35 +01:00
|
|
|
"'", SJID, "',"
|
|
|
|
"'", Nick, "',"
|
|
|
|
"'", SSubscription, "',"
|
|
|
|
"'", SAsk, "',"
|
2007-01-30 16:23:48 +01:00
|
|
|
"'", SAskMessage, "',"
|
2006-09-03 17:15:46 +02:00
|
|
|
"'N', '', 'item'"].
|
2004-12-19 21:47:35 +01:00
|
|
|
|
2006-02-12 19:40:03 +01:00
|
|
|
groups_to_string(#roster{us = {User, _Server},
|
2005-07-26 05:04:26 +02:00
|
|
|
jid = JID,
|
|
|
|
groups = Groups}) ->
|
|
|
|
Username = ejabberd_odbc:escape(User),
|
2005-10-15 16:41:48 +02:00
|
|
|
SJID = ejabberd_odbc:escape(jlib:jid_to_string(jlib:jid_tolower(JID))),
|
2006-09-03 17:15:46 +02:00
|
|
|
|
|
|
|
%% Empty groups do not need to be converted to string to be inserted in
|
|
|
|
%% the database
|
|
|
|
lists:foldl(fun([], Acc) -> Acc;
|
2008-10-07 14:34:23 +02:00
|
|
|
(Group, Acc) ->
|
2006-09-03 17:15:46 +02:00
|
|
|
String = ["'", Username, "',"
|
|
|
|
"'", SJID, "',"
|
|
|
|
"'", ejabberd_odbc:escape(Group), "'"],
|
|
|
|
[String|Acc] end, [], Groups).
|
2005-07-26 05:04:26 +02:00
|
|
|
|
2007-08-24 18:15:05 +02:00
|
|
|
webadmin_page(_, Host,
|
|
|
|
#request{us = _US,
|
|
|
|
path = ["user", U, "roster"],
|
|
|
|
q = Query,
|
|
|
|
lang = Lang} = _Request) ->
|
|
|
|
Res = user_roster(U, Host, Query, Lang),
|
|
|
|
{stop, Res};
|
|
|
|
|
|
|
|
webadmin_page(Acc, _, _) -> Acc.
|
|
|
|
|
|
|
|
user_roster(User, Server, Query, Lang) ->
|
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LServer = jlib:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
Items1 = get_roster(LUser, LServer),
|
|
|
|
Res = user_roster_parse_query(User, Server, Items1, Query),
|
|
|
|
Items = get_roster(LUser, LServer),
|
|
|
|
SItems = lists:sort(Items),
|
|
|
|
FItems =
|
|
|
|
case SItems of
|
|
|
|
[] ->
|
|
|
|
[?CT("None")];
|
|
|
|
_ ->
|
|
|
|
[?XE("table",
|
|
|
|
[?XE("thead",
|
|
|
|
[?XE("tr",
|
|
|
|
[?XCT("td", "Jabber ID"),
|
|
|
|
?XCT("td", "Nickname"),
|
|
|
|
?XCT("td", "Subscription"),
|
|
|
|
?XCT("td", "Pending"),
|
|
|
|
?XCT("td", "Groups")
|
|
|
|
])]),
|
|
|
|
?XE("tbody",
|
|
|
|
lists:map(
|
|
|
|
fun(R) ->
|
|
|
|
Groups =
|
|
|
|
lists:flatmap(
|
|
|
|
fun(Group) ->
|
|
|
|
[?C(Group), ?BR]
|
|
|
|
end, R#roster.groups),
|
|
|
|
Pending = ask_to_pending(R#roster.ask),
|
|
|
|
?XE("tr",
|
|
|
|
[?XAC("td", [{"class", "valign"}],
|
|
|
|
jlib:jid_to_string(R#roster.jid)),
|
|
|
|
?XAC("td", [{"class", "valign"}],
|
|
|
|
R#roster.name),
|
|
|
|
?XAC("td", [{"class", "valign"}],
|
|
|
|
atom_to_list(R#roster.subscription)),
|
|
|
|
?XAC("td", [{"class", "valign"}],
|
|
|
|
atom_to_list(Pending)),
|
|
|
|
?XAE("td", [{"class", "valign"}], Groups),
|
|
|
|
if
|
|
|
|
Pending == in ->
|
|
|
|
?XAE("td", [{"class", "valign"}],
|
|
|
|
[?INPUTT("submit",
|
|
|
|
"validate" ++
|
|
|
|
ejabberd_web_admin:term_to_id(R#roster.jid),
|
|
|
|
"Validate")]);
|
|
|
|
true ->
|
|
|
|
?X("td")
|
|
|
|
end,
|
|
|
|
?XAE("td", [{"class", "valign"}],
|
|
|
|
[?INPUTT("submit",
|
|
|
|
"remove" ++
|
|
|
|
ejabberd_web_admin:term_to_id(R#roster.jid),
|
|
|
|
"Remove")])])
|
|
|
|
end, SItems))])]
|
|
|
|
end,
|
|
|
|
[?XC("h1", ?T("Roster of ") ++ us_to_list(US))] ++
|
|
|
|
case Res of
|
|
|
|
ok -> [?CT("Submitted"), ?P];
|
|
|
|
error -> [?CT("Bad format"), ?P];
|
|
|
|
nothing -> []
|
|
|
|
end ++
|
|
|
|
[?XAE("form", [{"action", ""}, {"method", "post"}],
|
|
|
|
FItems ++
|
|
|
|
[?P,
|
|
|
|
?INPUT("text", "newjid", ""), ?C(" "),
|
|
|
|
?INPUTT("submit", "addjid", "Add Jabber ID")
|
|
|
|
])].
|
|
|
|
|
|
|
|
user_roster_parse_query(User, Server, Items, Query) ->
|
|
|
|
case lists:keysearch("addjid", 1, Query) of
|
|
|
|
{value, _} ->
|
|
|
|
case lists:keysearch("newjid", 1, Query) of
|
|
|
|
{value, {_, undefined}} ->
|
|
|
|
error;
|
|
|
|
{value, {_, SJID}} ->
|
|
|
|
case jlib:string_to_jid(SJID) of
|
|
|
|
JID when is_record(JID, jid) ->
|
|
|
|
user_roster_subscribe_jid(User, Server, JID),
|
|
|
|
ok;
|
|
|
|
error ->
|
|
|
|
error
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
error
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
case catch user_roster_item_parse_query(
|
|
|
|
User, Server, Items, Query) of
|
|
|
|
submitted ->
|
|
|
|
ok;
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
error;
|
|
|
|
_ ->
|
|
|
|
nothing
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
user_roster_subscribe_jid(User, Server, JID) ->
|
|
|
|
out_subscription(User, Server, JID, subscribe),
|
|
|
|
UJID = jlib:make_jid(User, Server, ""),
|
|
|
|
ejabberd_router:route(
|
|
|
|
UJID, JID, {xmlelement, "presence", [{"type", "subscribe"}], []}).
|
|
|
|
|
|
|
|
user_roster_item_parse_query(User, Server, Items, Query) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
|
|
|
JID = R#roster.jid,
|
|
|
|
case lists:keysearch(
|
|
|
|
"validate" ++ ejabberd_web_admin:term_to_id(JID), 1, Query) of
|
|
|
|
{value, _} ->
|
|
|
|
JID1 = jlib:make_jid(JID),
|
|
|
|
out_subscription(
|
|
|
|
User, Server, JID1, subscribed),
|
|
|
|
UJID = jlib:make_jid(User, Server, ""),
|
|
|
|
ejabberd_router:route(
|
|
|
|
UJID, JID1, {xmlelement, "presence",
|
|
|
|
[{"type", "subscribed"}], []}),
|
|
|
|
throw(submitted);
|
|
|
|
false ->
|
|
|
|
case lists:keysearch(
|
|
|
|
"remove" ++ ejabberd_web_admin:term_to_id(JID), 1, Query) of
|
|
|
|
{value, _} ->
|
|
|
|
UJID = jlib:make_jid(User, Server, ""),
|
|
|
|
process_iq(
|
|
|
|
UJID, UJID,
|
|
|
|
#iq{type = set,
|
|
|
|
sub_el = {xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_ROSTER}],
|
|
|
|
[{xmlelement, "item",
|
|
|
|
[{"jid", jlib:jid_to_string(JID)},
|
|
|
|
{"subscription", "remove"}],
|
|
|
|
[]}]}}),
|
|
|
|
throw(submitted);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end, Items),
|
|
|
|
nothing.
|
|
|
|
|
|
|
|
us_to_list({User, Server}) ->
|
|
|
|
jlib:jid_to_string({User, Server, ""}).
|
|
|
|
|
|
|
|
webadmin_user(Acc, _User, _Server, Lang) ->
|
|
|
|
Acc ++ [?XE("h3", [?ACT("roster/", "Roster")])].
|