25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-18 17:24:31 +01:00

Experimental support for joining Matrix rooms as MUC rooms

This commit is contained in:
Alexey Shchepin 2024-12-06 03:30:04 +03:00
parent ce6d5aa6a0
commit eb6f242d99
3 changed files with 800 additions and 224 deletions

View File

@ -57,6 +57,11 @@
-define(MAX_REQUEST_SIZE, 1000000). -define(MAX_REQUEST_SIZE, 1000000).
-define(CORS_HEADERS,
[{<<"Access-Control-Allow-Origin">>, <<"*">>},
{<<"Access-Control-Allow-Methods">>, <<"GET, POST, PUT, DELETE, OPTIONS">>},
{<<"Access-Control-Allow-Headers">>, <<"X-Requested-With, Content-Type, Authorization">>}]).
process([<<"key">>, <<"v2">>, <<"server">> | _], process([<<"key">>, <<"v2">>, <<"server">> | _],
#request{method = 'GET', host = _Host} = _Request) -> #request{method = 'GET', host = _Host} = _Request) ->
Host = ejabberd_config:get_myname(), Host = ejabberd_config:get_myname(),
@ -137,6 +142,7 @@ process([<<"federation">>, <<"v2">>, <<"invite">>, RoomID, EventID],
#request{method = 'PUT', host = _Host} = Request) -> #request{method = 'PUT', host = _Host} = Request) ->
case preprocess_federation_request(Request) of case preprocess_federation_request(Request) of
{ok, #{<<"event">> := #{%<<"origin">> := Origin, {ok, #{<<"event">> := #{%<<"origin">> := Origin,
<<"content">> := Content,
<<"room_id">> := RoomID, <<"room_id">> := RoomID,
<<"sender">> := Sender, <<"sender">> := Sender,
<<"state_key">> := UserID} = Event, <<"state_key">> := UserID} = Event,
@ -155,7 +161,12 @@ process([<<"federation">>, <<"v2">>, <<"invite">>, RoomID, EventID],
SEvent = sign_pruned_event(Host, PrunedEvent), SEvent = sign_pruned_event(Host, PrunedEvent),
?DEBUG("sign event ~p~n", [SEvent]), ?DEBUG("sign event ~p~n", [SEvent]),
ResJSON = #{<<"event">> => SEvent}, ResJSON = #{<<"event">> => SEvent},
mod_matrix_gw_room:join(Host, Origin, RoomID, Sender, UserID), case Content of
#{<<"is_direct">> := true} ->
mod_matrix_gw_room:join_direct(Host, Origin, RoomID, Sender, UserID);
_ ->
mod_matrix_gw_room:send_muc_invite(Host, Origin, RoomID, Sender, UserID, Event)
end,
?DEBUG("res ~s~n", [misc:json_encode(ResJSON)]), ?DEBUG("res ~s~n", [misc:json_encode(ResJSON)]),
{200, [{<<"Content-Type">>, <<"application/json;charset=UTF-8">>}], misc:json_encode(ResJSON)}; {200, [{<<"Content-Type">>, <<"application/json;charset=UTF-8">>}], misc:json_encode(ResJSON)};
_ -> _ ->
@ -386,6 +397,13 @@ process([<<"federation">>, <<"v2">>, <<"send_join">>, RoomID, EventID],
{result, HTTPResult} -> {result, HTTPResult} ->
HTTPResult HTTPResult
end; end;
%process([<<"client">> | ClientPath], Request) ->
% {HTTPCode, Headers, JSON} = mod_matrix_gw_c2s:process(ClientPath, Request),
% ?DEBUG("resp ~p~n", [JSON]),
% {HTTPCode,
% [{<<"Content-Type">>, <<"application/json;charset=UTF-8">>} |
% ?CORS_HEADERS] ++ Headers,
% jiffy:encode(JSON)};
process(Path, Request) -> process(Path, Request) ->
?DEBUG("matrix 404: ~p~n~p~n", [Path, Request]), ?DEBUG("matrix 404: ~p~n~p~n", [Path, Request]),
ejabberd_web:error(not_found). ejabberd_web:error(not_found).
@ -494,6 +512,7 @@ init([Host]) ->
process_flag(trap_exit, true), process_flag(trap_exit, true),
mod_matrix_gw_s2s:create_db(), mod_matrix_gw_s2s:create_db(),
mod_matrix_gw_room:create_db(), mod_matrix_gw_room:create_db(),
%mod_matrix_gw_c2s:create_db(),
Opts = gen_mod:get_module_opts(Host, ?MODULE), Opts = gen_mod:get_module_opts(Host, ?MODULE),
MyHost = gen_mod:get_opt(host, Opts), MyHost = gen_mod:get_opt(host, Opts),
register_routes(Host, [MyHost]), register_routes(Host, [MyHost]),
@ -780,10 +799,14 @@ send_request(Host, Method, MatrixServer, Path, Query, JSON,
_ -> _ ->
{URL, Headers, "application/json;charset=UTF-8", Content} {URL, Headers, "application/json;charset=UTF-8", Content}
end, end,
?DEBUG("httpc request ~p", [{Method, Request, HTTPOptions, Options}]),
HTTPRes =
httpc:request(Method, httpc:request(Method,
Request, Request,
HTTPOptions, HTTPOptions,
Options). Options),
?DEBUG("httpc request res ~p", [HTTPRes]),
HTTPRes.
make_auth_header(Host, MatrixServer, Method, URI, Content) -> make_auth_header(Host, MatrixServer, Method, URI, Content) ->
Origin = mod_matrix_gw_opt:matrix_domain(Host), Origin = mod_matrix_gw_opt:matrix_domain(Host),

File diff suppressed because it is too large Load Diff

View File

@ -337,6 +337,7 @@ handle_event(cast, {query, AuthParams, _Query, _JSON, _Request} = Msg,
[]} []}
end; end;
handle_event(cast, {key_reply, KeyID, HTTPResult}, State, Data) -> handle_event(cast, {key_reply, KeyID, HTTPResult}, State, Data) ->
KeyVal =
case HTTPResult of case HTTPResult of
{{_, 200, _}, _, SJSON} -> {{_, 200, _}, _, SJSON} ->
try try
@ -358,29 +359,22 @@ handle_event(cast, {key_reply, KeyID, HTTPResult}, State, Data) ->
ValidUntil2 = ValidUntil2 =
min(ValidUntil, min(ValidUntil,
erlang:system_time(millisecond) + timer:hours(24 * 7)), erlang:system_time(millisecond) + timer:hours(24 * 7)),
Keys = (Data#data.keys)#{KeyID => {ok, VerifyKey, ValidUntil2}}, {ok, VerifyKey, ValidUntil2}
catch
_:_ ->
error
end;
_ ->
error
end,
Keys = (Data#data.keys)#{KeyID => KeyVal},
Froms = maps:get(KeyID, Data#data.key_queue, []), Froms = maps:get(KeyID, Data#data.key_queue, []),
KeyQueue = maps:remove(KeyID, Data#data.key_queue), KeyQueue = maps:remove(KeyID, Data#data.key_queue),
Data2 = Data#data{keys = Keys, Data2 = Data#data{keys = Keys,
key_queue = KeyQueue}, key_queue = KeyQueue},
Replies = Replies = lists:map(fun(From) -> {reply, From, KeyVal} end, Froms),
lists:map(
fun(From) ->
{reply, From, {ok, VerifyKey, ValidUntil2}}
end, Froms),
?DEBUG("KEYS ~p~n", [{Keys, Data2}]), ?DEBUG("KEYS ~p~n", [{Keys, Data2}]),
{next_state, State, Data2, Replies} {next_state, State, Data2, Replies};
catch
_:_ ->
%% TODO
Keys2 = (Data#data.keys)#{KeyID => error},
{next_state, State, Data#data{keys = Keys2}, []}
end;
_ ->
%% TODO
Keys = (Data#data.keys)#{KeyID => error},
{next_state, State, Data#data{keys = Keys}, []}
end;
handle_event(cast, Msg, State, Data) -> handle_event(cast, Msg, State, Data) ->
?WARNING_MSG("Unexpected cast: ~p", [Msg]), ?WARNING_MSG("Unexpected cast: ~p", [Msg]),
{next_state, State, Data, []}; {next_state, State, Data, []};