]> granicus.if.org Git - ejabberd/commitdiff
Add a way to get all ejabberd_commands, not only those that was registered
authorPaweł Chmielowski <pchmielowski@process-one.net>
Tue, 26 Jan 2016 09:00:11 +0000 (10:00 +0100)
committerPaweł Chmielowski <pchmielowski@process-one.net>
Tue, 26 Jan 2016 09:00:28 +0000 (10:00 +0100)
This is part of (TECH-1828).

src/ejabberd_admin.erl
src/ejabberd_commands.erl
src/ejabberd_commands_doc.erl
src/ejabberd_s2s.erl
src/ejabberd_sm.erl
src/ext_mod.erl
src/mod_admin_extra.erl
src/mod_mam.erl
src/mod_muc_admin.erl

index 5352ae1eee6abd648640742032cb269e9a7da441..4aa5faedbb76dd16a8e02c595d439d665051b976 100644 (file)
@@ -51,7 +51,8 @@
         install_fallback_mnesia/1,
         dump_to_textfile/1, dump_to_textfile/2,
         mnesia_change_nodename/4,
-        restore/1 % Still used by some modules
+        restore/1, % Still used by some modules
+        get_commands_spec/0
        ]).
 
 -include("ejabberd.hrl").
 -include("ejabberd_commands.hrl").
 
 start() ->
-    ejabberd_commands:register_commands(commands()).
+    ejabberd_commands:register_commands(get_commands_spec()).
 
 stop() ->
-    ejabberd_commands:unregister_commands(commands()).
+    ejabberd_commands:unregister_commands(get_commands_spec()).
 
 %%%
 %%% ejabberd commands
 %%%
 
-commands() ->
+get_commands_spec() ->
     [
      %% The commands status, stop and restart are implemented also in ejabberd_ctl
      %% They are defined here so that other interfaces can use them too
index 547da028d01e75f00178d5f8e37e7c3e3d656e75..265d7141fdd5dd7a938ea36021148c62d601e503 100644 (file)
         unregister_commands/1,
         execute_command/2,
          execute_command/4,
-         opt_type/1
+         opt_type/1,
+         get_commands_spec/0
        ]).
 
 -include("ejabberd_commands.hrl").
 
 -define(POLICY_ACCESS, '$policy').
 
