]> granicus.if.org Git - ejabberd/commitdiff
Introduce option 'allow_transports'
authorEvgeniy Khramtsov <ekhramtsov@process-one.net>
Fri, 8 Sep 2017 20:10:01 +0000 (23:10 +0300)
committerEvgeniy Khramtsov <ekhramtsov@process-one.net>
Fri, 8 Sep 2017 20:10:01 +0000 (23:10 +0300)
This is a boolean option. If set to `true` and some server's JID
is in user's roster, then messages from any user of this server are
accepted even if no subscription present.

The option is enabled by default.

src/mod_block_strangers.erl

index bed2f45952111255264d5d0dd8f4f5706e939ecf..b2c56f36bf4d472c6af289639065d6736e8794a9 100644 (file)
@@ -86,20 +86,19 @@ check_message(#message{from = From, to = To} = Msg) ->
     case (Msg#message.body == [] andalso
           Msg#message.subject == [])
         orelse ((AllowLocalUsers orelse From#jid.luser == <<"">>) andalso
-                ejabberd_router:is_my_route(From#jid.lserver)) of
+                ejabberd_router:is_my_host(From#jid.lserver)) of
        false ->
-           {Sub, _} = ejabberd_hooks:run_fold(
-                        roster_get_jid_info, LServer,
-                        {none, []}, [To#jid.luser, LServer, From]),
-           case Sub of
+           case check_subscription(From, To) of
                none ->
                    Drop = gen_mod:get_module_opt(LServer, ?MODULE, drop, true),
                    Log = gen_mod:get_module_opt(LServer, ?MODULE, log, false),
                    if
                        Log ->
-                           ?INFO_MSG("Dropping message from ~s to ~s: "
-                                     "the sender is not in the roster",
-                                     [jid:encode(From), jid:encode(To)]);
+                           ?INFO_MSG("~s message from stranger ~s to ~s",
+                                     [if Drop -> "Dropping";
+                                         true -> "Allow"
+                                      end,
+                                      jid:encode(From), jid:encode(To)]);
                        true ->
                            ok
                    end,
@@ -109,13 +108,41 @@ check_message(#message{from = From, to = To} = Msg) ->
                        true ->
                            allow
                    end;
-               _ ->
+               some ->
                    allow
            end;
        true ->
            allow
     end.
 
+-spec check_subscription(jid(), jid()) -> none | some.
+check_subscription(From, To) ->
+    {LocalUser, LocalServer, _} = jid:tolower(To),
+    {RemoteUser, RemoteServer, _} = jid:tolower(From),
+    case ejabberd_hooks:run_fold(
+          roster_get_jid_info, LocalServer,
+          {none, []}, [LocalUser, LocalServer, From]) of
+       {none, _} when RemoteUser == <<"">> ->
+           none;
+       {none, _} ->
+           case gen_mod:get_module_opt(LocalServer, ?MODULE,
+                                       allow_transports, true) of
+               true ->
+                   %% Check if the contact's server is in the roster
+                   case ejabberd_hooks:run_fold(
+                          roster_get_jid_info, LocalServer,
+                          {none, []},
+                          [LocalUser, LocalServer, jid:make(RemoteServer)]) of
+                       {none, _} -> none;
+                       _ -> some
+                   end;
+               false ->
+                   none
+           end;
+       _ ->
+           some
+    end.
+
 sets_bare_member({U, S, <<"">>} = LBJID, Set) ->
     case ?SETS:next(sets_iterator_from(LBJID, Set)) of
         {{U, S, _}, _} -> true;
@@ -151,4 +178,6 @@ mod_opt_type(log) ->
     fun (B) when is_boolean(B) -> B end;
 mod_opt_type(allow_local_users) ->
     fun (B) when is_boolean(B) -> B end;
-mod_opt_type(_) -> [drop, log, allow_local_users].
+mod_opt_type(allow_transports) ->
+    fun (B) when is_boolean(B) -> B end;
+mod_opt_type(_) -> [drop, log, allow_local_users, allow_transports].