diff options
Diffstat (limited to 'lib/irc')
-rw-r--r-- | lib/irc/connection.ex | 34 | ||||
-rw-r--r-- | lib/irc/puppet_connection.ex | 16 |
2 files changed, 26 insertions, 24 deletions
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 |