diff options
-rw-r--r-- | lib/lsg_matrix/matrix.ex | 19 | ||||
-rw-r--r-- | lib/lsg_matrix/plug.ex | 2 | ||||
-rw-r--r-- | lib/lsg_matrix/room.ex | 45 | ||||
-rw-r--r-- | lib/lsg_web/router.ex | 4 | ||||
-rw-r--r-- | mix.exs | 3 | ||||
-rw-r--r-- | mix.lock | 4 |
6 files changed, 53 insertions, 24 deletions
diff --git a/lib/lsg_matrix/matrix.ex b/lib/lsg_matrix/matrix.ex index bea6d8b..49da6b2 100644 --- a/lib/lsg_matrix/matrix.ex +++ b/lib/lsg_matrix/matrix.ex @@ -5,6 +5,7 @@ defmodule LSG.Matrix do @behaviour MatrixAppService.Adapter.Room @behaviour MatrixAppService.Adapter.Transaction @behaviour MatrixAppService.Adapter.User + @env Mix.env def dets(part) do (LSG.data_path() <> "/matrix-#{to_string(part)}.dets") |> String.to_charlist() @@ -18,9 +19,16 @@ defmodule LSG.Matrix do end def myself?("@_dev:random.sh"), do: true + def myself?("@_bot:random.sh"), do: true def myself?("@_dev."<>_), do: true + def myself?("@_bot."<>_), do: true def myself?(_), do: false + def mxc_to_http(mxc = "mxc://"<>_) do + uri = URI.parse(mxc) + %URI{uri | scheme: "https", path: "/_matrix/media/r0/download/#{uri.authority}#{uri.path}"} + |> URI.to_string() + end def get_or_create_matrix_user(id) do if mxid = lookup_user(id) do @@ -31,7 +39,7 @@ defmodule LSG.Matrix do inhibit_login: true, device_id: "APP_SERVICE", initial_device_display_name: "Application Service", - username: "_dev.#{id}" + username: if(@env == :dev, do: "_dev.#{id}", else: "_bot.#{id}") ] Logger.debug("Registering user for #{id}") {:ok, %{"user_id" => mxid}} = Polyjuice.Client.LowLevel.register(client(), opts) @@ -89,7 +97,7 @@ defmodule LSG.Matrix do localpart = localpart(room_alias) with {:ok, network, channel} <- extract_network_channel_from_localpart(localpart), %IRC.Connection{} <- IRC.Connection.get_network(network, channel), - room = [visibility: :public, room_alias_name: localpart, name: "#{network}/#{channel}"], + room = [visibility: :public, room_alias_name: localpart, name: if(network == "random", do: channel, else: "#{network}/#{channel}")], {:ok, %{"room_id" => room_id}} <- Client.Room.create_room(client(), room) do Logger.info("Matrix: created room #{room_alias} #{room_id}") :dets.insert(dets(:rooms), {room_id, network, channel, %{}}) @@ -109,10 +117,11 @@ defmodule LSG.Matrix do def extract_network_channel_from_localpart(localpart) do s = localpart |> String.replace("dev.", "") - |> String.split("_", parts: 2) + |> String.split("/", parts: 2) case s do [network, channel] -> {:ok, network, channel} + [channel] -> {:ok, "random", channel} _ -> {:error, :invalid_localpart} end end @@ -130,7 +139,9 @@ defmodule LSG.Matrix do @impl MatrixAppService.Adapter.Transaction def new_event(event = %MatrixAppService.Event{}) do Logger.debug("New matrix event: #{inspect event}") - if room_id = event.room_id, do: LSG.Matrix.Room.start_and_send_matrix_event(room_id, event) + if event.room_id do + LSG.Matrix.Room.start_and_send_matrix_event(event.room_id, event) + end :noop end diff --git a/lib/lsg_matrix/plug.ex b/lib/lsg_matrix/plug.ex index 5d5b603..c0c027f 100644 --- a/lib/lsg_matrix/plug.ex +++ b/lib/lsg_matrix/plug.ex @@ -5,7 +5,7 @@ defmodule LSG.Matrix.Plug do state end - def call(conn, _) do + def call(conn, _) do hs = Application.get_env(:matrix_app_service, :homeserver_token) MatrixAppServiceWeb.AuthPlug.call(conn, hs) end diff --git a/lib/lsg_matrix/room.ex b/lib/lsg_matrix/room.ex index 31d1b06..e3ddd1d 100644 --- a/lib/lsg_matrix/room.ex +++ b/lib/lsg_matrix/room.ex @@ -41,9 +41,10 @@ defmodule LSG.Matrix.Room do case __MODULE__.start(room_id) do {:ok, pid} -> pid {:error, {:already_started, pid}} -> pid + :ignore -> nil end end - send(pid, {:matrix_event, event}) + if(pid, do: send(pid, {:matrix_event, event})) end def whereis(room_id) do @@ -59,18 +60,23 @@ defmodule LSG.Matrix.Room do end def init([room_id]) do - {:ok, state} = Matrix.lookup_room(room_id) - Logger.metadata(matrix_room: room_id) + case Matrix.lookup_room(room_id) do + {:ok, state} -> + Logger.metadata(matrix_room: room_id) - {:ok, _} = Registry.register(IRC.PubSub, "#{state.network}:events", plugin: __MODULE__) - for t <- ["messages", "triggers", "outputs", "events"] do - {:ok, _} = Registry.register(IRC.PubSub, "#{state.network}/#{state.channel}:#{t}", plugin: __MODULE__) - end + {:ok, _} = Registry.register(IRC.PubSub, "#{state.network}:events", plugin: __MODULE__) + for t <- ["messages", "triggers", "outputs", "events"] do + {:ok, _} = Registry.register(IRC.PubSub, "#{state.network}/#{state.channel}:#{t}", plugin: __MODULE__) + end - state = state - |> Map.put(:id, room_id) - Logger.info("Started Matrix room #{room_id}") - {:ok, state, {:continue, :update_state}} + state = state + |> Map.put(:id, room_id) + Logger.info("Started Matrix room #{room_id}") + {:ok, state, {:continue, :update_state}} + error -> + Logger.info("Received event for nonexistent room #{inspect room_id}: #{inspect error}") + :ignore + end end def handle_continue(:update_state, state) do @@ -79,12 +85,16 @@ defmodule LSG.Matrix.Room do if s["type"] == "m.room.member" do if s["content"]["membership"] == "join" do [s["user_id"] | acc] + else + # XXX: The user left, remove from IRC.Memberships ? + acc end else - # XXX: The user left, remove from IRC.Memberships ? acc end end) + |> Enum.filter(& &1) + for m <- members, do: IRC.UserTrack.joined(state.id, %{network: "matrix", nick: m, user: m, host: "matrix."}, [], true) accounts = IRC.UserTrack.channel(state.network, state.channel) @@ -127,6 +137,13 @@ defmodule LSG.Matrix.Room do {:noreply, state} end + def handle_irc(%{type: quit_or_part, account_id: account_id}, state) when quit_or_part in [:quit, :part] do + mxid = Matrix.get_or_create_matrix_user(account_id) + Client.Room.leave(client(user_id: mxid), state.id) + {:noreply, state} + end + + def handle_irc(event, state) do Logger.warn("Skipped irc event #{inspect event}") {:noreply, state} @@ -144,8 +161,8 @@ defmodule LSG.Matrix.Room do end def handle_matrix(event = %{type: "m.room.message", user_id: user_id, content: %{"msgtype" => "m.text", "body" => text}}, state) do - IRC.send_message_as(get_account(event, state), state.network, state.channel, text, true) - {:noreply, state} + IRC.send_message_as(get_account(event, state), state.network, state.channel, text, true) + {:noreply, state} end def handle_matrix(event, state) do diff --git a/lib/lsg_web/router.ex b/lib/lsg_web/router.ex index dc49c9f..e872cef 100644 --- a/lib/lsg_web/router.ex +++ b/lib/lsg_web/router.ex @@ -56,13 +56,13 @@ defmodule LSGWeb.Router do get "/:network/:chan/alcoolog/t/:token", AlcoologController, :token end - scope "/_matrix/appservice", MatrixAppServiceWeb.V1, as: :matrix do + scope "/_matrix/:appservice", MatrixAppServiceWeb.V1, as: :matrix do pipe_through :matrix_app_service put "/transactions/:txn_id", TransactionController, :push get "/users/:user_id", UserController, :query - get "/rooms/:room_alias", RoomController, :query + get "/rooms/*room_alias", RoomController, :query get "/thirdparty/protocol/:protocol", ThirdPartyController, :query_protocol get "/thirdparty/user/:protocol", ThirdPartyController, :query_users @@ -70,7 +70,8 @@ defmodule LSG.Mixfile do {:html_entities, "0.4.0", override: true}, {:file_size, "~> 3.0"}, {:ex2ms, "~> 1.0"}, - {:matrix_app_service, git: "https://gitlab.com/kazarma/matrix_app_service.ex", branch: "master"}, + {:polyjuice_client, git: "https://git.random.sh/ircbot/polyjuice_client.git", branch: "master", override: true}, + {:matrix_app_service, git: "https://git.random.sh/ircbot/matrix_app_service.ex.git", branch: "master"}, ] end end @@ -38,7 +38,7 @@ "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, "jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"}, "liquex": {:hex, :liquex, "0.6.1", "2e07fc177dfb2ecafe326f11bd641373f3f6b62704a0231832d8634e162e852a", [:mix], [{:date_time_parser, "~> 1.1", [hex: :date_time_parser, repo: "hexpm", optional: false]}, {:html_entities, "~> 0.5.1", [hex: :html_entities, repo: "hexpm", optional: false]}, {:html_sanitize_ex, "~> 1.3.0", [hex: :html_sanitize_ex, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:timex, "~> 3.6", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm", "2ec6c68fce04e10ca1fd3874d146991cf1b44adc0c8451615873263944353772"}, - "matrix_app_service": {:git, "https://gitlab.com/kazarma/matrix_app_service.ex", "79ec2d33a749eb3245a39156601323232b8f4c9e", [branch: "master"]}, + "matrix_app_service": {:git, "https://git.random.sh/ircbot/matrix_app_service.ex.git", "5a4efc102f97abad6b60fab7bf761acef6acc78d", [branch: "master"]}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, "mime": {:hex, :mime, "1.6.0", "dabde576a497cef4bbdd60aceee8160e02a6c89250d6c0b29e56c0dfb00db3d2", [:mix], [], "hexpm", "31a1a8613f8321143dde1dafc36006a17d28d02bdfecb9e95a880fa7aabd19a7"}, "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, @@ -64,7 +64,7 @@ "plug_cowboy": {:hex, :plug_cowboy, "2.5.1", "7cc96ff645158a94cf3ec9744464414f02287f832d6847079adfe0b58761cbd0", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "107d0a5865fa92bcb48e631cc0729ae9ccfa0a9f9a1bd8f01acb513abf1c2d64"}, "plug_crypto": {:hex, :plug_crypto, "1.2.2", "05654514ac717ff3a1843204b424477d9e60c143406aa94daf2274fdd280794d", [:mix], [], "hexpm", "87631c7ad914a5a445f0a3809f99b079113ae4ed4b867348dd9eec288cecb6db"}, "poison": {:hex, :poison, "4.0.1", "bcb755a16fac91cad79bfe9fc3585bb07b9331e50cfe3420a24bcc2d735709ae", [:mix], [], "hexpm", "ba8836feea4b394bb718a161fc59a288fe0109b5006d6bdf97b6badfcf6f0f25"}, - "polyjuice_client": {:git, "https://gitlab.com/kazarma/polyjuice_client.git", "99ab68b955a99169645fd5ede690c0757e69f812", [branch: "kazarma"]}, + "polyjuice_client": {:git, "https://git.random.sh/ircbot/polyjuice_client.git", "92c949be2def3cd0280cbc78849b109d34c8fcaa", [branch: "master"]}, "polyjuice_util": {:hex, :polyjuice_util, "0.1.0", "69901959c143245b47829c8302d0605dff6c0e1c3b116730c162982e0f512ee0", [:mix], [], "hexpm", "af5d1f614f52ce1da59a1f5a7c49249a2dbfda279d99d52d1b4e83e84c19a8d5"}, "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, "retry": {:hex, :retry, "0.14.1", "722d1b0cf87096b71213f5801d99fface7ca76adc83fc9dbf3e1daee952aef10", [:mix], [], "hexpm", "b3a609f286f6fe4f6b2c15f32cd4a8a60427d78d05d7b68c2dd9110981111ae0"}, |