summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Bracco <href@random.sh>2023-03-06 18:09:57 +0100
committerJordan Bracco <href@random.sh>2023-03-06 18:09:57 +0100
commit5ab36499520e3dd9958c79b5c124333f1313ec5f (patch)
tree5b2d1dc806e401b31b2de3d4ef7752e3635e6fc8
parentplugin: link/image: commit py node (diff)
hate this
-rw-r--r--lib/irc.ex6
-rw-r--r--lib/irc/connection.ex34
-rw-r--r--lib/irc/puppet_connection.ex16
-rw-r--r--lib/plugins/logger.ex3
-rw-r--r--lib/telegram/room.ex10
5 files changed, 36 insertions, 33 deletions
diff --git a/lib/irc.ex b/lib/irc.ex
index 1592955..2c7a468 100644
--- a/lib/irc.ex
+++ b/lib/irc.ex
@@ -4,14 +4,14 @@ defmodule Nola.Irc do
def env(), do: Nola.env(:irc, [])
def env(key, default \\ nil), do: Keyword.get(env(), key, default)
- def send_message_as(account, network, channel, text, force_puppet \\ false) do
+ def send_message_as(account, network, channel, text, force_puppet \\ false, meta \\ []) do
connection = Nola.Irc.Connection.get_network(network)
if connection && (force_puppet || Nola.Irc.PuppetConnection.whereis(account, connection)) do
- Nola.Irc.PuppetConnection.start_and_send_message(account, connection, channel, text)
+ Nola.Irc.PuppetConnection.start_and_send_message(account, connection, channel, text, meta)
else
user = Nola.UserTrack.find_by_account(network, account)
nick = if(user, do: user.nick, else: account.name)
- Nola.Irc.Connection.broadcast_message(network, channel, "<#{nick}> #{text}")
+ Nola.Irc.Connection.broadcast_message(network, channel, "<#{nick}> #{text}", meta)
end
end
diff --git a/lib/irc/connection.ex b/lib/irc/connection.ex
index 62d52d8..87da466 100644
--- a/lib/irc/connection.ex
+++ b/lib/irc/connection.ex
@@ -172,14 +172,14 @@ defmodule Nola.Irc.Connection do
GenServer.start_link(__MODULE__, [conn], name: {:global, conn.id})
end
- def broadcast_message(net, chan, message) do
- dispatch("conn", {:broadcast, net, chan, message}, Nola.Irc.ConnectionPubSub)
- end
- def broadcast_message(list, message) when is_list(list) do
- for {net, chan} <- list do
- broadcast_message(net, chan, message)
- end
+ def broadcast_message(net, chan, message, meta \\ []) do
+ dispatch("conn", {:broadcast, net, chan, message}, meta, Nola.Irc.ConnectionPubSub)
end
+ #def broadcast_message(list, message, meta \\ []) when is_list(list) do
+ # for {net, chan} <- list do
+ # broadcast_message(net, chan, message, meta)
+ # end
+ #send
def privmsg(channel, line) do
GenServer.cast(__MODULE__, {:privmsg, channel, line})
@@ -412,11 +412,11 @@ defmodule Nola.Irc.Connection do
def publish(pub), do: publish(pub, [])
def publish(m = %Nola.Message{trigger: nil}, keys) do
- dispatch(["messages"] ++ keys, {:irc, :text, m})
+ dispatch(["messages"] ++ keys, {:irc, :text, m}, Enum.into(m.meta, []))
end
def publish(m = %Nola.Message{trigger: t = %Nola.Trigger{trigger: trigger}}, keys) do
- dispatch(["triggers", "#{m.network}/#{m.channel}:triggers", "trigger:"<>trigger], {:irc, :trigger, trigger, m})
+ dispatch(["triggers", "#{m.network}/#{m.channel}:triggers", "trigger:"<>trigger], {:irc, :trigger, trigger, m}, Enum.into(m.meta, []))
end
def publish_event(net, event = %{type: _}) when is_binary(net) do
@@ -433,15 +433,17 @@ defmodule Nola.Irc.Connection do
dispatch("#{net}/#{chan}:events", {:irc, :event, event})
end
- def dispatch(keys, content, sub \\ Nola.PubSub)
+ def dispatch(keys, content, meta \\ [], sub \\ Nola.PubSub)
- def dispatch(key, content, sub) when is_binary(key), do: dispatch([key], content, sub)
- def dispatch(keys, content, sub) when is_list(keys) do
+ def dispatch(key, content, meta, sub) when is_binary(key), do: dispatch([key], content, meta, sub)
+ def dispatch(keys, content, meta, sub) when is_list(keys) do
+ Logger.debug("dispatching: #{inspect keys} #{inspect content}")
should_dispatch_data = should_dispatch_data(content)
+ origin = Keyword.get(meta, :origin, :no_origin)
for key <- keys do
spawn(fn() ->
Registry.dispatch(sub, key, fn h ->
- for {pid, reg} <- h, do: if(should_dispatch?(key, should_dispatch_data, reg), do: send(pid, content))
+ for {pid, reg} <- h, do: if(should_dispatch?(key, origin, should_dispatch_data, reg), do: send(pid, content))
end)
end)
end
@@ -461,10 +463,10 @@ defmodule Nola.Irc.Connection do
[]
end
- def should_dispatch?(_, [], _), do: true
- def should_dispatch?(_, data, reg) do
+ def should_dispatch?(_, _, [], _), do: true
+ def should_dispatch?(key, origin, data, reg) do
if plugin = Keyword.get(reg, :plugin) do
- !Enum.member?(data, plugin)
+ plugin != origin || !Enum.member?(data, plugin)
else
true
end
diff --git a/lib/irc/puppet_connection.ex b/lib/irc/puppet_connection.ex
index f5e2e5c..51b4c8a 100644
--- a/lib/irc/puppet_connection.ex
+++ b/lib/irc/puppet_connection.ex
@@ -35,11 +35,11 @@ defmodule Nola.Irc.PuppetConnection do
end
end
- def send_message(account = %Nola.Account{id: account_id}, connection = %Nola.Irc.Connection{id: connection_id}, channel, text) do
- GenServer.cast(name(account_id, connection_id), {:send_message, self(), channel, text})
+ def send_message(account = %Nola.Account{id: account_id}, connection = %Nola.Irc.Connection{id: connection_id}, channel, text, meta) do
+ GenServer.cast(name(account_id, connection_id), {:send_message, self(), channel, text, meta})
end
- def start_and_send_message(account = %Nola.Account{id: account_id}, connection = %Nola.Irc.Connection{id: connection_id}, channel, text) do
+ def start_and_send_message(account = %Nola.Account{id: account_id}, connection = %Nola.Irc.Connection{id: connection_id}, channel, text, meta) do
{:global, name} = name(account_id, connection_id)
pid = whereis(account, connection)
pid = if !pid do
@@ -50,7 +50,7 @@ defmodule Nola.Irc.PuppetConnection do
else
pid
end
- GenServer.cast(pid, {:send_message, self(), channel, text})
+ GenServer.cast(pid, {:send_message, self(), channel, text, meta})
end
def start(account = %Nola.Account{}, connection = %Nola.Irc.Connection{}) do
@@ -132,11 +132,11 @@ defmodule Nola.Irc.PuppetConnection do
{:noreply, %{state | buffer: []}}
end
- def handle_cast(cast = {:send_message, _pid, _channel, _text}, state = %{connected: false, buffer: buffer}) do
+ def handle_cast(cast = {:send_message, _pid, _channel, _text, _meta}, state = %{connected: false, buffer: buffer}) do
{:noreply, %{state | buffer: [cast | buffer]}}
end
- def handle_cast({:send_message, pid, channel, text}, state = %{connected: true}) do
+ def handle_cast({:send_message, pid, channel, text, pub_meta}, state = %{connected: true}) do
channels = if !Enum.member?(state.channels, channel) do
ExIRC.Client.join(state.client, channel)
[channel | state.channels]
@@ -145,12 +145,12 @@ defmodule Nola.Irc.PuppetConnection do
end
ExIRC.Client.msg(state.client, :privmsg, channel, text)
- meta = %{puppet: true, from: pid}
+ meta = %{puppet: true, from: pid, origin: Keyword.get(pub_meta, :origin, __MODULE__)}
account = Nola.Account.get(state.account_id)
nick = make_nick(state)
sender = %ExIRC.SenderInfo{network: state.network, nick: suffix_nick(nick), user: nick, host: "puppet."}
reply_fun = fn(text) ->
- Nola.Irc.Connection.broadcast_message(state.network, channel, text)
+ Nola.Irc.Connection.broadcast_message(state.network, channel, text, pub_meta)
end
message = %Nola.Message{id: FlakeId.get(), at: NaiveDateTime.utc_now(), text: text, network: state.network, account: account, sender: sender, channel: channel, replyfun: reply_fun, trigger: Nola.Irc.Connection.extract_trigger(text), meta: meta}
message = case Nola.UserTrack.messaged(message) do
diff --git a/lib/plugins/logger.ex b/lib/plugins/logger.ex
index 9b47cd8..46c2a5b 100644
--- a/lib/plugins/logger.ex
+++ b/lib/plugins/logger.ex
@@ -70,7 +70,8 @@ defmodule Nola.Plugins.Logger do
%{"_id" => id,
"type" => "nola.message:v1",
- "object" => msg}
+ "object" => %Nola.Message{msg | meta: Map.delete(msg.meta, :from)}
+ }
end
def format_to_db(anything) do
diff --git a/lib/telegram/room.ex b/lib/telegram/room.ex
index 7a8f427..9db551b 100644
--- a/lib/telegram/room.ex
+++ b/lib/telegram/room.ex
@@ -53,7 +53,7 @@ defmodule Nola.TelegramRoom do
%Nola.Irc.Connection{} -> {net, chan}
_ -> {nil, nil}
end
- _ = Couch.post(@couch, %{"_id" => id, "network" => net, "channel" => chan})
+ {:ok, _, _} = Couch.post(@couch, %{"_id" => id, "network" => net, "channel" => chan})
{:ok, tg_room} = room(to_string(id))
tg_room
end
@@ -102,14 +102,14 @@ defmodule Nola.TelegramRoom do
def handle_update(%{"message" => %{"from" => from = %{"id" => user_id}, "text" => text}}, _token, state) do
account = find_or_create_meta_account(from, state)
#connection = Nola.Irc.Connection.get_network(state.net)
- Nola.Irc.send_message_as(account, state.net, state.chan, text, true)
+ Nola.Irc.send_message_as(account, state.net, state.chan, text, true, origin: __MODULE__)
{:ok, state}
end
def handle_update(data = %{"message" => %{"from" => from = %{"id" => user_id}, "location" => %{"latitude" => lat, "longitude" => lon}}}, _token, state) do
account = find_or_create_meta_account(from, state)
#connection = Nola.Irc.Connection.get_network(state.net)
- Nola.Irc.send_message_as(account, state.net, state.chan, "@ #{lat}, #{lon}", true)
+ Nola.Irc.send_message_as(account, state.net, state.chan, "@ #{lat}, #{lon}", true, origin: __MODULE__)
{:ok, state}
end
@@ -128,7 +128,7 @@ defmodule Nola.TelegramRoom do
end
def handle_info({:irc, _, message = %Nola.Message{sender: %{nick: nick}, text: text}}, state) do
- if Map.get(message.meta, :from) == self() do
+ if Map.get(message.meta, :from) == self() || Map.get(message.meta, :origin) == __MODULE__ do
else
body = if Map.get(message.meta, :self), do: text, else: "<#{nick}> #{text}"
Nola.Telegram.send_message(state.id, body)
@@ -179,7 +179,7 @@ defmodule Nola.TelegramRoom do
path = NolaWeb.Router.Helpers.url(NolaWeb.Endpoint) <> "/files/#{s3path}"
txt = "#{type}: #{text}#{path}"
#connection = Nola.Irc.Connection.get_network(state.net)
- Nola.Irc.send_message_as(account, state.net, state.chan, txt, true)
+ Nola.Irc.send_message_as(account, state.net, state.chan, txt, true, origin: __MODULE__)
else
error ->
Telegram.Api.request(token, "sendMessage", chat_id: chat_id, text: "File upload failed, sorry.")