summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorhref <href@random.sh>2022-12-11 01:47:26 +0000
committerJordan Bracco <href@random.sh>2022-12-11 02:03:36 +0000
commitd97e6d09f0db87989df42993499429883f1c224a (patch)
tree314132c1d68b271979c293318b6bf8e7aacbedce /lib
parentfix(user-track): ensure we only get an user per network even if it's over mul... (diff)
feat(message): assign id (flake_id)
Diffstat (limited to 'lib')
-rw-r--r--lib/irc/connection.ex10
-rw-r--r--lib/irc/irc.ex6
-rw-r--r--lib/irc/puppet_connection.ex48
-rw-r--r--lib/lsg_irc/logger_plugin.ex15
-rw-r--r--lib/lsg_irc/sms_plugin.ex3
-rw-r--r--lib/lsg_telegram/telegram.ex1
6 files changed, 50 insertions, 33 deletions
diff --git a/lib/irc/connection.ex b/lib/irc/connection.ex
index fcb6c5e..1e94003 100644
--- a/lib/irc/connection.ex
+++ b/lib/irc/connection.ex
@@ -294,7 +294,9 @@ defmodule IRC.Connection do
if !Map.get(user.options, :puppet) do
reply_fun = fn(text) -> irc_reply(state, {chan, sender}, text) end
account = IRC.Account.lookup(sender)
- message = %IRC.Message{at: NaiveDateTime.utc_now(), text: text, network: state.network, account: account, sender: sender, channel: chan, replyfun: reply_fun, trigger: extract_trigger(text)}
+ message = %IRC.Message{id: FlakeId.get(), transport: :irc, at: NaiveDateTime.utc_now(), text: text, network: state.network,
+ account: account, sender: sender, channel: chan, replyfun: reply_fun,
+ trigger: extract_trigger(text)}
message = case IRC.UserTrack.messaged(message) do
:ok -> message
{:ok, message} -> message
@@ -309,7 +311,8 @@ defmodule IRC.Connection do
def handle_info({:received, text, sender}, state) do
reply_fun = fn(text) -> irc_reply(state, {sender.nick, sender}, text) end
account = IRC.Account.lookup(sender)
- message = %IRC.Message{text: text, network: state.network, account: account, sender: sender, replyfun: reply_fun, trigger: extract_trigger(text)}
+ message = %IRC.Message{id: FlakeId.get(), transport: irc, text: text, network: state.network, at: NaiveDateTime.utc_now(),
+ account: account, sender: sender, replyfun: reply_fun, trigger: extract_trigger(text)}
message = case IRC.UserTrack.messaged(message) do
:ok -> message
{:ok, message} -> message
@@ -460,7 +463,8 @@ defmodule IRC.Connection do
|> List.flatten()
outputs = for line <- lines do
ExIRC.Client.msg(client, :privmsg, target, line)
- {:irc, :out, %IRC.Message{network: network, channel: target, text: line, sender: %ExIRC.SenderInfo{nick: state.conn.nick}, at: NaiveDateTime.utc_now(), meta: %{self: true}}}
+ {:irc, :out, %IRC.Message{id: FlakeId.get(), transport: :irc, network: network,
+ channel: target, text: line, sender: %ExIRC.SenderInfo{nick: state.conn.nick}, at: NaiveDateTime.utc_now(), meta: %{self: true}}}
end
for f <- outputs, do: dispatch(["irc:outputs", "#{network}/#{target}:outputs"], f)
end
diff --git a/lib/irc/irc.ex b/lib/irc/irc.ex
index 78b0611..fbad6e1 100644
--- a/lib/irc/irc.ex
+++ b/lib/irc/irc.ex
@@ -1,8 +1,9 @@
defmodule IRC do
- @derive {Poison.Encoder, except: [:replyfun]}
defmodule Message do
- defstruct [:text,
+ @derive {Poison.Encoder, except: [:replyfun]}
+ defstruct [:id,
+ :text,
{:transport, :irc},
:network,
:account,
@@ -15,6 +16,7 @@ defmodule IRC do
]
end
defmodule Trigger do
+ @derive Poison.Encoder
defstruct [:type, :trigger, :args]
end
diff --git a/lib/irc/puppet_connection.ex b/lib/irc/puppet_connection.ex
index 4604b04..f12cbf7 100644
--- a/lib/irc/puppet_connection.ex
+++ b/lib/irc/puppet_connection.ex
@@ -76,14 +76,14 @@ defmodule IRC.PuppetConnection do
end
def handle_continue(:connect, state) do
- ipv6 = if @env == :prod do
- subnet = LSG.Subnet.assign(state.account_id)
- IRC.Account.put_meta(IRC.Account.get(state.account_id), "subnet", subnet)
- ip = Pfx.host(subnet, 1)
- {:ok, ipv6} = :inet_parse.ipv6_address(to_charlist(ip))
- System.cmd("add-ip6", [ip])
- ipv6
- end
+ #ipv6 = if @env == :prod do
+ # subnet = LSG.Subnet.assign(state.account_id)
+ # IRC.Account.put_meta(IRC.Account.get(state.account_id), "subnet", subnet)
+ # ip = Pfx.host(subnet, 1)
+ # {:ok, ipv6} = :inet_parse.ipv6_address(to_charlist(ip))
+ # System.cmd("add-ip6", [ip])
+ # ipv6
+ #end
conn = IRC.Connection.lookup(state.connection_id)
client_opts = []
@@ -102,21 +102,21 @@ defmodule IRC.PuppetConnection do
{:nodelay, true}
]
- {ip, opts} = case {ipv6, :inet_res.resolve(to_charlist(conn.host), :in, :aaaa)} do
- {ipv6, {:ok, {:dns_rec, _dns_header, _query, rrs = [{:dns_rr, _, _, _, _, _, _, _, _, _} | _], _, _}}} ->
- ip = rrs
- |> Enum.map(fn({:dns_rr, _, :aaaa, :in, _, _, ipv6, _, _, _}) -> ipv6 end)
- |> Enum.shuffle()
- |> List.first()
-
- opts = [
- :inet6,
- {:ifaddr, ipv6}
- ]
- {ip, opts}
- _ ->
- {to_charlist(conn.host), []}
- end
+ #{ip, opts} = case {ipv6, :inet_res.resolve(to_charlist(conn.host), :in, :aaaa)} do
+ # {ipv6, {:ok, {:dns_rec, _dns_header, _query, rrs = [{:dns_rr, _, _, _, _, _, _, _, _, _} | _], _, _}}} ->
+ # ip = rrs
+ # |> Enum.map(fn({:dns_rr, _, :aaaa, :in, _, _, ipv6, _, _, _}) -> ipv6 end)
+ # |> Enum.shuffle()
+ # |> List.first()
+
+ # opts = [
+ # :inet6,
+ # {:ifaddr, ipv6}
+ # ]
+ # {ip, opts}
+ # _ ->
+ {ip, opts} = {to_charlist(conn.host), []}
+ #end
conn_fun = if conn.tls, do: :connect_ssl!, else: :connect!
apply(ExIRC.Client, conn_fun, [client, ip, conn.port, base_opts ++ opts])
@@ -152,7 +152,7 @@ defmodule IRC.PuppetConnection do
reply_fun = fn(text) ->
IRC.Connection.broadcast_message(state.network, channel, text)
end
- message = %IRC.Message{at: NaiveDateTime.utc_now(), text: text, network: state.network, account: account, sender: sender, channel: channel, replyfun: reply_fun, trigger: IRC.Connection.extract_trigger(text), meta: meta}
+ message = %IRC.Message{id: FlakeId.get(), at: NaiveDateTime.utc_now(), text: text, network: state.network, account: account, sender: sender, channel: channel, replyfun: reply_fun, trigger: IRC.Connection.extract_trigger(text), meta: meta}
message = case IRC.UserTrack.messaged(message) do
:ok -> message
{:ok, message} -> message
diff --git a/lib/lsg_irc/logger_plugin.ex b/lib/lsg_irc/logger_plugin.ex
index 667f714..e5307bc 100644
--- a/lib/lsg_irc/logger_plugin.ex
+++ b/lib/lsg_irc/logger_plugin.ex
@@ -13,6 +13,7 @@ defmodule LSG.IRC.LoggerPlugin do
regopts = [plugin: __MODULE__]
{:ok, _} = Registry.register(IRC.PubSub, "triggers", regopts)
{:ok, _} = Registry.register(IRC.PubSub, "messages", regopts)
+ {:ok, _} = Registry.register(IRC.PubSub, "messages:private", regopts)
{:ok, nil}
end
@@ -49,12 +50,20 @@ defmodule LSG.IRC.LoggerPlugin do
state
end
- def format_to_db(m = %IRC.Message{}) do
- %IRC.Message{m | replyfun: nil}
+ def format_to_db(msg = %IRC.Message{id: id}) do
+ msg
+ |> Poison.encode!()
+ |> Map.drop("id")
+
+ %{"_id" => id || FlakeId.get(),
+ "type" => "irc.message/v1",
+ "object" => msg}
end
def format_to_db(anything) do
- anything
+ %{"_id" => FlakeId.get(),
+ "type" => "object",
+ "object" => anything}
end
end
diff --git a/lib/lsg_irc/sms_plugin.ex b/lib/lsg_irc/sms_plugin.ex
index b183f7d..be1611f 100644
--- a/lib/lsg_irc/sms_plugin.ex
+++ b/lib/lsg_irc/sms_plugin.ex
@@ -34,6 +34,7 @@ defmodule LSG.IRC.SmsPlugin do
"!"<>message
end
message = %IRC.Message{
+ id: FlakeId.get(),
transport: :sms,
network: "sms",
channel: nil,
@@ -43,7 +44,7 @@ defmodule LSG.IRC.SmsPlugin do
replyfun: reply_fun,
trigger: IRC.Connection.extract_trigger(trigger_text)
}
- IO.puts("converted sms to message: #{inspect message}")
+ Logger.debug("converted sms to message: #{inspect message}")
IRC.Connection.publish(message, ["messages:sms"])
message
end
diff --git a/lib/lsg_telegram/telegram.ex b/lib/lsg_telegram/telegram.ex
index 63940dc..748a456 100644
--- a/lib/lsg_telegram/telegram.ex
+++ b/lib/lsg_telegram/telegram.ex
@@ -196,6 +196,7 @@ defmodule LSG.Telegram do
"!"<>text
end
message = %IRC.Message{
+ id: FlakeId.get(),
transport: :telegram,
network: "telegram",
channel: nil,