diff options
Diffstat (limited to '')
-rw-r--r-- | lib/irc.ex | 11 | ||||
-rw-r--r-- | lib/irc/puppet_connection.ex | 28 |
2 files changed, 34 insertions, 5 deletions
@@ -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 |