From 735a8dd998b2c0aebde9c96daefc7aa2c223218e Mon Sep 17 00:00:00 2001 From: href Date: Sat, 4 Sep 2021 05:41:09 +0200 Subject: matrix appservice, puppet improvements --- lib/lsg/telegram_room.ex | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'lib/lsg/telegram_room.ex') diff --git a/lib/lsg/telegram_room.ex b/lib/lsg/telegram_room.ex index 1eeec8f..f7e42c6 100644 --- a/lib/lsg/telegram_room.ex +++ b/lib/lsg/telegram_room.ex @@ -3,6 +3,20 @@ defmodule LSG.TelegramRoom do @behaviour Telegram.ChatBot alias Telegram.Api + def dets() do + (LSG.data_path() <> "/telegram-rooms.dets") |> String.to_charlist() + end + + def setup() do + {:ok, _} = :dets.open_file(dets(), []) + :ok + end + + def after_start() do + rooms = :dets.foldl(fn({id, _, _}, acc) -> [id | acc] end, [], dets()) + for id <- rooms, do: Telegram.Bot.ChatBot.Chat.Session.Supervisor.start_child(LSG.Telegram, id) + end + @impl Telegram.ChatBot def init(id) do token = Keyword.get(Application.get_env(:lsg, :telegram, []), :key) @@ -11,12 +25,13 @@ defmodule LSG.TelegramRoom do [net, chan] = String.split(chat["title"], "/", parts: 2) case IRC.Connection.get_network(net, chan) do %IRC.Connection{} -> - :global.register_name({__MODULE__, net, chan}, self()) {:ok, _} = Registry.register(IRC.PubSub, "#{net}/#{chan}:messages", plugin: __MODULE__) {:ok, _} = Registry.register(IRC.PubSub, "#{net}/#{chan}:triggers", plugin: __MODULE__) + {:ok, _} = Registry.register(IRC.PubSub, "#{net}/#{chan}:outputs", plugin: __MODULE__) err -> Logger.warn("Did not found telegram match for #{id} \"#{chat["title"]}\"") end + :dets.insert(dets(), {id, net, chan}) {:ok, %{id: id, net: net, chan: chan}} end @@ -48,22 +63,17 @@ defmodule LSG.TelegramRoom do handle_info({:irc, nil, message}, state) end - def handle_info({:irc, _, %IRC.Message{sender: %{nick: nick}, text: text}}, state) do - LSG.Telegram.send_message(state.id, "<#{nick}> #{text}") - {:ok, state} - end - - def handle_info({:raw, lines}, state) when is_list(lines) do - formatted = for l <- lines, into: <<>>, do: l <> "\n" - LSG.Telegram.send_message(state.id, formatted) + def handle_info({:irc, _, message = %IRC.Message{sender: %{nick: nick}, text: text}}, state) do + if Map.get(message.meta, :from) == self() do + else + body = if Map.get(message.meta, :self), do: text, else: "<#{nick}> #{text}" + LSG.Telegram.send_message(state.id, body) + end {:ok, state} end - def handle_info({:raw, line}, state) do - handle_info({:raw, [line]}, state) - end - def handle_info(info, state) do + Logger.info("UNhandled #{inspect info}") {:ok, state} end -- cgit v1.2.3