-init() ->
-    ets:new(ejabberd_commands, [named_table, set, public,
-                               {keypos, #ejabberd_commands.name}]),
-    register_commands([
+get_commands_spec() ->
+    [
         #ejabberd_commands{name = gen_html_doc_for_commands, tags = [documentation],
                            desc = "Generates html documentation for ejabberd_commands",
                            module = ejabberd_commands_doc, function = generate_html_output,
@@ -259,7 +258,11 @@ init() ->
                                         "that will have example invocation include in markdown document"],
                            result_desc = "0 if command failed, 1 when succedded",
                            args_example = ["/home/me/docs/api.html", "mod_admin", "java,json"],
-                           result_example = ok}]).
+                           result_example = ok}].
+init() ->
+    ets:new(ejabberd_commands, [named_table, set, public,
+                               {keypos, #ejabberd_commands.name}]),
+    register_commands(get_commands_spec()).
 
 -spec register_commands([ejabberd_commands()]) -> ok.
 
index 999d4cd806c73900db433c2784288258f69353c7..3368e3dec91b89658a3f06784e706722689d7ce4 100644 (file)
@@ -5,7 +5,7 @@
 %%% Created : 20 May 2008 by Badlop <badlop@process-one.net>
 %%%
 %%%
-%%% ejabberd, Copyright (C) 2002-2016   ProcessOne
+%%% ejabberd, Copyright (C) 2002-2015   ProcessOne
 %%%
 %%% This program is free software; you can redistribute it and/or
 %%% modify it under the terms of the GNU General Public License as
@@ -373,10 +373,27 @@ gen_doc(#ejabberd_commands{name=Name, tags=_Tags, desc=Desc, longdesc=LongDesc,
      ?TAG(h2, <<"Examples:">>),
      gen_calls(Cmd, HTMLOutput, Langs)].
 
+find_commands_definitions() ->
+    case code:lib_dir(ejabberd, ebin) of
+        {error, _} ->
+            lists:map(fun({N, _, _}) ->
+                              ejabberd_commands:get_command_definition(N)
+                      end, ejabberd_commands:list_commands());
+        Path ->
+            lists:flatmap(fun(P) ->
+                                  Mod = list_to_atom(filename:rootname(P)),
+                                  code:ensure_loaded(Mod),
+                                  case erlang:function_exported(Mod, get_commands_spec, 0) of
+                                      true ->
+                                          apply(Mod, get_commands_spec, []);
+                                      _ ->
+                                          []
+                                  end
+                          end, filelib:wildcard("*.beam", Path))
+    end.
+
 generate_html_output(File, RegExp, Languages) ->
-    Cmds = lists:map(fun({N, _, _}) ->
-                             ejabberd_commands:get_command_definition(N)
-                     end, ejabberd_commands:list_commands()),
+    Cmds = find_commands_definitions(),
     {ok, RE} = re:compile(RegExp),
     Cmds2 = lists:filter(fun(#ejabberd_commands{name=Name, module=Module}) ->
                                  re:run(atom_to_list(Name), RE, [{capture, none}]) == match orelse
@@ -393,9 +410,7 @@ generate_html_output(File, RegExp, Languages) ->
     ok.
 
 generate_md_output(File, RegExp, Languages) ->
-    Cmds = lists:map(fun({N, _, _}) ->
-                             ejabberd_commands:get_command_definition(N)
-                     end, ejabberd_commands:list_commands()),
+    Cmds = find_commands_definitions(),
     {ok, RE} = re:compile(RegExp),
     Cmds2 = lists:filter(fun(#ejabberd_commands{name=Name, module=Module}) ->
                                  re:run(atom_to_list(Name), RE, [{capture, none}]) == match orelse
index 06cad6af69d7a0f0c0c570d18aa9764633ca0270..2f026b32adce429e52dd6b3874769bab1d37b4d5 100644 (file)
@@ -42,7 +42,8 @@
         clean_temporarily_blocked_table/0,
         list_temporarily_blocked_hosts/0,
         external_host_overloaded/1, is_temporarly_blocked/1,
-        check_peer_certificate/3]).
+        check_peer_certificate/3,
+        get_commands_spec/0]).
 
 %% gen_server callbacks
 -export([init/1, handle_call/3, handle_cast/2,
@@ -239,7 +240,7 @@ init([]) ->
                         {attributes, record_info(fields, s2s)}]),
     mnesia:add_table_copy(s2s, node(), ram_copies),
     mnesia:subscribe(system),
-    ejabberd_commands:register_commands(commands()),
+    ejabberd_commands:register_commands(get_commands_spec()),
     mnesia:create_table(temporarily_blocked,
                        [{ram_copies, [node()]},
                         {attributes, record_info(fields, temporarily_blocked)}]),
@@ -265,7 +266,7 @@ handle_info({route, From, To, Packet}, State) ->
 handle_info(_Info, State) -> {noreply, State}.
 
 terminate(_Reason, _State) ->
-    ejabberd_commands:unregister_commands(commands()),
+    ejabberd_commands:unregister_commands(get_commands_spec()),
     ok.
 
 code_change(_OldVsn, State, _Extra) ->
@@ -469,7 +470,7 @@ send_element(Pid, El) ->
 %%%----------------------------------------------------------------------
 %%% ejabberd commands
 
-commands() ->
+get_commands_spec() ->
     [#ejabberd_commands{name = incoming_s2s_number,
                        tags = [stats, s2s],
                        desc =
index ddc5697f2c467726c4d91770b06d550ea51ebebb..3045a417b1c14ca815754f72ee6796d97169ad44 100644 (file)
@@ -63,7 +63,8 @@
         get_user_ip/3,
         get_max_user_sessions/2,
         get_all_pids/0,
-        is_existing_resource/3
+        is_existing_resource/3,
+        get_commands_spec/0
        ]).
 
 -export([init/1, handle_call/3, handle_cast/2,
@@ -323,7 +324,7 @@ init([]) ->
              ejabberd_hooks:add(remove_user, Host,
                                 ejabberd_sm, disconnect_removed_user, 100)
       end, ?MYHOSTS),
-    ejabberd_commands:register_commands(commands()),
+    ejabberd_commands:register_commands(get_commands_spec()),
     {ok, #state{}}.
 
 handle_call(_Request, _From, State) ->
@@ -361,7 +362,7 @@ handle_info({unregister_iq_handler, Host, XMLNS},
 handle_info(_Info, State) -> {noreply, State}.
 
 terminate(_Reason, _State) ->
-    ejabberd_commands:unregister_commands(commands()),
+    ejabberd_commands:unregister_commands(get_commands_spec()),
     ok.
 
 code_change(_OldVsn, State, _Extra) -> {ok, State}.
@@ -723,7 +724,7 @@ get_sm_backend() ->
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%% ejabberd commands
 
-commands() ->
+get_commands_spec() ->
     [#ejabberd_commands{name = connected_users,
                        tags = [session],
                        desc = "List all established sessions",
index c6fbf50960b2f22336b28ae154b3cb439efd266f..46ece873c5467d85bdc10a0fd3ca79a52a8e471e 100644 (file)
@@ -33,7 +33,7 @@
         installed_command/0, installed/0, installed/1,
         install/1, uninstall/1, upgrade/0, upgrade/1,
         add_sources/2, del_sources/1, modules_dir/0,
-        config_dir/0, opt_type/1]).
+        config_dir/0, opt_type/1, get_commands_spec/0]).
 
 -include("ejabberd_commands.hrl").
 -include("logger.hrl").
@@ -46,12 +46,12 @@ start() ->
     [code:add_patha(module_ebin_dir(Module))
      || {Module, _} <- installed()],
     application:start(inets),
-    ejabberd_commands:register_commands(commands()).
+    ejabberd_commands:register_commands(get_commands_spec()).
 
 stop() ->
-    ejabberd_commands:unregister_commands(commands()).
+    ejabberd_commands:unregister_commands(get_commands_spec()).
 
-commands() ->
+get_commands_spec() ->
     [#ejabberd_commands{name = modules_update_specs,
                         tags = [admin,modules],
                         desc = "",
index fed2a037a782fabeb591522a92ca38676c532308..dae775e4712d3681edfab3976ad935249bd05563 100644 (file)
@@ -47,7 +47,7 @@
         srg_delete/2, srg_list/1, srg_get_info/2,
         srg_get_members/2, srg_user_add/4, srg_user_del/4,
         send_message/5, send_stanza/3, send_stanza_c2s/4, privacy_set/3,
-        stats/1, stats/2, mod_opt_type/1]).
+        stats/1, stats/2, mod_opt_type/1, get_commands_spec/0]).
 
 
 -include("ejabberd.hrl").
 %%%
 
 start(_Host, _Opts) ->
