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:
parent
ce6d5aa6a0
commit
eb6f242d99
@ -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,
|
||||||
httpc:request(Method,
|
?DEBUG("httpc request ~p", [{Method, Request, HTTPOptions, Options}]),
|
||||||
Request,
|
HTTPRes =
|
||||||
HTTPOptions,
|
httpc:request(Method,
|
||||||
Options).
|
Request,
|
||||||
|
HTTPOptions,
|
||||||
|
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
@ -337,50 +337,44 @@ 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) ->
|
||||||
case HTTPResult of
|
KeyVal =
|
||||||
{{_, 200, _}, _, SJSON} ->
|
case HTTPResult of
|
||||||
try
|
{{_, 200, _}, _, SJSON} ->
|
||||||
JSON = misc:json_decode(SJSON),
|
try
|
||||||
?DEBUG("key ~p~n", [JSON]),
|
JSON = misc:json_decode(SJSON),
|
||||||
#{<<"verify_keys">> := VerifyKeys} = JSON,
|
?DEBUG("key ~p~n", [JSON]),
|
||||||
#{KeyID := KeyData} = VerifyKeys,
|
#{<<"verify_keys">> := VerifyKeys} = JSON,
|
||||||
#{<<"key">> := SKey} = KeyData,
|
#{KeyID := KeyData} = VerifyKeys,
|
||||||
VerifyKey = mod_matrix_gw:base64_decode(SKey),
|
#{<<"key">> := SKey} = KeyData,
|
||||||
?DEBUG("key ~p~n", [VerifyKey]),
|
VerifyKey = mod_matrix_gw:base64_decode(SKey),
|
||||||
?DEBUG("check ~p~n",
|
?DEBUG("key ~p~n", [VerifyKey]),
|
||||||
[catch check_signature(
|
?DEBUG("check ~p~n",
|
||||||
JSON, Data#data.matrix_server,
|
[catch check_signature(
|
||||||
KeyID, VerifyKey)]),
|
JSON, Data#data.matrix_server,
|
||||||
true = check_signature(
|
KeyID, VerifyKey)]),
|
||||||
JSON, Data#data.matrix_server,
|
true = check_signature(
|
||||||
KeyID, VerifyKey),
|
JSON, Data#data.matrix_server,
|
||||||
#{<<"valid_until_ts">> := ValidUntil} = JSON,
|
KeyID, VerifyKey),
|
||||||
ValidUntil2 =
|
#{<<"valid_until_ts">> := ValidUntil} = JSON,
|
||||||
min(ValidUntil,
|
ValidUntil2 =
|
||||||
erlang:system_time(millisecond) + timer:hours(24 * 7)),
|
min(ValidUntil,
|
||||||
Keys = (Data#data.keys)#{KeyID => {ok, VerifyKey, ValidUntil2}},
|
erlang:system_time(millisecond) + timer:hours(24 * 7)),
|
||||||
Froms = maps:get(KeyID, Data#data.key_queue, []),
|
{ok, VerifyKey, ValidUntil2}
|
||||||
KeyQueue = maps:remove(KeyID, Data#data.key_queue),
|
catch
|
||||||
Data2 = Data#data{keys = Keys,
|
_:_ ->
|
||||||
key_queue = KeyQueue},
|
error
|
||||||
Replies =
|
end;
|
||||||
lists:map(
|
_ ->
|
||||||
fun(From) ->
|
error
|
||||||
{reply, From, {ok, VerifyKey, ValidUntil2}}
|
end,
|
||||||
end, Froms),
|
Keys = (Data#data.keys)#{KeyID => KeyVal},
|
||||||
?DEBUG("KEYS ~p~n", [{Keys, Data2}]),
|
Froms = maps:get(KeyID, Data#data.key_queue, []),
|
||||||
{next_state, State, Data2, Replies}
|
KeyQueue = maps:remove(KeyID, Data#data.key_queue),
|
||||||
catch
|
Data2 = Data#data{keys = Keys,
|
||||||
_:_ ->
|
key_queue = KeyQueue},
|
||||||
%% TODO
|
Replies = lists:map(fun(From) -> {reply, From, KeyVal} end, Froms),
|
||||||
Keys2 = (Data#data.keys)#{KeyID => error},
|
?DEBUG("KEYS ~p~n", [{Keys, Data2}]),
|
||||||
{next_state, State, Data#data{keys = Keys2}, []}
|
{next_state, State, Data2, Replies};
|
||||||
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, []};
|
||||||
|
Loading…
Reference in New Issue
Block a user