summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhref <href@random.sh>2021-09-02 13:00:55 +0200
committerhref <href@random.sh>2021-09-02 13:00:55 +0200
commit28ab854e9dbcd2e40fc4b7986f4e5dd303bf27a1 (patch)
tree62bda7f25e7d9282283fc8eca290f60d7d0d8c86
parentlastfm: improvements, fixes (diff)
improve puppets, fix uploads
-rw-r--r--lib/irc.ex11
-rw-r--r--lib/irc/puppet_connection.ex28
-rw-r--r--lib/lsg/telegram.ex7
-rw-r--r--lib/lsg/telegram_room.ex65
-rw-r--r--lib/lsg_irc/say_plugin.ex3
-rw-r--r--lib/lsg_irc/user_mention_plugin.ex3
-rw-r--r--mix.exs2
-rw-r--r--mix.lock2
8 files changed, 106 insertions, 15 deletions
diff --git a/lib/irc.ex b/lib/irc.ex
index ee44f37..7f0d7e1 100644
--- a/lib/irc.ex
+++ b/lib/irc.ex
@@ -15,6 +15,17 @@ defmodule IRC do
defstruct [:type, :trigger, :args]
end
+ def send_message_as(account, network, channel, text, force_puppet \\ false) do
+ connection = IRC.Connection.get_network(network)
+ if connection && (force_puppet || IRC.PuppetConnection.whereis(account, connection)) do
+ IRC.PuppetConnection.start_and_send_message(account, connection, channel, text)
+ else
+ user = IRC.UserTrack.find_by_account(network, account)
+ nick = if(user, do: user.nick, else: account.name)
+ IRC.Connection.broadcast_message(network, channel, "<#{nick}> #{text}")
+ end
+ end
+
def register(key) do
case Registry.register(IRC.PubSub, key, []) do
{:ok, _} -> :ok
diff --git a/lib/irc/puppet_connection.ex b/lib/irc/puppet_connection.ex
index 88b4f8a..68f1425 100644
--- a/lib/irc/puppet_connection.ex
+++ b/lib/irc/puppet_connection.ex
@@ -26,10 +26,28 @@ defmodule IRC.PuppetConnection do
end
end
+ def whereis(account = %IRC.Account{id: account_id}, connection = %IRC.Connection{id: connection_id}) do
+ {:global, name} = name(account_id, connection_id)
+ case :global.whereis_name(name) do
+ :undefined -> nil
+ pid -> pid
+ end
+ end
+
def send_message(account = %IRC.Account{id: account_id}, connection = %IRC.Connection{id: connection_id}, channel, text) do
- pid = case IRC.PuppetConnection.Supervisor.start_child(account, connection) do
- {:ok, pid} -> pid
- {:error, {:already_started, pid}} -> pid
+ GenServer.cast(name(account_id, connection_id), {:send_message, channel, text})
+ end
+
+ def start_and_send_message(account = %IRC.Account{id: account_id}, connection = %IRC.Connection{id: connection_id}, channel, text) do
+ {:global, name} = name(account_id, connection_id)
+ pid = whereis(account, connection)
+ pid = if !pid do
+ case IRC.PuppetConnection.Supervisor.start_child(account, connection) do
+ {:ok, pid} -> pid
+ {:error, {:already_started, pid}} -> pid
+ end
+ else
+ pid
end
GenServer.cast(pid, {:send_message, channel, text})
end
@@ -74,7 +92,7 @@ defmodule IRC.PuppetConnection do
end
def handle_continue(:connected, state) do
- state = Enum.reduce(state.buffer, state, fn(b, state) ->
+ state = Enum.reduce(Enum.reverse(state.buffer), state, fn(b, state) ->
{:noreply, state} = handle_cast(b, state)
state
end)
@@ -105,7 +123,7 @@ defmodule IRC.PuppetConnection do
end
def handle_info(:idle, state) do
- ExIRC.Client.quit(state.client, "Puppet is idle for too long")
+ ExIRC.Client.quit(state.client, "Puppet was idle for too long")
ExIRC.Client.stop!(state.client)
{:stop, :normal, state}
end
diff --git a/lib/lsg/telegram.ex b/lib/lsg/telegram.ex
index e5790da..e8758e3 100644
--- a/lib/lsg/telegram.ex
+++ b/lib/lsg/telegram.ex
@@ -66,7 +66,6 @@ defmodule LSG.Telegram do
{:ok, %{account: account.id}}
end
-
#[debug] Unhandled update: %{"message" =>
# %{"chat" => %{"first_name" => "J", "id" => 2075406, "type" => "private", "username" => "ahref"},
# "date" => 1591096015,
@@ -145,10 +144,8 @@ defmodule LSG.Telegram do
do
path = LSGWeb.Router.Helpers.url(LSGWeb.Endpoint) <> "/files/#{s3path}"
sent = for {net, chan} <- target do
- user = IRC.UserTrack.find_by_account(net, account)
- nick = if(user, do: user.nick, else: account.name)
- txt = "#{nick} sent#{type}#{text} #{path}"
- IRC.Connection.broadcast_message(net, chan, txt)
+ txt = "sent#{type}#{text} #{path}"
+ IRC.send_message_as(account, net, chan, txt)
"#{net}/#{chan}"
end
if caption = op["caption"], do: as_irc_message(chat_id, caption, account)
diff --git a/lib/lsg/telegram_room.ex b/lib/lsg/telegram_room.ex
index f973c58..9504cd4 100644
--- a/lib/lsg/telegram_room.ex
+++ b/lib/lsg/telegram_room.ex
@@ -23,10 +23,23 @@ defmodule LSG.TelegramRoom do
def handle_update(%{"message" => %{"from" => %{"id" => user_id}, "text" => text}}, _token, state) do
account = IRC.Account.find_meta_account("telegram-id", user_id)
connection = IRC.Connection.get_network(state.net)
- IRC.PuppetConnection.send_message(account, connection, state.chan, text)
+ IRC.send_message_as(account, state.net, state.chan, text, true)
{:ok, state}
end
+ def handle_update(data = %{"message" => %{"from" => %{"id" => user_id}, "location" => %{"latitude" => lat, "longitude" => lon}}}, _token, state) do
+ account = IRC.Account.find_meta_account("telegram-id", user_id)
+ connection = IRC.Connection.get_network(state.net)
+ IRC.send_message_as(account, state.net, state.chan, "@ #{lat}, #{lon}", true)
+ {:ok, state}
+ end
+
+ for type <- ~w(photo voice video document animation) do
+ def handle_update(data = %{"message" => %{unquote(type) => _}}, token, state) do
+ upload(unquote(type), data, token, state)
+ end
+ end
+
def handle_update(update, token, state) do
{:ok, state}
end
@@ -50,4 +63,54 @@ defmodule LSG.TelegramRoom do
{:ok, state}
end
+ defp upload(_type, %{"message" => m = %{"chat" => %{"id" => chat_id}, "from" => %{"id" => user_id}}}, token, state) do
+ account = IRC.Account.find_meta_account("telegram-id", user_id)
+ if account do
+ {content, type} = cond do
+ m["photo"] -> {m["photo"], "photo"}
+ m["voice"] -> {m["voice"], "voice message"}
+ m["video"] -> {m["video"], "video"}
+ m["document"] -> {m["document"], "file"}
+ m["animation"] -> {m["animation"], "gif"}
+ end
+
+ file = if is_list(content) && Enum.count(content) > 1 do
+ Enum.sort_by(content, fn(p) -> p["file_size"] end, &>=/2)
+ |> List.first()
+ else
+ content
+ end
+
+ file_id = file["file_id"]
+ file_unique_id = file["file_unique_id"]
+ text = if(m["caption"], do: m["caption"] <> " ", else: "")
+
+ spawn(fn() ->
+ with \
+ {:ok, file} <- Telegram.Api.request(token, "getFile", file_id: file_id),
+ path = "https://api.telegram.org/file/bot#{token}/#{file["file_path"]}",
+ {:ok, %HTTPoison.Response{status_code: 200, body: body}} <- HTTPoison.get(path),
+ <<smol_body::binary-size(20), _::binary>> = body,
+ {:ok, magic} <- GenMagic.Pool.perform(LSG.GenMagic, {:bytes, smol_body}),
+ bucket = Application.get_env(:lsg, :s3, []) |> Keyword.get(:bucket),
+ ext = Path.extname(file["file_path"]),
+ s3path = "#{account.id}/#{file_unique_id}#{ext}",
+ s3req = ExAws.S3.put_object(bucket, s3path, body, acl: :public_read, content_type: magic.mime_type),
+ {:ok, _} <- ExAws.request(s3req)
+ do
+ path = LSGWeb.Router.Helpers.url(LSGWeb.Endpoint) <> "/files/#{s3path}"
+ txt = "#{type}: #{text}#{path}"
+ connection = IRC.Connection.get_network(state.net)
+ IRC.send_message_as(account, state.net, state.chan, txt, true)
+ else
+ error ->
+ Telegram.Api.request(token, "sendMessage", chat_id: chat_id, text: "File upload failed, sorry.")
+ Logger.error("Failed upload from Telegram: #{inspect error}")
+ end
+ end)
+
+ {:ok, state}
+ end
+ end
+
end
diff --git a/lib/lsg_irc/say_plugin.ex b/lib/lsg_irc/say_plugin.ex
index c385e77..085ca92 100644
--- a/lib/lsg_irc/say_plugin.ex
+++ b/lib/lsg_irc/say_plugin.ex
@@ -62,8 +62,7 @@ defmodule LSG.IRC.SayPlugin do
chan2 = String.replace(chan, "#", "")
if (target == "#{net}/#{chan}" || target == "#{net}/#{chan2}" || target == chan || target == chan2) do
if with_nick? do
- connection = IRC.Connection.get_network(net)
- IRC.PuppetConnection.send_message(account, connection, chan, text)
+ IRC.send_message_as(account, net, chan, text)
else
IRC.Connection.broadcast_message(net, chan, text)
end
diff --git a/lib/lsg_irc/user_mention_plugin.ex b/lib/lsg_irc/user_mention_plugin.ex
index 5f7b10a..ca743c4 100644
--- a/lib/lsg_irc/user_mention_plugin.ex
+++ b/lib/lsg_irc/user_mention_plugin.ex
@@ -20,6 +20,9 @@ defmodule LSG.IRC.UserMentionPlugin do
end
def handle_info({:irc, :trigger, nick, message = %IRC.Message{sender: sender, account: account, network: network, channel: channel, trigger: %IRC.Trigger{type: :at, args: content}}}, state) do
+ nick = nick
+ |> String.trim(":")
+ |> String.trim(",")
target = IRC.Account.find_always_by_nick(network, channel, nick)
if target do
telegram = IRC.Account.get_meta(target, "telegram-id")
diff --git a/mix.exs b/mix.exs
index 8169666..65dc176 100644
--- a/mix.exs
+++ b/mix.exs
@@ -65,7 +65,7 @@ defmodule LSG.Mixfile do
{:telegram, git: "https://github.com/hrefhref/telegram.git", branch: "master"},
{:ex_aws, "~> 2.0"},
{:ex_aws_s3, "~> 2.0"},
- {:gen_magic, git: "https://github.com/hrefhref/gen_magic", commit: "b788ef982fc9b9cf9669b35227bac336415c5a1c"},
+ {:gen_magic, git: "https://github.com/hrefhref/gen_magic", branch: "develop"},
{:liquex, "~> 0.3"},
{:html_entities, "0.4.0", override: true},
{:file_size, "~> 3.0"},
diff --git a/mix.lock b/mix.lock
index 34df688..5990ac2 100644
--- a/mix.lock
+++ b/mix.lock
@@ -24,7 +24,7 @@
"file_size": {:hex, :file_size, "3.0.1", "ad447a69442a82fc701765a73992d7b1110136fa0d4a9d3190ea685d60034dcd", [:mix], [{:decimal, ">= 1.0.0 and < 3.0.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:number, "~> 1.0", [hex: :number, repo: "hexpm", optional: false]}], "hexpm", "64dd665bc37920480c249785788265f5d42e98830d757c6a477b3246703b8e20"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"floki": {:hex, :floki, "0.19.3", "652d1447767f783bd6cae1d882fd2145f25db28c6841ab87659225b468cff101", [:mix], [{:html_entities, "~> 0.4.0", [hex: :html_entities, repo: "hexpm", optional: false]}, {:mochiweb, "~> 2.15", [hex: :mochiweb, repo: "hexpm", optional: false]}], "hexpm", "1c8da482a0848c55a1d22af49ce6547790077adac2a04cf265e1f26583781adb"},
- "gen_magic": {:git, "https://github.com/hrefhref/gen_magic", "52f6aa7281623c77018ec12c227f4dc64c6313f6", []},
+ "gen_magic": {:git, "https://github.com/hrefhref/gen_magic", "48a12cca10305c8d357fe16b10fd7ead9b64a56a", [branch: "develop"]},
"gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"},
"gun": {:hex, :gun, "1.3.3", "cf8b51beb36c22b9c8df1921e3f2bc4d2b1f68b49ad4fbc64e91875aa14e16b4", [:rebar3], [{:cowlib, "~> 2.7.0", [hex: :cowlib, repo: "hexpm", optional: false]}], "hexpm", "3106ce167f9c9723f849e4fb54ea4a4d814e3996ae243a1c828b256e749041e0"},
"hackney": {:hex, :hackney, "1.17.4", "99da4674592504d3fb0cfef0db84c3ba02b4508bae2dff8c0108baa0d6e0977c", [:rebar3], [{:certifi, "~>2.6.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "de16ff4996556c8548d512f4dbe22dd58a587bf3332e7fd362430a7ef3986b16"},