From c1439ddd5bff82292919774c224a2bf3c8f63258 Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Sun, 26 Feb 2017 10:07:12 +0300 Subject: [PATCH] Get rid of jid:to_string/1 and jid:from_string/1 --- rebar.config | 2 +- src/ejabberd_c2s.erl | 6 +-- src/ejabberd_captcha.erl | 6 +-- src/ejabberd_oauth.erl | 20 ++++----- src/ejabberd_oauth_rest.erl | 4 +- src/ejabberd_oauth_sql.erl | 4 +- src/ejabberd_piefxis.erl | 6 +-- src/ejabberd_router.erl | 2 +- src/ejabberd_router_multicast.erl | 2 +- src/ejabberd_sm.erl | 2 +- src/ejabberd_system_monitor.erl | 4 +- src/ejabberd_web_admin.erl | 34 +++++++------- src/jlib.erl | 13 +++--- src/mod_admin_extra.erl | 21 ++++----- src/mod_client_state.erl | 12 ++--- src/mod_configure.erl | 18 ++++---- src/mod_echo.erl | 2 +- src/mod_http_api.erl | 8 ++-- src/mod_http_upload.erl | 22 ++++----- src/mod_http_upload_quota.erl | 10 ++--- src/mod_irc.erl | 4 +- src/mod_irc_connection.erl | 4 +- src/mod_irc_sql.erl | 8 ++-- src/mod_mam.erl | 2 +- src/mod_mam_sql.erl | 28 ++++++------ src/mod_mix.erl | 4 +- src/mod_muc_admin.erl | 30 +++++++------ src/mod_muc_log.erl | 6 +-- src/mod_muc_mnesia.erl | 2 +- src/mod_muc_riak.erl | 2 +- src/mod_muc_room.erl | 24 +++++----- src/mod_muc_sql.erl | 8 ++-- src/mod_multicast.erl | 10 ++--- src/mod_offline.erl | 12 ++--- src/mod_offline_sql.erl | 20 ++++----- src/mod_pres_counter.erl | 4 +- src/mod_privacy.erl | 4 +- src/mod_privacy_sql.erl | 8 ++-- src/mod_proxy65_service.erl | 4 +- src/mod_proxy65_stream.erl | 4 +- src/mod_pubsub.erl | 4 +- src/mod_register.erl | 10 ++--- src/mod_roster.erl | 19 ++++---- src/mod_roster_sql.erl | 22 ++++----- src/mod_shared_roster.erl | 14 +++--- src/mod_shared_roster_mnesia.erl | 2 +- src/mod_shared_roster_riak.erl | 2 +- src/mod_shared_roster_sql.erl | 4 +- src/mod_stream_mgmt.erl | 30 ++++++------- src/mod_vcard.erl | 4 +- src/node_flat_sql.erl | 6 +-- src/nodetree_virtual.erl | 4 +- src/prosody2ejabberd.erl | 74 +++++++++++++++---------------- src/xmpp_stream_out.erl | 2 +- src/xmpp_stream_pkix.erl | 12 +++-- 55 files changed, 301 insertions(+), 294 deletions(-) diff --git a/rebar.config b/rebar.config index 467857148..bc8fc4d05 100644 --- a/rebar.config +++ b/rebar.config @@ -24,7 +24,7 @@ {fast_tls, ".*", {git, "https://github.com/processone/fast_tls", {tag, "1.0.10"}}}, {stringprep, ".*", {git, "https://github.com/processone/stringprep", {tag, "1.0.7"}}}, {fast_xml, ".*", {git, "https://github.com/processone/fast_xml", {tag, "1.1.21"}}}, - {xmpp, ".*", {git, "https://github.com/processone/xmpp", {tag, "1.1.8"}}}, + {xmpp, ".*", {git, "https://github.com/processone/xmpp", "f5cbd6dc0f3ec786b33a7e708979ed27dfbbb0ec"}}, {stun, ".*", {git, "https://github.com/processone/stun", {tag, "1.0.9"}}}, {esip, ".*", {git, "https://github.com/processone/esip", {tag, "1.0.10"}}}, {fast_yaml, ".*", {git, "https://github.com/processone/fast_yaml", {tag, "1.0.8"}}}, diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index 27dbb14d2..aca4ac5dc 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -262,7 +262,7 @@ process_terminated(#{sockmod := SockMod, socket := Socket, jid := JID} = State, Reason) -> Status = format_reason(State, Reason), ?INFO_MSG("(~s) Closing c2s session for ~s: ~s", - [SockMod:pp(Socket), jid:to_string(JID), Status]), + [SockMod:pp(Socket), jid:encode(JID), Status]), State1 = case maps:is_key(pres_last, State) of true -> Pres = #presence{type = unavailable, @@ -402,12 +402,12 @@ bind(R, #{user := U, server := S, access := Access, lang := Lang, State3 = ejabberd_hooks:run_fold( c2s_session_opened, LServer, State2, []), ?INFO_MSG("(~s) Opened c2s session for ~s", - [SockMod:pp(Socket), jid:to_string(JID)]), + [SockMod:pp(Socket), jid:encode(JID)]), {ok, State3}; deny -> ejabberd_hooks:run(forbidden_session_hook, LServer, [JID]), ?INFO_MSG("(~s) Forbidden c2s session for ~s", - [SockMod:pp(Socket), jid:to_string(JID)]), + [SockMod:pp(Socket), jid:encode(JID)]), Txt = <<"Denied by ACL">>, {error, xmpp:err_not_allowed(Txt, Lang), State} end diff --git a/src/ejabberd_captcha.erl b/src/ejabberd_captcha.erl index 176245782..9d0835bf2 100644 --- a/src/ejabberd_captcha.erl +++ b/src/ejabberd_captcha.erl @@ -89,12 +89,12 @@ create_captcha(SID, From, To, Lang, Limiter, Args) -> case create_image(Limiter) of {ok, Type, Key, Image} -> Id = <<(randoms:get_string())/binary>>, - JID = jid:to_string(From), + JID = jid:encode(From), CID = <<"sha1+", (p1_sha:sha(Image))/binary, "@bob.xmpp.org">>, Data = #bob_data{cid = CID, 'max-age' = 0, type = Type, data = Image}, Fs = [mk_field(hidden, <<"FORM_TYPE">>, ?NS_CAPTCHA), - mk_field(hidden, <<"from">>, jid:to_string(To)), + mk_field(hidden, <<"from">>, jid:encode(To)), mk_field(hidden, <<"challenge">>, Id), mk_field(hidden, <<"sid">>, SID), mk_ocr_field(Lang, CID, Type)], @@ -134,7 +134,7 @@ create_captcha_x(SID, To, Lang, Limiter, #xdata{fields = Fs} = X) -> label = translate:translate( Lang, <<"CAPTCHA web page">>), values = [Imageurl]}, - mk_field(hidden, <<"from">>, jid:to_string(To)), + mk_field(hidden, <<"from">>, jid:encode(To)), mk_field(hidden, <<"challenge">>, Id), mk_field(hidden, <<"sid">>, SID), mk_ocr_field(Lang, CID, Type)], diff --git a/src/ejabberd_oauth.erl b/src/ejabberd_oauth.erl index bda38fc07..cc8af85b5 100644 --- a/src/ejabberd_oauth.erl +++ b/src/ejabberd_oauth.erl @@ -108,18 +108,18 @@ get_commands_spec() -> oauth_issue_token(Jid, TTLSeconds, ScopesString) -> Scopes = [list_to_binary(Scope) || Scope <- string:tokens(ScopesString, ";")], - case jid:from_string(list_to_binary(Jid)) of + try jid:decode(list_to_binary(Jid)) of #jid{luser =Username, lserver = Server} -> case oauth2:authorize_password({Username, Server}, Scopes, admin_generated) of {ok, {_Ctx,Authorization}} -> {ok, {_AppCtx2, Response}} = oauth2:issue_token(Authorization, [{expiry_time, TTLSeconds}]), - {ok, AccessToken} = oauth2_response:access_token(Response), - {ok, VerifiedScope} = oauth2_response:scope(Response), + {ok, AccessToken} = oauth2_response:access_token(Response), + {ok, VerifiedScope} = oauth2_response:scope(Response), {AccessToken, VerifiedScope, integer_to_list(TTLSeconds) ++ " seconds"}; - {error, Error} -> - {error, Error} - end; - error -> + {error, Error} -> + {error, Error} + end + catch _:{bad_jid, _} -> {error, "Invalid JID: " ++ Jid} end. @@ -127,7 +127,7 @@ oauth_list_tokens() -> Tokens = mnesia:dirty_match_object(#oauth_token{_ = '_'}), {MegaSecs, Secs, _MiniSecs} = os:timestamp(), TS = 1000000 * MegaSecs + Secs, - [{Token, jid:to_string(jid:make(U,S)), Scope, integer_to_list(Expires - TS) ++ " seconds"} || + [{Token, jid:encode(jid:make(U,S)), Scope, integer_to_list(Expires - TS) ++ " seconds"} || #oauth_token{token=Token, scope=Scope, us= {U,S},expire=Expires} <- Tokens]. @@ -479,7 +479,7 @@ process(_Handlers, RedirectURI = proplists:get_value(<<"redirect_uri">>, Q, <<"">>), SScope = proplists:get_value(<<"scope">>, Q, <<"">>), StringJID = proplists:get_value(<<"username">>, Q, <<"">>), - #jid{user = Username, server = Server} = jid:from_string(StringJID), + #jid{user = Username, server = Server} = jid:decode(StringJID), Password = proplists:get_value(<<"password">>, Q, <<"">>), State = proplists:get_value(<<"state">>, Q, <<"">>), Scope = str:tokens(SScope, <<" ">>), @@ -542,7 +542,7 @@ process(_Handlers, <<"password">> -> SScope = proplists:get_value(<<"scope">>, Q, <<"">>), StringJID = proplists:get_value(<<"username">>, Q, <<"">>), - #jid{user = Username, server = Server} = jid:from_string(StringJID), + #jid{user = Username, server = Server} = jid:decode(StringJID), Password = proplists:get_value(<<"password">>, Q, <<"">>), Scope = str:tokens(SScope, <<" ">>), TTL = proplists:get_value(<<"ttl">>, Q, <<"">>), diff --git a/src/ejabberd_oauth_rest.erl b/src/ejabberd_oauth_rest.erl index ec9ef0d88..b9614eb09 100644 --- a/src/ejabberd_oauth_rest.erl +++ b/src/ejabberd_oauth_rest.erl @@ -45,7 +45,7 @@ store(R) -> Path = path(<<"store">>), %% Retry 2 times, with a backoff of 500millisec {User, Server} = R#oauth_token.us, - SJID = jid:to_string({User, Server, <<"">>}), + SJID = jid:encode({User, Server, <<"">>}), case rest:with_retry( post, [?MYNAME, Path, [], @@ -68,7 +68,7 @@ lookup(Token) -> 2, 500) of {ok, 200, {Data}} -> SJID = proplists:get_value(<<"user">>, Data, <<>>), - JID = jid:from_string(SJID), + JID = jid:decode(SJID), US = {JID#jid.luser, JID#jid.lserver}, Scope = proplists:get_value(<<"scope">>, Data, []), Expire = proplists:get_value(<<"expire">>, Data, 0), diff --git a/src/ejabberd_oauth_sql.erl b/src/ejabberd_oauth_sql.erl index 4906600f4..10ca49844 100644 --- a/src/ejabberd_oauth_sql.erl +++ b/src/ejabberd_oauth_sql.erl @@ -44,7 +44,7 @@ init() -> store(R) -> Token = R#oauth_token.token, {User, Server} = R#oauth_token.us, - SJID = jid:to_string({User, Server, <<"">>}), + SJID = jid:encode({User, Server, <<"">>}), Scope = str:join(R#oauth_token.scope, <<" ">>), Expire = R#oauth_token.expire, ?SQL_UPSERT( @@ -61,7 +61,7 @@ lookup(Token) -> ?SQL("select @(jid)s, @(scope)s, @(expire)d" " from oauth_token where token=%(Token)s")) of {selected, [{SJID, Scope, Expire}]} -> - JID = jid:from_string(SJID), + JID = jid:decode(SJID), US = {JID#jid.luser, JID#jid.lserver}, #oauth_token{token = Token, us = US, diff --git a/src/ejabberd_piefxis.erl b/src/ejabberd_piefxis.erl index 6ce8a1ae3..d16f826c7 100644 --- a/src/ejabberd_piefxis.erl +++ b/src/ejabberd_piefxis.erl @@ -345,15 +345,15 @@ process_el({xmlstreamelement, #xmlel{name = <<"host">>, attrs = Attrs, children = Els}}, State) -> JIDS = fxml:get_attr_s(<<"jid">>, Attrs), - case jid:from_string(JIDS) of + try jid:decode(JIDS) of #jid{lserver = S} -> case ejabberd_router:is_my_host(S) of true -> process_users(Els, State#state{server = S}); false -> stop("Unknown host: ~s", [S]) - end; - error -> + end + catch _:{bad_jid, _} -> stop("Invalid 'jid': ~s", [JIDS]) end; process_el({xmlstreamstart, <<"user">>, Attrs}, State = #state{server = S}) diff --git a/src/ejabberd_router.erl b/src/ejabberd_router.erl index 17b43c15d..e54abbefa 100644 --- a/src/ejabberd_router.erl +++ b/src/ejabberd_router.erl @@ -96,7 +96,7 @@ route(#jid{} = From, #jid{} = To, #xmlel{} = El) -> catch _:{xmpp_codec, Why} -> ?ERROR_MSG("failed to decode xml element ~p when " "routing from ~s to ~s: ~s", - [El, jid:to_string(From), jid:to_string(To), + [El, jid:encode(From), jid:encode(To), xmpp:format_error(Why)]) end; route(#jid{} = From, #jid{} = To, Packet) -> diff --git a/src/ejabberd_router_multicast.erl b/src/ejabberd_router_multicast.erl index ff2862a72..39c119fbb 100644 --- a/src/ejabberd_router_multicast.erl +++ b/src/ejabberd_router_multicast.erl @@ -213,7 +213,7 @@ code_change(_OldVsn, State, _Extra) -> do_route(Domain, Destinations, Packet) -> ?DEBUG("route multicast:~n~s~nDomain: ~s~nDestinations: ~s~n", [xmpp:pp(Packet), Domain, - str:join([jid:to_string(To) || To <- Destinations], <<", ">>)]), + str:join([jid:encode(To) || To <- Destinations], <<", ">>)]), %% Try to find an appropriate multicast service case mnesia:dirty_read(route_multicast, Domain) of diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index 747434405..1600a2c92 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -501,7 +501,7 @@ do_route(#jid{lresource = <<"">>} = To, Term) -> do_route(jid:replace_resource(To, R), Term) end, get_user_resources(To#jid.user, To#jid.server)); do_route(To, Term) -> - ?DEBUG("broadcasting ~p to ~s", [Term, jid:to_string(To)]), + ?DEBUG("broadcasting ~p to ~s", [Term, jid:encode(To)]), {U, S, R} = jid:tolower(To), Mod = get_sm_backend(S), case online(Mod:get_sessions(U, S, R)) of diff --git a/src/ejabberd_system_monitor.erl b/src/ejabberd_system_monitor.erl index e4c8f4ef7..7b2d9d264 100644 --- a/src/ejabberd_system_monitor.erl +++ b/src/ejabberd_system_monitor.erl @@ -209,7 +209,7 @@ get_admin_jids() -> watchdog_admins, fun(JIDs) -> [jid:tolower( - jid:from_string( + jid:decode( iolist_to_binary(S))) || S <- JIDs] end, []). @@ -341,7 +341,7 @@ process_remote_command(_) -> throw(unknown_command). opt_type(watchdog_admins) -> fun (JIDs) -> - [jid:tolower(jid:from_string(iolist_to_binary(S))) + [jid:tolower(jid:decode(iolist_to_binary(S))) || S <- JIDs] end; opt_type(watchdog_large_heap) -> diff --git a/src/ejabberd_web_admin.erl b/src/ejabberd_web_admin.erl index 968a122bc..0a6f68735 100644 --- a/src/ejabberd_web_admin.erl +++ b/src/ejabberd_web_admin.erl @@ -257,15 +257,16 @@ get_auth_admin(Auth, HostHTTP, RPath, Method) -> case Auth of {SJID, Pass} -> {HostOfRule, AccessRule} = get_acl_rule(RPath, Method), - case jid:from_string(SJID) of - error -> {unauthorized, <<"badformed-jid">>}; - #jid{user = <<"">>, server = User} -> - get_auth_account(HostOfRule, AccessRule, User, HostHTTP, - Pass); - #jid{user = User, server = Server} -> - get_auth_account(HostOfRule, AccessRule, User, Server, - Pass) - end; + try jid:decode(SJID) of + #jid{user = <<"">>, server = User} -> + get_auth_account(HostOfRule, AccessRule, User, HostHTTP, + Pass); + #jid{user = User, server = Server} -> + get_auth_account(HostOfRule, AccessRule, User, Server, + Pass) + catch _:{bad_jid, _} -> + {unauthorized, <<"badformed-jid">>} + end; undefined -> {unauthorized, <<"no-auth-provided">>} end. @@ -1213,7 +1214,7 @@ string_to_spec(<<"server_regexp">>, Val) -> {server_regexp, Val}; string_to_spec(<<"node_regexp">>, Val) -> #jid{luser = U, lserver = S, resource = <<"">>} = - jid:from_string(Val), + jid:decode(Val), {node_regexp, U, S}; string_to_spec(<<"user_glob">>, Val) -> string_to_spec2(user_glob, Val); @@ -1221,7 +1222,7 @@ string_to_spec(<<"server_glob">>, Val) -> {server_glob, Val}; string_to_spec(<<"node_glob">>, Val) -> #jid{luser = U, lserver = S, resource = <<"">>} = - jid:from_string(Val), + jid:decode(Val), {node_glob, U, S}; string_to_spec(<<"all">>, _) -> all; string_to_spec(<<"raw">>, Val) -> @@ -1231,7 +1232,7 @@ string_to_spec(<<"raw">>, Val) -> string_to_spec2(ACLName, Val) -> #jid{luser = U, lserver = S, resource = <<"">>} = - jid:from_string(Val), + jid:decode(Val), case U of <<"">> -> {ACLName, S}; _ -> {ACLName, {U, S}} @@ -1447,16 +1448,17 @@ list_users_parse_query(Query, Host) -> lists:keysearch(<<"newusername">>, 1, Query), {value, {_, Password}} = lists:keysearch(<<"newuserpassword">>, 1, Query), - case jid:from_string(<>) of - error -> error; #jid{user = User, server = Server} -> case ejabberd_auth:try_register(User, Server, Password) of {error, _Reason} -> error; _ -> ok end + catch _:{bad_jid, _} -> + error end; false -> nothing end. @@ -1547,10 +1549,10 @@ get_lastactivity_menuitem_list(Server) -> end. us_to_list({User, Server}) -> - jid:to_string({User, Server, <<"">>}). + jid:encode({User, Server, <<"">>}). su_to_list({Server, User}) -> - jid:to_string({User, Server, <<"">>}). + jid:encode({User, Server, <<"">>}). %%%================================== %%%% get_stats diff --git a/src/jlib.erl b/src/jlib.erl index 227838f27..580ad1ffa 100644 --- a/src/jlib.erl +++ b/src/jlib.erl @@ -218,8 +218,8 @@ replace_from_to_attrs(From, To, Attrs) -> replace_from_to(From, To, #xmlel{name = Name, attrs = Attrs, children = Els}) -> NewAttrs = - replace_from_to_attrs(jid:to_string(From), - jid:to_string(To), Attrs), + replace_from_to_attrs(jid:encode(From), + jid:encode(To), Attrs), #xmlel{name = Name, attrs = NewAttrs, children = Els}. -spec replace_from_attrs(binary(), [attr()]) -> [attr()]. @@ -232,7 +232,7 @@ replace_from_attrs(From, Attrs) -> replace_from(From, #xmlel{name = Name, attrs = Attrs, children = Els}) -> - NewAttrs = replace_from_attrs(jid:to_string(From), + NewAttrs = replace_from_attrs(jid:encode(From), Attrs), #xmlel{name = Name, attrs = NewAttrs, children = Els}. @@ -261,12 +261,13 @@ split_jid(J) -> -spec string_to_jid(binary()) -> jid() | error. string_to_jid(S) -> - jid:from_string(S). + try jid:decode(S) + catch _:_ -> error end. -spec jid_to_string(jid() | ljid()) -> binary(). jid_to_string(J) -> - jid:to_string(J). + jid:encode(J). -spec is_nodename(binary()) -> boolean(). @@ -626,7 +627,7 @@ add_delay_info(El, From, Time, Desc) -> -> xmlel() | error. create_delay_tag(TimeStamp, FromJID, Desc) when is_tuple(FromJID) -> - From = jid:to_string(FromJID), + From = jid:encode(FromJID), Stamp = now_to_utc_string(TimeStamp, 3), Children = case Desc of <<"">> -> []; diff --git a/src/mod_admin_extra.erl b/src/mod_admin_extra.erl index 3130d66c5..943882967 100644 --- a/src/mod_admin_extra.erl +++ b/src/mod_admin_extra.erl @@ -1009,10 +1009,10 @@ get_presence(U, S) -> OnlinePids = [Pid || Pid <- Pids, Pid=/=none], case OnlinePids of [] -> - {jid:to_string({U, S, <<>>}), <<"unavailable">>, <<"">>}; + {jid:encode({U, S, <<>>}), <<"unavailable">>, <<"">>}; [SessionPid|_] -> {_User, Resource, Show, Status} = get_presence(SessionPid), - FullJID = jid:to_string({U, S, Resource}), + FullJID = jid:encode({U, S, Resource}), {FullJID, Show, Status} end. @@ -1238,7 +1238,7 @@ get_roster(User, Server) -> make_roster_xmlrpc(Roster) -> lists:foldl( fun(Item, Res) -> - JIDS = jid:to_string(Item#roster.jid), + JIDS = jid:encode(Item#roster.jid), Nick = Item#roster.name, Subs = atom_to_list(Item#roster.subscription), Ask = atom_to_list(Item#roster.ask), @@ -1418,7 +1418,7 @@ btl(B) -> binary_to_list(B). srg_get_members(Group, Host) -> Members = mod_shared_roster:get_group_explicit_users(Host,Group), - [jid:to_string(jid:make(MUser, MServer)) + [jid:encode(jid:make(MUser, MServer)) || {MUser, MServer} <- Members]. srg_user_add(User, Host, Group, GroupHost) -> @@ -1437,8 +1437,8 @@ srg_user_del(User, Host, Group, GroupHost) -> %% @doc Send a message to a Jabber account. %% @spec (Type::binary(), From::binary(), To::binary(), Subject::binary(), Body::binary()) -> ok send_message(Type, From, To, Subject, Body) -> - FromJID = jid:from_string(From), - ToJID = jid:from_string(To), + FromJID = jid:decode(From), + ToJID = jid:decode(To), Packet = build_packet(Type, Subject, Body), ejabberd_router:route(xmpp:set_from_to(Packet, FromJID, ToJID)). @@ -1450,8 +1450,8 @@ build_packet(Type, Subject, Body) -> send_stanza(FromString, ToString, Stanza) -> try #xmlel{} = El = fxml_stream:parse_element(Stanza), - #jid{} = From = jid:from_string(FromString), - #jid{} = To = jid:from_string(ToString), + From = jid:decode(FromString), + To = jid:decode(ToString), Pkt = xmpp:decode(El, ?NS_CLIENT, [ignore_els]), ejabberd_router:route(xmpp:set_from_to(Pkt, From, To)) catch _:{xmpp_codec, Why} -> @@ -1460,7 +1460,8 @@ send_stanza(FromString, ToString, Stanza) -> _:{badmatch, {error, Why}} -> io:format("invalid xml: ~p~n", [Why]), {error, Why}; - _:{badmatch, error} -> + _:{bad_jid, S} -> + io:format("malformed JID: ~s~n", [S]), {error, "JID malformed"} end. @@ -1619,7 +1620,7 @@ decide_rip_jid({UName, UServer, _UResource}, Match_list) -> decide_rip_jid({UName, UServer}, Match_list) -> lists:any( fun(Match_string) -> - MJID = jid:from_string(list_to_binary(Match_string)), + MJID = jid:decode(list_to_binary(Match_string)), MName = MJID#jid.luser, MServer = MJID#jid.lserver, Is_server = is_glob_match(UServer, MServer), diff --git a/src/mod_client_state.erl b/src/mod_client_state.erl index f6d7292b5..bbce7259c 100644 --- a/src/mod_client_state.erl +++ b/src/mod_client_state.erl @@ -241,7 +241,7 @@ filter_presence({#presence{meta = #{csi_resend := true}}, _} = Acc) -> filter_presence({#presence{to = To, type = Type} = Pres, #{csi_state := inactive} = C2SState}) when Type == available; Type == unavailable -> - ?DEBUG("Got availability presence stanza for ~s", [jid:to_string(To)]), + ?DEBUG("Got availability presence stanza for ~s", [jid:encode(To)]), enqueue_stanza(presence, Pres, C2SState); filter_presence(Acc) -> Acc. @@ -261,7 +261,7 @@ filter_chat_states({#message{from = From, to = To} = Msg, Acc; _ -> ?DEBUG("Got standalone chat state notification for ~s", - [jid:to_string(To)]), + [jid:encode(To)]), enqueue_stanza(chatstate, Msg, C2SState) end; false -> @@ -279,7 +279,7 @@ filter_pep({#message{to = To} = Msg, undefined -> Acc; Node -> - ?DEBUG("Got PEP notification for ~s", [jid:to_string(To)]), + ?DEBUG("Got PEP notification for ~s", [jid:encode(To)]), enqueue_stanza({pep, Node}, Msg, C2SState) end; filter_pep(Acc) -> @@ -291,7 +291,7 @@ filter_other({Stanza, #{jid := JID} = C2SState} = Acc) when ?is_stanza(Stanza) - #{csi_resend := true} -> Acc; _ -> - ?DEBUG("Won't add stanza for ~s to CSI queue", [jid:to_string(JID)]), + ?DEBUG("Won't add stanza for ~s to CSI queue", [jid:encode(JID)]), From = xmpp:get_from(Stanza), C2SState1 = dequeue_sender(From, C2SState), {Stanza, C2SState1} @@ -331,7 +331,7 @@ enqueue_stanza(_Type, Stanza, State) -> dequeue_sender(#jid{luser = U, lserver = S}, #{csi_queue := Q, jid := JID} = C2SState) -> ?DEBUG("Flushing packets of ~s@~s from CSI queue of ~s", - [U, S, jid:to_string(JID)]), + [U, S, jid:encode(JID)]), case queue_take({U, S}, Q) of {Stanzas, Q1} -> C2SState1 = flush_stanzas(C2SState, Stanzas), @@ -342,7 +342,7 @@ dequeue_sender(#jid{luser = U, lserver = S}, -spec flush_queue(c2s_state()) -> c2s_state(). flush_queue(#{csi_queue := Q, jid := JID} = C2SState) -> - ?DEBUG("Flushing CSI queue of ~s", [jid:to_string(JID)]), + ?DEBUG("Flushing CSI queue of ~s", [jid:encode(JID)]), C2SState1 = flush_stanzas(C2SState, queue_to_list(Q)), C2SState1#{csi_queue => queue_new()}. diff --git a/src/mod_configure.erl b/src/mod_configure.erl index 1af8bf6ff..65ac6a35d 100644 --- a/src/mod_configure.erl +++ b/src/mod_configure.erl @@ -396,7 +396,7 @@ get_permission_level(JID) -> allow -> PermLev = get_permission_level(From), case get_local_items({PermLev, LServer}, LNode, - jid:to_string(To), Lang) + jid:encode(To), Lang) of {result, Res} -> {result, Res}; {error, Error} -> {error, Error} @@ -418,7 +418,7 @@ get_local_items(Acc, From, #jid{lserver = LServer} = To, allow -> PermLev = get_permission_level(From), case get_local_items({PermLev, LServer}, [], - jid:to_string(To), Lang) + jid:encode(To), Lang) of {result, Res} -> {result, Items ++ Res}; {error, _Error} -> {result, Items} @@ -1522,7 +1522,7 @@ set_form(From, Host, ?NS_ADMINL(<<"add-user">>), _Lang, AccountString = get_value(<<"accountjid">>, XData), Password = get_value(<<"password">>, XData), Password = get_value(<<"password-verify">>, XData), - AccountJID = jid:from_string(AccountString), + AccountJID = jid:decode(AccountString), User = AccountJID#jid.luser, Server = AccountJID#jid.lserver, true = lists:member(Server, ?MYHOSTS), @@ -1536,7 +1536,7 @@ set_form(From, Host, ?NS_ADMINL(<<"delete-user">>), XData), [_ | _] = AccountStringList, ASL2 = lists:map(fun (AccountString) -> - JID = jid:from_string(AccountString), + JID = jid:decode(AccountString), User = JID#jid.luser, Server = JID#jid.lserver, true = Server == Host orelse @@ -1551,7 +1551,7 @@ set_form(From, Host, ?NS_ADMINL(<<"delete-user">>), set_form(From, Host, ?NS_ADMINL(<<"end-user-session">>), Lang, XData) -> AccountString = get_value(<<"accountjid">>, XData), - JID = jid:from_string(AccountString), + JID = jid:decode(AccountString), LUser = JID#jid.luser, LServer = JID#jid.lserver, true = LServer == Host orelse @@ -1587,7 +1587,7 @@ set_form(From, Host, ?NS_ADMINL(<<"end-user-session">>), set_form(From, Host, ?NS_ADMINL(<<"get-user-password">>), Lang, XData) -> AccountString = get_value(<<"accountjid">>, XData), - JID = jid:from_string(AccountString), + JID = jid:decode(AccountString), User = JID#jid.luser, Server = JID#jid.lserver, true = Server == Host orelse @@ -1605,7 +1605,7 @@ set_form(From, Host, ?NS_ADMINL(<<"change-user-password">>), _Lang, XData) -> AccountString = get_value(<<"accountjid">>, XData), Password = get_value(<<"password">>, XData), - JID = jid:from_string(AccountString), + JID = jid:decode(AccountString), User = JID#jid.luser, Server = JID#jid.lserver, true = Server == Host orelse @@ -1616,7 +1616,7 @@ set_form(From, Host, set_form(From, Host, ?NS_ADMINL(<<"get-user-lastlogin">>), Lang, XData) -> AccountString = get_value(<<"accountjid">>, XData), - JID = jid:from_string(AccountString), + JID = jid:decode(AccountString), User = JID#jid.luser, Server = JID#jid.lserver, true = Server == Host orelse @@ -1648,7 +1648,7 @@ set_form(From, Host, set_form(From, Host, ?NS_ADMINL(<<"user-stats">>), Lang, XData) -> AccountString = get_value(<<"accountjid">>, XData), - JID = jid:from_string(AccountString), + JID = jid:decode(AccountString), User = JID#jid.luser, Server = JID#jid.lserver, true = Server == Host orelse diff --git a/src/mod_echo.erl b/src/mod_echo.erl index 7eeb8d4e9..861b1a0ef 100644 --- a/src/mod_echo.erl +++ b/src/mod_echo.erl @@ -196,7 +196,7 @@ do_client_version(enabled, From, To) -> #iq{to = To, from = From2, id = ID, type = result, sub_els = [#version{} = V]}} -> ?INFO_MSG("Version of the client ~s:~n~s", - [jid:to_string(To), xmpp:pp(V)]) + [jid:encode(To), xmpp:pp(V)]) after 5000 -> % Timeout in miliseconds: 5 seconds [] end. diff --git a/src/mod_http_api.erl b/src/mod_http_api.erl index c0e2d8421..e3b1edc9c 100644 --- a/src/mod_http_api.erl +++ b/src/mod_http_api.erl @@ -139,16 +139,16 @@ depends(_Host, _Opts) -> extract_auth(#request{auth = HTTPAuth, ip = {IP, _}}) -> Info = case HTTPAuth of {SJID, Pass} -> - case jid:from_string(SJID) of + try jid:decode(SJID) of #jid{luser = User, lserver = Server} -> case ejabberd_auth:check_password(User, <<"">>, Server, Pass) of true -> #{usr => {User, Server, <<"">>}, caller_server => Server}; false -> {error, invalid_auth} - end; - _ -> - {error, invalid_auth} + end + catch _:{bad_jid, _} -> + {error, invalid_auth} end; {oauth, Token, _} -> case ejabberd_oauth:check_token(Token) of diff --git a/src/mod_http_upload.erl b/src/mod_http_upload.erl index 88982839c..a10f059bd 100644 --- a/src/mod_http_upload.erl +++ b/src/mod_http_upload.erl @@ -542,7 +542,7 @@ process_iq(#iq{type = get, lang = Lang, from = From, end; deny -> ?DEBUG("Denying HTTP upload slot request from ~s", - [jid:to_string(From)]), + [jid:encode(From)]), Txt = <<"Denied by ACL">>, xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang)) end; @@ -559,7 +559,7 @@ create_slot(#state{service_url = undefined, max_size = MaxSize}, Size > MaxSize -> Text = {<<"File larger than ~w bytes">>, [MaxSize]}, ?INFO_MSG("Rejecting file ~s from ~s (too large: ~B bytes)", - [File, jid:to_string(JID), Size]), + [File, jid:encode(JID), Size]), {error, xmpp:err_not_acceptable(Text, Lang)}; create_slot(#state{service_url = undefined, jid_in_url = JIDinURL, @@ -575,7 +575,7 @@ create_slot(#state{service_url = undefined, RandStr = make_rand_string(SecretLength), FileStr = make_file_string(File), ?INFO_MSG("Got HTTP upload slot for ~s (file: ~s)", - [jid:to_string(JID), File]), + [jid:encode(JID), File]), {ok, [UserStr, RandStr, FileStr]}; deny -> {error, xmpp:err_service_unavailable()}; @@ -589,7 +589,7 @@ create_slot(#state{service_url = ServiceURL}, HttpOptions = [{timeout, ?SERVICE_REQUEST_TIMEOUT}], SizeStr = integer_to_binary(Size), GetRequest = binary_to_list(ServiceURL) ++ - "?jid=" ++ ?URL_ENC(jid:to_string({U, S, <<"">>})) ++ + "?jid=" ++ ?URL_ENC(jid:encode({U, S, <<"">>})) ++ "&name=" ++ ?URL_ENC(File) ++ "&size=" ++ ?URL_ENC(SizeStr) ++ "&content_type=" ++ ?URL_ENC(ContentType), @@ -599,33 +599,33 @@ create_slot(#state{service_url = ServiceURL}, [<<"http", _/binary>> = PutURL, <<"http", _/binary>> = GetURL] -> ?INFO_MSG("Got HTTP upload slot for ~s (file: ~s)", - [jid:to_string(JID), File]), + [jid:encode(JID), File]), {ok, PutURL, GetURL}; Lines -> ?ERROR_MSG("Can't parse data received for ~s from <~s>: ~p", - [jid:to_string(JID), ServiceURL, Lines]), + [jid:encode(JID), ServiceURL, Lines]), Txt = <<"Failed to parse HTTP response">>, {error, xmpp:err_service_unavailable(Txt, Lang)} end; {ok, {402, _Body}} -> ?INFO_MSG("Got status code 402 for ~s from <~s>", - [jid:to_string(JID), ServiceURL]), + [jid:encode(JID), ServiceURL]), {error, xmpp:err_resource_constraint()}; {ok, {403, _Body}} -> ?INFO_MSG("Got status code 403 for ~s from <~s>", - [jid:to_string(JID), ServiceURL]), + [jid:encode(JID), ServiceURL]), {error, xmpp:err_not_allowed()}; {ok, {413, _Body}} -> ?INFO_MSG("Got status code 413 for ~s from <~s>", - [jid:to_string(JID), ServiceURL]), + [jid:encode(JID), ServiceURL]), {error, xmpp:err_not_acceptable()}; {ok, {Code, _Body}} -> ?ERROR_MSG("Got unexpected status code for ~s from <~s>: ~B", - [jid:to_string(JID), ServiceURL, Code]), + [jid:encode(JID), ServiceURL, Code]), {error, xmpp:err_service_unavailable()}; {error, Reason} -> ?ERROR_MSG("Error requesting upload slot for ~s from <~s>: ~p", - [jid:to_string(JID), ServiceURL, Reason]), + [jid:encode(JID), ServiceURL, Reason]), {error, xmpp:err_service_unavailable()} end. diff --git a/src/mod_http_upload_quota.erl b/src/mod_http_upload_quota.erl index 1fbaafe3a..8d79d16d6 100644 --- a/src/mod_http_upload_quota.erl +++ b/src/mod_http_upload_quota.erl @@ -169,24 +169,24 @@ handle_cast({handle_slot_request, #jid{user = U, server = S} = JID, Path, Size}, NewSize = case {HardQuota, SoftQuota} of {0, 0} -> ?DEBUG("No quota specified for ~s", - [jid:to_string(JID)]), + [jid:encode(JID)]), undefined; {0, _} -> ?WARNING_MSG("No hard quota specified for ~s", - [jid:to_string(JID)]), + [jid:encode(JID)]), enforce_quota(Path, Size, OldSize, SoftQuota, SoftQuota); {_, 0} -> ?WARNING_MSG("No soft quota specified for ~s", - [jid:to_string(JID)]), + [jid:encode(JID)]), enforce_quota(Path, Size, OldSize, HardQuota, HardQuota); _ when SoftQuota > HardQuota -> ?WARNING_MSG("Bad quota for ~s (soft: ~p, hard: ~p)", - [jid:to_string(JID), + [jid:encode(JID), SoftQuota, HardQuota]), enforce_quota(Path, Size, OldSize, SoftQuota, SoftQuota); _ -> ?DEBUG("Enforcing quota for ~s", - [jid:to_string(JID)]), + [jid:encode(JID)]), enforce_quota(Path, Size, OldSize, SoftQuota, HardQuota) end, NewDiskUsage = if is_integer(NewSize) -> diff --git a/src/mod_irc.erl b/src/mod_irc.erl index c7af2834b..091a26797 100644 --- a/src/mod_irc.erl +++ b/src/mod_irc.erl @@ -688,7 +688,7 @@ adhoc_join(From, To, #adhoc_command{lang = Lang, xdata = X} = Request) -> To#jid.server), Reason = translate:translate(Lang, <<"Join the IRC channel here.">>), BodyTxt = {<<"Join the IRC channel in this Jabber ID: ~s">>, - [jid:to_string(RoomJID)]}, + [jid:encode(RoomJID)]}, Invite = #message{ from = RoomJID, to = From, body = xmpp:mk_text(BodyTxt, Lang), @@ -934,7 +934,7 @@ data_to_binary(JID, Data) -> ?ERROR_MSG("failed to convert " "parameter ~p for user ~s", [Param, - jid:to_string(JID)]); + jid:encode(JID)]); true -> ?ERROR_MSG("failed to convert " "parameter ~p", diff --git a/src/mod_irc_connection.erl b/src/mod_irc_connection.erl index 2b2042bf0..46ed8767e 100644 --- a/src/mod_irc_connection.erl +++ b/src/mod_irc_connection.erl @@ -971,7 +971,7 @@ process_userinfo(StateData, _Nick, From) -> send_text(StateData, io_lib:format("NOTICE ~s :\001USERINFO xmpp:~s\001\r\n", [FromUser, - jid:to_string(StateData#state.user)])). + jid:encode(StateData#state.user)])). process_topic(StateData, Chan, From, String) -> [FromUser | _] = str:tokens(From, <<"!">>), @@ -1162,7 +1162,7 @@ iq_admin(StateData, Channel, From, _To, ejabberd_router:route_error(IQ, Error) catch E:R -> ?ERROR_MSG("failed to process admin query from ~s: ~p", - [jid:to_string(From), {E, {R, erlang:get_stacktrace()}}]), + [jid:encode(From), {E, {R, erlang:get_stacktrace()}}]), ejabberd_router:route_error( IQ, xmpp:err_internal_server_error()) end. diff --git a/src/mod_irc_sql.erl b/src/mod_irc_sql.erl index dd419875d..42c075bfe 100644 --- a/src/mod_irc_sql.erl +++ b/src/mod_irc_sql.erl @@ -42,7 +42,7 @@ init(_Host, _Opts) -> ok. get_data(LServer, Host, From) -> - SJID = jid:to_string(jid:tolower(jid:remove_resource(From))), + SJID = jid:encode(jid:tolower(jid:remove_resource(From))), case catch ejabberd_sql:sql_query( LServer, ?SQL("select @(data)s from irc_custom" @@ -54,7 +54,7 @@ get_data(LServer, Host, From) -> end. set_data(LServer, Host, From, Data) -> - SJID = jid:to_string(jid:tolower(jid:remove_resource(From))), + SJID = jid:encode(jid:tolower(jid:remove_resource(From))), SData = jlib:term_to_expr(Data), F = fun () -> ?SQL_UPSERT_T( @@ -72,7 +72,7 @@ export(_Server) -> data = Data}) -> case str:suffix(Host, IRCHost) of true -> - SJID = jid:to_string(jid:make(U, S)), + SJID = jid:encode(jid:make(U, S)), SData = jlib:term_to_expr(Data), [?SQL("delete from irc_custom" " where jid=%(SJID)s and host=%(IRCHost)s;"), @@ -86,7 +86,7 @@ export(_Server) -> import(_LServer) -> [{<<"select jid, host, data from irc_custom;">>, fun([SJID, IRCHost, SData]) -> - #jid{luser = U, lserver = S} = jid:from_string(SJID), + #jid{luser = U, lserver = S} = jid:decode(SJID), Data = ejabberd_sql:decode_term(SData), #irc_custom{us_host = {{U, S}, IRCHost}, data = Data} diff --git a/src/mod_mam.erl b/src/mod_mam.erl index 5cafea5c3..47a9c6ce1 100644 --- a/src/mod_mam.erl +++ b/src/mod_mam.erl @@ -889,7 +889,7 @@ msg_to_el(#archive_msg{timestamp = TS, packet = El, nick = Nick, catch _:{xmpp_codec, Why} -> ?ERROR_MSG("Failed to decode raw element ~p from message " "archive of user ~s: ~s", - [El, jid:to_string(JidArchive), xmpp:format_error(Why)]), + [El, jid:encode(JidArchive), xmpp:format_error(Why)]), {error, invalid_xml} end. diff --git a/src/mod_mam_sql.erl b/src/mod_mam_sql.erl index d553c2e64..5379f4e88 100644 --- a/src/mod_mam_sql.erl +++ b/src/mod_mam_sql.erl @@ -53,7 +53,7 @@ remove_user(LUser, LServer) -> ?SQL("delete from archive_prefs where username=%(LUser)s")). remove_room(LServer, LName, LHost) -> - LUser = jid:to_string({LName, LHost, <<>>}), + LUser = jid:encode({LName, LHost, <<>>}), remove_user(LUser, LServer). delete_old_messages(ServerHost, TimeStamp, Type) -> @@ -74,12 +74,12 @@ store(Pkt, LServer, {LUser, LHost}, Type, Peer, Nick, _Dir) -> ID = integer_to_binary(TSinteger), SUser = case Type of chat -> LUser; - groupchat -> jid:to_string({LUser, LHost, <<>>}) + groupchat -> jid:encode({LUser, LHost, <<>>}) end, - BarePeer = jid:to_string( + BarePeer = jid:encode( jid:tolower( jid:remove_resource(Peer))), - LPeer = jid:to_string( + LPeer = jid:encode( jid:tolower(Peer)), XML = fxml:element_to_binary(Pkt), Body = fxml:get_subtag_cdata(Pkt, <<"body">>), @@ -143,7 +143,7 @@ select(LServer, JidRequestor, #jid{luser = LUser} = JidArchive, MAMQuery, RSM, MsgType) -> User = case MsgType of chat -> LUser; - {groupchat, _Role, _MUCState} -> jid:to_string(JidArchive) + {groupchat, _Role, _MUCState} -> jid:encode(JidArchive) end, {Query, CountQuery} = make_sql_query(User, LServer, MAMQuery, RSM), % TODO from XEP-0313 v0.2: "To conserve resources, a server MAY place a @@ -228,11 +228,11 @@ make_sql_query(User, LServer, MAMQuery, RSM) -> WithClause = case catch jid:tolower(With) of {_, _, <<>>} -> [<<" and bare_peer='">>, - Escape(jid:to_string(With)), + Escape(jid:encode(With)), <<"'">>]; {_, _, _} -> [<<" and peer='">>, - Escape(jid:to_string(With)), + Escape(jid:encode(With)), <<"'">>]; _ -> [] @@ -314,8 +314,8 @@ make_archive_el(TS, XML, Peer, Kind, Nick, MsgType, JidRequestor, JidArchive) -> #xmlel{} = El -> try binary_to_integer(TS) of TSInt -> - case jid:from_string(Peer) of - #jid{} = PeerJID -> + try jid:decode(Peer) of + PeerJID -> Now = usec_to_now(TSInt), PeerLJID = jid:tolower(PeerJID), T = case Kind of @@ -330,24 +330,24 @@ make_archive_el(TS, XML, Peer, Kind, Nick, MsgType, JidRequestor, JidArchive) -> type = T, nick = Nick, peer = PeerLJID}, - MsgType, JidRequestor, JidArchive); - error -> + MsgType, JidRequestor, JidArchive) + catch _:{bad_jid, _} -> ?ERROR_MSG("Malformed 'peer' field with value " "'~s' detected for user ~s in table " "'archive': invalid JID", - [Peer, jid:to_string(JidArchive)]), + [Peer, jid:encode(JidArchive)]), {error, invalid_jid} end catch _:_ -> ?ERROR_MSG("Malformed 'timestamp' field with value '~s' " "detected for user ~s in table 'archive': " "not an integer", - [TS, jid:to_string(JidArchive)]), + [TS, jid:encode(JidArchive)]), {error, invalid_timestamp} end; {error, {_, Reason}} -> ?ERROR_MSG("Malformed 'xml' field with value '~s' detected " "for user ~s in table 'archive': ~s", - [XML, jid:to_string(JidArchive), Reason]), + [XML, jid:encode(JidArchive), Reason]), {error, invalid_xml} end. diff --git a/src/mod_mix.erl b/src/mod_mix.erl index edacd7b91..940e9898f 100644 --- a/src/mod_mix.erl +++ b/src/mod_mix.erl @@ -261,7 +261,7 @@ publish_participant(From, To) -> LFrom = jid:tolower(BareFrom), LTo = jid:tolower(jid:remove_resource(To)), Participant = #mix_participant{jid = BareFrom}, - ItemID = p1_sha:sha(jid:to_string(LFrom)), + ItemID = p1_sha:sha(jid:encode(LFrom)), mod_pubsub:publish_item( LTo, To#jid.lserver, ?NS_MIX_NODES_PARTICIPANTS, From, ItemID, [xmpp:encode(Participant)]). @@ -284,7 +284,7 @@ delete_presence(From, To) -> delete_participant(From, To) -> LFrom = jid:tolower(jid:remove_resource(From)), - ItemID = p1_sha:sha(jid:to_string(LFrom)), + ItemID = p1_sha:sha(jid:encode(LFrom)), delete_presence(From, To), delete_item(From, To, ?NS_MIX_NODES_PARTICIPANTS, ItemID). diff --git a/src/mod_muc_admin.erl b/src/mod_muc_admin.erl index 714e3dd3a..4b1509fc9 100644 --- a/src/mod_muc_admin.erl +++ b/src/mod_muc_admin.erl @@ -717,7 +717,7 @@ get_room_occupants(Pid) -> S = get_room_state(Pid), lists:map( fun({_LJID, Info}) -> - {jid:to_string(Info#user.jid), + {jid:encode(Info#user.jid), Info#user.nick, atom_to_list(Info#user.role)} end, @@ -744,11 +744,11 @@ get_users_to_invite(RoomJid, UsersString) -> UsersStrings = binary:split(UsersString, <<":">>, [global]), OccupantsTuples = get_room_occupants(RoomJid#jid.luser, RoomJid#jid.lserver), - OccupantsJids = [jid:from_string(JidString) + OccupantsJids = [jid:decode(JidString) || {JidString, _Nick, _} <- OccupantsTuples], lists:filtermap( fun(UserString) -> - UserJid = jid:from_string(UserString), + UserJid = jid:decode(UserString), Val = lists:all(fun(OccupantJid) -> UserJid#jid.luser /= OccupantJid#jid.luser orelse UserJid#jid.lserver /= OccupantJid#jid.lserver @@ -918,7 +918,7 @@ set_room_affiliation(Name, Service, JID, AffiliationString) -> case mod_muc:find_online_room(Name, Service) of {ok, Pid} -> %% Get the PID for the online room so we can get the state of the room - {ok, StateData} = gen_fsm:sync_send_all_state_event(Pid, {process_item_change, {jid:from_string(JID), affiliation, Affiliation, <<"">>}, <<"">>}), + {ok, StateData} = gen_fsm:sync_send_all_state_event(Pid, {process_item_change, {jid:decode(JID), affiliation, Affiliation, <<"">>}, <<"">>}), mod_muc:store_room(StateData#state.server_host, StateData#state.host, StateData#state.room, make_opts(StateData)), ok; error -> @@ -933,11 +933,9 @@ subscribe_room(_User, Nick, _Room, _Nodes) when Nick == <<"">> -> throw({error, "Nickname must be set"}); subscribe_room(User, Nick, Room, Nodes) -> NodeList = re:split(Nodes, "\\h*,\\h*"), - case jid:from_string(Room) of + try jid:decode(Room) of #jid{luser = Name, lserver = Host} when Name /= <<"">> -> - case jid:from_string(User) of - error -> - throw({error, "Malformed user JID"}); + try jid:decode(User) of #jid{lresource = <<"">>} -> throw({error, "User's JID should have a resource"}); UserJID -> @@ -954,17 +952,19 @@ subscribe_room(User, Nick, Room, Nodes) -> _ -> throw({error, "The room does not exist"}) end + catch _:{bad_jid, _} -> + throw({error, "Malformed user JID"}) end; _ -> throw({error, "Malformed room JID"}) + catch _:{bad_jid, _} -> + throw({error, "Malformed room JID"}) end. unsubscribe_room(User, Room) -> - case jid:from_string(Room) of + try jid:decode(Room) of #jid{luser = Name, lserver = Host} when Name /= <<"">> -> - case jid:from_string(User) of - error -> - throw({error, "Malformed user JID"}); + try jid:decode(User) of UserJID -> case get_room_pid(Name, Host) of Pid when is_pid(Pid) -> @@ -979,16 +979,20 @@ unsubscribe_room(User, Room) -> _ -> throw({error, "The room does not exist"}) end + catch _:{bad_jid, _} -> + throw({error, "Malformed user JID"}) end; _ -> throw({error, "Malformed room JID"}) + catch _:{bad_jid, _} -> + throw({error, "Malformed room JID"}) end. get_subscribers(Name, Host) -> case get_room_pid(Name, Host) of Pid when is_pid(Pid) -> {ok, JIDList} = gen_fsm:sync_send_all_state_event(Pid, get_subscribers), - [jid:to_string(jid:remove_resource(J)) || J <- JIDList]; + [jid:encode(jid:remove_resource(J)) || J <- JIDList]; _ -> throw({error, "The room does not exist"}) end. diff --git a/src/mod_muc_log.erl b/src/mod_muc_log.erl index 13f0efdfe..9e040c237 100644 --- a/src/mod_muc_log.erl +++ b/src/mod_muc_log.erl @@ -278,7 +278,7 @@ build_filename_string(TimeStamp, OutDir, RoomJID, {Fd, Fn, Fnrel}. get_room_name(RoomJID) -> - JID = jid:from_string(RoomJID), JID#jid.user. + JID = jid:decode(RoomJID), JID#jid.user. %% calculate day before get_timestamp_daydiff(TimeStamp, Daydiff) -> @@ -998,7 +998,7 @@ get_room_info(RoomJID, Opts) -> {value, {_, SA}} -> SA; false -> <<"">> end, - #room{jid = jid:to_string(RoomJID), title = Title, + #room{jid = jid:encode(RoomJID), title = Title, subject = Subject, subject_author = SubjectAuthor, config = Opts}. @@ -1159,7 +1159,7 @@ role_users_to_string(RoleS, Users) -> <>. get_room_occupants(RoomJIDString) -> - RoomJID = jid:from_string(RoomJIDString), + RoomJID = jid:decode(RoomJIDString), RoomName = RoomJID#jid.luser, MucService = RoomJID#jid.lserver, StateData = get_room_state(RoomName, MucService), diff --git a/src/mod_muc_mnesia.erl b/src/mod_muc_mnesia.erl index 881ec758e..f237c564f 100644 --- a/src/mod_muc_mnesia.erl +++ b/src/mod_muc_mnesia.erl @@ -279,7 +279,7 @@ import(_LServer, <<"muc_room">>, opts = Opts}); import(_LServer, <<"muc_registered">>, [J, RoomHost, Nick, _TimeStamp]) -> - #jid{user = U, server = S} = jid:from_string(J), + #jid{user = U, server = S} = jid:decode(J), mnesia:dirty_write( #muc_registered{us_host = {{U, S}, RoomHost}, nick = Nick}). diff --git a/src/mod_muc_riak.erl b/src/mod_muc_riak.erl index 23681e883..0577ef60b 100644 --- a/src/mod_muc_riak.erl +++ b/src/mod_muc_riak.erl @@ -178,7 +178,7 @@ import(_LServer, <<"muc_room">>, muc_room_schema()); import(_LServer, <<"muc_registered">>, [J, RoomHost, Nick, _TimeStamp]) -> - #jid{user = U, server = S} = jid:from_string(J), + #jid{user = U, server = S} = jid:decode(J), R = #muc_registered{us_host = {{U, S}, RoomHost}, nick = Nick}, ejabberd_riak:put(R, muc_registered_schema(), [{'2i', [{<<"nick_host">>, {Nick, RoomHost}}]}]). diff --git a/src/mod_muc_room.erl b/src/mod_muc_room.erl index 3f3208b68..8ab14a92b 100644 --- a/src/mod_muc_room.erl +++ b/src/mod_muc_room.erl @@ -132,7 +132,7 @@ init([Host, ServerHost, Access, Room, HistorySize, State1 = set_opts(DefRoomOpts, State), store_room(State1), ?INFO_MSG("Created MUC room ~s@~s by ~s", - [Room, Host, jid:to_string(Creator)]), + [Room, Host, jid:encode(Creator)]), add_to_log(room_existence, created, State1), add_to_log(room_existence, started, State1), {ok, normal_state, State1}; @@ -483,12 +483,12 @@ handle_event({destroy, Reason}, _StateName, destroy_room(#muc_destroy{xmlns = ?NS_MUC_OWNER, reason = Reason}, StateData), ?INFO_MSG("Destroyed MUC room ~s with reason: ~p", - [jid:to_string(StateData#state.jid), Reason]), + [jid:encode(StateData#state.jid), Reason]), add_to_log(room_existence, destroyed, StateData), {stop, shutdown, StateData}; handle_event(destroy, StateName, StateData) -> ?INFO_MSG("Destroyed MUC room ~s", - [jid:to_string(StateData#state.jid)]), + [jid:encode(StateData#state.jid)]), handle_event({destroy, undefined}, StateName, StateData); handle_event({set_affiliations, Affiliations}, StateName, StateData) -> @@ -1056,7 +1056,7 @@ close_room_if_temporary_and_empty(StateData1) -> true -> ?INFO_MSG("Destroyed MUC room ~s because it's temporary " "and empty", - [jid:to_string(StateData1#state.jid)]), + [jid:encode(StateData1#state.jid)]), add_to_log(room_existence, destroyed, StateData1), {stop, normal, StateData1}; _ -> {next_state, normal_state, StateData1} @@ -1178,7 +1178,7 @@ decide_fate_message(#message{type = error} = Msg, true -> Reason = str:format("This participant is considered a ghost " "and is expulsed: ~s", - [jid:to_string(From)]), + [jid:encode(From)]), {expulse_sender, Reason}; false -> continue_delivery end, @@ -2575,8 +2575,8 @@ process_admin_items_set(UJID, Items, Lang, StateData) -> {result, Res} -> ?INFO_MSG("Processing MUC admin query from ~s in " "room ~s:~n ~p", - [jid:to_string(UJID), - jid:to_string(StateData#state.jid), Res]), + [jid:encode(UJID), + jid:encode(StateData#state.jid), Res]), case lists:foldl(process_item_change(UJID), StateData, lists:flatten(Res)) of {error, _} = Err -> @@ -2650,7 +2650,7 @@ process_item_change(Item, SD, UJID) -> end catch E:R -> ?ERROR_MSG("failed to set item ~p from ~s: ~p", - [Item, jid:to_string(UJID), + [Item, jid:encode(UJID), {E, {R, erlang:get_stacktrace()}}]), {error, xmpp:err_internal_server_error()} end. @@ -2995,7 +2995,7 @@ process_iq_owner(From, #iq{type = set, lang = Lang, {error, xmpp:err_forbidden(ErrText, Lang)}; Destroy /= undefined, Config == undefined, Items == [] -> ?INFO_MSG("Destroyed MUC room ~s by the owner ~s", - [jid:to_string(StateData#state.jid), jid:to_string(From)]), + [jid:encode(StateData#state.jid), jid:encode(From)]), add_to_log(room_existence, destroyed, StateData), destroy_room(Destroy, StateData); Config /= undefined, Destroy == undefined, Items == [] -> @@ -3140,7 +3140,7 @@ get_config(Lang, StateData, From) -> MaxUsersRoom = get_max_users(StateData), Title = str:format( translate:translate(Lang, <<"Configuration of room ~s">>), - [jid:to_string(StateData#state.jid)]), + [jid:encode(StateData#state.jid)]), Fs = [{roomname, Config#config.title}, {roomdesc, Config#config.description}] ++ case acl:match_rule(StateData#state.server_host, AccessPersistent, From) of @@ -3898,8 +3898,8 @@ route_invitation(From, Invitation, Lang, StateData) -> translate:translate( Lang, <<"~s invites you to the room ~s">>), - [jid:to_string(From), - jid:to_string({StateData#state.room, StateData#state.host, <<"">>})]), + [jid:encode(From), + jid:encode({StateData#state.room, StateData#state.host, <<"">>})]), case (StateData#state.config)#config.password_protected of true -> <<", ", diff --git a/src/mod_muc_sql.erl b/src/mod_muc_sql.erl index d242eb9b7..df1319ce3 100644 --- a/src/mod_muc_sql.erl +++ b/src/mod_muc_sql.erl @@ -82,7 +82,7 @@ forget_room(LServer, Host, Name) -> ejabberd_sql:sql_transaction(LServer, F). can_use_nick(LServer, Host, JID, Nick) -> - SJID = jid:to_string(jid:tolower(jid:remove_resource(JID))), + SJID = jid:encode(jid:tolower(jid:remove_resource(JID))), case catch ejabberd_sql:sql_query( LServer, ?SQL("select @(jid)s from muc_registered " @@ -110,7 +110,7 @@ get_rooms(LServer, Host) -> end. get_nick(LServer, Host, From) -> - SJID = jid:to_string(jid:tolower(jid:remove_resource(From))), + SJID = jid:encode(jid:tolower(jid:remove_resource(From))), case catch ejabberd_sql:sql_query( LServer, ?SQL("select @(nick)s from muc_registered where" @@ -120,7 +120,7 @@ get_nick(LServer, Host, From) -> end. set_nick(LServer, Host, From, Nick) -> - JID = jid:to_string(jid:tolower(jid:remove_resource(From))), + JID = jid:encode(jid:tolower(jid:remove_resource(From))), F = fun () -> case Nick of <<"">> -> @@ -215,7 +215,7 @@ export(_Server) -> nick = Nick}) -> case str:suffix(Host, RoomHost) of true -> - SJID = jid:to_string(jid:make(U, S)), + SJID = jid:encode(jid:make(U, S)), [?SQL("delete from muc_registered where" " jid=%(SJID)s and host=%(RoomHost)s;"), ?SQL("insert into muc_registered(jid, host, " diff --git a/src/mod_multicast.erl b/src/mod_multicast.erl index 48a2b5962..4f4f96614 100644 --- a/src/mod_multicast.erl +++ b/src/mod_multicast.erl @@ -242,7 +242,7 @@ handle_iq(Packet, State) -> ejabberd_router:route_error(Packet, Error); reply -> To = xmpp:get_to(IQ), - LServiceS = jid:to_string(To), + LServiceS = jid:encode(To), case Packet#iq.type of result -> process_iqreply_result(LServiceS, IQ); @@ -297,7 +297,7 @@ route_trusted(LServiceS, LServerS, FromJID, Delivereds = [], Dests2 = lists:map( fun(D) -> - #dest{jid_string = jid:to_string(D), + #dest{jid_string = jid:encode(D), jid_jid = D, type = bcc, full_xml = #address{type = bcc, jid = D}} end, Destinations), @@ -463,7 +463,7 @@ convert_dest_record(Addrs) -> fun(#address{jid = undefined} = Addr) -> #dest{jid_string = none, full_xml = Addr}; (#address{jid = JID, type = Type} = Addr) -> - #dest{jid_string = jid:to_string(JID), jid_jid = JID, + #dest{jid_string = jid:encode(JID), jid_jid = JID, type = Type, full_xml = Addr} end, Addrs). @@ -1120,9 +1120,9 @@ make_reply(internal_server_error, Lang, ErrText) -> make_reply(forbidden, Lang, ErrText) -> xmpp:err_forbidden(ErrText, Lang). -stj(String) -> jid:from_string(String). +stj(String) -> jid:decode(String). -jts(String) -> jid:to_string(String). +jts(String) -> jid:encode(String). depends(_Host, _Opts) -> []. diff --git a/src/mod_offline.erl b/src/mod_offline.erl index 0a11864db..ba304f821 100644 --- a/src/mod_offline.erl +++ b/src/mod_offline.erl @@ -306,7 +306,7 @@ get_sm_items(_Acc, #jid{luser = U, lserver = S} = JID, Node = integer_to_binary(Seq), #disco_item{jid = BareJID, node = Node, - name = jid:to_string(From)} + name = jid:encode(From)} end, Hdrs)}; get_sm_items(Acc, _From, _To, _Node, _Lang) -> Acc. @@ -673,7 +673,7 @@ offline_msg_to_route(LServer, #offline_msg{from = From, to = To} = R) -> {route, Pkt2} catch _:{xmpp_codec, Why} -> ?ERROR_MSG("failed to decode packet ~p of user ~s: ~s", - [R#offline_msg.packet, jid:to_string(To), + [R#offline_msg.packet, jid:encode(To), xmpp:format_error(Why)]), error end. @@ -693,7 +693,7 @@ read_messages(LUser, LServer) -> catch _:{xmpp_codec, Why} -> ?ERROR_MSG("failed to decode packet ~p " "of user ~s: ~s", - [El, jid:to_string(To), + [El, jid:encode(To), xmpp:format_error(Why)]), [] end @@ -704,8 +704,8 @@ format_user_queue(Hdrs) -> fun({Seq, From, To, TS, El}) -> ID = integer_to_binary(Seq), FPacket = ejabberd_web_admin:pretty_print_xml(El), - SFrom = jid:to_string(From), - STo = jid:to_string(To), + SFrom = jid:encode(From), + STo = jid:encode(To), Time = case TS of undefined -> Stamp = fxml:get_path_s(El, [{elem, <<"delay">>}, @@ -792,7 +792,7 @@ user_queue_parse_query(LUser, LServer, Query) -> end. us_to_list({User, Server}) -> - jid:to_string({User, Server, <<"">>}). + jid:encode({User, Server, <<"">>}). get_queue_length(LUser, LServer) -> count_offline_messages(LUser, LServer). diff --git a/src/mod_offline_sql.erl b/src/mod_offline_sql.erl index 6a8d03f33..a8c587679 100644 --- a/src/mod_offline_sql.erl +++ b/src/mod_offline_sql.erl @@ -227,17 +227,17 @@ xml_to_offline_msg(XML) -> el_to_offline_msg(El) -> To_s = fxml:get_tag_attr_s(<<"to">>, El), From_s = fxml:get_tag_attr_s(<<"from">>, El), - To = jid:from_string(To_s), - From = jid:from_string(From_s), - if To == error -> + try + To = jid:decode(To_s), + From = jid:decode(From_s), + {ok, #offline_msg{us = {To#jid.luser, To#jid.lserver}, + from = From, + to = To, + packet = El}} + catch _:{bad_jid, To_s} -> ?ERROR_MSG("failed to get 'to' JID from offline XML ~p", [El]), {error, bad_jid_to}; - From == error -> + _:{bad_jid, From_s} -> ?ERROR_MSG("failed to get 'from' JID from offline XML ~p", [El]), - {error, bad_jid_from}; - true -> - {ok, #offline_msg{us = {To#jid.luser, To#jid.lserver}, - from = From, - to = To, - packet = El}} + {error, bad_jid_from} end. diff --git a/src/mod_pres_counter.erl b/src/mod_pres_counter.erl index f70e2d9f0..463ac90da 100644 --- a/src/mod_pres_counter.erl +++ b/src/mod_pres_counter.erl @@ -105,14 +105,14 @@ update(Server, JID, Dir) -> in -> ?WARNING_MSG("User ~s is being flooded, ignoring received " "presence subscriptions", - [jid:to_string(JID)]); + [jid:encode(JID)]); out -> IP = ejabberd_sm:get_user_ip(JID#jid.luser, JID#jid.lserver, JID#jid.lresource), ?WARNING_MSG("Flooder detected: ~s, on IP: ~s ignoring " "sent presence subscriptions~n", - [jid:to_string(JID), + [jid:encode(JID), jlib:ip_to_list(IP)]) end, {stop, deny}; diff --git a/src/mod_privacy.erl b/src/mod_privacy.erl index 836dfe66d..7a34588e5 100644 --- a/src/mod_privacy.erl +++ b/src/mod_privacy.erl @@ -236,7 +236,7 @@ encode_list_item(#listitem{action = Action, -spec encode_value(listitem_type(), listitem_value()) -> binary(). encode_value(Type, Val) -> case Type of - jid -> jid:to_string(Val); + jid -> jid:encode(Val); group -> Val; subscription -> case Val of @@ -252,7 +252,7 @@ encode_value(Type, Val) -> listitem_value(). decode_value(Type, Value) -> case Type of - jid -> jid:tolower(jid:from_string(Value)); + jid -> jid:tolower(jid:decode(Value)); subscription -> case Value of <<"from">> -> from; diff --git a/src/mod_privacy_sql.erl b/src/mod_privacy_sql.erl index 4260fcb5d..615ef7a0d 100644 --- a/src/mod_privacy_sql.erl +++ b/src/mod_privacy_sql.erl @@ -278,10 +278,8 @@ raw_to_item({SType, SValue, SAction, Order, MatchAll, {Type, Value} = case SType of <<"n">> -> {none, none}; <<"j">> -> - case jid:from_string(SValue) of - #jid{} = JID -> - {jid, jid:tolower(JID)} - end; + JID = jid:decode(SValue), + {jid, jid:tolower(JID)}; <<"g">> -> {group, SValue}; <<"s">> -> case SValue of @@ -312,7 +310,7 @@ item_to_raw(#listitem{type = Type, value = Value, match_presence_out = MatchPresenceOut}) -> {SType, SValue} = case Type of none -> {<<"n">>, <<"">>}; - jid -> {<<"j">>, jid:to_string(Value)}; + jid -> {<<"j">>, jid:encode(Value)}; group -> {<<"g">>, Value}; subscription -> case Value of diff --git a/src/mod_proxy65_service.erl b/src/mod_proxy65_service.erl index b9a96ca71..58aee60ca 100644 --- a/src/mod_proxy65_service.erl +++ b/src/mod_proxy65_service.erl @@ -215,8 +215,8 @@ process_bytestreams(#iq{type = set, lang = Lang, from = InitiatorJID, to = To, case acl:match_rule(ServerHost, ACL, InitiatorJID) of allow -> Node = ejabberd_cluster:get_node_by_id(To#jid.lresource), - Target = jid:to_string(jid:tolower(TargetJID)), - Initiator = jid:to_string(jid:tolower(InitiatorJID)), + Target = jid:encode(jid:tolower(TargetJID)), + Initiator = jid:encode(jid:tolower(InitiatorJID)), SHA1 = p1_sha:sha(<>), Mod = gen_mod:ram_db_mod(global, mod_proxy65), MaxConnections = max_connections(ServerHost), diff --git a/src/mod_proxy65_stream.erl b/src/mod_proxy65_stream.erl index a04e9e94b..45ce2f44c 100644 --- a/src/mod_proxy65_stream.erl +++ b/src/mod_proxy65_stream.erl @@ -121,8 +121,8 @@ activate({P1, J1}, {P2, J2}) -> {S1, S2} when is_port(S1), is_port(S2) -> P1 ! {activate, P2, S2, J1, J2}, P2 ! {activate, P1, S1, J1, J2}, - JID1 = jid:to_string(J1), - JID2 = jid:to_string(J2), + JID1 = jid:encode(J1), + JID2 = jid:encode(J2), ?INFO_MSG("(~w:~w) Activated bytestream for ~s " "-> ~s", [P1, P2, JID1, JID2]), diff --git a/src/mod_pubsub.erl b/src/mod_pubsub.erl index 970ef5226..4ea840011 100644 --- a/src/mod_pubsub.erl +++ b/src/mod_pubsub.erl @@ -1414,7 +1414,7 @@ get_pending_nodes(Host, Owner, Plugins) -> binary()) -> adhoc_command() | {error, stanza_error()}. send_pending_auth_events(Host, Node, Owner, Lang) -> ?DEBUG("Sending pending auth events for ~s on ~s:~s", - [jid:to_string(Owner), Host, Node]), + [jid:encode(Owner), Host, Node]), Action = fun(#pubsub_node{id = Nidx, type = Type}) -> case lists:member(<<"get-pending">>, plugin_features(Host, Type)) of @@ -2819,7 +2819,7 @@ broadcast_publish_item(Host, Node, Nidx, Type, NodeOptions, ItemId, From, Payloa EventItem0; publisher -> EventItem0#ps_item{ - publisher = jid:to_string(From)}; + publisher = jid:encode(From)}; none -> EventItem0 end, diff --git a/src/mod_register.erl b/src/mod_register.erl index 3a435778f..5764ad585 100644 --- a/src/mod_register.erl +++ b/src/mod_register.erl @@ -287,7 +287,7 @@ try_set_password(User, Server, Password, #iq{lang = Lang, meta = M} = IQ) -> case ejabberd_auth:set_password(User, Server, Password) of ok -> ?INFO_MSG("~s has changed password from ~s", - [jid:to_string({User, Server, <<"">>}), + [jid:encode({User, Server, <<"">>}), ejabberd_config:may_hide_data( jlib:ip_to_list(maps:get(ip, M, {0,0,0,0})))]), xmpp:make_iq_result(IQ); @@ -405,8 +405,7 @@ send_registration_notifications(Mod, UJID, Source) -> case gen_mod:get_module_opt( Host, Mod, registration_watchers, fun(Ss) -> - [#jid{} = jid:from_string(iolist_to_binary(S)) - || S <- Ss] + [jid:decode(iolist_to_binary(S)) || S <- Ss] end, []) of [] -> ok; JIDs when is_list(JIDs) -> @@ -414,7 +413,7 @@ send_registration_notifications(Mod, UJID, Source) -> (str:format("[~s] The account ~s was registered from " "IP address ~s on node ~w using ~p.", [get_time_string(), - jid:to_string(UJID), + jid:encode(UJID), ip_to_string(Source), node(), Mod])), lists:foreach( @@ -643,8 +642,7 @@ mod_opt_type(password_strength) -> fun (N) when is_number(N), N >= 0 -> N end; mod_opt_type(registration_watchers) -> fun (Ss) -> - [#jid{} = jid:from_string(iolist_to_binary(S)) - || S <- Ss] + [jid:decode(iolist_to_binary(S)) || S <- Ss] end; mod_opt_type(welcome_message) -> fun (Opts) -> diff --git a/src/mod_roster.erl b/src/mod_roster.erl index c624af56d..5f499ec74 100644 --- a/src/mod_roster.erl +++ b/src/mod_roster.erl @@ -333,7 +333,7 @@ process_iq_get(#iq{to = To, lang = Lang, end) catch E:R -> ?ERROR_MSG("failed to process roster get for ~s: ~p", - [jid:to_string(To), {E, {R, erlang:get_stacktrace()}}]), + [jid:encode(To), {E, {R, erlang:get_stacktrace()}}]), Txt = <<"Roster module has failed">>, xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang)) end. @@ -1038,10 +1038,10 @@ build_contact_jid_td(RosterJID) -> case JIDURI of <<>> -> ?XAC(<<"td">>, [{<<"class">>, <<"valign">>}], - (jid:to_string(RosterJID))); + (jid:encode(RosterJID))); URI when is_binary(URI) -> ?XAE(<<"td">>, [{<<"class">>, <<"valign">>}], - [?AC(JIDURI, (jid:to_string(RosterJID)))]) + [?AC(JIDURI, (jid:encode(RosterJID)))]) end. user_roster_parse_query(User, Server, Items, Query) -> @@ -1049,10 +1049,11 @@ user_roster_parse_query(User, Server, Items, Query) -> {value, _} -> case lists:keysearch(<<"newjid">>, 1, Query) of {value, {_, SJID}} -> - case jid:from_string(SJID) of - JID when is_record(JID, jid) -> - user_roster_subscribe_jid(User, Server, JID), ok; - error -> error + try jid:decode(SJID) of + JID -> + user_roster_subscribe_jid(User, Server, JID), ok + catch _:{bad_jid, _} -> + error end; false -> error end; @@ -1114,7 +1115,7 @@ user_roster_item_parse_query(User, Server, Items, nothing. us_to_list({User, Server}) -> - jid:to_string({User, Server, <<"">>}). + jid:encode({User, Server, <<"">>}). webadmin_user(Acc, _User, _Server, Lang) -> Acc ++ @@ -1145,7 +1146,7 @@ import_stop(_LServer, _DBType) -> ok. import(LServer, {sql, _}, _DBType, <<"rostergroups">>, [LUser, SJID, Group]) -> - LJID = jid:tolower(jid:from_string(SJID)), + LJID = jid:tolower(jid:decode(SJID)), ets:insert(rostergroups_tmp, {{LUser, LServer, LJID}, Group}), ok; import(LServer, {sql, _}, DBType, <<"rosterusers">>, Row) -> diff --git a/src/mod_roster_sql.erl b/src/mod_roster_sql.erl index ba0fd529e..112c13a72 100644 --- a/src/mod_roster_sql.erl +++ b/src/mod_roster_sql.erl @@ -83,7 +83,7 @@ get_roster(LUser, LServer) -> %% Bad JID in database: error -> []; R -> - SJID = jid:to_string(R#roster.jid), + SJID = jid:encode(R#roster.jid), Groups = case dict:find(SJID, GroupsDict) of {ok, Gs} -> Gs; error -> [] @@ -97,7 +97,7 @@ get_roster(LUser, LServer) -> get_roster_by_jid(LUser, LServer, LJID) -> {selected, Res} = - sql_queries:get_roster_by_jid(LServer, LUser, jid:to_string(LJID)), + sql_queries:get_roster_by_jid(LServer, LUser, jid:encode(LJID)), case Res of [] -> #roster{usj = {LUser, LServer, LJID}, @@ -136,7 +136,7 @@ transaction(LServer, F) -> ejabberd_sql:sql_transaction(LServer, F). get_roster_by_jid_with_groups(LUser, LServer, LJID) -> - SJID = jid:to_string(LJID), + SJID = jid:encode(LJID), case sql_queries:get_roster_by_jid(LServer, LUser, SJID) of {selected, [I]} -> case raw_to_record(LServer, I) of @@ -162,18 +162,18 @@ remove_user(LUser, LServer) -> {atomic, ok}. update_roster(LUser, LServer, LJID, Item) -> - SJID = jid:to_string(LJID), + SJID = jid:encode(LJID), ItemVals = record_to_row(Item), ItemGroups = Item#roster.groups, sql_queries:update_roster(LServer, LUser, SJID, ItemVals, ItemGroups). del_roster(LUser, LServer, LJID) -> - SJID = jid:to_string(LJID), + SJID = jid:encode(LJID), sql_queries:del_roster(LServer, LUser, SJID). read_subscription_and_groups(LUser, LServer, LJID) -> - SJID = jid:to_string(LJID), + SJID = jid:encode(LJID), case catch sql_queries:get_subscription(LServer, LUser, SJID) of {selected, [{SSubscription}]} -> Subscription = case SSubscription of @@ -234,10 +234,7 @@ raw_to_record(LServer, raw_to_record(LServer, {User, SJID, Nick, SSubscription, SAsk, SAskMessage, _SServer, _SSubscribe, _SType}) -> - case jid:from_string(SJID) of - error -> - ?ERROR_MSG("~s", [format_row_error(User, LServer, {jid, SJID})]), - error; + try jid:decode(SJID) of JID -> LJID = jid:tolower(JID), Subscription = case SSubscription of @@ -268,13 +265,16 @@ raw_to_record(LServer, us = {User, LServer}, jid = LJID, name = Nick, subscription = Subscription, ask = Ask, askmessage = SAskMessage} + catch _:{bad_jid, _} -> + ?ERROR_MSG("~s", [format_row_error(User, LServer, {jid, SJID})]), + error end. record_to_row( #roster{us = {LUser, _LServer}, jid = JID, name = Name, subscription = Subscription, ask = Ask, askmessage = AskMessage}) -> - SJID = jid:to_string(jid:tolower(JID)), + SJID = jid:encode(jid:tolower(JID)), SSubscription = case Subscription of both -> <<"B">>; to -> <<"T">>; diff --git a/src/mod_shared_roster.erl b/src/mod_shared_roster.erl index 5d3983361..8a775cb52 100644 --- a/src/mod_shared_roster.erl +++ b/src/mod_shared_roster.erl @@ -983,15 +983,13 @@ shared_roster_group_parse_query(Host, Group, Query) -> <<"@all@">> -> USs; <<"@online@">> -> USs; _ -> - case jid:from_string(SJID) - of - JID - when is_record(JID, - jid) -> + try jid:decode(SJID) of + JID -> [{JID#jid.luser, JID#jid.lserver} - | USs]; - error -> error + | USs] + catch _:{bad_jid, _} -> + error end end end, @@ -1043,7 +1041,7 @@ get_opt(Opts, Opt, Default) -> end. us_to_list({User, Server}) -> - jid:to_string({User, Server, <<"">>}). + jid:encode({User, Server, <<"">>}). split_grouphost(Host, Group) -> case str:tokens(Group, <<"@">>) of diff --git a/src/mod_shared_roster_mnesia.erl b/src/mod_shared_roster_mnesia.erl index 702e27a09..1e3012794 100644 --- a/src/mod_shared_roster_mnesia.erl +++ b/src/mod_shared_roster_mnesia.erl @@ -140,7 +140,7 @@ import(LServer, <<"sr_group">>, [Group, SOpts, _TimeStamp]) -> opts = ejabberd_sql:decode_term(SOpts)}, mnesia:dirty_write(G); import(LServer, <<"sr_user">>, [SJID, Group, _TimeStamp]) -> - #jid{luser = U, lserver = S} = jid:from_string(SJID), + #jid{luser = U, lserver = S} = jid:decode(SJID), User = #sr_user{us = {U, S}, group_host = {Group, LServer}}, mnesia:dirty_write(User). diff --git a/src/mod_shared_roster_riak.erl b/src/mod_shared_roster_riak.erl index 1bf7a1429..441aafd06 100644 --- a/src/mod_shared_roster_riak.erl +++ b/src/mod_shared_roster_riak.erl @@ -142,7 +142,7 @@ import(LServer, <<"sr_group">>, [Group, SOpts, _TimeStamp]) -> opts = ejabberd_sql:decode_term(SOpts)}, ejabberd_riak:put(G, sr_group_schema(), [{'2i', [{<<"host">>, LServer}]}]); import(LServer, <<"sr_user">>, [SJID, Group|_]) -> - #jid{luser = U, lserver = S} = jid:from_string(SJID), + #jid{luser = U, lserver = S} = jid:decode(SJID), User = #sr_user{us = {U, S}, group_host = {Group, LServer}}, ejabberd_riak:put(User, sr_user_schema(), [{i, {{U, S}, {Group, LServer}}}, diff --git a/src/mod_shared_roster_sql.erl b/src/mod_shared_roster_sql.erl index 8a32cc385..6378ea963 100644 --- a/src/mod_shared_roster_sql.erl +++ b/src/mod_shared_roster_sql.erl @@ -123,7 +123,7 @@ get_group_explicit_users(Host, Group) -> {selected, Rs} -> lists:map( fun({JID}) -> - {U, S, _} = jid:tolower(jid:from_string(JID)), + {U, S, _} = jid:tolower(jid:decode(JID)), {U, S} end, Rs); _ -> @@ -198,6 +198,6 @@ import(_, _, _) -> %%% Internal functions %%%=================================================================== make_jid_s(U, S) -> - jid:to_string(jid:tolower(jid:make(U, S))). + jid:encode(jid:tolower(jid:make(U, S))). make_jid_s({U, S}) -> make_jid_s(U, S). diff --git a/src/mod_stream_mgmt.erl b/src/mod_stream_mgmt.erl index 6420c257b..5b333ede5 100644 --- a/src/mod_stream_mgmt.erl +++ b/src/mod_stream_mgmt.erl @@ -229,20 +229,20 @@ c2s_handle_call(State, _Call, _From) -> c2s_handle_info(#{mgmt_ack_timer := TRef, jid := JID, mod := Mod} = State, {timeout, TRef, ack_timeout}) -> ?DEBUG("Timed out waiting for stream management acknowledgement of ~s", - [jid:to_string(JID)]), + [jid:encode(JID)]), State1 = State#{stop_reason => {socket, timeout}}, State2 = Mod:close(State1, _SendTrailer = false), {stop, transition_to_pending(State2)}; c2s_handle_info(#{mgmt_state := pending, jid := JID, mod := Mod} = State, {timeout, _, pending_timeout}) -> ?DEBUG("Timed out waiting for resumption of stream for ~s", - [jid:to_string(JID)]), + [jid:encode(JID)]), Mod:stop(State#{mgmt_state => timeout}); c2s_handle_info(#{jid := JID} = State, {_Ref, {resume, OldState}}) -> %% This happens if the resume_session/1 request timed out; the new session %% now receives the late response. ?DEBUG("Received old session state for ~s after failed resumption", - [jid:to_string(JID)]), + [jid:encode(JID)]), route_unacked_stanzas(OldState#{mgmt_resend => false}), State; c2s_handle_info(State, _) -> @@ -257,7 +257,7 @@ c2s_closed(State, _Reason) -> c2s_terminated(#{mgmt_state := resumed, jid := JID} = State, _Reason) -> ?INFO_MSG("Closing former stream of resumed session for ~s", - [jid:to_string(JID)]), + [jid:encode(JID)]), bounce_message_queue(), {stop, State}; c2s_terminated(#{mgmt_state := MgmtState, mgmt_stanzas_in := In, sid := SID, @@ -328,14 +328,14 @@ handle_enable(#{mgmt_timeout := DefaultTimeout, end, Res = if Timeout > 0 -> ?INFO_MSG("Stream management with resumption enabled for ~s", - [jid:to_string(JID)]), + [jid:encode(JID)]), #sm_enabled{xmlns = Xmlns, id = make_resume_id(State), resume = true, max = Timeout}; true -> ?INFO_MSG("Stream management without resumption enabled for ~s", - [jid:to_string(JID)]), + [jid:encode(JID)]), #sm_enabled{xmlns = Xmlns} end, State1 = State#{mgmt_state => active, @@ -383,7 +383,7 @@ handle_resume(#{user := User, lserver := LServer, sockmod := SockMod, %% csi_flush_queue(State4), State5 = ejabberd_hooks:run_fold(c2s_session_resumed, LServer, State4, []), ?INFO_MSG("(~s) Resumed session for ~s", - [SockMod:pp(Socket), jid:to_string(JID)]), + [SockMod:pp(Socket), jid:encode(JID)]), {ok, State5}; {error, El, Msg} -> ?INFO_MSG("Cannot resume session for ~s@~s: ~s", @@ -398,7 +398,7 @@ transition_to_pending(#{mgmt_state := active, mod := Mod, transition_to_pending(#{mgmt_state := active, jid := JID, lserver := LServer, mgmt_timeout := Timeout} = State) -> State1 = cancel_ack_timer(State), - ?INFO_MSG("Waiting for resumption of stream for ~s", [jid:to_string(JID)]), + ?INFO_MSG("Waiting for resumption of stream for ~s", [jid:encode(JID)]), erlang:start_timer(timer:seconds(Timeout), self(), pending_timeout), State2 = State1#{mgmt_state => pending}, ejabberd_hooks:run_fold(c2s_session_pending, LServer, State2, []); @@ -409,11 +409,11 @@ transition_to_pending(State) -> check_h_attribute(#{mgmt_stanzas_out := NumStanzasOut, jid := JID} = State, H) when H > NumStanzasOut -> ?DEBUG("~s acknowledged ~B stanzas, but only ~B were sent", - [jid:to_string(JID), H, NumStanzasOut]), + [jid:encode(JID), H, NumStanzasOut]), mgmt_queue_drop(State#{mgmt_stanzas_out => H}, NumStanzasOut); check_h_attribute(#{mgmt_stanzas_out := NumStanzasOut, jid := JID} = State, H) -> ?DEBUG("~s acknowledged ~B of ~B stanzas", - [jid:to_string(JID), H, NumStanzasOut]), + [jid:encode(JID), H, NumStanzasOut]), mgmt_queue_drop(State, H). -spec update_num_stanzas_in(state(), xmpp_element()) -> state(). @@ -490,7 +490,7 @@ resend_unacked_stanzas(#{mgmt_state := MgmtState, MgmtState == pending orelse MgmtState == timeout) andalso QueueLen > 0 -> ?DEBUG("Resending ~B unacknowledged stanza(s) to ~s", - [QueueLen, jid:to_string(JID)]), + [QueueLen, jid:encode(JID)]), queue_foldl( fun({_, Time, Pkt}, AccState) -> NewPkt = add_resent_delay_info(AccState, Pkt, Time), @@ -522,10 +522,10 @@ route_unacked_stanzas(#{mgmt_state := MgmtState, end end, ?DEBUG("Re-routing ~B unacknowledged stanza(s) to ~s", - [QueueLen, jid:to_string(JID)]), + [QueueLen, jid:encode(JID)]), queue_foreach( fun({_, _Time, #presence{from = From}}) -> - ?DEBUG("Dropping presence stanza from ~s", [jid:to_string(From)]); + ?DEBUG("Dropping presence stanza from ~s", [jid:encode(From)]); ({_, _Time, #iq{} = El}) -> Txt = <<"User session terminated">>, ejabberd_router:route_error( @@ -537,14 +537,14 @@ route_unacked_stanzas(#{mgmt_state := MgmtState, %% back to the original sender." Resending such a stanza could %% easily lead to unexpected results as well. ?DEBUG("Dropping forwarded message stanza from ~s", - [jid:to_string(From)]); + [jid:encode(From)]); ({_, Time, #message{} = Msg}) -> case ejabberd_hooks:run_fold(message_is_archived, LServer, false, [State, Msg]) of true -> ?DEBUG("Dropping archived message stanza from ~s", - [jid:to_string(xmpp:get_from(Msg))]); + [jid:encode(xmpp:get_from(Msg))]); false when ResendOnTimeout -> NewEl = add_resent_delay_info(State, Msg, Time), ejabberd_router:route(NewEl); diff --git a/src/mod_vcard.erl b/src/mod_vcard.erl index 0e97f81ae..e7677b4bf 100644 --- a/src/mod_vcard.erl +++ b/src/mod_vcard.erl @@ -393,7 +393,7 @@ mk_field(Var, Val) -> -spec mk_search_form(jid(), binary(), binary()) -> search(). mk_search_form(JID, ServerHost, Lang) -> Title = <<(translate:translate(Lang, <<"Search users in ">>))/binary, - (jid:to_string(JID))/binary>>, + (jid:encode(JID))/binary>>, Mod = gen_mod:db_mod(ServerHost, ?MODULE), SearchFields = Mod:search_fields(ServerHost), Fs = [mk_tfield(Label, Var, Lang) || {Label, Var} <- SearchFields], @@ -419,7 +419,7 @@ search_result(Lang, JID, ServerHost, XFields) -> #xdata{type = result, title = <<(translate:translate(Lang, <<"Search Results for ">>))/binary, - (jid:to_string(JID))/binary>>, + (jid:encode(JID))/binary>>, reported = Reported, items = lists:map(fun (Item) -> item_to_field(Item) end, search(ServerHost, XFields))}. diff --git a/src/node_flat_sql.erl b/src/node_flat_sql.erl index 2df1ffb26..d3294cc68 100644 --- a/src/node_flat_sql.erl +++ b/src/node_flat_sql.erl @@ -960,7 +960,7 @@ update_subscription(Nidx, JID, Subscription) -> -spec decode_jid(SJID :: binary()) -> ljid(). decode_jid(SJID) -> - jid:tolower(jid:from_string(SJID)). + jid:tolower(jid:decode(SJID)). -spec decode_affiliation(Arg :: binary()) -> atom(). decode_affiliation(<<"o">>) -> owner; @@ -988,11 +988,11 @@ decode_subscriptions(Subscriptions) -> -spec encode_jid(JID :: ljid()) -> binary(). encode_jid(JID) -> - jid:to_string(JID). + jid:encode(JID). -spec encode_jid_like(JID :: ljid()) -> binary(). encode_jid_like(JID) -> - ejabberd_sql:escape_like_arg_circumflex(jid:to_string(JID)). + ejabberd_sql:escape_like_arg_circumflex(jid:encode(JID)). -spec encode_host(Host :: host()) -> binary(). encode_host({_U, _S, _R} = LJID) -> encode_jid(LJID); diff --git a/src/nodetree_virtual.erl b/src/nodetree_virtual.erl index d29d9224f..86876b631 100644 --- a/src/nodetree_virtual.erl +++ b/src/nodetree_virtual.erl @@ -112,13 +112,13 @@ node_record(Host, Node, Nidx) -> options = Module:options()}. nodeidx({U,S,R}, Node) -> - JID = jid:to_string(jid:make(U,S,R)), + JID = jid:encode(jid:make(U,S,R)), <>; nodeidx(Host, Node) -> <>. nodeid(Nidx) -> [Head, Node] = binary:split(Nidx, <<":">>), - case jid:from_string(Head) of + case jid:decode(Head) of {jid,<<>>,Host,<<>>,_,_,_} -> {Host, Node}; {jid,U,S,R,_,_,_} -> {{U,S,R}, Node} end. diff --git a/src/prosody2ejabberd.erl b/src/prosody2ejabberd.erl index 881a5b3e7..5dd0b6766 100644 --- a/src/prosody2ejabberd.erl +++ b/src/prosody2ejabberd.erl @@ -172,7 +172,7 @@ convert_data(Host, "vcard", User, [Data]) -> ok end; convert_data(_Host, "config", _User, [Data]) -> - RoomJID = jid:from_string(proplists:get_value(<<"jid">>, Data, <<"">>)), + RoomJID = jid:decode(proplists:get_value(<<"jid">>, Data, <<"">>)), Config = proplists:get_value(<<"_data">>, Data, []), RoomCfg = convert_room_config(Data), case proplists:get_bool(<<"persistent">>, Config) of @@ -217,14 +217,14 @@ convert_data(_Host, _Type, _User, _Data) -> convert_pending_item(LUser, LServer, LuaList) -> lists:flatmap( fun({S, true}) -> - case jid:from_string(S) of - #jid{} = J -> + try jid:decode(S) of + J -> LJID = jid:tolower(J), [#roster{usj = {LUser, LServer, LJID}, us = {LUser, LServer}, jid = LJID, - ask = in}]; - error -> + ask = in}] + catch _:{bad_jid, _} -> [] end; (_) -> @@ -232,8 +232,8 @@ convert_pending_item(LUser, LServer, LuaList) -> end, LuaList). convert_roster_item(LUser, LServer, JIDstring, LuaList) -> - case jid:from_string(JIDstring) of - #jid{} = JID -> + try jid:decode(JIDstring) of + JID -> LJID = jid:tolower(JID), InitR = #roster{usj = {LUser, LServer, LJID}, us = {LUser, LServer}, @@ -253,18 +253,18 @@ convert_roster_item(LUser, LServer, JIDstring, LuaList) -> ({<<"name">>, Name}, R) -> R#roster{name = Name} end, InitR, LuaList), - [Roster]; - error -> + [Roster] + catch _:{bad_jid, _} -> [] end. convert_room_affiliations(Data) -> lists:flatmap( fun({J, Aff}) -> - case jid:from_string(J) of + try jid:decode(J) of #jid{luser = U, lserver = S} -> - [{{U, S, <<>>}, jlib:binary_to_atom(Aff)}]; - error -> + [{{U, S, <<>>}, jlib:binary_to_atom(Aff)}] + catch _:{bad_jid, _} -> [] end end, proplists:get_value(<<"_affiliations">>, Data, [])). @@ -278,13 +278,13 @@ convert_room_config(Data) -> [{password_protected, true}, {password, Password}] end, - Subj = case jid:from_string( + Subj = try jid:decode( proplists:get_value( <<"subject_from">>, Config, <<"">>)) of #jid{lresource = Nick} when Nick /= <<"">> -> [{subject, proplists:get_value(<<"subject">>, Config, <<"">>)}, - {subject_author, Nick}]; - _ -> + {subject_author, Nick}] + catch _:{bad_jid, _} -> [] end, Anonymous = case proplists:get_value(<<"whois">>, Config, <<"moderators">>) of @@ -316,7 +316,7 @@ convert_privacy_item({_, Item}) -> {Type, Value} = try case T of none -> {T, none}; group -> {T, V}; - jid -> {T, jid:tolower(jid:from_string(V))}; + jid -> {T, jid:tolower(jid:decode(V))}; subscription -> {T, jlib:binary_to_atom(V)} end catch _:_ -> @@ -333,29 +333,27 @@ convert_privacy_item({_, Item}) -> match_presence_out = MatchPresOut}. el_to_offline_msg(LUser, LServer, #xmlel{attrs = Attrs} = El) -> - try xmpp_util:decode_timestamp( - fxml:get_attr_s(<<"stamp">>, Attrs)) of - {_, _, _} = TS -> - Attrs1 = lists:filter( - fun(<<"stamp">>) -> false; - (<<"stamp_legacy">>) -> false; - (_) -> true - end, Attrs), - Packet = El#xmlel{attrs = Attrs1}, - case {jid:from_string(fxml:get_attr_s(<<"from">>, Attrs)), - jid:from_string(fxml:get_attr_s(<<"to">>, Attrs))} of - {#jid{} = From, #jid{} = To} -> - [#offline_msg{ - us = {LUser, LServer}, - timestamp = TS, - expire = never, - from = From, - to = To, - packet = Packet}]; - _ -> - [] - end + try + TS = xmpp_util:decode_timestamp( + fxml:get_attr_s(<<"stamp">>, Attrs)), + Attrs1 = lists:filter( + fun(<<"stamp">>) -> false; + (<<"stamp_legacy">>) -> false; + (_) -> true + end, Attrs), + Packet = El#xmlel{attrs = Attrs1}, + From = jid:decode(fxml:get_attr_s(<<"from">>, Attrs)), + To = jid:decode(fxml:get_attr_s(<<"to">>, Attrs)), + [#offline_msg{ + us = {LUser, LServer}, + timestamp = TS, + expire = never, + from = From, + to = To, + packet = Packet}] catch _:{bad_timestamp, _} -> + []; + _:{bad_jid, _} -> [] end. diff --git a/src/xmpp_stream_out.erl b/src/xmpp_stream_out.erl index 5efc940c2..67fdf7db9 100644 --- a/src/xmpp_stream_out.erl +++ b/src/xmpp_stream_out.erl @@ -560,7 +560,7 @@ process_sasl_mechanisms(Mechs, #{user := User, server := Server} = State) -> case lists:member(<<"EXTERNAL">>, Mechs) of true -> State1 = State#{stream_state => wait_for_sasl_response}, - Authzid = jid:to_string(jid:make(User, Server)), + Authzid = jid:encode(jid:make(User, Server)), send_pkt(State1, #sasl_auth{mechanism = Mech, text = Authzid}); false -> process_sasl_failure( diff --git a/src/xmpp_stream_pkix.erl b/src/xmpp_stream_pkix.erl index 16b7efa42..d556510f6 100644 --- a/src/xmpp_stream_pkix.erl +++ b/src/xmpp_stream_pkix.erl @@ -106,11 +106,13 @@ get_cert_domains(Cert) -> true -> error end, if D /= error -> - case jid:from_string(D) of + try jid:decode(D) of #jid{luser = <<"">>, lserver = LD, lresource = <<"">>} -> [LD]; _ -> [] + catch _:{bad_jid, _} -> + [] end; true -> [] end; @@ -131,7 +133,7 @@ get_cert_domains(Cert) -> value = XmppAddr}}) -> case 'XmppAddr':decode('XmppAddr', XmppAddr) of {ok, D} when is_binary(D) -> - case jid:from_string(D) of + try jid:decode(D) of #jid{luser = <<"">>, lserver = LD, lresource = <<"">>} -> @@ -142,16 +144,20 @@ get_cert_domains(Cert) -> [PCLD] end; _ -> [] + catch _:{bad_jid, _} -> + [] end; _ -> [] end; ({dNSName, D}) when is_list(D) -> - case jid:from_string(list_to_binary(D)) of + try jid:decode(list_to_binary(D)) of #jid{luser = <<"">>, lserver = LD, lresource = <<"">>} -> [LD]; _ -> [] + catch _:{bad_jid, _} -> + [] end; (_) -> [] end, SANs);