-    ejabberd_commands:register_commands(commands()).
+    ejabberd_commands:register_commands(get_commands_spec()).
 
 stop(_Host) ->
-    ejabberd_commands:unregister_commands(commands()).
+    ejabberd_commands:unregister_commands(get_commands_spec()).
 
 
 %%%
 %%% Register commands
 %%%
 
-commands() ->
+get_commands_spec() ->
     Vcard1FieldsString = "Some vcard field names in get/set_vcard are:\n"
        " FN            - Full Name\n"
        " NICKNAME      - Nickname\n"
index 88206716c32c31ce9949a523d4ed27a1067e7a7b..d6e4e93685e1819ee96777c08b8520ea53f39cb2 100644 (file)
@@ -36,7 +36,8 @@
 -export([user_send_packet/4, user_receive_packet/5,
         process_iq_v0_2/3, process_iq_v0_3/3, disco_sm_features/5,
         remove_user/2, remove_user/3, mod_opt_type/1, muc_process_iq/4,
-        muc_filter_message/5, message_is_archived/5, delete_old_messages/2]).
+        muc_filter_message/5, message_is_archived/5, delete_old_messages/2,
+        get_commands_spec/0]).
 
 -include_lib("stdlib/include/ms_transform.hrl").
 -include("jlib.hrl").
