25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-30 16:36:29 +01:00

Convert to exmpp (not tested yet).

SVN Revision: 1582
This commit is contained in:
Jean-Sébastien Pédron 2008-09-29 11:29:09 +00:00
parent f5f3c85112
commit 12d515a8fe
2 changed files with 141 additions and 127 deletions

View File

@ -17,6 +17,8 @@
* src/mod_adhoc.erl: Remove compatibility code (not tested yet). * src/mod_adhoc.erl: Remove compatibility code (not tested yet).
* src/mod_shared_roster.erl: Convert to exmpp (not tested yet).
2008-09-25 Jean-Sébastien Pédron <js.pedron@meetic-corp.com> 2008-09-25 Jean-Sébastien Pédron <js.pedron@meetic-corp.com>
* src/jlib.erl (timestamp_to_xml): Create an #xmlel element, not an * src/jlib.erl (timestamp_to_xml): Create an #xmlel element, not an

View File

@ -49,8 +49,9 @@
add_user_to_group/3, add_user_to_group/3,
remove_user_from_group/3]). remove_user_from_group/3]).
-include_lib("exmpp/include/exmpp.hrl").
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-include("jlib.hrl").
-include("mod_roster.hrl"). -include("mod_roster.hrl").
-include("web/ejabberd_http.hrl"). -include("web/ejabberd_http.hrl").
-include("web/ejabberd_web_admin.hrl"). -include("web/ejabberd_web_admin.hrl").
@ -145,9 +146,9 @@ get_user_roster(Items, US) ->
end, SRUsers, Items), end, SRUsers, Items),
%% Export items in roster format: %% Export items in roster format:
SRItems = [#roster{usj = {U, S, {U1, S1, ""}}, SRItems = [#roster{usj = {U, S, {U1, S1, undefined}},
us = US, us = US,
jid = {U1, S1, ""}, jid = {U1, S1, undefined},
name = "", name = "",
subscription = both, subscription = both,
ask = none, ask = none,
@ -193,7 +194,7 @@ process_item(RosterItem, Host) ->
end. end.
build_roster_record(User1, Server1, User2, Server2, Groups) -> build_roster_record(User1, Server1, User2, Server2, Groups) ->
USR2 = {User2, Server2, ""}, USR2 = {User2, Server2, undefined},
#roster{usj = {User1, Server1, USR2}, #roster{usj = {User1, Server1, USR2},
us = {User1, Server1}, us = {User1, Server1},
jid = USR2, jid = USR2,
@ -214,12 +215,12 @@ set_new_rosteritems(UserFrom, ServerFrom,
RIFrom = build_roster_record(UserFrom, ServerFrom, RIFrom = build_roster_record(UserFrom, ServerFrom,
UserTo, ServerTo, GroupsFrom), UserTo, ServerTo, GroupsFrom),
set_item(UserFrom, ServerFrom, ResourceTo, RIFrom), set_item(UserFrom, ServerFrom, ResourceTo, RIFrom),
JIDTo = jlib:make_jid(UserTo, ServerTo, ""), JIDTo = exmpp_jid:make_bare_jid(UserTo, ServerTo),
JIDFrom = jlib:make_jid(UserFrom, ServerFrom, ""), JIDFrom = exmpp_jid:make_bare_jid(UserFrom, ServerFrom),
RITo = build_roster_record(UserTo, ServerTo, RITo = build_roster_record(UserTo, ServerTo,
UserFrom, ServerFrom, []), UserFrom, ServerFrom, []),
set_item(UserTo, ServerTo, "", RITo), set_item(UserTo, ServerTo, undefined, RITo),
%% From requests %% From requests
Mod:out_subscription(UserFrom, ServerFrom, JIDTo, subscribe), Mod:out_subscription(UserFrom, ServerFrom, JIDTo, subscribe),
@ -240,20 +241,19 @@ set_new_rosteritems(UserFrom, ServerFrom,
RIFrom. RIFrom.
set_item(User, Server, Resource, Item) -> set_item(User, Server, Resource, Item) ->
ResIQ = #iq{type = set, xmlns = ?NS_ROSTER, Request = #xmlel{ns = ?NS_ROSTER, name = 'query',
id = "push", children = [mod_roster:item_to_xml(Item)]},
sub_el = [{xmlelement, "query", ResIQ = exmpp_iq:set(?NS_JABBER_CLIENT, Request, "push"),
[{"xmlns", ?NS_ROSTER}],
[mod_roster:item_to_xml(Item)]}]},
ejabberd_router:route( ejabberd_router:route(
jlib:make_jid(User, Server, Resource), exmpp_jid:make_jid(User, Server, Resource),
jlib:make_jid("", Server, ""), exmpp_jid:make_bare_jid(Server),
jlib:iq_to_xml(ResIQ)). ResIQ).
get_subscription_lists({F, T}, User, Server) -> get_subscription_lists({F, T}, User, Server) ->
LUser = jlib:nodeprep(User), try
LServer = jlib:nameprep(Server), LUser = exmpp_stringprep:nodeprep(User),
LServer = exmpp_stringprep:nameprep(Server),
US = {LUser, LServer}, US = {LUser, LServer},
DisplayedGroups = get_user_displayed_groups(US), DisplayedGroups = get_user_displayed_groups(US),
SRUsers = SRUsers =
@ -262,14 +262,19 @@ get_subscription_lists({F, T}, User, Server) ->
fun(Group) -> fun(Group) ->
get_group_users(LServer, Group) get_group_users(LServer, Group)
end, DisplayedGroups)), end, DisplayedGroups)),
SRJIDs = [{U1, S1, ""} || {U1, S1} <- SRUsers], SRJIDs = [{U1, S1, undefined} || {U1, S1} <- SRUsers],
{lists:usort(SRJIDs ++ F), lists:usort(SRJIDs ++ T)}. {lists:usort(SRJIDs ++ F), lists:usort(SRJIDs ++ T)}
catch
_ ->
{[], []}
end.
get_jid_info({Subscription, Groups}, User, Server, JID) -> get_jid_info({Subscription, Groups}, User, Server, JID) ->
LUser = jlib:nodeprep(User), try
LServer = jlib:nameprep(Server), LUser = exmpp_stringprep:nodeprep(User),
LServer = exmpp_stringprep:nameprep(Server),
US = {LUser, LServer}, US = {LUser, LServer},
{U1, S1, _} = jlib:jid_tolower(JID), {U1, S1, _} = jlib:short_prepd_jid(JID),
US1 = {U1, S1}, US1 = {U1, S1},
DisplayedGroups = get_user_displayed_groups(US), DisplayedGroups = get_user_displayed_groups(US),
SRUsers = SRUsers =
@ -290,6 +295,10 @@ get_jid_info({Subscription, Groups}, User, Server, JID) ->
{both, NewGroups}; {both, NewGroups};
error -> error ->
{Subscription, Groups} {Subscription, Groups}
end
catch
_ ->
{[], []}
end. end.
in_subscription(Acc, User, Server, JID, Type, _Reason) -> in_subscription(Acc, User, Server, JID, Type, _Reason) ->
@ -299,10 +308,11 @@ out_subscription(User, Server, JID, Type) ->
process_subscription(out, User, Server, JID, Type, false). process_subscription(out, User, Server, JID, Type, false).
process_subscription(Direction, User, Server, JID, _Type, Acc) -> process_subscription(Direction, User, Server, JID, _Type, Acc) ->
LUser = jlib:nodeprep(User), try
LServer = jlib:nameprep(Server), LUser = exmpp_stringprep:nodeprep(User),
LServer = exmpp_stringprep:nameprep(Server),
US = {LUser, LServer}, US = {LUser, LServer},
{U1, S1, _} = jlib:jid_tolower(jlib:jid_remove_resource(JID)), {U1, S1, _} = jlib:short_prepd_bare_jid(JID),
US1 = {U1, S1}, US1 = {U1, S1},
DisplayedGroups = get_user_displayed_groups(US), DisplayedGroups = get_user_displayed_groups(US),
SRUsers = SRUsers =
@ -321,6 +331,10 @@ process_subscription(Direction, User, Server, JID, _Type, Acc) ->
end; end;
false -> false ->
Acc Acc
end
catch
_ ->
Acc
end. end.
list_groups(Host) -> list_groups(Host) ->
@ -511,8 +525,9 @@ remove_user_from_group(Host, US, Group) ->
mnesia:transaction(F). mnesia:transaction(F).
user_registered(User, Server) -> user_registered(User, Server) ->
LUser = jlib:nodeprep(User), try
LServer = jlib:nameprep(Server), LUser = exmpp_stringprep:nodeprep(User),
LServer = exmpp_stringprep:nameprep(Server),
GroupsOpts = groups_with_opts(LServer), GroupsOpts = groups_with_opts(LServer),
SpecialGroups = get_special_displayed_groups(GroupsOpts), SpecialGroups = get_special_displayed_groups(GroupsOpts),
UserGroups = get_user_displayed_groups(LUser, LServer, GroupsOpts), UserGroups = get_user_displayed_groups(LUser, LServer, GroupsOpts),
@ -522,16 +537,20 @@ user_registered(User, Server) ->
GroupName = proplists:get_value(name, GroupOpts, Group), GroupName = proplists:get_value(name, GroupOpts, Group),
lists:foreach( lists:foreach(
fun({U, S}) -> fun({U, S}) ->
Item = #roster{usj = {U, S, {LUser, LServer, ""}}, Item = #roster{usj = {U, S, {LUser, LServer, undefined}},
us = {U, S}, us = {U, S},
jid = {LUser, LServer, ""}, jid = {LUser, LServer, undefined},
name = "", name = "",
subscription = both, subscription = both,
ask = none, ask = none,
groups = [GroupName]}, groups = [GroupName]},
push_item(U, S, jlib:make_jid("", S, ""), Item) push_item(U, S, exmpp_jid:make_bare_jid(S), Item)
end, get_group_users(LUser, LServer, Group, GroupOpts)) end, get_group_users(LUser, LServer, Group, GroupOpts))
end, lists:usort(SpecialGroups++UserGroups)). end, lists:usort(SpecialGroups++UserGroups))
catch
_ ->
ok
end.
push_item(_User, _Server, _From, none) -> push_item(_User, _Server, _From, none) ->
ok; ok;
@ -540,56 +559,48 @@ push_item(User, Server, From, Item) ->
%% ejabberd_sm:route(jlib:make_jid("", "", ""), %% ejabberd_sm:route(jlib:make_jid("", "", ""),
%% jlib:make_jid(User, Server, "") %% jlib:make_jid(User, Server, "")
%% why? %% why?
ejabberd_sm:route(From, jlib:make_jid(User, Server, ""), ejabberd_sm:route(From, exmpp_jid:make_bare_jid(User, Server),
{xmlelement, "broadcast", [], #xmlel{name = 'broadcast', children =
[{item, [{item,
Item#roster.jid, Item#roster.jid,
Item#roster.subscription}]}), Item#roster.subscription}]}),
Stanza = jlib:iq_to_xml( Request = #xmlel{ns = ?NS_ROSTER, name = 'query',
#iq{type = set, xmlns = ?NS_ROSTER, children = [item_to_xml(Item)]},
id = "push", Stanza = exmpp_iq:set(?NS_JABBER_CLIENT, Request, "push"),
sub_el = [{xmlelement, "query",
[{"xmlns", ?NS_ROSTER}],
[item_to_xml(Item)]}]}),
lists:foreach( lists:foreach(
fun(Resource) -> fun(Resource) ->
JID = jlib:make_jid(User, Server, Resource), JID = exmpp_jid:make_jid(User, Server, Resource),
ejabberd_router:route(JID, JID, Stanza) ejabberd_router:route(JID, JID, Stanza)
end, ejabberd_sm:get_user_resources(User, Server)). end, ejabberd_sm:get_user_resources(User, Server)).
item_to_xml(Item) -> item_to_xml(Item) ->
Attrs1 = [{"jid", jlib:jid_to_string(Item#roster.jid)}], {U, S, R} = Item#roster.jid,
Attrs1 = exmpp_xml:set_attribute_in_list([],
'jid', exmpp_jid:jid_to_list(U, S, R)),
Attrs2 = case Item#roster.name of Attrs2 = case Item#roster.name of
"" -> "" ->
Attrs1; Attrs1;
Name -> Name ->
[{"name", Name} | Attrs1] exmpp_xml:set_attribute_in_list(Attrs1, 'name', Name)
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, end,
Attrs3 = exmpp_xml:set_attribute_in_list(Attrs2,
'subscription', Item#roster.subscription),
Attrs4 = case ask_to_pending(Item#roster.ask) of Attrs4 = case ask_to_pending(Item#roster.ask) of
out -> out ->
[{"ask", "subscribe"} | Attrs3]; exmpp_xml:set_attribute_in_list(Attrs3,
'ask', "subscribe");
both -> both ->
[{"ask", "subscribe"} | Attrs3]; exmpp_xml:set_attribute_in_list(Attrs3,
'ask', "subscribe");
_ -> _ ->
Attrs3 Attrs3
end, end,
SubEls1 = lists:map(fun(G) -> SubEls1 = lists:map(fun(G) ->
{xmlelement, "group", [], [{xmlcdata, G}]} exmpp_xml:set_cdata(
#xmlel{ns = ?NS_ROSTER, name = 'group'}, G)
end, Item#roster.groups), end, Item#roster.groups),
SubEls = SubEls1 ++ Item#roster.xs, SubEls = SubEls1 ++ Item#roster.xs,
{xmlelement, "item", Attrs4, SubEls}. #xmlel{ns = ?NS_ROSTER, name = 'item', attrs = Attrs4, children = SubEls}.
ask_to_pending(subscribe) -> out; ask_to_pending(subscribe) -> out;
ask_to_pending(unsubscribe) -> none; ask_to_pending(unsubscribe) -> none;
@ -791,10 +802,11 @@ shared_roster_group_parse_query(Host, Group, Query) ->
"@all@" -> "@all@" ->
USs; USs;
_ -> _ ->
case jlib:string_to_jid(SJID) of try
JID when is_record(JID, jid) -> JID = exmpp_jid:list_to_jid(SJID),
[{JID#jid.luser, JID#jid.lserver} | USs]; [{JID#jid.lnode, JID#jid.ldomain} | USs]
error -> catch
_ ->
error error
end end
end end
@ -839,4 +851,4 @@ get_opt(Opts, Opt, Default) ->
end. end.
us_to_list({User, Server}) -> us_to_list({User, Server}) ->
jlib:jid_to_string({User, Server, ""}). exmpp_jid:bare_jid_to_list(User, Server).