diff --git a/lib/irc/irc.ex b/lib/irc/irc.ex index dfc11a3..cb290e6 100644 --- a/lib/irc/irc.ex +++ b/lib/irc/irc.ex @@ -29,9 +29,16 @@ defmodule Omnibot.Irc do def cfg(irc), do: GenServer.call(irc, :cfg) + defp route_msg(irc, cfg, :connect) do + handle_msg(irc, cfg.plugins, :connect) + end + defp route_msg(irc, cfg, msg) do plugins = Config.channel_plugins(cfg, Msg.channel(msg)) + handle_msg(irc, plugins, msg) + end + defp handle_msg(irc, plugins, msg) do Task.Supervisor.async_stream_nolink( Omnibot.RouterSupervisor, plugins, @@ -40,7 +47,6 @@ defmodule Omnibot.Irc do timeout: 30_000, on_timeout: :kill_task ) |> Stream.run() - end ## Server callbacks @@ -57,6 +63,7 @@ defmodule Omnibot.Irc do send_msg(self(), "USER", [cfg.user, "0", "*", cfg.real]) :inet.setopts(socket, active: true) + route_msg(self(), cfg, :connect) {:ok, {socket, cfg}} end diff --git a/lib/plugin/base.ex b/lib/plugin/base.ex index 4afff9f..6c625db 100644 --- a/lib/plugin/base.ex +++ b/lib/plugin/base.ex @@ -19,6 +19,9 @@ defmodule Omnibot.Plugin.Base do @impl true def on_init(_cfg), do: nil + @impl true + def on_connect(_irc), do: nil + @impl true def on_msg(irc, msg) do route_msg(irc, msg) @@ -38,6 +41,8 @@ defmodule Omnibot.Plugin.Base do @behaviour Plugin.Base + defp route_msg(irc, :connect), do: on_connect(irc) + defp route_msg(irc, %Irc.Msg {prefix: nil}), do: nil defp route_msg(irc, msg) do @@ -95,6 +100,7 @@ defmodule Omnibot.Plugin.Base do @callback on_part(irc :: pid(), channel :: String.t(), nick :: String.t()) :: any @callback on_kick(irc :: pid(), channel :: String.t(), nick :: String.t(), target :: String.t()) :: any @callback on_init(cfg :: any) :: any + @callback on_connect(irc :: pid()) :: any @callback default_config() :: any defmacro command(cmd, opts) do