@@ -116,7 +117,7 @@ start(Host, Opts) ->
            ejabberd_hooks:add(message_is_archived, Host, ?MODULE,
                               message_is_archived, 50)
     end,
-    ejabberd_commands:register_commands(commands()),
+    ejabberd_commands:register_commands(get_commands_spec()),
     ok.
 
 init_db(mnesia, _Host) ->
@@ -172,7 +173,7 @@ stop(Host) ->
            ejabberd_hooks:delete(message_is_archived, Host, ?MODULE,
                                  message_is_archived, 50)
     end,
-    ejabberd_commands:unregister_commands(commands()),
+    ejabberd_commands:unregister_commands(get_commands_spec()),
     ok.
 
 remove_user(User, Server) ->
@@ -1364,7 +1365,7 @@ update(LServer, Table, Fields, Vals, Where) ->
 join([], _Sep) -> [];
 join([H | T], Sep) -> [H, [[Sep, X] || X <- T]].
 
-commands() ->
+get_commands_spec() ->
     [#ejabberd_commands{name = delete_old_mam_messages, tags = [purge],
                        desc = "Delete MAM messages older than DAYS",
                        longdesc = "Valid message TYPEs: "
index 603e96c65c39f9dace8eb3b2112bbaef2f3ce94a..c08757375d3a7568ce340bd2a241795e404382f1 100644 (file)
@@ -20,7 +20,7 @@
         change_room_option/4, get_room_options/2,
         set_room_affiliation/4, get_room_affiliations/2,
         web_menu_main/2, web_page_main/2, web_menu_host/3,
-        web_page_host/3, mod_opt_type/1]).
+        web_page_host/3, mod_opt_type/1, get_commands_spec/0]).
 
 -include("ejabberd.hrl").
 -include("logger.hrl").
 %%----------------------------
 
 start(Host, _Opts) ->
-    ejabberd_commands:register_commands(commands()),
+    ejabberd_commands:register_commands(get_commands_spec()),
     ejabberd_hooks:add(webadmin_menu_main, ?MODULE, web_menu_main, 50),
     ejabberd_hooks:add(webadmin_menu_host, Host, ?MODULE, web_menu_host, 50),
     ejabberd_hooks:add(webadmin_page_main, ?MODULE, web_page_main, 50),
     ejabberd_hooks:add(webadmin_page_host, Host, ?MODULE, web_page_host, 50).
 
 stop(Host) ->
-    ejabberd_commands:unregister_commands(commands()),
+    ejabberd_commands:unregister_commands(get_commands_spec()),
     ejabberd_hooks:delete(webadmin_menu_main, ?MODULE, web_menu_main, 50),
     ejabberd_hooks:delete(webadmin_menu_host, Host, ?MODULE, web_menu_host, 50),
     ejabberd_hooks:delete(webadmin_page_main, ?MODULE, web_page_main, 50),
@@ -53,7 +53,7 @@ stop(Host) ->
 %%% Register commands
 %%%
 
-commands() ->
+get_commands_spec() ->
     [
      #ejabberd_commands{name = muc_online_rooms, tags = [muc],
                       desc = "List existing rooms ('global' to get all vhosts)",