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