diff --git a/lib/core.ex b/lib/core.ex new file mode 100644 index 0000000..4ba9bb0 --- /dev/null +++ b/lib/core.ex @@ -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 + diff --git a/lib/core/join.ex b/lib/core/join.ex deleted file mode 100644 index f5340db..0000000 --- a/lib/core/join.ex +++ /dev/null @@ -1,11 +0,0 @@ -defmodule Omnibot.Core.Join do - use Omnibot.Module - alias Omnibot.State - - def on_join(channel, nick) do - cfg = State.cfg() - if nick == cfg.nick do - State.add_channel(channel) - end - end -end diff --git a/lib/core/welcome.ex b/lib/core/welcome.ex deleted file mode 100644 index 383536b..0000000 --- a/lib/core/welcome.ex +++ /dev/null @@ -1,11 +0,0 @@ -defmodule Omnibot.Core.Welcome do - use Omnibot.Module - - require Logger - - @impl true - def on_msg(irc, _) do - Logger.info("Syncing channels") - Irc.sync_channels(irc) - end -end diff --git a/lib/module_supervisor.ex b/lib/module_supervisor.ex index 7258104..df31ff3 100644 --- a/lib/module_supervisor.ex +++ b/lib/module_supervisor.ex @@ -14,11 +14,13 @@ defmodule Omnibot.ModuleSupervisor do compile_files(cfg.module_paths || []) # These are modules that need to be loaded for core functionality of the bot - #{Omnibot.Core.Welcome, cfg: [channels: :all]}, - #{Omnibot.Core.Join, cfg: [channels: :all]}, + core = [ + {Omnibot.Core, cfg: [channels: :all]}, + ] # Map the modules in the configuration to the children children = + core ++ for mod <- cfg.modules do case mod do {name, cfg} -> {name, cfg: cfg, name: name} @@ -27,7 +29,7 @@ defmodule Omnibot.ModuleSupervisor do end # Add each child to the "loaded modules" list in the State - Enum.each(children, fn module -> State.add_loaded_module(module) end) + Enum.each(IO.inspect(children), fn {module, opts} -> State.add_loaded_module({module, opts[:cfg]}) end) Supervisor.init(children, strategy: :one_for_one) end diff --git a/lib/router.ex b/lib/router.ex index f148882..6e444ef 100644 --- a/lib/router.ex +++ b/lib/router.ex @@ -3,9 +3,7 @@ defmodule Omnibot.Router do alias Omnibot.{Irc.Msg, State} def route(irc, msg) do - #channel = Msg.channel(msg) - channel = IO.inspect(Msg.channel(msg)) - IO.inspect(State.channel_modules(channel)) + channel = Msg.channel(msg) State.channel_modules(channel) |> Enum.each(fn {module, _} -> module.on_msg(irc, msg) end) end