From 675cc3e0ea5d3fe24952845859389896396d61a3 Mon Sep 17 00:00:00 2001 From: Christophe Romain Date: Tue, 31 Oct 2017 11:04:32 +0100 Subject: [PATCH] Allow auth and pubsub plugin to use Elixir module --- src/ejabberd.erl | 19 ++++++++++++++++++- src/ejabberd_auth.erl | 4 ++-- src/mod_pubsub.erl | 15 +++++++-------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/ejabberd.erl b/src/ejabberd.erl index 4edab98f1..1d2d77907 100644 --- a/src/ejabberd.erl +++ b/src/ejabberd.erl @@ -37,7 +37,7 @@ -protocol({xep, 270, '1.0'}). -export([start/0, stop/0, start_app/1, start_app/2, - get_pid_file/0, check_app/1]). + get_pid_file/0, check_app/1, module_name/1]). -include("logger.hrl"). @@ -148,3 +148,20 @@ get_module_file(App, Mod) -> Dir -> filename:join([Dir, BaseName ++ ".beam"]) end. + +module_name([Dir, _, <> | _] = Mod) when H >= 65, H =< 90 -> + Module = str:join([elixir_name(M) || M<-tl(Mod)], <<>>), + Prefix = case elixir_name(Dir) of + <<"Ejabberd">> -> <<"Elixir.Ejabberd.">>; + Lib -> <<"Elixir.Ejabberd.", Lib/binary, ".">> + end, + misc:binary_to_atom(<>); +module_name([<<"ejabberd">> | _] = Mod) -> + misc:binary_to_atom(str:join(Mod,$_)); +module_name(Mod) when is_list(Mod) -> + misc:binary_to_atom(str:join(tl(Mod),$_)). + +elixir_name(<>) when H >= 65, H =< 90 -> + <>; +elixir_name(<>) -> + <<(H-32), T/binary>>. diff --git a/src/ejabberd_auth.erl b/src/ejabberd_auth.erl index 4ab82a4e3..f49bef43b 100644 --- a/src/ejabberd_auth.erl +++ b/src/ejabberd_auth.erl @@ -735,8 +735,8 @@ auth_modules(Server) -> LServer = jid:nameprep(Server), Default = ejabberd_config:default_db(LServer, ?MODULE), Methods = ejabberd_config:get_option({auth_method, LServer}, [Default]), - [misc:binary_to_atom(<<"ejabberd_auth_", - (misc:atom_to_binary(M))/binary>>) + [ejabberd:module_name([<<"ejabberd">>, <<"auth">>, + misc:atom_to_binary(M)]) || M <- Methods]. -spec match_passwords(password(), password(), diff --git a/src/mod_pubsub.erl b/src/mod_pubsub.erl index e84d727eb..877c884c5 100644 --- a/src/mod_pubsub.erl +++ b/src/mod_pubsub.erl @@ -3380,11 +3380,11 @@ tree(Host) -> tree(_Host, <<"virtual">>) -> nodetree_virtual; % special case, virtual does not use any backend tree(Host, Name) -> - submodule(Host, <<"nodetree_", Name/binary>>). + submodule(Host, <<"nodetree">>, Name). -spec plugin(host(), binary()) -> atom(). plugin(Host, Name) -> - submodule(Host, <<"node_", Name/binary>>). + submodule(Host, <<"node">>, Name). -spec plugins(host()) -> [binary()]. plugins(Host) -> @@ -3396,14 +3396,13 @@ plugins(Host) -> -spec subscription_plugin(host()) -> atom(). subscription_plugin(Host) -> - submodule(Host, <<"pubsub_subscription">>). + submodule(Host, <<"pubsub">>, <<"subscription">>). --spec submodule(host(), binary()) -> atom(). -submodule(Host, Name) -> +-spec submodule(host(), binary(), binary()) -> atom(). +submodule(Host, Type, Name) -> case gen_mod:db_type(serverhost(Host), ?MODULE) of - mnesia -> misc:binary_to_atom(Name); - Type -> misc:binary_to_atom(<>) + mnesia -> ejabberd:module_name([<<"pubsub">>, Type, Name]); + Db -> ejabberd:module_name([<<"pubsub">>, Type, Name, Db]) end. -spec config(binary(), any()) -> any().