summaryrefslogtreecommitdiff
path: root/lib/irc/puppet_connection.ex
diff options
context:
space:
mode:
authorhref <href@random.sh>2021-09-04 05:41:09 +0200
committerhref <href@random.sh>2021-09-04 05:41:09 +0200
commit735a8dd998b2c0aebde9c96daefc7aa2c223218e (patch)
tree4a165897c8557065dfab401455943524087f79af /lib/irc/puppet_connection.ex
parentchat_live improvements (diff)
matrix appservice, puppet improvements
Diffstat (limited to 'lib/irc/puppet_connection.ex')
-rw-r--r--lib/irc/puppet_connection.ex48
1 files changed, 39 insertions, 9 deletions
diff --git a/lib/irc/puppet_connection.ex b/lib/irc/puppet_connection.ex
index 59bce1b..da6cc93 100644
--- a/lib/irc/puppet_connection.ex
+++ b/lib/irc/puppet_connection.ex
@@ -35,7 +35,7 @@ defmodule IRC.PuppetConnection do
end
def send_message(account = %IRC.Account{id: account_id}, connection = %IRC.Connection{id: connection_id}, channel, text) do
- GenServer.cast(name(account_id, connection_id), {:send_message, channel, text})
+ GenServer.cast(name(account_id, connection_id), {:send_message, self(), channel, text})
end
def start_and_send_message(account = %IRC.Account{id: account_id}, connection = %IRC.Connection{id: connection_id}, channel, text) do
@@ -49,7 +49,7 @@ defmodule IRC.PuppetConnection do
else
pid
end
- GenServer.cast(pid, {:send_message, channel, text})
+ GenServer.cast(pid, {:send_message, self(), channel, text})
end
def start(account = %IRC.Account{}, connection = %IRC.Connection{}) do
@@ -103,11 +103,11 @@ defmodule IRC.PuppetConnection do
{:noreply, %{state | buffer: []}}
end
- def handle_cast(cast = {:send_message, channel, text}, state = %{connected: false, buffer: buffer}) do
+ def handle_cast(cast = {:send_message, _pid, _channel, _text}, state = %{connected: false, buffer: buffer}) do
{:noreply, %{state | buffer: [cast | buffer]}}
end
- def handle_cast({:send_message, channel, text}, state = %{connected: true}) do
+ def handle_cast({:send_message, pid, channel, text}, state = %{connected: true}) do
channels = if !Enum.member?(state.channels, channel) do
ExIRC.Client.join(state.client, channel)
[channel | state.channels]
@@ -116,6 +116,20 @@ defmodule IRC.PuppetConnection do
end
ExIRC.Client.msg(state.client, :privmsg, channel, text)
+ meta = %{puppet: true, from: pid}
+ account = IRC.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) ->
+ 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 = case IRC.UserTrack.messaged(message) do
+ :ok -> message
+ {:ok, message} -> message
+ end
+ IRC.Connection.publish(message, ["#{message.network}/#{channel}:messages"])
+
idle = if length(state.buffer) == 0 do
:erlang.cancel_timer(state.idle)
:erlang.send_after(@max_idle, self(), :idle)
@@ -147,11 +161,8 @@ defmodule IRC.PuppetConnection do
def handle_info({:connected, server, port}, state) do
Logger.info("#{inspect(self())} Connected to #{server}:#{port} #{inspect state}")
{_, backoff} = :backoff.succeed(state.backoff)
- account = IRC.Account.get(state.account_id)
- user = IRC.UserTrack.find_by_account(state.network, account)
- base_nick = if(user, do: user.nick, else: account.name)
- nick = "#{base_nick}[p]"
- ExIRC.Client.logon(state.client, "", nick, base_nick, "#{base_nick}'s puppet")
+ base_nick = make_nick(state)
+ ExIRC.Client.logon(state.client, "", suffix_nick(base_nick), base_nick, "#{base_nick}'s puppet")
{:noreply, %{state | backoff: backoff, connected_server: server, connected_port: port}}
end
@@ -159,6 +170,8 @@ defmodule IRC.PuppetConnection do
def handle_info(:logged_in, state) do
Logger.info("#{inspect(self())} Logged in")
{_, backoff} = :backoff.succeed(state.backoff)
+ # Create an UserTrack entry for the client so it's authenticated to the right account_id already.
+ IRC.UserTrack.connected(state.network, suffix_nick(make_nick(state)), make_nick(state), "puppet.", state.account_id, %{puppet: true})
{:noreply, %{state | backoff: backoff}}
end
@@ -176,4 +189,21 @@ defmodule IRC.PuppetConnection do
{:noreply, state}
end
+ def make_nick(state) do
+ account = IRC.Account.get(state.account_id)
+ user = IRC.UserTrack.find_by_account(state.network, account)
+ base_nick = if(user, do: user.nick, else: account.name)
+ clean_nick = case String.split(base_nick, ":", parts: 2) do
+ ["@"<>nick, _] -> nick
+ [nick] -> nick
+ end
+ clean_nick
+ end
+
+ if Mix.env == :dev do
+ def suffix_nick(nick), do: "#{nick}[d]"
+ else
+ def suffix_nick(nick), do: "#{nick}[p]"
+ end
+
end