Previously, core behavior was handled in the Omnibot.Router module. However, since the module system is robust enough, this work has been delegated to the Omnibot.Core module. Additionally, the Omnibot.State module has been augmented to keep track of the modules that have been currently loaded, and methods that are used to get the list of loaded modules and routing information for those modules are coming from Omnibot.State as well (as opposed to Omnibot.Config). This is to avoid requiring the Omnibot.Core module be included in the config, plus avoiding modification of the runtime config after it has already been loaded. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
44 lines
1.1 KiB
Elixir
44 lines
1.1 KiB
Elixir
defmodule Omnibot.Core do
|
|
use Omnibot.Module
|
|
alias Omnibot.State
|
|
|
|
def on_join(irc, channel, nick) do
|
|
cfg = State.cfg()
|
|
if nick == cfg.nick do
|
|
State.add_channel(channel)
|
|
# Sync if we join a channel we shouldn't be in
|
|
if !Enum.member?(State.all_channels(), channel),
|
|
do: Irc.sync_channels(irc)
|
|
end
|
|
end
|
|
|
|
def on_part(irc, channel, nick) do
|
|
cfg = State.cfg()
|
|
if nick == cfg.nick do
|
|
State.remove_channel(channel)
|
|
# Sync if we join a channel we forcibly part a channel we shouldn't leave
|
|
if Enum.member?(State.all_channels(), channel),
|
|
do: Irc.sync_channels(irc)
|
|
end
|
|
end
|
|
|
|
def on_kick(irc, channel, _nick, target) do
|
|
cfg = State.cfg()
|
|
if target == cfg.nick do
|
|
State.remove_channel(channel)
|
|
# Generally, being kicked is not intentionally leaving a channel, so always sync here
|
|
Irc.sync_channels(irc)
|
|
end
|
|
end
|
|
|
|
@impl true
|
|
def on_msg(irc, msg) do
|
|
case String.upcase(msg.command) do
|
|
"001" -> Irc.sync_channels(irc)
|
|
"PING" -> Irc.send_msg(irc, "PONG", msg.params)
|
|
_ -> route_msg(irc, msg)
|
|
end
|
|
end
|
|
end
|
|
|