Improved mod_conversejs to support @HOST@, auto and conversejs_resources
Changes: - The options can use the @HOST@ keyword - default_domain option is @HOST@ by default, not the first defined vhost - New conversejs_resources option to serve converse.js files (no need for web server) - conversejs_* now support 'auto', which uses local or remote Converse files
This commit is contained in:
parent
8065ec831e
commit
f461bcb597
|
@ -50,15 +50,22 @@ reload(_Host, _NewOpts, _OldOpts) ->
|
||||||
depends(_Host, _Opts) ->
|
depends(_Host, _Opts) ->
|
||||||
[].
|
[].
|
||||||
|
|
||||||
process([], #request{method = 'GET'}) ->
|
process([], #request{method = 'GET', host = Host, raw_path = RawPath}) ->
|
||||||
Host = ejabberd_config:get_myname(),
|
DomainRaw = gen_mod:get_module_opt(Host, ?MODULE, default_domain),
|
||||||
Domain = gen_mod:get_module_opt(Host, ?MODULE, default_domain),
|
Domain = misc:expand_keyword(<<"@HOST@">>, DomainRaw, Host),
|
||||||
Script = gen_mod:get_module_opt(Host, ?MODULE, conversejs_script),
|
Script = get_file_url(Host, conversejs_script,
|
||||||
CSS = gen_mod:get_module_opt(Host, ?MODULE, conversejs_css),
|
<<RawPath/binary, "/converse.min.js">>,
|
||||||
|
<<"https://cdn.conversejs.org/dist/converse.min.js">>),
|
||||||
|
CSS = get_file_url(Host, conversejs_css,
|
||||||
|
<<RawPath/binary, "/converse.min.css">>,
|
||||||
|
<<"https://cdn.conversejs.org/dist/converse.min.css">>),
|
||||||
Init = [{<<"discover_connection_methods">>, false},
|
Init = [{<<"discover_connection_methods">>, false},
|
||||||
{<<"jid">>, Domain},
|
{<<"jid">>, Domain},
|
||||||
{<<"default_domain">>, Domain},
|
{<<"default_domain">>, Domain},
|
||||||
{<<"domain_placeholder">>, Domain},
|
{<<"domain_placeholder">>, Domain},
|
||||||
|
{<<"registration_domain">>, Domain},
|
||||||
|
{<<"assets_path">>, RawPath},
|
||||||
|
{<<"i18n">>, ejabberd_option:language(Host)},
|
||||||
{<<"view_mode">>, <<"fullscreen">>}],
|
{<<"view_mode">>, <<"fullscreen">>}],
|
||||||
Init2 =
|
Init2 =
|
||||||
case gen_mod:get_module_opt(Host, ?MODULE, websocket_url) of
|
case gen_mod:get_module_opt(Host, ?MODULE, websocket_url) of
|
||||||
|
@ -85,13 +92,96 @@ process([], #request{method = 'GET'}) ->
|
||||||
<<"</script>">>,
|
<<"</script>">>,
|
||||||
<<"</body>">>,
|
<<"</body>">>,
|
||||||
<<"</html>">>]};
|
<<"</html>">>]};
|
||||||
process(_, _) ->
|
process(LocalPath, #request{host = Host}) ->
|
||||||
ejabberd_web:error(not_found).
|
case is_served_file(LocalPath) of
|
||||||
|
true -> serve(Host, LocalPath);
|
||||||
|
false -> ejabberd_web:error(not_found)
|
||||||
|
end.
|
||||||
|
|
||||||
|
%%----------------------------------------------------------------------
|
||||||
|
%% File server
|
||||||
|
%%----------------------------------------------------------------------
|
||||||
|
|
||||||
|
is_served_file([<<"converse.min.js">>]) -> true;
|
||||||
|
is_served_file([<<"converse.min.css">>]) -> true;
|
||||||
|
is_served_file([<<"converse.min.js.map">>]) -> true;
|
||||||
|
is_served_file([<<"converse.min.css.map">>]) -> true;
|
||||||
|
is_served_file([<<"emojis.js">>]) -> true;
|
||||||
|
is_served_file([<<"locales">>, _]) -> true;
|
||||||
|
is_served_file([<<"locales">>, <<"dayjs">>, _]) -> true;
|
||||||
|
is_served_file([<<"webfonts">>, _]) -> true;
|
||||||
|
is_served_file(_) -> false.
|
||||||
|
|
||||||
|
serve(Host, LocalPath) ->
|
||||||
|
case get_conversejs_resources(Host) of
|
||||||
|
undefined -> ejabberd_web:error(not_found);
|
||||||
|
MainPath -> serve2(LocalPath, MainPath)
|
||||||
|
end.
|
||||||
|
|
||||||
|
get_conversejs_resources(Host) ->
|
||||||
|
Opts = gen_mod:get_module_opts(Host, ?MODULE),
|
||||||
|
mod_conversejs_opt:conversejs_resources(Opts).
|
||||||
|
|
||||||
|
%% Copied from mod_muc_log_http.erl
|
||||||
|
|
||||||
|
serve2(LocalPathBin, MainPathBin) ->
|
||||||
|
LocalPath = [binary_to_list(LPB) || LPB <- LocalPathBin],
|
||||||
|
MainPath = binary_to_list(MainPathBin),
|
||||||
|
FileName = filename:join(filename:split(MainPath) ++ LocalPath),
|
||||||
|
case file:read_file(FileName) of
|
||||||
|
{ok, FileContents} ->
|
||||||
|
?DEBUG("Delivering content.", []),
|
||||||
|
{200,
|
||||||
|
[{<<"Content-Type">>, content_type(FileName)}],
|
||||||
|
FileContents};
|
||||||
|
{error, eisdir} ->
|
||||||
|
{403, [], "Forbidden"};
|
||||||
|
{error, Error} ->
|
||||||
|
?DEBUG("Delivering error: ~p", [Error]),
|
||||||
|
case Error of
|
||||||
|
eacces -> {403, [], "Forbidden"};
|
||||||
|
enoent -> {404, [], "Not found"};
|
||||||
|
_Else -> {404, [], atom_to_list(Error)}
|
||||||
|
end
|
||||||
|
end.
|
||||||
|
|
||||||
|
content_type(Filename) ->
|
||||||
|
case string:to_lower(filename:extension(Filename)) of
|
||||||
|
".css" -> "text/css";
|
||||||
|
".js" -> "text/javascript";
|
||||||
|
".map" -> "application/json";
|
||||||
|
".ttf" -> "font/ttf";
|
||||||
|
".woff" -> "font/woff";
|
||||||
|
".woff2" -> "font/woff2"
|
||||||
|
end.
|
||||||
|
|
||||||
|
%%----------------------------------------------------------------------
|
||||||
|
%% Options parsing
|
||||||
|
%%----------------------------------------------------------------------
|
||||||
|
|
||||||
|
get_file_url(Host, Option, Filename, Default) ->
|
||||||
|
FileRaw = case gen_mod:get_module_opt(Host, ?MODULE, Option) of
|
||||||
|
auto -> get_auto_file_url(Host, Filename, Default);
|
||||||
|
F -> F
|
||||||
|
end,
|
||||||
|
misc:expand_keyword(<<"@HOST@">>, FileRaw, Host).
|
||||||
|
|
||||||
|
get_auto_file_url(Host, Filename, Default) ->
|
||||||
|
case get_conversejs_resources(Host) of
|
||||||
|
undefined -> Default;
|
||||||
|
_ -> Filename
|
||||||
|
end.
|
||||||
|
|
||||||
|
%%----------------------------------------------------------------------
|
||||||
|
%%
|
||||||
|
%%----------------------------------------------------------------------
|
||||||
|
|
||||||
mod_opt_type(bosh_service_url) ->
|
mod_opt_type(bosh_service_url) ->
|
||||||
econf:either(undefined, econf:binary());
|
econf:either(undefined, econf:binary());
|
||||||
mod_opt_type(websocket_url) ->
|
mod_opt_type(websocket_url) ->
|
||||||
econf:either(undefined, econf:binary());
|
econf:either(undefined, econf:binary());
|
||||||
|
mod_opt_type(conversejs_resources) ->
|
||||||
|
econf:either(undefined, econf:directory());
|
||||||
mod_opt_type(conversejs_script) ->
|
mod_opt_type(conversejs_script) ->
|
||||||
econf:binary();
|
econf:binary();
|
||||||
mod_opt_type(conversejs_css) ->
|
mod_opt_type(conversejs_css) ->
|
||||||
|
@ -102,9 +192,10 @@ mod_opt_type(default_domain) ->
|
||||||
mod_options(_) ->
|
mod_options(_) ->
|
||||||
[{bosh_service_url, undefined},
|
[{bosh_service_url, undefined},
|
||||||
{websocket_url, undefined},
|
{websocket_url, undefined},
|
||||||
{default_domain, ejabberd_config:get_myname()},
|
{default_domain, <<"@HOST@">>},
|
||||||
{conversejs_script, <<"https://cdn.conversejs.org/dist/converse.min.js">>},
|
{conversejs_resources, undefined},
|
||||||
{conversejs_css, <<"https://cdn.conversejs.org/dist/converse.min.css">>}].
|
{conversejs_script, auto},
|
||||||
|
{conversejs_css, auto}].
|
||||||
|
|
||||||
mod_doc() ->
|
mod_doc() ->
|
||||||
#{desc =>
|
#{desc =>
|
||||||
|
@ -115,9 +206,8 @@ mod_doc() ->
|
||||||
"section, you must also enable it in 'listen' -> 'ejabberd_http' -> "
|
"section, you must also enable it in 'listen' -> 'ejabberd_http' -> "
|
||||||
"http://../listen-options/#request-handlers[request_handlers]."), "",
|
"http://../listen-options/#request-handlers[request_handlers]."), "",
|
||||||
?T("You must also setup either the option 'websocket_url' or 'bosh_service_url'."), "",
|
?T("You must also setup either the option 'websocket_url' or 'bosh_service_url'."), "",
|
||||||
?T("By default, the options 'conversejs_css' and 'conversejs_script'"
|
?T("When 'conversejs_css' and 'conversejs_script' are 'auto', "
|
||||||
" point to the public Converse.js client. Alternatively, you can"
|
"by default they point to the public Converse client.")
|
||||||
" host the client locally using _`mod_http_fileserver`_.")
|
|
||||||
],
|
],
|
||||||
example =>
|
example =>
|
||||||
["listen:",
|
["listen:",
|
||||||
|
@ -130,6 +220,7 @@ mod_doc() ->
|
||||||
"",
|
"",
|
||||||
"modules:",
|
"modules:",
|
||||||
" mod_conversejs:",
|
" mod_conversejs:",
|
||||||
|
" conversejs_resources: \"/home/ejabberd/conversejs-9.0.0/package/dist\"",
|
||||||
" websocket_url: \"ws://example.org:5280/websocket\""],
|
" websocket_url: \"ws://example.org:5280/websocket\""],
|
||||||
opts =>
|
opts =>
|
||||||
[{websocket_url,
|
[{websocket_url,
|
||||||
|
@ -144,14 +235,23 @@ mod_doc() ->
|
||||||
#{value => ?T("Domain"),
|
#{value => ?T("Domain"),
|
||||||
desc =>
|
desc =>
|
||||||
?T("Specify a domain to act as the default for user JIDs. "
|
?T("Specify a domain to act as the default for user JIDs. "
|
||||||
"The default value is the first domain defined in the "
|
"The keyword '@HOST@' is replaced with the hostname. "
|
||||||
"ejabberd configuration file.")}},
|
"The default value is '@HOST@'.")}},
|
||||||
|
{conversejs_resources,
|
||||||
|
#{value => ?T("Path"),
|
||||||
|
desc =>
|
||||||
|
?T("Local path to the Converse files. "
|
||||||
|
"If not set, the public Converse client will be used instead.")}},
|
||||||
{conversejs_script,
|
{conversejs_script,
|
||||||
#{value => ?T("URL"),
|
#{value => ?T("auto | URL"),
|
||||||
desc =>
|
desc =>
|
||||||
?T("Converse.js main script URL.")}},
|
?T("Converse main script URL. "
|
||||||
|
"The keyword '@HOST@' is replaced with the hostname. "
|
||||||
|
"The default value is 'auto'.")}},
|
||||||
{conversejs_css,
|
{conversejs_css,
|
||||||
#{value => ?T("URL"),
|
#{value => ?T("auto | URL"),
|
||||||
desc =>
|
desc =>
|
||||||
?T("Converse.js CSS URL.")}}]
|
?T("Converse CSS URL. "
|
||||||
|
"The keyword '@HOST@' is replaced with the hostname. "
|
||||||
|
"The default value is 'auto'.")}}]
|
||||||
}.
|
}.
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
-export([bosh_service_url/1]).
|
-export([bosh_service_url/1]).
|
||||||
-export([conversejs_css/1]).
|
-export([conversejs_css/1]).
|
||||||
|
-export([conversejs_resources/1]).
|
||||||
-export([conversejs_script/1]).
|
-export([conversejs_script/1]).
|
||||||
-export([default_domain/1]).
|
-export([default_domain/1]).
|
||||||
-export([websocket_url/1]).
|
-export([websocket_url/1]).
|
||||||
|
@ -15,13 +16,19 @@ bosh_service_url(Opts) when is_map(Opts) ->
|
||||||
bosh_service_url(Host) ->
|
bosh_service_url(Host) ->
|
||||||
gen_mod:get_module_opt(Host, mod_conversejs, bosh_service_url).
|
gen_mod:get_module_opt(Host, mod_conversejs, bosh_service_url).
|
||||||
|
|
||||||
-spec conversejs_css(gen_mod:opts() | global | binary()) -> binary().
|
-spec conversejs_css(gen_mod:opts() | global | binary()) -> 'auto' | binary().
|
||||||
conversejs_css(Opts) when is_map(Opts) ->
|
conversejs_css(Opts) when is_map(Opts) ->
|
||||||
gen_mod:get_opt(conversejs_css, Opts);
|
gen_mod:get_opt(conversejs_css, Opts);
|
||||||
conversejs_css(Host) ->
|
conversejs_css(Host) ->
|
||||||
gen_mod:get_module_opt(Host, mod_conversejs, conversejs_css).
|
gen_mod:get_module_opt(Host, mod_conversejs, conversejs_css).
|
||||||
|
|
||||||
-spec conversejs_script(gen_mod:opts() | global | binary()) -> binary().
|
-spec conversejs_resources(gen_mod:opts() | global | binary()) -> 'undefined' | binary().
|
||||||
|
conversejs_resources(Opts) when is_map(Opts) ->
|
||||||
|
gen_mod:get_opt(conversejs_resources, Opts);
|
||||||
|
conversejs_resources(Host) ->
|
||||||
|
gen_mod:get_module_opt(Host, mod_conversejs, conversejs_resources).
|
||||||
|
|
||||||
|
-spec conversejs_script(gen_mod:opts() | global | binary()) -> 'auto' | binary().
|
||||||
conversejs_script(Opts) when is_map(Opts) ->
|
conversejs_script(Opts) when is_map(Opts) ->
|
||||||
gen_mod:get_opt(conversejs_script, Opts);
|
gen_mod:get_opt(conversejs_script, Opts);
|
||||||
conversejs_script(Host) ->
|
conversejs_script(Host) ->
|
||||||
|
|
Loading…
Reference in New Issue