Finish migration to using a module to control core behavior

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>
This commit is contained in:
2020-06-13 18:01:40 -04:00
parent 304a8d4164
commit 4d27e30b88
5 changed files with 49 additions and 28 deletions

43
lib/core.ex Normal file
View File

@@ -0,0 +1,43 @@
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