Skip to content

Commit aaf2975

Browse files
committed
allow to set landing_page to auto
1 parent 3b63839 commit aaf2975

File tree

8 files changed

+71
-96
lines changed

8 files changed

+71
-96
lines changed

src/ejabberd_captcha.erl

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ get_prog_name() ->
454454

455455
maybe_warning_norequesthandler() ->
456456
Host = hd(ejabberd_option:hosts()),
457-
AutoURL = get_auto_url(any, ?MODULE, Host),
457+
AutoURL = ejabberd_http:get_auto_url(any, ?MODULE, Host),
458458
ManualURL = ejabberd_option:captcha_url(),
459459
case (AutoURL == undefined) and not is_binary(ManualURL) of
460460
true ->
@@ -474,7 +474,7 @@ get_url(Str) ->
474474
case ejabberd_option:captcha_url() of
475475
auto ->
476476
Host = ejabberd_config:get_myname(),
477-
URL = get_auto_url(any, ?MODULE, Host),
477+
URL = ejabberd_http:get_auto_url(any, ?MODULE, Host),
478478
<<URL/binary, $/, Str/binary>>;
479479
undefined ->
480480
URL = parse_captcha_host(),
@@ -500,40 +500,6 @@ parse_captcha_host() ->
500500
<<"http://", (ejabberd_config:get_myname())/binary>>
501501
end.
502502

503-
get_auto_url(Tls, Module, Host) ->
504-
case find_handler_port_path(Tls, Module) of
505-
[] -> undefined;
506-
TPPs ->
507-
{ThisTls, Port, Path} = case lists:keyfind(true, 1, TPPs) of
508-
false ->
509-
lists:keyfind(false, 1, TPPs);
510-
TPP ->
511-
TPP
512-
end,
513-
Protocol = case ThisTls of
514-
false -> <<"http">>;
515-
true -> <<"https">>
516-
end,
517-
<<Protocol/binary,
518-
"://", Host/binary, ":",
519-
(integer_to_binary(Port))/binary,
520-
"/",
521-
(str:join(Path, <<"/">>))/binary>>
522-
end.
523-
524-
find_handler_port_path(Tls, Module) ->
525-
lists:filtermap(
526-
fun({{Port, _, _},
527-
ejabberd_http,
528-
#{tls := ThisTls, request_handlers := Handlers}})
529-
when is_integer(Port) and ((Tls == any) or (Tls == ThisTls)) ->
530-
case lists:keyfind(Module, 2, Handlers) of
531-
false -> false;
532-
{Path, Module} -> {true, {ThisTls, Port, Path}}
533-
end;
534-
(_) -> false
535-
end, ets:tab2list(ejabberd_listener)).
536-
537503
get_transfer_protocol(PortString) ->
538504
PortNumber = binary_to_integer(PortString),
539505
PortListeners = get_port_listeners(PortNumber),

src/ejabberd_http.erl

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
-export([start/3, start_link/3,
3333
accept/1, receive_headers/1, recv_file/2,
3434
listen_opt_type/1, listen_options/0,
35-
apply_custom_headers/2]).
35+
apply_custom_headers/2,
36+
get_auto_url/3, find_handler_port_path/2]).
3637

3738
-export([init/3]).
3839

