summaryrefslogtreecommitdiff
path: root/lib/lsg
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lsg')
-rw-r--r--lib/lsg/application.ex6
-rw-r--r--lib/lsg/telegram_room.ex36
2 files changed, 29 insertions, 13 deletions
diff --git a/lib/lsg/application.ex b/lib/lsg/application.ex
index cdd7915..5dd9d9c 100644
--- a/lib/lsg/application.ex
+++ b/lib/lsg/application.ex
@@ -6,6 +6,9 @@ defmodule LSG.Application do
def start(_type, _args) do
import Supervisor.Spec
+ :ok = LSG.Matrix.setup()
+ :ok = LSG.TelegramRoom.setup()
+
# Define workers and child supervisors to be supervised
children = [
# Start the endpoint when the application starts
@@ -19,6 +22,7 @@ defmodule LSG.Application do
{GenMagic.Pool, [name: LSG.GenMagic, pool_size: 2]},
#worker(LSG.Icecast, []),
] ++ LSG.IRC.application_childs
+ ++ LSG.Matrix.application_childs
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
@@ -26,6 +30,8 @@ defmodule LSG.Application do
sup = Supervisor.start_link(children, opts)
start_telegram()
spawn_link(fn() -> LSG.IRC.after_start() end)
+ spawn_link(fn() -> LSG.Matrix.after_start() end)
+ spawn_link(fn() -> LSG.TelegramRoom.after_start() end)
sup
end
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