Add Omnibot.Plugin.Base.on_connect/1 callback
This new callback gets called immediately upon connecting to the server. It is automatically implemented as a no-op by default. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -29,9 +29,16 @@ defmodule Omnibot.Irc do
|
|||||||
|
|
||||||
def cfg(irc), do: GenServer.call(irc, :cfg)
|
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
|
defp route_msg(irc, cfg, msg) do
|
||||||
plugins = Config.channel_plugins(cfg, Msg.channel(msg))
|
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(
|
Task.Supervisor.async_stream_nolink(
|
||||||
Omnibot.RouterSupervisor,
|
Omnibot.RouterSupervisor,
|
||||||
plugins,
|
plugins,
|
||||||
@@ -40,7 +47,6 @@ defmodule Omnibot.Irc do
|
|||||||
timeout: 30_000,
|
timeout: 30_000,
|
||||||
on_timeout: :kill_task
|
on_timeout: :kill_task
|
||||||
) |> Stream.run()
|
) |> Stream.run()
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
## Server callbacks
|
## Server callbacks
|
||||||
@@ -57,6 +63,7 @@ defmodule Omnibot.Irc do
|
|||||||
send_msg(self(), "USER", [cfg.user, "0", "*", cfg.real])
|
send_msg(self(), "USER", [cfg.user, "0", "*", cfg.real])
|
||||||
:inet.setopts(socket, active: true)
|
:inet.setopts(socket, active: true)
|
||||||
|
|
||||||
|
route_msg(self(), cfg, :connect)
|
||||||
{:ok, {socket, cfg}}
|
{:ok, {socket, cfg}}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ defmodule Omnibot.Plugin.Base do
|
|||||||
@impl true
|
@impl true
|
||||||
def on_init(_cfg), do: nil
|
def on_init(_cfg), do: nil
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def on_connect(_irc), do: nil
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def on_msg(irc, msg) do
|
def on_msg(irc, msg) do
|
||||||
route_msg(irc, msg)
|
route_msg(irc, msg)
|
||||||
@@ -38,6 +41,8 @@ defmodule Omnibot.Plugin.Base do
|
|||||||
|
|
||||||
@behaviour Plugin.Base
|
@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, %Irc.Msg {prefix: nil}), do: nil
|
||||||
|
|
||||||
defp route_msg(irc, msg) do
|
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_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_kick(irc :: pid(), channel :: String.t(), nick :: String.t(), target :: String.t()) :: any
|
||||||
@callback on_init(cfg :: any) :: any
|
@callback on_init(cfg :: any) :: any
|
||||||
|
@callback on_connect(irc :: pid()) :: any
|
||||||
@callback default_config() :: any
|
@callback default_config() :: any
|
||||||
|
|
||||||
defmacro command(cmd, opts) do
|
defmacro command(cmd, opts) do
|
||||||
|
|||||||
Reference in New Issue
Block a user