2015-03-11 14:14:28 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ext_mod.erl
|
|
|
|
%%% Author : Christophe Romain <christophe.romain@process-one.net>
|
|
|
|
%%% Purpose : external modules management
|
|
|
|
%%% Created : 19 Feb 2015 by Christophe Romain <christophe.romain@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2023-01-09 17:09:06 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2006-2023 ProcessOne
|
2015-03-11 14:14:28 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ext_mod).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-behaviour(gen_server).
|
2015-03-11 14:14:28 +01:00
|
|
|
-author("Christophe Romain <christophe.romain@process-one.net>").
|
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-export([start_link/0, update/0, check/1,
|
2016-12-07 09:27:21 +01:00
|
|
|
available_command/0, available/0, available/1,
|
|
|
|
installed_command/0, installed/0, installed/1,
|
2019-04-03 12:50:28 +02:00
|
|
|
install/1, uninstall/1, upgrade/0, upgrade/1, add_paths/0,
|
2018-05-22 16:16:13 +02:00
|
|
|
add_sources/1, add_sources/2, del_sources/1, modules_dir/0,
|
2023-05-17 17:09:09 +02:00
|
|
|
install_contrib_modules/2,
|
2019-06-14 11:33:26 +02:00
|
|
|
config_dir/0, get_commands_spec/0]).
|
2019-11-07 08:47:11 +01:00
|
|
|
-export([modules_configs/0, module_ebin_dir/1]).
|
2016-12-07 09:27:21 +01:00
|
|
|
-export([compile_erlang_file/2, compile_elixir_file/2]).
|
2022-08-05 16:10:25 +02:00
|
|
|
-export([web_menu_node/3, web_page_node/5, get_page/3]).
|
2015-03-11 14:14:28 +01:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
|
2015-03-11 14:14:28 +01:00
|
|
|
-include("ejabberd_commands.hrl").
|
2022-07-21 13:04:55 +02:00
|
|
|
-include("ejabberd_web_admin.hrl").
|
2015-10-23 17:21:19 +02:00
|
|
|
-include("logger.hrl").
|
2022-07-21 13:04:55 +02:00
|
|
|
-include("translate.hrl").
|
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2015-03-11 14:14:28 +01:00
|
|
|
|
2023-10-04 19:38:56 +02:00
|
|
|
-define(REPOS, "git@github.com:processone/ejabberd-contrib.git").
|
2015-03-11 14:14:28 +01:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
-record(state, {}).
|
|
|
|
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
2015-03-11 14:14:28 +01:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
init([]) ->
|
|
|
|
process_flag(trap_exit, true),
|
2019-04-03 12:50:28 +02:00
|
|
|
add_paths(),
|
2018-04-23 17:40:44 +02:00
|
|
|
application:start(inets),
|
|
|
|
inets:start(httpc, [{profile, ext_mod}]),
|
2017-02-26 13:10:59 +01:00
|
|
|
ejabberd_commands:register_commands(get_commands_spec()),
|
2022-07-21 13:04:55 +02:00
|
|
|
ejabberd_hooks:add(webadmin_menu_node, ?MODULE, web_menu_node, 50),
|
|
|
|
ejabberd_hooks:add(webadmin_page_node, ?MODULE, web_page_node, 50),
|
2017-02-26 13:10:59 +01:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
2019-04-03 12:50:28 +02:00
|
|
|
add_paths() ->
|
2023-01-25 13:25:56 +01:00
|
|
|
[code:add_pathsz([module_ebin_dir(Module)|module_deps_dirs(Module)])
|
2019-04-03 12:50:28 +02:00
|
|
|
|| {Module, _} <- installed()].
|
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2015-03-11 14:14:28 +01:00
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
2017-02-26 13:10:59 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_info(Info, State) ->
|
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2017-02-26 13:10:59 +01:00
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
2022-07-21 13:04:55 +02:00
|
|
|
ejabberd_hooks:delete(webadmin_menu_node, ?MODULE, web_menu_node, 50),
|
|
|
|
ejabberd_hooks:delete(webadmin_page_node, ?MODULE, web_page_node, 50),
|
2016-01-26 10:00:11 +01:00
|
|
|
ejabberd_commands:unregister_commands(get_commands_spec()).
|
2015-03-11 14:14:28 +01:00
|
|
|
|
2017-02-26 13:10:59 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%% -- ejabberd commands
|
2016-01-26 10:00:11 +01:00
|
|
|
get_commands_spec() ->
|
2015-03-13 11:54:32 +01:00
|
|
|
[#ejabberd_commands{name = modules_update_specs,
|
2021-07-30 00:58:08 +02:00
|
|
|
tags = [modules],
|
2017-07-21 11:18:44 +02:00
|
|
|
desc = "Update the module source code from Git",
|
|
|
|
longdesc = "A connection to Internet is required",
|
2015-03-11 14:14:28 +01:00
|
|
|
module = ?MODULE, function = update,
|
|
|
|
args = [],
|
2017-06-20 14:45:31 +02:00
|
|
|
result = {res, rescode}},
|
2015-03-13 11:54:32 +01:00
|
|
|
#ejabberd_commands{name = modules_available,
|
2021-07-30 00:58:08 +02:00
|
|
|
tags = [modules],
|
2017-07-21 11:18:44 +02:00
|
|
|
desc = "List the contributed modules available to install",
|
2015-03-11 14:14:28 +01:00
|
|
|
module = ?MODULE, function = available_command,
|
2017-07-21 11:18:44 +02:00
|
|
|
result_desc = "List of tuples with module name and description",
|
|
|
|
result_example = [{mod_cron, "Execute scheduled commands"},
|
|
|
|
{mod_rest, "ReST frontend"}],
|
2015-03-11 14:14:28 +01:00
|
|
|
args = [],
|
|
|
|
result = {modules, {list,
|
|
|
|
{module, {tuple,
|
|
|
|
[{name, atom},
|
|
|
|
{summary, string}]}}}}},
|
2015-03-13 11:54:32 +01:00
|
|
|
#ejabberd_commands{name = modules_installed,
|
2021-07-30 00:58:08 +02:00
|
|
|
tags = [modules],
|
2017-07-21 11:18:44 +02:00
|
|
|
desc = "List the contributed modules already installed",
|
2015-03-11 14:14:28 +01:00
|
|
|
module = ?MODULE, function = installed_command,
|
2017-07-21 11:18:44 +02:00
|
|
|
result_desc = "List of tuples with module name and description",
|
|
|
|
result_example = [{mod_cron, "Execute scheduled commands"},
|
|
|
|
{mod_rest, "ReST frontend"}],
|
2015-03-11 14:14:28 +01:00
|
|
|
args = [],
|
|
|
|
result = {modules, {list,
|
|
|
|
{module, {tuple,
|
|
|
|
[{name, atom},
|
|
|
|
{summary, string}]}}}}},
|
2015-03-13 11:54:32 +01:00
|
|
|
#ejabberd_commands{name = module_install,
|
2021-07-30 00:58:08 +02:00
|
|
|
tags = [modules],
|
2017-07-21 11:18:44 +02:00
|
|
|
desc = "Compile, install and start an available contributed module",
|
2015-03-11 14:14:28 +01:00
|
|
|
module = ?MODULE, function = install,
|
2017-07-21 11:42:03 +02:00
|
|
|
args_desc = ["Module name"],
|
|
|
|
args_example = [<<"mod_rest">>],
|
2015-03-11 14:14:28 +01:00
|
|
|
args = [{module, binary}],
|
2017-06-20 14:45:31 +02:00
|
|
|
result = {res, rescode}},
|
2015-03-13 11:54:32 +01:00
|
|
|
#ejabberd_commands{name = module_uninstall,
|
2021-07-30 00:58:08 +02:00
|
|
|
tags = [modules],
|
2017-07-21 11:18:44 +02:00
|
|
|
desc = "Uninstall a contributed module",
|
2015-03-11 14:14:28 +01:00
|
|
|
module = ?MODULE, function = uninstall,
|
2017-07-21 11:42:03 +02:00
|
|
|
args_desc = ["Module name"],
|
|
|
|
args_example = [<<"mod_rest">>],
|
2015-03-11 14:14:28 +01:00
|
|
|
args = [{module, binary}],
|
2017-06-20 14:45:31 +02:00
|
|
|
result = {res, rescode}},
|
2015-03-13 11:54:32 +01:00
|
|
|
#ejabberd_commands{name = module_upgrade,
|
2021-07-30 00:58:08 +02:00
|
|
|
tags = [modules],
|
2017-07-21 11:18:44 +02:00
|
|
|
desc = "Upgrade the running code of an installed module",
|
|
|
|
longdesc = "In practice, this uninstalls and installs the module",
|
2015-03-11 14:14:28 +01:00
|
|
|
module = ?MODULE, function = upgrade,
|
2017-07-21 11:42:03 +02:00
|
|
|
args_desc = ["Module name"],
|
|
|
|
args_example = [<<"mod_rest">>],
|
2015-03-11 14:14:28 +01:00
|
|
|
args = [{module, binary}],
|
2017-06-20 14:45:31 +02:00
|
|
|
result = {res, rescode}},
|
2015-03-13 11:54:32 +01:00
|
|
|
#ejabberd_commands{name = module_check,
|
2021-07-30 00:58:08 +02:00
|
|
|
tags = [modules],
|
2017-07-21 11:18:44 +02:00
|
|
|
desc = "Check the contributed module repository compliance",
|
2015-03-11 14:14:28 +01:00
|
|
|
module = ?MODULE, function = check,
|
2017-07-21 11:42:03 +02:00
|
|
|
args_desc = ["Module name"],
|
|
|
|
args_example = [<<"mod_rest">>],
|
2015-03-11 14:14:28 +01:00
|
|
|
args = [{module, binary}],
|
2017-06-20 14:45:31 +02:00
|
|
|
result = {res, rescode}}
|
2015-03-11 14:14:28 +01:00
|
|
|
].
|
|
|
|
%% -- public modules functions
|
|
|
|
|
|
|
|
update() ->
|
2017-08-01 13:33:14 +02:00
|
|
|
Contrib = maps:put(?REPOS, [], maps:new()),
|
|
|
|
Jungles = lists:foldl(fun({Package, Spec}, Acc) ->
|
|
|
|
Repo = proplists:get_value(url, Spec, ""),
|
|
|
|
Mods = maps:get(Repo, Acc, []),
|
|
|
|
maps:put(Repo, [Package|Mods], Acc)
|
|
|
|
end, Contrib, modules_spec(sources_dir(), "*/*")),
|
|
|
|
Repos = maps:fold(fun(Repo, _Mods, Acc) ->
|
|
|
|
Update = add_sources(Repo),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Update packages from repo ~ts: ~p", [Repo, Update]),
|
2017-08-01 13:33:14 +02:00
|
|
|
case Update of
|
|
|
|
ok -> Acc;
|
|
|
|
Error -> [{repository, Repo, Error}|Acc]
|
|
|
|
end
|
|
|
|
end, [], Jungles),
|
2015-10-23 17:21:19 +02:00
|
|
|
Res = lists:foldl(fun({Package, Spec}, Acc) ->
|
2017-08-01 13:33:14 +02:00
|
|
|
Repo = proplists:get_value(url, Spec, ""),
|
|
|
|
Update = add_sources(Package, Repo),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Update package ~ts: ~p", [Package, Update]),
|
2015-10-23 17:21:19 +02:00
|
|
|
case Update of
|
|
|
|
ok -> Acc;
|
2017-08-01 13:33:14 +02:00
|
|
|
Error -> [{Package, Repo, Error}|Acc]
|
2015-10-23 17:21:19 +02:00
|
|
|
end
|
2017-08-01 13:33:14 +02:00
|
|
|
end, Repos, modules_spec(sources_dir(), "*")),
|
2015-10-23 17:21:19 +02:00
|
|
|
case Res of
|
|
|
|
[] -> ok;
|
|
|
|
[Error|_] -> Error
|
|
|
|
end.
|
2015-03-11 14:14:28 +01:00
|
|
|
|
|
|
|
available() ->
|
|
|
|
Jungle = modules_spec(sources_dir(), "*/*"),
|
|
|
|
Standalone = modules_spec(sources_dir(), "*"),
|
|
|
|
lists:keysort(1,
|
|
|
|
lists:foldl(fun({Key, Val}, Acc) ->
|
|
|
|
lists:keystore(Key, 1, Acc, {Key, Val})
|
|
|
|
end, Jungle, Standalone)).
|
|
|
|
available(Module) when is_atom(Module) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
available(misc:atom_to_binary(Module));
|
2015-03-11 14:14:28 +01:00
|
|
|
available(Package) when is_binary(Package) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
Available = [misc:atom_to_binary(K) || K<-proplists:get_keys(available())],
|
2015-03-11 14:14:28 +01:00
|
|
|
lists:member(Package, Available).
|
|
|
|
|
|
|
|
available_command() ->
|
|
|
|
[short_spec(Item) || Item <- available()].
|
|
|
|
|
|
|
|
installed() ->
|
|
|
|
modules_spec(modules_dir(), "*").
|
|
|
|
installed(Module) when is_atom(Module) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
installed(misc:atom_to_binary(Module));
|
2015-03-11 14:14:28 +01:00
|
|
|
installed(Package) when is_binary(Package) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
Installed = [misc:atom_to_binary(K) || K<-proplists:get_keys(installed())],
|
2015-03-11 14:14:28 +01:00
|
|
|
lists:member(Package, Installed).
|
|
|
|
|
|
|
|
installed_command() ->
|
|
|
|
[short_spec(Item) || Item <- installed()].
|
|
|
|
|
|
|
|
install(Module) when is_atom(Module) ->
|
2023-05-17 17:09:09 +02:00
|
|
|
install(misc:atom_to_binary(Module), undefined);
|
2015-03-11 14:14:28 +01:00
|
|
|
install(Package) when is_binary(Package) ->
|
2023-05-17 17:09:09 +02:00
|
|
|
install(Package, undefined).
|
|
|
|
|
|
|
|
install(Package, Config) when is_binary(Package) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
Spec = [S || {Mod, S} <- available(), misc:atom_to_binary(Mod)==Package],
|
2023-05-17 17:09:09 +02:00
|
|
|
case {Spec, installed(Package), is_contrib_allowed(Config)} of
|
2015-03-11 14:14:28 +01:00
|
|
|
{_, _, false} ->
|
|
|
|
{error, not_allowed};
|
|
|
|
{[], _, _} ->
|
|
|
|
{error, not_available};
|
|
|
|
{_, true, _} ->
|
|
|
|
{error, conflict};
|
|
|
|
{[Attrs], _, _} ->
|
2017-04-11 12:13:58 +02:00
|
|
|
Module = misc:binary_to_atom(Package),
|
2023-05-17 17:09:09 +02:00
|
|
|
case compile_and_install(Module, Attrs, Config) of
|
2015-03-11 14:14:28 +01:00
|
|
|
ok ->
|
2023-01-25 13:25:56 +01:00
|
|
|
code:add_pathsz([module_ebin_dir(Module)|module_deps_dirs(Module)]),
|
2023-05-17 17:09:09 +02:00
|
|
|
ejabberd_config_reload(Config),
|
2022-07-21 13:04:55 +02:00
|
|
|
copy_commit_json(Package, Attrs),
|
2023-07-13 13:32:24 +02:00
|
|
|
ModuleRuntime = get_runtime_module_name(Module),
|
|
|
|
case erlang:function_exported(ModuleRuntime, post_install, 0) of
|
|
|
|
true -> ModuleRuntime:post_install();
|
2016-11-28 17:15:57 +01:00
|
|
|
_ -> ok
|
|
|
|
end;
|
2015-03-11 14:14:28 +01:00
|
|
|
Error ->
|
|
|
|
delete_path(module_lib_dir(Module)),
|
|
|
|
Error
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2023-05-17 17:09:09 +02:00
|
|
|
ejabberd_config_reload(Config) when is_list(Config) ->
|
|
|
|
%% Don't reload config when ejabberd is starting
|
|
|
|
%% because it will be reloaded after installing
|
|
|
|
%% all the external modules from install_contrib_modules
|
|
|
|
ok;
|
|
|
|
ejabberd_config_reload(undefined) ->
|
|
|
|
ejabberd_config:reload().
|
|
|
|
|
2015-03-11 14:14:28 +01:00
|
|
|
uninstall(Module) when is_atom(Module) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
uninstall(misc:atom_to_binary(Module));
|
2015-03-11 14:14:28 +01:00
|
|
|
uninstall(Package) when is_binary(Package) ->
|
|
|
|
case installed(Package) of
|
|
|
|
true ->
|
2017-04-11 12:13:58 +02:00
|
|
|
Module = misc:binary_to_atom(Package),
|
2023-07-13 13:32:24 +02:00
|
|
|
ModuleRuntime = get_runtime_module_name(Module),
|
|
|
|
case erlang:function_exported(ModuleRuntime, pre_uninstall, 0) of
|
|
|
|
true -> ModuleRuntime:pre_uninstall();
|
2016-11-28 17:15:57 +01:00
|
|
|
_ -> ok
|
|
|
|
end,
|
2023-07-13 13:32:24 +02:00
|
|
|
[catch gen_mod:stop_module(Host, ModuleRuntime)
|
2019-06-14 11:33:26 +02:00
|
|
|
|| Host <- ejabberd_option:hosts()],
|
2023-07-13 13:32:24 +02:00
|
|
|
code:purge(ModuleRuntime),
|
|
|
|
code:delete(ModuleRuntime),
|
2023-01-25 13:25:56 +01:00
|
|
|
[code:del_path(PathDelete) || PathDelete <- [module_ebin_dir(Module)|module_deps_dirs(Module)]],
|
2015-07-24 15:09:57 +02:00
|
|
|
delete_path(module_lib_dir(Module)),
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_config:reload();
|
2015-03-11 14:14:28 +01:00
|
|
|
false ->
|
|
|
|
{error, not_installed}
|
|
|
|
end.
|
|
|
|
|
|
|
|
upgrade() ->
|
|
|
|
[{Package, upgrade(Package)} || {Package, _Spec} <- installed()].
|
|
|
|
upgrade(Module) when is_atom(Module) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
upgrade(misc:atom_to_binary(Module));
|
2015-03-11 14:14:28 +01:00
|
|
|
upgrade(Package) when is_binary(Package) ->
|
|
|
|
uninstall(Package),
|
|
|
|
install(Package).
|
|
|
|
|
|
|
|
add_sources(Path) when is_list(Path) ->
|
2015-03-13 17:59:19 +01:00
|
|
|
add_sources(iolist_to_binary(module_name(Path)), Path).
|
2015-03-11 14:14:28 +01:00
|
|
|
add_sources(_, "") ->
|
|
|
|
{error, no_url};
|
|
|
|
add_sources(Module, Path) when is_atom(Module), is_list(Path) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
add_sources(misc:atom_to_binary(Module), Path);
|
2015-03-11 14:14:28 +01:00
|
|
|
add_sources(Package, Path) when is_binary(Package), is_list(Path) ->
|
|
|
|
DestDir = sources_dir(),
|
|
|
|
RepDir = filename:join(DestDir, module_name(Path)),
|
2023-10-04 19:39:57 +02:00
|
|
|
delete_path(RepDir, binary_to_list(Package)),
|
2015-03-11 14:14:28 +01:00
|
|
|
case filelib:ensure_dir(RepDir) of
|
|
|
|
ok ->
|
|
|
|
case {string:left(Path, 4), string:right(Path, 2)} of
|
|
|
|
{"http", "ip"} -> extract(zip, geturl(Path), DestDir);
|
|
|
|
{"http", "gz"} -> extract(tar, geturl(Path), DestDir);
|
|
|
|
{"http", _} -> extract_url(Path, DestDir);
|
|
|
|
{"git@", _} -> extract_github_master(Path, DestDir);
|
|
|
|
{_, "ip"} -> extract(zip, Path, DestDir);
|
|
|
|
{_, "gz"} -> extract(tar, Path, DestDir);
|
|
|
|
_ -> {error, unsupported_source}
|
|
|
|
end;
|
|
|
|
Error ->
|
|
|
|
Error
|
|
|
|
end.
|
|
|
|
|
|
|
|
del_sources(Module) when is_atom(Module) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
del_sources(misc:atom_to_binary(Module));
|
2015-03-11 14:14:28 +01:00
|
|
|
del_sources(Package) when is_binary(Package) ->
|
|
|
|
case uninstall(Package) of
|
|
|
|
ok ->
|
2017-04-11 12:13:58 +02:00
|
|
|
SrcDir = module_src_dir(misc:binary_to_atom(Package)),
|
2015-03-11 14:14:28 +01:00
|
|
|
delete_path(SrcDir);
|
|
|
|
Error ->
|
|
|
|
Error
|
|
|
|
end.
|
|
|
|
|
|
|
|
check(Module) when is_atom(Module) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
check(misc:atom_to_binary(Module));
|
2015-03-11 14:14:28 +01:00
|
|
|
check(Package) when is_binary(Package) ->
|
|
|
|
case {available(Package), installed(Package)} of
|
|
|
|
{false, _} ->
|
|
|
|
{error, not_available};
|
|
|
|
{_, false} ->
|
|
|
|
Status = install(Package),
|
|
|
|
uninstall(Package),
|
|
|
|
case Status of
|
2017-04-11 12:13:58 +02:00
|
|
|
ok -> check_sources(misc:binary_to_atom(Package));
|
2015-03-11 14:14:28 +01:00
|
|
|
Error -> Error
|
|
|
|
end;
|
|
|
|
_ ->
|
2017-04-11 12:13:58 +02:00
|
|
|
check_sources(misc:binary_to_atom(Package))
|
2015-03-11 14:14:28 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% -- archives and variables functions
|
|
|
|
|
|
|
|
geturl(Url) ->
|
2018-04-23 17:40:44 +02:00
|
|
|
case getenv("PROXY_SERVER", "", ":") of
|
|
|
|
[H, Port] ->
|
|
|
|
httpc:set_options([{proxy, {{H, list_to_integer(Port)}, []}}], ext_mod);
|
|
|
|
[H] ->
|
|
|
|
httpc:set_options([{proxy, {{H, 8080}, []}}], ext_mod);
|
|
|
|
_ ->
|
|
|
|
ok
|
2015-03-11 14:14:28 +01:00
|
|
|
end,
|
2020-10-29 16:36:18 +01:00
|
|
|
User = case getenv("PROXY_USER", "", ":") of
|
2018-04-23 17:40:44 +02:00
|
|
|
[U, Pass] -> [{proxy_auth, {U, Pass}}];
|
2015-03-11 14:14:28 +01:00
|
|
|
_ -> []
|
|
|
|
end,
|
2022-07-21 13:04:55 +02:00
|
|
|
UA = {"User-Agent", "ejabberd/ext_mod"},
|
|
|
|
case httpc:request(get, {Url, [UA]}, User, [{body_format, binary}], ext_mod) of
|
2018-04-23 17:40:44 +02:00
|
|
|
{ok, {{_, 200, _}, Headers, Response}} ->
|
2015-03-11 14:14:28 +01:00
|
|
|
{ok, Headers, Response};
|
2023-10-04 19:39:27 +02:00
|
|
|
{ok, {{_, 403, Reason}, _Headers, _Response}} ->
|
|
|
|
{error, Reason};
|
2018-04-23 17:40:44 +02:00
|
|
|
{ok, {{_, Code, _}, _Headers, Response}} ->
|
2015-03-11 14:14:28 +01:00
|
|
|
{error, {Code, Response}};
|
|
|
|
{error, Reason} ->
|
|
|
|
{error, Reason}
|
|
|
|
end.
|
|
|
|
|
|
|
|
getenv(Env) ->
|
|
|
|
getenv(Env, "").
|
|
|
|
getenv(Env, Default) ->
|
|
|
|
case os:getenv(Env) of
|
|
|
|
false -> Default;
|
|
|
|
"" -> Default;
|
|
|
|
Value -> Value
|
|
|
|
end.
|
|
|
|
getenv(Env, Default, Separator) ->
|
|
|
|
string:tokens(getenv(Env, Default), Separator).
|
|
|
|
|
|
|
|
extract(zip, {ok, _, Body}, DestDir) ->
|
|
|
|
extract(zip, iolist_to_binary(Body), DestDir);
|
|
|
|
extract(tar, {ok, _, Body}, DestDir) ->
|
|
|
|
extract(tar, {binary, iolist_to_binary(Body)}, DestDir);
|
|
|
|
extract(_, {error, Reason}, _) ->
|
|
|
|
{error, Reason};
|
|
|
|
extract(zip, Zip, DestDir) ->
|
|
|
|
case zip:extract(Zip, [{cwd, DestDir}]) of
|
|
|
|
{ok, _} -> ok;
|
|
|
|
Error -> Error
|
|
|
|
end;
|
|
|
|
extract(tar, Tar, DestDir) ->
|
2015-03-11 17:15:42 +01:00
|
|
|
erl_tar:extract(Tar, [compressed, {cwd, DestDir}]).
|
2015-03-11 14:14:28 +01:00
|
|
|
|
|
|
|
extract_url(Path, DestDir) ->
|
|
|
|
hd([extract_github_master(Path, DestDir) || string:str(Path, "github") > 0]
|
|
|
|
++[{error, unsupported_source}]).
|
|
|
|
|
|
|
|
extract_github_master(Repos, DestDir) ->
|
2015-04-29 10:19:15 +02:00
|
|
|
Base = case string:tokens(Repos, ":") of
|
|
|
|
["git@github.com", T1] -> "https://github.com/"++T1;
|
|
|
|
_ -> Repos
|
|
|
|
end,
|
|
|
|
Url = case lists:reverse(Base) of
|
|
|
|
[$t,$i,$g,$.|T2] -> lists:reverse(T2);
|
|
|
|
_ -> Base
|
|
|
|
end,
|
|
|
|
case extract(zip, geturl(Url++"/archive/master.zip"), DestDir) of
|
2015-03-11 14:14:28 +01:00
|
|
|
ok ->
|
|
|
|
RepDir = filename:join(DestDir, module_name(Repos)),
|
2023-10-04 19:39:57 +02:00
|
|
|
RepDirSpec = filename:join(DestDir, module_spec_name(RepDir)),
|
|
|
|
file:rename(RepDir++"-master", RepDirSpec),
|
|
|
|
maybe_write_commit_json(Url, RepDirSpec);
|
2015-03-11 14:14:28 +01:00
|
|
|
Error ->
|
|
|
|
Error
|
|
|
|
end.
|
|
|
|
|
2015-04-29 13:30:03 +02:00
|
|
|
copy(From, To) ->
|
|
|
|
case filelib:is_dir(From) of
|
|
|
|
true ->
|
|
|
|
Copy = fun(F) ->
|
|
|
|
SubFrom = filename:join(From, F),
|
|
|
|
SubTo = filename:join(To, F),
|
|
|
|
copy(SubFrom, SubTo)
|
|
|
|
end,
|
2016-12-07 09:27:21 +01:00
|
|
|
lists:foldl(fun(ok, ok) -> ok;
|
|
|
|
(ok, Error) -> Error;
|
2015-04-29 13:30:03 +02:00
|
|
|
(Error, _) -> Error
|
2016-12-07 09:27:21 +01:00
|
|
|
end, ok,
|
2015-04-29 13:30:03 +02:00
|
|
|
[Copy(filename:basename(X)) || X<-filelib:wildcard(From++"/*")]);
|
|
|
|
false ->
|
|
|
|
filelib:ensure_dir(To),
|
2016-12-07 09:27:21 +01:00
|
|
|
case file:copy(From, To) of
|
|
|
|
{ok, _} -> ok;
|
|
|
|
Error -> Error
|
|
|
|
end
|
2015-04-29 13:30:03 +02:00
|
|
|
end.
|
2015-03-11 14:14:28 +01:00
|
|
|
|
|
|
|
delete_path(Path) ->
|
|
|
|
case filelib:is_dir(Path) of
|
|
|
|
true ->
|
|
|
|
[delete_path(SubPath) || SubPath <- filelib:wildcard(Path++"/*")],
|
|
|
|
file:del_dir(Path);
|
|
|
|
false ->
|
|
|
|
file:delete(Path)
|
|
|
|
end.
|
|
|
|
|
2023-10-04 19:39:57 +02:00
|
|
|
delete_path(Path, Package) ->
|
|
|
|
delete_path(filename:join(filename:dirname(Path), Package)).
|
|
|
|
|
2015-03-11 14:14:28 +01:00
|
|
|
modules_dir() ->
|
|
|
|
DefaultDir = filename:join(getenv("HOME"), ".ejabberd-modules"),
|
|
|
|
getenv("CONTRIB_MODULES_PATH", DefaultDir).
|
|
|
|
|
|
|
|
sources_dir() ->
|
|
|
|
filename:join(modules_dir(), "sources").
|
|
|
|
|
2015-09-22 12:49:55 +02:00
|
|
|
config_dir() ->
|
|
|
|
DefaultDir = filename:join(modules_dir(), "conf"),
|
|
|
|
getenv("CONTRIB_MODULES_CONF_DIR", DefaultDir).
|
|
|
|
|
2019-11-07 08:47:11 +01:00
|
|
|
-spec modules_configs() -> [binary()].
|
|
|
|
modules_configs() ->
|
|
|
|
Fs = [{filename:rootname(filename:basename(F)), F}
|
|
|
|
|| F <- filelib:wildcard(config_dir() ++ "/*.{yml,yaml}")
|
|
|
|
++ filelib:wildcard(modules_dir() ++ "/*/conf/*.{yml,yaml}")],
|
|
|
|
[unicode:characters_to_binary(proplists:get_value(F, Fs))
|
|
|
|
|| F <- proplists:get_keys(Fs)].
|
|
|
|
|
2015-03-11 14:14:28 +01:00
|
|
|
module_lib_dir(Package) ->
|
|
|
|
filename:join(modules_dir(), Package).
|
|
|
|
|
|
|
|
module_ebin_dir(Package) ->
|
|
|
|
filename:join(module_lib_dir(Package), "ebin").
|
|
|
|
|
|
|
|
module_src_dir(Package) ->
|
|
|
|
Rep = module_name(Package),
|
|
|
|
SrcDir = sources_dir(),
|
|
|
|
Standalone = filelib:wildcard(Rep, SrcDir),
|
|
|
|
Jungle = filelib:wildcard("*/"++Rep, SrcDir),
|
|
|
|
case Standalone++Jungle of
|
|
|
|
[RepDir|_] -> filename:join(SrcDir, RepDir);
|
|
|
|
_ -> filename:join(SrcDir, Rep)
|
|
|
|
end.
|
|
|
|
|
|
|
|
module_name(Id) ->
|
|
|
|
filename:basename(filename:rootname(Id)).
|
|
|
|
|
2023-10-04 19:39:57 +02:00
|
|
|
module_spec_name(Path) ->
|
|
|
|
case filelib:wildcard(filename:join(Path++"-master", "*.spec")) of
|
|
|
|
"" ->
|
|
|
|
module_name(Path);
|
|
|
|
ModuleName ->
|
|
|
|
filename:basename(ModuleName, ".spec")
|
|
|
|
end.
|
|
|
|
|
2015-03-11 14:14:28 +01:00
|
|
|
module(Id) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
misc:binary_to_atom(iolist_to_binary(module_name(Id))).
|
2015-03-11 14:14:28 +01:00
|
|
|
|
|
|
|
module_spec(Spec) ->
|
|
|
|
[{path, filename:dirname(Spec)}
|
|
|
|
| case consult(Spec) of
|
|
|
|
{ok, Meta} -> Meta;
|
|
|
|
_ -> []
|
|
|
|
end].
|
|
|
|
|
|
|
|
modules_spec(Dir, Path) ->
|
|
|
|
Wildcard = filename:join(Path, "*.spec"),
|
|
|
|
lists:sort(
|
|
|
|
[{module(Match), module_spec(filename:join(Dir, Match))}
|
|
|
|
|| Match <- filelib:wildcard(Wildcard, Dir)]).
|
|
|
|
|
|
|
|
short_spec({Module, Attrs}) when is_atom(Module), is_list(Attrs) ->
|
|
|
|
{Module, proplists:get_value(summary, Attrs, "")}.
|
|
|
|
|
2023-05-17 17:09:09 +02:00
|
|
|
is_contrib_allowed(Config) when is_list(Config) ->
|
|
|
|
case lists:keyfind(allow_contrib_modules, 1, Config) of
|
|
|
|
false -> true;
|
|
|
|
{_, false} -> false;
|
|
|
|
{_, true} -> true
|
|
|
|
end;
|
|
|
|
is_contrib_allowed(undefined) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:allow_contrib_modules().
|
2015-03-11 14:14:28 +01:00
|
|
|
|
|
|
|
%% -- build functions
|
|
|
|
|
|
|
|
check_sources(Module) ->
|
|
|
|
SrcDir = module_src_dir(Module),
|
|
|
|
SpecFile = filename:flatten([Module, ".spec"]),
|
|
|
|
{ok, Dir} = file:get_cwd(),
|
|
|
|
file:set_cwd(SrcDir),
|
|
|
|
HaveSrc = case filelib:is_dir("src") or filelib:is_dir("lib") of
|
|
|
|
true -> [];
|
|
|
|
false -> [{missing, "src (Erlang) or lib (Elixir) sources directory"}]
|
|
|
|
end,
|
|
|
|
DirCheck = lists:foldl(
|
|
|
|
fun({Type, Name}, Acc) ->
|
|
|
|
case filelib:Type(Name) of
|
|
|
|
true -> Acc;
|
|
|
|
false -> [{missing, Name}|Acc]
|
|
|
|
end
|
|
|
|
end, HaveSrc, [{is_file, "README.txt"},
|
|
|
|
{is_file, "COPYING"},
|
|
|
|
{is_file, SpecFile}]),
|
|
|
|
SpecCheck = case consult(SpecFile) of
|
|
|
|
{ok, Spec} ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(Key, Acc) ->
|
|
|
|
case lists:keysearch(Key, 1, Spec) of
|
|
|
|
false -> [{missing_meta, Key}|Acc];
|
|
|
|
{value, {Key, [_NoEmpty|_]}} -> Acc;
|
|
|
|
{value, {Key, Val}} -> [{invalid_meta, {Key, Val}}|Acc]
|
|
|
|
end
|
|
|
|
end, [], [author, summary, home, url]);
|
|
|
|
{error, Error} ->
|
|
|
|
[{invalid_spec, Error}]
|
|
|
|
end,
|
|
|
|
file:set_cwd(Dir),
|
|
|
|
Result = DirCheck ++ SpecCheck,
|
|
|
|
case Result of
|
|
|
|
[] -> ok;
|
|
|
|
_ -> {error, Result}
|
|
|
|
end.
|
|
|
|
|
2023-05-17 17:09:09 +02:00
|
|
|
compile_and_install(Module, Spec, Config) ->
|
2015-03-11 14:14:28 +01:00
|
|
|
SrcDir = module_src_dir(Module),
|
|
|
|
LibDir = module_lib_dir(Module),
|
|
|
|
case filelib:is_dir(SrcDir) of
|
|
|
|
true ->
|
2016-12-07 09:27:21 +01:00
|
|
|
case compile_deps(SrcDir) of
|
2015-07-22 10:48:44 +02:00
|
|
|
ok ->
|
2016-12-07 09:27:21 +01:00
|
|
|
case compile(SrcDir) of
|
2023-05-17 17:09:09 +02:00
|
|
|
ok -> install(Module, Spec, SrcDir, LibDir, Config);
|
2015-07-22 10:48:44 +02:00
|
|
|
Error -> Error
|
|
|
|
end;
|
|
|
|
Error ->
|
|
|
|
Error
|
2016-12-07 09:27:21 +01:00
|
|
|
end;
|
2015-03-11 14:14:28 +01:00
|
|
|
false ->
|
|
|
|
Path = proplists:get_value(url, Spec, ""),
|
|
|
|
case add_sources(Module, Path) of
|
2023-05-17 17:09:09 +02:00
|
|
|
ok -> compile_and_install(Module, Spec, Config);
|
2015-03-11 14:14:28 +01:00
|
|
|
Error -> Error
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2016-12-07 09:27:21 +01:00
|
|
|
compile_deps(LibDir) ->
|
|
|
|
Deps = filename:join(LibDir, "deps"),
|
|
|
|
case filelib:is_dir(Deps) of
|
|
|
|
true -> ok; % assume deps are included
|
|
|
|
false -> fetch_rebar_deps(LibDir)
|
2015-07-22 10:48:44 +02:00
|
|
|
end,
|
2016-12-07 09:27:21 +01:00
|
|
|
Rs = [compile(Dep) || Dep <- filelib:wildcard(filename:join(Deps, "*"))],
|
|
|
|
compile_result(Rs).
|
|
|
|
|
|
|
|
compile(LibDir) ->
|
|
|
|
Bin = filename:join(LibDir, "ebin"),
|
|
|
|
Inc = filename:join(LibDir, "include"),
|
|
|
|
Lib = filename:join(LibDir, "lib"),
|
|
|
|
Src = filename:join(LibDir, "src"),
|
|
|
|
Options = [{outdir, Bin}, {i, Inc} | compile_options()],
|
|
|
|
filelib:ensure_dir(filename:join(Bin, ".")),
|
|
|
|
[copy(App, Bin) || App <- filelib:wildcard(Src++"/*.app")],
|
2022-12-30 14:14:50 +01:00
|
|
|
compile_c_files(LibDir),
|
2016-12-07 09:27:21 +01:00
|
|
|
Er = [compile_erlang_file(Bin, File, Options)
|
|
|
|
|| File <- filelib:wildcard(Src++"/*.erl")],
|
|
|
|
Ex = [compile_elixir_file(Bin, File)
|
|
|
|
|| File <- filelib:wildcard(Lib ++ "/*.ex")],
|
|
|
|
compile_result(Er++Ex).
|
|
|
|
|
2022-12-30 14:14:50 +01:00
|
|
|
compile_c_files(LibDir) ->
|
|
|
|
case file:read_file_info(filename:join(LibDir, "c_src/Makefile")) of
|
|
|
|
{ok, _} ->
|
|
|
|
os:cmd("cd "++LibDir++"; make -C c_src");
|
|
|
|
{error, _} ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2016-12-07 09:27:21 +01:00
|
|
|
compile_result(Results) ->
|
2015-07-22 10:48:44 +02:00
|
|
|
case lists:dropwhile(
|
2016-12-07 09:27:21 +01:00
|
|
|
fun({ok, _}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end, Results) of
|
2015-07-22 10:48:44 +02:00
|
|
|
[] -> ok;
|
|
|
|
[Error|_] -> Error
|
|
|
|
end.
|
|
|
|
|
2021-02-15 11:31:08 +01:00
|
|
|
maybe_define_lager_macro() ->
|
|
|
|
case list_to_integer(erlang:system_info(otp_release)) < 22 of
|
|
|
|
true -> [{d, 'LAGER'}];
|
|
|
|
false -> []
|
|
|
|
end.
|
|
|
|
|
2016-12-07 09:27:21 +01:00
|
|
|
compile_options() ->
|
2021-01-07 15:10:39 +01:00
|
|
|
[verbose, report_errors, report_warnings, debug_info, ?ALL_DEFS]
|
2021-02-15 11:31:08 +01:00
|
|
|
++ maybe_define_lager_macro()
|
2017-04-07 12:09:43 +02:00
|
|
|
++ [{i, filename:join(app_dir(App), "include")}
|
2017-08-01 13:33:14 +02:00
|
|
|
|| App <- [fast_xml, xmpp, p1_utils, ejabberd]]
|
|
|
|
++ [{i, filename:join(mod_dir(Mod), "include")}
|
|
|
|
|| Mod <- installed()].
|
2016-12-07 09:27:21 +01:00
|
|
|
|
2017-04-07 12:09:43 +02:00
|
|
|
app_dir(App) ->
|
|
|
|
case code:lib_dir(App) of
|
|
|
|
{error, bad_name} ->
|
|
|
|
case code:which(App) of
|
|
|
|
Beam when is_list(Beam) ->
|
|
|
|
filename:dirname(filename:dirname(Beam));
|
|
|
|
_ ->
|
|
|
|
"."
|
|
|
|
end;
|
|
|
|
Dir ->
|
|
|
|
Dir
|
|
|
|
end.
|
|
|
|
|
2017-08-01 13:33:14 +02:00
|
|
|
mod_dir({Package, Spec}) ->
|
|
|
|
Default = filename:join(modules_dir(), Package),
|
|
|
|
proplists:get_value(path, Spec, Default).
|
|
|
|
|
2016-12-07 09:27:21 +01:00
|
|
|
compile_erlang_file(Dest, File) ->
|
|
|
|
compile_erlang_file(Dest, File, compile_options()).
|
|
|
|
|
|
|
|
compile_erlang_file(Dest, File, ErlOptions) ->
|
|
|
|
Options = [{outdir, Dest} | ErlOptions],
|
|
|
|
case compile:file(File, Options) of
|
|
|
|
{ok, Module} -> {ok, Module};
|
|
|
|
{ok, Module, _} -> {ok, Module};
|
|
|
|
{ok, Module, _, _} -> {ok, Module};
|
|
|
|
error -> {error, {compilation_failed, File}};
|
|
|
|
{error, E, W} -> {error, {compilation_failed, File, E, W}}
|
2015-03-11 14:14:28 +01:00
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-ifdef(ELIXIR_ENABLED).
|
2016-07-05 12:36:49 +02:00
|
|
|
compile_elixir_file(Dest, File) when is_list(Dest) and is_list(File) ->
|
|
|
|
compile_elixir_file(list_to_binary(Dest), list_to_binary(File));
|
|
|
|
|
|
|
|
compile_elixir_file(Dest, File) ->
|
|
|
|
try 'Elixir.Kernel.ParallelCompiler':files_to_path([File], Dest, []) of
|
|
|
|
[Module] -> {ok, Module}
|
|
|
|
catch
|
2016-12-07 09:27:21 +01:00
|
|
|
_ -> {error, {compilation_failed, File}}
|
2016-07-05 12:36:49 +02:00
|
|
|
end.
|
2019-06-14 11:33:26 +02:00
|
|
|
-else.
|
|
|
|
compile_elixir_file(_, File) ->
|
|
|
|
{error, {compilation_failed, File}}.
|
|
|
|
-endif.
|
2016-07-05 12:36:49 +02:00
|
|
|
|
2023-05-17 17:09:09 +02:00
|
|
|
install(Module, Spec, SrcDir, LibDir, Config) ->
|
2016-12-07 09:27:21 +01:00
|
|
|
{ok, CurDir} = file:get_cwd(),
|
|
|
|
file:set_cwd(SrcDir),
|
2017-03-13 11:26:58 +01:00
|
|
|
Files1 = [{File, copy(File, filename:join(LibDir, File))}
|
|
|
|
|| File <- filelib:wildcard("{ebin,priv,conf,include}/**")],
|
|
|
|
Files2 = [{File, copy(File, filename:join(LibDir, filename:join(lists:nthtail(2,filename:split(File)))))}
|
2023-01-25 13:25:56 +01:00
|
|
|
|| File <- filelib:wildcard("deps/*/ebin/**")],
|
|
|
|
Files3 = [{File, copy(File, filename:join(LibDir, File))}
|
|
|
|
|| File <- filelib:wildcard("deps/*/priv/**")],
|
2016-12-07 09:27:21 +01:00
|
|
|
Errors = lists:dropwhile(fun({_, ok}) -> true;
|
2015-04-29 13:30:03 +02:00
|
|
|
(_) -> false
|
2023-01-25 13:25:56 +01:00
|
|
|
end, Files1++Files2++Files3),
|
2023-05-17 17:09:09 +02:00
|
|
|
inform_module_configuration(Module, LibDir, Files1, Config),
|
2015-04-29 13:30:03 +02:00
|
|
|
Result = case Errors of
|
|
|
|
[{F, {error, E}}|_] ->
|
|
|
|
{error, {F, E}};
|
|
|
|
[] ->
|
|
|
|
SpecPath = proplists:get_value(path, Spec),
|
|
|
|
SpecFile = filename:flatten([Module, ".spec"]),
|
2016-12-07 09:27:21 +01:00
|
|
|
copy(filename:join(SpecPath, SpecFile), filename:join(LibDir, SpecFile))
|
2015-04-29 13:30:03 +02:00
|
|
|
end,
|
2016-12-07 09:27:21 +01:00
|
|
|
file:set_cwd(CurDir),
|
|
|
|
Result.
|
2015-03-11 14:14:28 +01:00
|
|
|
|
2023-05-17 17:09:09 +02:00
|
|
|
inform_module_configuration(Module, LibDir, Files1, Config) ->
|
2021-07-01 13:17:00 +02:00
|
|
|
Res = lists:filter(fun({[$c, $o, $n, $f |_], ok}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end, Files1),
|
2023-05-17 17:09:09 +02:00
|
|
|
AlreadyConfigured = lists:keymember(Module, 1, get_modules(Config)),
|
2023-05-23 13:48:49 +02:00
|
|
|
case {Res, AlreadyConfigured} of
|
|
|
|
{[{ConfigPath, ok}], false} ->
|
2021-07-01 13:17:00 +02:00
|
|
|
FullConfigPath = filename:join(LibDir, ConfigPath),
|
|
|
|
io:format("Module ~p has been installed and started.~n"
|
|
|
|
"It's configured in the file:~n ~s~n"
|
|
|
|
"Configure the module in that file, or remove it~n"
|
|
|
|
"and configure in your main ejabberd.yml~n",
|
|
|
|
[Module, FullConfigPath]);
|
2023-05-23 13:48:49 +02:00
|
|
|
{[{ConfigPath, ok}], true} ->
|
|
|
|
FullConfigPath = filename:join(LibDir, ConfigPath),
|
|
|
|
file:rename(FullConfigPath, FullConfigPath++".example"),
|
|
|
|
io:format("Module ~p has been installed and started.~n"
|
|
|
|
"The ~p configuration in your ejabberd.yml is used.~n",
|
|
|
|
[Module, Module]);
|
|
|
|
{[], _} ->
|
2021-07-01 13:17:00 +02:00
|
|
|
io:format("Module ~p has been installed.~n"
|
|
|
|
"Now you can configure it in your ejabberd.yml~n",
|
|
|
|
[Module])
|
|
|
|
end.
|
|
|
|
|
2023-05-17 17:09:09 +02:00
|
|
|
get_modules(Config) when is_list(Config) ->
|
|
|
|
{modules, Modules} = lists:keyfind(modules, 1, Config),
|
|
|
|
Modules;
|
|
|
|
get_modules(undefined) ->
|
|
|
|
ejabberd_config:get_option(modules).
|
|
|
|
|
2015-07-22 10:48:44 +02:00
|
|
|
%% -- minimalist rebar spec parser, only support git
|
|
|
|
|
2016-12-07 09:27:21 +01:00
|
|
|
fetch_rebar_deps(SrcDir) ->
|
|
|
|
case rebar_deps(filename:join(SrcDir, "rebar.config"))
|
|
|
|
++ rebar_deps(filename:join(SrcDir, "rebar.config.script")) of
|
2015-07-22 10:48:44 +02:00
|
|
|
[] ->
|
|
|
|
ok;
|
|
|
|
Deps ->
|
2016-12-07 09:27:21 +01:00
|
|
|
{ok, CurDir} = file:get_cwd(),
|
|
|
|
file:set_cwd(SrcDir),
|
2015-07-22 10:48:44 +02:00
|
|
|
filelib:ensure_dir(filename:join("deps", ".")),
|
|
|
|
lists:foreach(fun({_App, Cmd}) ->
|
|
|
|
os:cmd("cd deps; "++Cmd++"; cd ..")
|
2016-12-07 09:27:21 +01:00
|
|
|
end, Deps),
|
|
|
|
file:set_cwd(CurDir)
|
2015-07-22 10:48:44 +02:00
|
|
|
end.
|
2016-12-07 09:27:21 +01:00
|
|
|
|
2015-07-22 10:48:44 +02:00
|
|
|
rebar_deps(Script) ->
|
|
|
|
case file:script(Script) of
|
|
|
|
{ok, Config} when is_list(Config) ->
|
|
|
|
[rebar_dep(Dep) || Dep <- proplists:get_value(deps, Config, [])];
|
|
|
|
{ok, {deps, Deps}} ->
|
|
|
|
[rebar_dep(Dep) || Dep <- Deps];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
rebar_dep({App, _, {git, Url}}) ->
|
|
|
|
{App, "git clone "++Url++" "++filename:basename(App)};
|
|
|
|
rebar_dep({App, _, {git, Url, {branch, Ref}}}) ->
|
|
|
|
{App, "git clone -n "++Url++" "++filename:basename(App)++
|
|
|
|
"; (cd "++filename:basename(App)++
|
|
|
|
"; git checkout -q origin/"++Ref++")"};
|
|
|
|
rebar_dep({App, _, {git, Url, {tag, Ref}}}) ->
|
|
|
|
{App, "git clone -n "++Url++" "++filename:basename(App)++
|
|
|
|
"; (cd "++filename:basename(App)++
|
|
|
|
"; git checkout -q "++Ref++")"};
|
|
|
|
rebar_dep({App, _, {git, Url, Ref}}) ->
|
|
|
|
{App, "git clone -n "++Url++" "++filename:basename(App)++
|
|
|
|
"; (cd "++filename:basename(App)++
|
|
|
|
"; git checkout -q "++Ref++")"}.
|
|
|
|
|
2023-01-25 13:25:56 +01:00
|
|
|
module_deps_dirs(Module) ->
|
|
|
|
SrcDir = module_src_dir(Module),
|
|
|
|
LibDir = module_lib_dir(Module),
|
|
|
|
DepsDir = filename:join(LibDir, "deps"),
|
|
|
|
Deps = rebar_deps(filename:join(SrcDir, "rebar.config"))
|
|
|
|
++ rebar_deps(filename:join(SrcDir, "rebar.config.script")),
|
|
|
|
[filename:join(DepsDir, App) || {App, _Cmd} <- Deps].
|
|
|
|
|
2015-03-11 14:14:28 +01:00
|
|
|
%% -- YAML spec parser
|
|
|
|
|
|
|
|
consult(File) ->
|
2016-02-03 11:30:48 +01:00
|
|
|
case fast_yaml:decode_from_file(File, [plain_as_atom]) of
|
2015-03-11 14:14:28 +01:00
|
|
|
{ok, []} -> {ok, []};
|
|
|
|
{ok, [Doc|_]} -> {ok, [format(Spec) || Spec <- Doc]};
|
2016-02-03 11:30:48 +01:00
|
|
|
{error, Err} -> {error, fast_yaml:format_error(Err)}
|
2015-03-11 14:14:28 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
format({Key, Val}) when is_binary(Val) ->
|
|
|
|
{Key, binary_to_list(Val)};
|
|
|
|
format({Key, Val}) -> % TODO: improve Yaml parsing
|
|
|
|
{Key, Val}.
|
2022-07-21 13:04:55 +02:00
|
|
|
|
|
|
|
%% -- COMMIT.json
|
|
|
|
|
2022-07-27 12:40:07 +02:00
|
|
|
maybe_write_commit_json(Url, RepDir) ->
|
|
|
|
case (os:getenv("GITHUB_ACTIONS") == "true") of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
write_commit_json(Url, RepDir)
|
|
|
|
end.
|
|
|
|
|
2022-07-21 13:04:55 +02:00
|
|
|
write_commit_json(Url, RepDir) ->
|
|
|
|
Url2 = string_replace(Url, "https://github.com", "https://api.github.com/repos"),
|
|
|
|
BranchUrl = lists:flatten(Url2 ++ "/branches/master"),
|
2023-10-04 19:39:27 +02:00
|
|
|
case geturl(BranchUrl) of
|
|
|
|
{ok, _Headers, Body} ->
|
|
|
|
{ok, F} = file:open(filename:join(RepDir, "COMMIT.json"), [raw, write]),
|
|
|
|
file:write(F, Body),
|
|
|
|
file:close(F);
|
|
|
|
{error, Reason} ->
|
|
|
|
Reason
|
|
|
|
end.
|
2022-07-21 13:04:55 +02:00
|
|
|
|
|
|
|
find_commit_json(Attrs) ->
|
|
|
|
{_, FromPath} = lists:keyfind(path, 1, Attrs),
|
|
|
|
case {find_commit_json_path(FromPath),
|
|
|
|
find_commit_json_path(filename:join(FromPath, ".."))}
|
|
|
|
of
|
|
|
|
{{ok, FromFile}, _} ->
|
|
|
|
FromFile;
|
|
|
|
{_, {ok, FromFile}} ->
|
2022-07-27 12:29:06 +02:00
|
|
|
FromFile;
|
|
|
|
_ ->
|
|
|
|
not_found
|
2022-07-21 13:04:55 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-ifdef(HAVE_URI_STRING). %% Erlang/OTP 20 or higher can use this:
|
|
|
|
string_replace(Subject, Pattern, Replacement) ->
|
|
|
|
string:replace(Subject, Pattern, Replacement).
|
|
|
|
|
|
|
|
find_commit_json_path(Path) ->
|
|
|
|
filelib:find_file("COMMIT.json", Path).
|
|
|
|
-else. % Workaround for Erlang/OTP older than 20:
|
|
|
|
string_replace(Subject, Pattern, Replacement) ->
|
|
|
|
B = binary:replace(list_to_binary(Subject),
|
|
|
|
list_to_binary(Pattern),
|
|
|
|
list_to_binary(Replacement)),
|
|
|
|
binary_to_list(B).
|
|
|
|
|
|
|
|
find_commit_json_path(Path) ->
|
|
|
|
case filelib:wildcard("COMMIT.json", Path) of
|
|
|
|
[] ->
|
|
|
|
{error, commit_json_not_found};
|
|
|
|
["COMMIT.json"] = File ->
|
|
|
|
{ok, filename:join(Path, File)}
|
|
|
|
end.
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
copy_commit_json(Package, Attrs) ->
|
|
|
|
DestPath = module_lib_dir(Package),
|
2022-07-27 12:29:06 +02:00
|
|
|
case find_commit_json(Attrs) of
|
|
|
|
not_found ->
|
|
|
|
ok;
|
|
|
|
FromFile ->
|
|
|
|
file:copy(FromFile, filename:join(DestPath, "COMMIT.json"))
|
|
|
|
end.
|
2022-07-21 13:04:55 +02:00
|
|
|
|
|
|
|
get_commit_details(Dirname) ->
|
|
|
|
RepDir = filename:join(sources_dir(), Dirname),
|
|
|
|
get_commit_details2(filename:join(RepDir, "COMMIT.json")).
|
|
|
|
|
|
|
|
get_commit_details2(Path) ->
|
|
|
|
case file:read_file(Path) of
|
|
|
|
{ok, Body} ->
|
|
|
|
parse_details(Body);
|
|
|
|
_ ->
|
2022-08-04 17:09:44 +02:00
|
|
|
#{sha => unknown_sha,
|
2022-07-21 13:04:55 +02:00
|
|
|
date => <<>>,
|
|
|
|
message => <<>>,
|
|
|
|
html => <<>>,
|
2022-08-04 17:09:44 +02:00
|
|
|
author_name => <<>>,
|
2022-07-21 13:04:55 +02:00
|
|
|
commit_html_url => <<>>}
|
|
|
|
end.
|
|
|
|
|
|
|
|
parse_details(Body) ->
|
|
|
|
{Contents} = jiffy:decode(Body),
|
|
|
|
|
|
|
|
{_, {Commit}} = lists:keyfind(<<"commit">>, 1, Contents),
|
|
|
|
{_, Sha} = lists:keyfind(<<"sha">>, 1, Commit),
|
|
|
|
{_, CommitHtmlUrl} = lists:keyfind(<<"html_url">>, 1, Commit),
|
|
|
|
|
|
|
|
{_, {Commit2}} = lists:keyfind(<<"commit">>, 1, Commit),
|
|
|
|
{_, Message} = lists:keyfind(<<"message">>, 1, Commit2),
|
|
|
|
{_, {Author}} = lists:keyfind(<<"author">>, 1, Commit2),
|
|
|
|
{_, AuthorName} = lists:keyfind(<<"name">>, 1, Author),
|
|
|
|
{_, {Committer}} = lists:keyfind(<<"committer">>, 1, Commit2),
|
|
|
|
{_, Date} = lists:keyfind(<<"date">>, 1, Committer),
|
|
|
|
|
|
|
|
{_, {Links}} = lists:keyfind(<<"_links">>, 1, Contents),
|
|
|
|
{_, Html} = lists:keyfind(<<"html">>, 1, Links),
|
|
|
|
|
|
|
|
#{sha => Sha,
|
|
|
|
date => Date,
|
|
|
|
message => Message,
|
|
|
|
html => Html,
|
|
|
|
author_name => AuthorName,
|
|
|
|
commit_html_url => CommitHtmlUrl}.
|
|
|
|
|
|
|
|
%% -- Web Admin
|
|
|
|
|
|
|
|
-define(AXC(URL, Attributes, Text),
|
|
|
|
?XAE(<<"a">>, [{<<"href">>, URL} | Attributes], [?C(Text)])
|
|
|
|
).
|
|
|
|
|
|
|
|
-define(INPUTCHECKED(Type, Name, Value),
|
|
|
|
?XA(<<"input">>,
|
|
|
|
[{<<"type">>, Type},
|
|
|
|
{<<"name">>, Name},
|
|
|
|
{<<"disabled">>, <<"true">>},
|
|
|
|
{<<"checked">>, <<"true">>},
|
|
|
|
{<<"value">>, Value}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
).
|
|
|
|
|
|
|
|
web_menu_node(Acc, _Node, Lang) ->
|
|
|
|
Acc ++ [{<<"contrib">>, translate:translate(Lang, ?T("Contrib Modules"))}].
|
|
|
|
|
|
|
|
web_page_node(_, Node, [<<"contrib">>], Query, Lang) ->
|
2022-08-05 16:10:25 +02:00
|
|
|
Res = rpc:call(Node, ?MODULE, get_page, [Node, Query, Lang]),
|
|
|
|
{stop, Res};
|
|
|
|
web_page_node(Acc, _, _, _, _) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
get_page(Node, Query, Lang) ->
|
2022-07-21 13:04:55 +02:00
|
|
|
QueryRes = list_modules_parse_query(Query),
|
|
|
|
Title = ?H1GL(translate:translate(Lang, ?T("Contrib Modules")),
|
|
|
|
<<"../../developer/extending-ejabberd/modules/#ejabberd-contrib">>,
|
|
|
|
<<"ejabberd-contrib">>),
|
|
|
|
Contents = get_content(Node, Query, Lang),
|
|
|
|
Result = case QueryRes of
|
|
|
|
ok -> [?XREST(?T("Submitted"))];
|
|
|
|
nothing -> []
|
|
|
|
end,
|
2022-08-05 16:10:25 +02:00
|
|
|
Title ++ Result ++ Contents.
|
2022-07-21 13:04:55 +02:00
|
|
|
|
|
|
|
get_module_home(Module, Attrs) ->
|
|
|
|
case element(2, lists:keyfind(home, 1, Attrs)) of
|
|
|
|
"https://github.com/processone/ejabberd-contrib/tree/master/" = P1 ->
|
|
|
|
P1 ++ atom_to_list(Module);
|
|
|
|
Other ->
|
|
|
|
Other
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_module_summary(Attrs) ->
|
|
|
|
element(2, lists:keyfind(summary, 1, Attrs)).
|
|
|
|
|
|
|
|
get_module_author(Attrs) ->
|
|
|
|
element(2, lists:keyfind(author, 1, Attrs)).
|
|
|
|
|
|
|
|
get_installed_module_el({ModAtom, Attrs}, Lang) ->
|
|
|
|
Mod = misc:atom_to_binary(ModAtom),
|
|
|
|
Home = list_to_binary(get_module_home(ModAtom, Attrs)),
|
|
|
|
Summary = list_to_binary(get_module_summary(Attrs)),
|
|
|
|
Author = list_to_binary(get_module_author(Attrs)),
|
|
|
|
{_, FromPath} = lists:keyfind(path, 1, Attrs),
|
2022-08-04 17:09:44 +02:00
|
|
|
FromFile = case find_commit_json_path(FromPath) of
|
|
|
|
{ok, FF} -> FF;
|
|
|
|
{error, _} -> "dummypath"
|
|
|
|
end,
|
2022-07-21 13:04:55 +02:00
|
|
|
#{sha := CommitSha,
|
|
|
|
date := CommitDate,
|
|
|
|
message := CommitMessage,
|
|
|
|
author_name := CommitAuthorName,
|
|
|
|
commit_html_url := CommitHtmlUrl} = get_commit_details2(FromFile),
|
|
|
|
|
|
|
|
[SourceSpec] = [S || {M, S} <- available(), M == ModAtom],
|
|
|
|
SourceFile = find_commit_json(SourceSpec),
|
|
|
|
#{sha := SourceSha,
|
|
|
|
date := SourceDate,
|
|
|
|
message := SourceMessage,
|
|
|
|
author_name := SourceAuthorName,
|
|
|
|
commit_html_url := SourceHtmlUrl} = get_commit_details2(SourceFile),
|
|
|
|
|
|
|
|
UpgradeEls =
|
|
|
|
case CommitSha == SourceSha of
|
|
|
|
true ->
|
|
|
|
[];
|
|
|
|
false ->
|
|
|
|
SourceTitleEl = make_title_el(SourceDate, SourceMessage, SourceAuthorName),
|
|
|
|
[?XE(<<"td">>,
|
|
|
|
[?INPUT(<<"checkbox">>, <<"selected_upgrade">>, Mod),
|
|
|
|
?C(<<" ">>),
|
|
|
|
?AXC(SourceHtmlUrl, [SourceTitleEl], binary:part(SourceSha, {0, 8}))
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
end,
|
|
|
|
|
|
|
|
Started =
|
|
|
|
case gen_mod:is_loaded(hd(ejabberd_option:hosts()), ModAtom) of
|
|
|
|
false ->
|
2022-08-04 17:09:44 +02:00
|
|
|
[?C(<<" ">>)];
|
2022-07-21 13:04:55 +02:00
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
TitleEl = make_title_el(CommitDate, CommitMessage, CommitAuthorName),
|
2022-12-20 16:13:27 +01:00
|
|
|
Status = get_module_status_el(ModAtom),
|
2022-07-21 13:04:55 +02:00
|
|
|
HomeTitleEl = make_home_title_el(Summary, Author),
|
|
|
|
?XE(<<"tr">>,
|
|
|
|
[?XE(<<"td">>, [?AXC(Home, [HomeTitleEl], Mod)]),
|
|
|
|
?XE(<<"td">>,
|
|
|
|
[?INPUTTD(<<"checkbox">>, <<"selected_uninstall">>, Mod),
|
|
|
|
?C(<<" ">>),
|
2022-08-04 17:09:44 +02:00
|
|
|
get_commit_link(CommitHtmlUrl, TitleEl, CommitSha),
|
|
|
|
?C(<<" - ">>)]
|
2022-07-21 13:04:55 +02:00
|
|
|
++ Started
|
|
|
|
++ Status)
|
|
|
|
| UpgradeEls]).
|
|
|
|
|
2022-12-20 16:13:27 +01:00
|
|
|
get_module_status_el(ModAtom) ->
|
|
|
|
case {get_module_status(ModAtom),
|
|
|
|
get_module_status(elixir_module_name(ModAtom))} of
|
|
|
|
{Str, unknown} when is_list(Str) ->
|
|
|
|
[?C(<<" ">>), ?C(Str)];
|
|
|
|
{unknown, Str} when is_list(Str) ->
|
|
|
|
[?C(<<" ">>), ?C(Str)];
|
|
|
|
{unknown, unknown} ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_module_status(Module) ->
|
|
|
|
try Module:mod_status() of
|
|
|
|
Str when is_list(Str) ->
|
|
|
|
Str
|
|
|
|
catch
|
|
|
|
_:_ ->
|
|
|
|
unknown
|
|
|
|
end.
|
|
|
|
|
2023-07-13 13:32:24 +02:00
|
|
|
%% When a module named mod_whatever in ejabberd-modules
|
|
|
|
%% is written in Elixir, its runtime name is 'Elixir.ModWhatever'
|
|
|
|
get_runtime_module_name(Module) ->
|
|
|
|
case is_elixir_module(Module) of
|
|
|
|
true -> elixir_module_name(Module);
|
|
|
|
false -> Module
|
|
|
|
end.
|
|
|
|
|
|
|
|
is_elixir_module(Module) ->
|
|
|
|
LibDir = module_src_dir(Module),
|
|
|
|
Lib = filename:join(LibDir, "lib"),
|
|
|
|
Src = filename:join(LibDir, "src"),
|
|
|
|
case {filelib:wildcard(Lib++"/*.{ex}"),
|
|
|
|
filelib:wildcard(Src++"/*.{erl}")} of
|
|
|
|
{[_ | _], []} ->
|
|
|
|
true;
|
|
|
|
{[], [_ | _]} ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2022-12-20 16:13:27 +01:00
|
|
|
%% Converts mod_some_thing to Elixir.ModSomeThing
|
|
|
|
elixir_module_name(ModAtom) ->
|
|
|
|
list_to_atom("Elixir." ++ elixir_module_name("_" ++ atom_to_list(ModAtom), [])).
|
|
|
|
|
|
|
|
elixir_module_name([], Res) ->
|
|
|
|
lists:reverse(Res);
|
|
|
|
elixir_module_name([$_, Char | Remaining], Res) ->
|
|
|
|
[Upper] = uppercase([Char]),
|
|
|
|
elixir_module_name(Remaining, [Upper | Res]);
|
|
|
|
elixir_module_name([Char | Remaining], Res) ->
|
|
|
|
elixir_module_name(Remaining, [Char | Res]).
|
|
|
|
|
|
|
|
-ifdef(HAVE_URI_STRING).
|
|
|
|
uppercase(String) ->
|
|
|
|
string:uppercase(String). % OTP 20 or higher
|
|
|
|
-else.
|
|
|
|
uppercase(String) ->
|
|
|
|
string:to_upper(String). % OTP older than 20
|
|
|
|
-endif.
|
|
|
|
|
2022-07-21 13:04:55 +02:00
|
|
|
get_available_module_el({ModAtom, Attrs}) ->
|
|
|
|
Installed = installed(),
|
|
|
|
Mod = misc:atom_to_binary(ModAtom),
|
|
|
|
Home = list_to_binary(get_module_home(ModAtom, Attrs)),
|
|
|
|
Summary = list_to_binary(get_module_summary(Attrs)),
|
|
|
|
Author = list_to_binary(get_module_author(Attrs)),
|
|
|
|
HomeTitleEl = make_home_title_el(Summary, Author),
|
|
|
|
InstallCheckbox =
|
|
|
|
case lists:keymember(ModAtom, 1, Installed) of
|
|
|
|
false -> [?INPUT(<<"checkbox">>, <<"selected_install">>, Mod)];
|
|
|
|
true -> [?INPUTCHECKED(<<"checkbox">>, <<"selected_install">>, Mod)]
|
|
|
|
end,
|
|
|
|
?XE(<<"tr">>,
|
|
|
|
[?XE(<<"td">>, InstallCheckbox ++ [?C(<<" ">>), ?AXC(Home, [HomeTitleEl], Mod)]),
|
|
|
|
?XE(<<"td">>, [?C(Summary)])]).
|
|
|
|
|
|
|
|
get_installed_modules_table(Lang) ->
|
|
|
|
Modules = installed(),
|
|
|
|
Tail = [?XE(<<"tr">>,
|
|
|
|
[?XE(<<"td">>, []),
|
|
|
|
?XE(<<"td">>,
|
|
|
|
[?INPUTTD(<<"submit">>, <<"uninstall">>, ?T("Uninstall"))]
|
|
|
|
),
|
|
|
|
?XE(<<"td">>,
|
|
|
|
[?INPUTT(<<"submit">>, <<"upgrade">>, ?T("Upgrade"))]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
],
|
|
|
|
TBody = [get_installed_module_el(Module, Lang) || Module <- lists:sort(Modules)],
|
|
|
|
?XAE(<<"table">>,
|
|
|
|
[],
|
|
|
|
[?XE(<<"tbody">>, TBody ++ Tail)]
|
|
|
|
).
|
|
|
|
|
|
|
|
get_available_modules_table(Lang) ->
|
|
|
|
Modules = get_available_notinstalled(),
|
|
|
|
Tail = [?XE(<<"tr">>,
|
|
|
|
[?XE(<<"td">>,
|
|
|
|
[?INPUTT(<<"submit">>, <<"install">>, ?T("Install"))]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
],
|
|
|
|
TBody = [get_available_module_el(Module) || Module <- lists:sort(Modules)],
|
|
|
|
?XAE(<<"table">>,
|
|
|
|
[],
|
|
|
|
[?XE(<<"tbody">>, TBody ++ Tail)]
|
|
|
|
).
|
|
|
|
|
|
|
|
make_title_el(Date, Message, AuthorName) ->
|
|
|
|
LinkTitle = <<Message/binary, "\n", AuthorName/binary, "\n", Date/binary>>,
|
|
|
|
{<<"title">>, LinkTitle}.
|
|
|
|
|
|
|
|
make_home_title_el(Summary, Author) ->
|
|
|
|
LinkTitle = <<Summary/binary, "\n", Author/binary>>,
|
|
|
|
{<<"title">>, LinkTitle}.
|
|
|
|
|
2022-08-04 17:09:44 +02:00
|
|
|
get_commit_link(_CommitHtmlUrl, _TitleErl, unknown_sha) ->
|
|
|
|
?C(<<"Please Update Specs">>);
|
|
|
|
get_commit_link(CommitHtmlUrl, TitleEl, CommitSha) ->
|
|
|
|
?AXC(CommitHtmlUrl, [TitleEl], binary:part(CommitSha, {0, 8})).
|
|
|
|
|
2022-07-21 13:04:55 +02:00
|
|
|
get_content(Node, Query, Lang) ->
|
|
|
|
{{_CommandCtl}, _Res} =
|
|
|
|
case catch parse_and_execute(Query, Node) of
|
2022-10-12 11:41:14 +02:00
|
|
|
{'EXIT', _} -> {{""}, <<"">>};
|
2022-07-21 13:04:55 +02:00
|
|
|
Result_tuple -> Result_tuple
|
|
|
|
end,
|
|
|
|
|
|
|
|
AvailableModulesEls = get_available_modules_table(Lang),
|
|
|
|
InstalledModulesEls = get_installed_modules_table(Lang),
|
|
|
|
|
|
|
|
Sources = get_sources_list(),
|
|
|
|
SourceEls = (?XAE(<<"table">>,
|
|
|
|
[],
|
|
|
|
[?XE(<<"tbody">>,
|
|
|
|
(lists:map(
|
|
|
|
fun(Dirname) ->
|
|
|
|
#{sha := CommitSha,
|
|
|
|
date := CommitDate,
|
|
|
|
message := CommitMessage,
|
|
|
|
html := Html,
|
|
|
|
author_name := AuthorName,
|
|
|
|
commit_html_url := CommitHtmlUrl
|
|
|
|
} = get_commit_details(Dirname),
|
|
|
|
TitleEl = make_title_el(CommitDate, CommitMessage, AuthorName),
|
|
|
|
?XE(<<"tr">>,
|
|
|
|
[?XE(<<"td">>, [?AC(Html, Dirname)]),
|
|
|
|
?XE(<<"td">>,
|
2022-08-04 17:09:44 +02:00
|
|
|
[get_commit_link(CommitHtmlUrl, TitleEl, CommitSha)]
|
2022-07-21 13:04:55 +02:00
|
|
|
),
|
|
|
|
?XE(<<"td">>, [?C(CommitMessage)])
|
|
|
|
])
|
|
|
|
end,
|
|
|
|
lists:sort(Sources)
|
|
|
|
))
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)),
|
|
|
|
|
|
|
|
[?XC(<<"p">>,
|
|
|
|
translate:translate(
|
|
|
|
Lang, ?T("Update specs to get modules source, then install desired ones.")
|
|
|
|
)
|
|
|
|
),
|
|
|
|
?XAE(<<"form">>,
|
|
|
|
[{<<"method">>, <<"post">>}],
|
|
|
|
[?XCT(<<"h3">>, ?T("Sources Specs:")),
|
|
|
|
SourceEls,
|
|
|
|
?BR,
|
|
|
|
?INPUTT(<<"submit">>,
|
|
|
|
<<"updatespecs">>,
|
|
|
|
translate:translate(Lang, ?T("Update Specs"))),
|
|
|
|
|
|
|
|
?XCT(<<"h3">>, ?T("Installed Modules:")),
|
|
|
|
InstalledModulesEls,
|
|
|
|
?BR,
|
|
|
|
|
|
|
|
?XCT(<<"h3">>, ?T("Other Modules Available:")),
|
|
|
|
AvailableModulesEls
|
|
|
|
]
|
|
|
|
)
|
|
|
|
].
|
|
|
|
|
|
|
|
get_sources_list() ->
|
|
|
|
case file:list_dir(sources_dir()) of
|
|
|
|
{ok, Filenames} -> Filenames;
|
|
|
|
{error, enoent} -> []
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_available_notinstalled() ->
|
|
|
|
Installed = installed(),
|
|
|
|
lists:filter(
|
|
|
|
fun({Mod, _}) ->
|
|
|
|
not lists:keymember(Mod, 1, Installed)
|
|
|
|
end,
|
|
|
|
available()
|
|
|
|
).
|
|
|
|
|
|
|
|
parse_and_execute(Query, Node) ->
|
|
|
|
{[Exec], _} = lists:partition(
|
|
|
|
fun(ExType) ->
|
|
|
|
lists:keymember(ExType, 1, Query)
|
|
|
|
end,
|
|
|
|
[<<"updatespecs">>]
|
|
|
|
),
|
|
|
|
Commands = {get_val(<<"updatespecs">>, Query)},
|
|
|
|
{_, R} = parse1_command(Exec, Commands, Node),
|
|
|
|
{Commands, R}.
|
|
|
|
|
|
|
|
get_val(Val, Query) ->
|
|
|
|
{value, {_, R}} = lists:keysearch(Val, 1, Query),
|
|
|
|
binary_to_list(R).
|
|
|
|
|
|
|
|
parse1_command(<<"updatespecs">>, {_}, _Node) ->
|
|
|
|
Res = update(),
|
|
|
|
{oook, io_lib:format("~p", [Res])}.
|
|
|
|
|
|
|
|
list_modules_parse_query(Query) ->
|
|
|
|
case {lists:keysearch(<<"install">>, 1, Query),
|
|
|
|
lists:keysearch(<<"upgrade">>, 1, Query),
|
|
|
|
lists:keysearch(<<"uninstall">>, 1, Query)}
|
|
|
|
of
|
|
|
|
{{value, _}, _, _} -> list_modules_parse_install(Query);
|
|
|
|
{_, {value, _}, _} -> list_modules_parse_upgrade(Query);
|
|
|
|
{_, _, {value, _}} -> list_modules_parse_uninstall(Query);
|
|
|
|
_ -> nothing
|
|
|
|
end.
|
|
|
|
|
|
|
|
list_modules_parse_install(Query) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({Mod, _}) ->
|
|
|
|
ModBin = misc:atom_to_binary(Mod),
|
|
|
|
case lists:member({<<"selected_install">>, ModBin}, Query) of
|
|
|
|
true -> install(Mod);
|
|
|
|
_ -> ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
get_available_notinstalled()),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
list_modules_parse_upgrade(Query) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({Mod, _}) ->
|
|
|
|
ModBin = misc:atom_to_binary(Mod),
|
|
|
|
case lists:member({<<"selected_upgrade">>, ModBin}, Query) of
|
|
|
|
true -> upgrade(Mod);
|
|
|
|
_ -> ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
installed()),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
list_modules_parse_uninstall(Query) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({Mod, _}) ->
|
|
|
|
ModBin = misc:atom_to_binary(Mod),
|
|
|
|
case lists:member({<<"selected_uninstall">>, ModBin}, Query) of
|
|
|
|
true -> uninstall(Mod);
|
|
|
|
_ -> ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
installed()),
|
|
|
|
ok.
|
2023-05-17 17:09:09 +02:00
|
|
|
|
|
|
|
install_contrib_modules(Modules, Config) ->
|
|
|
|
lists:filter(fun(Module) ->
|
|
|
|
case install(misc:atom_to_binary(Module), Config) of
|
|
|
|
{error, conflict} ->
|
|
|
|
false;
|
|
|
|
ok ->
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Modules).
|