@@ -925,3 +926,37 @@ listen_options() ->
925926
{request_handlers, []},
926927
{tag, <<>>},
927928
{custom_headers, []}].
929+
930+
get_auto_url(Tls, Module, Host) ->
931+
case find_handler_port_path(Tls, Module) of
932+
[] -> undefined;
933+
TPPs ->
934+
{ThisTls, Port, Path} = case lists:keyfind(true, 1, TPPs) of
935+
false ->
936+
lists:keyfind(false, 1, TPPs);
937+
TPP ->
938+
TPP
939+
end,
940+
Protocol = case ThisTls of
941+
false -> <<"http">>;
942+
true -> <<"https">>
943+
end,
944+
<<Protocol/binary,
945+
"://", Host/binary, ":",
946+
(integer_to_binary(Port))/binary,
947+
"/",
948+
(str:join(Path, <<"/">>))/binary>>
949+
end.
950+
951+
find_handler_port_path(Tls, Module) ->
952+
lists:filtermap(
953+
fun({{Port, _, _},
954+
?MODULE,
955+
#{tls := ThisTls, request_handlers := Handlers}})
956+
when is_integer(Port) and ((Tls == any) or (Tls == ThisTls)) ->
957+
case lists:keyfind(Module, 2, Handlers) of
958+
false -> false;
959+
{Path, Module} -> {true, {ThisTls, Port, Path}}
960+
end;
961+
(_) -> false
962+
end, ets:tab2list(ejabberd_listener)).

src/mod_host_meta.erl

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
-export([mod_doc/0]).
3737
-export([get_url/4, get_auto_url/2]).
3838

39+
-import(ejabberd_http, [find_handler_port_path/2]).
40+
3941
-include("logger.hrl").
4042

4143
-include_lib("xmpp/include/xmpp.hrl").
@@ -163,19 +165,6 @@ get_auto_url(Tls, Module) ->
163165
(str:join(Path, <<"/">>))/binary>>
164166
end.
165167

166-
find_handler_port_path(Tls, Module) ->
167-
lists:filtermap(
168-
fun({{Port, _, _},
169-
ejabberd_http,
170-
#{tls := ThisTls, request_handlers := Handlers}})
171-
when is_integer(Port) and ((Tls == any) or (Tls == ThisTls)) ->
172-
case lists:keyfind(Module, 2, Handlers) of
173-
false -> false;
174-
{Path, Module} -> {true, {ThisTls, Port, Path}}
175-
end;
176-
(_) -> false
177-
end, ets:tab2list(ejabberd_listener)).
178-
179168
report_hostmeta_listener() ->
180169
case {find_handler_port_path(false, ?MODULE),
181170
find_handler_port_path(true, ?MODULE)} of

src/mod_invites.erl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ mod_doc() ->
9494
?T("Same as top-level _`default_db`_ option, but applied to this "
9595
"module only.")}},
9696
{landing_page,
97-
#{value => "none | binary()",
98-
desc => ?T("Web address of service handling invite links. This is either a local address handled by `mod_invites` configured as a handler at `ejabberd_http` or an external service like 'easy-xmpp-invitation'. The address must be given as a template pattern with fields from the `invite` that will then get replaced accordingly. Eg.: 'https://{{ host }}:5281/invites/{{ invite.token }}' or as an external service like 'http://{{ host }}:8080/easy-xmpp-invites/#{{ invite.uri|strip_protocol }}'. This is the landing page that is being communicated when creating an invite using one of the ad-hoc commands.")}
97+
#{value => "none | auto | LandingPageURLTemplate",
98+
desc => ?T("This is the landing page that is being communicated when creating an invite using one of the ad-hoc commands, the web address of service handling invite links. This is either a local address handled by `mod_invites` configured as a handler at `ejabberd_http` or an external service like 'easy-xmpp-invitation'. The address must be given as a template pattern with fields from the `invite` that will then get replaced accordingly. Eg.: 'https://{{ host }}:5281/invites/{{ invite.token }}' or as an external service like 'http://{{ host }}:8080/easy-xmpp-invites/#{{ invite.uri|strip_protocol }}'. For convenience you can choose 'auto' here and the ejabberd_http handler will be used.")}
9999
},
100100
{max_invites,
101101
#{value => "pos_integer() | infinity",
@@ -126,9 +126,12 @@ mod_doc() ->
126126
"",
127127
"modules:",
128128
" mod_invites:",
129-
" access_create_account: register"]},
129+
" access_create_account: register"
130+
" landing_page: auto"
131+
]},
130132
{?T("If you want all your users to be able to send 'create account' "
131-
"invites, you would configure your server like this instead:"),
133+
"invites and have a proxy in front of `ejabberd_http` to not expose its port "
134+
"directly, you would configure your server like this instead:"),
132135
["acl:",
133136
" local:",
134137
" user_regexp: \"\"",
@@ -138,7 +141,8 @@ mod_doc() ->
138141
"",
139142
"modules:",
140143
" mod_invites:",
141-
" access_create_account: create_account_invite"]},
144+
" access_create_account: create_account_invite"
145+
" landing_page: https://yourhost/invites/{{ invite.token }}"]},
142146
?T("Note that the names of the access rules are just examples and "
143147
"you're free to change them.")
144148
%% TODO add example for invite page
@@ -184,7 +188,7 @@ mod_opt_type(access_create_account) ->
184188
mod_opt_type(db_type) ->
185189
econf:db_type(?MODULE);
186190
mod_opt_type(landing_page) ->
187-
econf:either(none, econf:binary("^http[s]?://"));
191+
econf:either(none, econf:binary());
188192
mod_opt_type(max_invites) ->
189193
econf:pos_int(infinity);
190194
mod_opt_type(site_name) ->

src/mod_invites_http.erl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,22 @@ landing_page(Host, Invite) ->
6363
case mod_invites_opt:landing_page(Host) of
6464
none ->
6565
<<>>;
66+
<<"auto">> ->
67+
case ejabberd_http:get_auto_url(any, mod_invites, Host) of
68+
undefined ->
69+
?WARNING_MSG("'auto' URL configured for mod_invites but no request_handler found in your ~s listeners configuration.", [Host]),
70+
<<>>;
71+
AutoURL ->
72+
render_landing_page_url(<<AutoURL/binary, "/{{ invite.token }}">>, Host, Invite)
73+
end;
6674
Tmpl ->
67-
Ctx = [{invite, invite_to_proplist(Invite)}, {host, Host}],
68-
render_url(Tmpl, Ctx)
75+
render_landing_page_url(Tmpl, Host, Invite)
6976
end.
7077

78+
render_landing_page_url(Tmpl, Host, Invite) ->
79+
Ctx = [{invite, invite_to_proplist(Invite)}, {host, Host}],
80+
render_url(Tmpl, Ctx).
81+
7182
-spec process(LocalPath::[binary()], #request{}) ->
7283
{HTTPCode::integer(), [{binary(), binary()}], Page::string()}.
7384
process([?STATIC | StaticFile], #request{host = Host} = Request) ->

src/mod_providers.erl

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
-export([start/2, stop/1, reload/3, process/2, mod_opt_type/1, mod_options/1, depends/2]).
4040
-export([mod_doc/0]).
4141

42+
-import(ejabberd_http, [find_handler_port_path/2]).
43+
4244
-include("logger.hrl").
4345

4446
-include_lib("xmpp/include/xmpp.hrl").
@@ -51,14 +53,14 @@
5153
%%| gen_mod callbacks
5254

5355
start(_Host, _Opts) ->
54-
report_hostmeta_listener(),
56+
report_providers_listener(),
5557
ok.
5658

5759
stop(_Host) ->
5860
ok.
5961

6062
reload(_Host, _NewOpts, _OldOpts) ->
61-
report_hostmeta_listener(),
63+
report_providers_listener(),
6264
ok.
6365

6466
depends(_Host, _Opts) ->
@@ -172,7 +174,7 @@ get_password_url2(Host) ->
172174
end.
173175

174176
get_password_url3(Host) ->
175-
case find_handler_port_path2(any, mod_register_web) of
177+
case find_handler_port_path(any, mod_register_web) of
176178
[] ->
177179
<<"">>;
178180
[{ThisTls, Port, Path} | _] ->
@@ -193,23 +195,6 @@ get_password_url3(Host) ->
193195
"/">>
194196
end.
195197

196-
%% TODO Ya hay otra funciona como esta
197-
find_handler_port_path2(Tls, Module) ->
198-
lists:filtermap(fun ({{Port, _, _},
199-
ejabberd_http,
200-
#{tls := ThisTls, request_handlers := Handlers}})
201-
when (Tls == any) or (Tls == ThisTls) ->
202-
case lists:keyfind(Module, 2, Handlers) of
203-
false ->
204-
false;
205-
{Path, Module} ->
206-
{true, {ThisTls, Port, Path}}
207-
end;
208-
(_) ->
209-
false
210-
end,
211-
ets:tab2list(ejabberd_listener)).
212-
213198
%%--------------------------------------------------------------------
214199
%%| Build URLs
215200

@@ -222,23 +207,7 @@ build_urls(Host, Url) when not is_atom(Url) ->
222207
maps:from_list([{L, misc:expand_keyword(<<"@LANGUAGE_URL@">>, Url, L)}
223208
|| L <- Languages]).
224209

225-
find_handler_port_path(Tls, Module) ->
226-
lists:filtermap(fun ({{Port, _, _},
227-
ejabberd_http,
228-
#{tls := ThisTls, request_handlers := Handlers}})
229-
when is_integer(Port) and ((Tls == any) or (Tls == ThisTls)) ->
230-
case lists:keyfind(Module, 2, Handlers) of
231-
false ->
232-
false;
233-
{Path, Module} ->
234-
{true, {ThisTls, Port, Path}}
235-
end;
236-
(_) ->
237-
false
238-
end,
239-
ets:tab2list(ejabberd_listener)).
240-
241-
report_hostmeta_listener() ->
210+
report_providers_listener() ->
242211
case {find_handler_port_path(false, ?MODULE), find_handler_port_path(true, ?MODULE)} of
243212
{[], []} ->
244213
?CRITICAL_MSG("It seems you enabled ~p in 'modules' but forgot to "

test/ejabberd_SUITE_data/ejabberd.mnesia.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ define_macro:
1919
db_type: internal
2020
mod_invites:
2121
db_type: internal
22-
landing_page: "http://@HOST@:5280/invites/{{ invite.token }}" # fixme
22+
landing_page: auto
2323
mod_last:
2424
db_type: internal
2525
mod_muc:

test/ejabberd_SUITE_data/ejabberd.mysql.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ define_macro:
1818
db_type: sql
1919
mod_invites:
2020
db_type: sql
21+
landing_page: auto
2122
mod_last:
2223
db_type: sql
2324
mod_muc:

0 commit comments

Comments
 (0)