Rename Module -> Plugin with basic find/replace
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
defmodule Omnibot.Contrib.Fortune do
|
||||
use Omnibot.Module
|
||||
use Omnibot.Plugin
|
||||
|
||||
@fortunes [
|
||||
"Reply hazy, try again",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
defmodule Omnibot.Contrib.Linkbot do
|
||||
use Omnibot.Module
|
||||
use Omnibot.Plugin
|
||||
require Logger
|
||||
|
||||
@default_config timeout: 30_000
|
||||
@@ -19,9 +19,6 @@ defmodule Omnibot.Contrib.Linkbot do
|
||||
plug Tesla.Middleware.FollowRedirects, max_redirects: 10
|
||||
plug Tesla.Middleware.Compression, format: "gzip"
|
||||
|
||||
|
||||
@title_regex ~r"<title>(?<title>.+)</title>"i
|
||||
|
||||
def get_title(url) do
|
||||
# 1. check for "meta" tag (in the header) with a "property" attribute of "og:title", and fetch the "content" attribute of that tag
|
||||
# 2. check for meta tag with attribute "name" == "title", and fetch "content" attribute
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
defmodule Omnibot.Contrib.Wordbot do
|
||||
use Omnibot.Module.Base
|
||||
use Omnibot.Plugin.Base
|
||||
use Supervisor
|
||||
require Logger
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
defmodule Omnibot.Contrib.Wordbot.Bot do
|
||||
use Omnibot.Module.Base
|
||||
use Omnibot.Module.GenServer
|
||||
use Omnibot.Plugin.Base
|
||||
use Omnibot.Plugin.GenServer
|
||||
|
||||
alias Omnibot.{Contrib.Wordbot, Irc, State, Util}
|
||||
require Logger
|
||||
@@ -128,7 +128,7 @@ defmodule Omnibot.Contrib.Wordbot.Bot do
|
||||
start_round(irc, channel)
|
||||
end
|
||||
|
||||
## Module callbacks
|
||||
## Plugin callbacks
|
||||
|
||||
@impl true
|
||||
def on_init(cfg) do
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
defmodule Omnibot.Core do
|
||||
use Omnibot.Module
|
||||
use Omnibot.Plugin
|
||||
alias Omnibot.State
|
||||
|
||||
@default_config [channels: :all]
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
defmodule Omnibot.Module.Agent do
|
||||
defmacro __using__([]) do
|
||||
quote do
|
||||
alias Omnibot.Module
|
||||
use Agent
|
||||
|
||||
def start_link(opts) do
|
||||
cfg = opts[:cfg]
|
||||
state = opts[:state] || on_init(cfg)
|
||||
Module.Agent.start_link(cfg, state, opts)
|
||||
end
|
||||
|
||||
def cfg, do: Module.Agent.cfg(__MODULE__)
|
||||
def state, do: Module.Agent.state(__MODULE__)
|
||||
|
||||
def update_state(update, timeout \\ 5000),
|
||||
do: Module.Agent.update_state(__MODULE__, update, timeout)
|
||||
end
|
||||
end
|
||||
|
||||
def start_link(cfg, state, opts) do
|
||||
Agent.start_link(fn -> {cfg, state} end, opts)
|
||||
end
|
||||
|
||||
def cfg(agent) do
|
||||
Agent.get(agent, fn {cfg, _} -> cfg end)
|
||||
end
|
||||
|
||||
def state(agent) do
|
||||
Agent.get(agent, fn {_, state} -> state end)
|
||||
end
|
||||
|
||||
def update_state(agent, update, timeout \\ 5000) do
|
||||
Agent.update(
|
||||
agent,
|
||||
fn {cfg, state} -> {cfg, apply(update, [state])} end,
|
||||
timeout
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -1,129 +0,0 @@
|
||||
defmodule Omnibot.Module.Base do
|
||||
defmodule Hooks do
|
||||
defmacro __before_compile__(_env) do
|
||||
quote generated: true do
|
||||
@impl true
|
||||
def on_channel_msg(_irc, _channel, _nick, _line), do: nil
|
||||
|
||||
@impl true
|
||||
def on_channel_msg(_irc, _channel, _nick, _cmd, _params), do: nil
|
||||
|
||||
@impl true
|
||||
def on_join(_irc, _channel, _nick), do: nil
|
||||
|
||||
@impl true
|
||||
def on_part(_irc, _channel, _nick), do: nil
|
||||
|
||||
@impl true
|
||||
def on_kick(_irc, _channel, _nick, _target), do: nil
|
||||
|
||||
@impl true
|
||||
def on_init(_cfg), do: nil
|
||||
|
||||
@impl true
|
||||
def default_config(), do: @default_config
|
||||
|
||||
def commands(), do: MapSet.to_list(@commands)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro __using__([]) do
|
||||
quote do
|
||||
alias Omnibot.{Irc, Module}
|
||||
import Omnibot.Module.Base
|
||||
|
||||
@behaviour Module.Base
|
||||
|
||||
@impl Module.Base
|
||||
def on_msg(irc, msg) do
|
||||
# TODO - instead of using a router for modules, consider using a PubSub with a Registry:
|
||||
# https://hexdocs.pm/elixir/master/Registry.html#module-using-as-a-pubsub
|
||||
route_msg(irc, msg)
|
||||
end
|
||||
|
||||
defp route_msg(irc, msg) do
|
||||
nick = msg.prefix.nick
|
||||
case String.upcase(msg.command) do
|
||||
"PRIVMSG" ->
|
||||
[channel | params] = msg.params
|
||||
line = Enum.join(params, " ")
|
||||
|
||||
case String.split(line, " ") do
|
||||
[cmd | params] -> if Enum.member?(commands(), cmd),
|
||||
do: on_channel_msg(irc, channel, nick, cmd, params),
|
||||
else: on_channel_msg(irc, channel, nick, line)
|
||||
_ -> on_channel_msg(irc, channel, nick, line)
|
||||
end
|
||||
|
||||
"JOIN" ->
|
||||
[channel | _] = msg.params
|
||||
on_join(irc, channel, nick)
|
||||
|
||||
"PART" ->
|
||||
[channel | _] = msg.params
|
||||
on_part(irc, channel, nick)
|
||||
|
||||
"KICK" ->
|
||||
[channel, target | _] = msg.params
|
||||
on_kick(irc, channel, nick, target)
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defoverridable Module.Base
|
||||
|
||||
@commands MapSet.new()
|
||||
@default_config []
|
||||
@before_compile Omnibot.Module.Base.Hooks
|
||||
end
|
||||
end
|
||||
|
||||
@callback on_msg(irc :: pid(), msg :: %Omnibot.Irc.Msg{}) :: any
|
||||
@callback on_channel_msg(irc :: pid(), channel :: String.t(), nick :: String.t(), line :: String.t()) :: any
|
||||
@callback on_channel_msg(
|
||||
irc :: pid(),
|
||||
channel :: String.t(),
|
||||
nick :: String.t(),
|
||||
cmd :: String.t(),
|
||||
params :: [String.t()]
|
||||
) :: any
|
||||
@callback on_join(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_init(cfg :: any) :: any
|
||||
@callback default_config() :: any
|
||||
|
||||
defmacro command(cmd, opts) do
|
||||
quote generated: true do
|
||||
@commands MapSet.put(@commands, unquote(cmd))
|
||||
@impl Omnibot.Module.Base
|
||||
def on_channel_msg(var!(irc), var!(channel), var!(nick), unquote(cmd), var!(params)) do
|
||||
unquote(opts[:do])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro command(cmd, params, opts) do
|
||||
params =
|
||||
Enum.map(
|
||||
params,
|
||||
fn param ->
|
||||
case param do
|
||||
{_, _, _} -> quote(do: var!(unquote(param)))
|
||||
lit -> Macro.escape(lit)
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
quote generated: true do
|
||||
@commands MapSet.put(@commands, unquote(cmd))
|
||||
@impl Omnibot.Module.Base
|
||||
def on_channel_msg(var!(irc), var!(channel), var!(nick), unquote(cmd), unquote(params)) do
|
||||
unquote(opts[:do])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,50 +0,0 @@
|
||||
defmodule Omnibot.Module.GenServer do
|
||||
defmacro __using__([]) do
|
||||
quote do
|
||||
alias Omnibot.Module
|
||||
use GenServer
|
||||
|
||||
def start_link(opts) do
|
||||
cfg = opts[:cfg]
|
||||
state = opts[:state]
|
||||
GenServer.start_link(__MODULE__, {cfg, state}, opts)
|
||||
end
|
||||
|
||||
def cfg() do
|
||||
GenServer.call(__MODULE__, :cfg)
|
||||
end
|
||||
|
||||
def state() do
|
||||
GenServer.call(__MODULE__, :state)
|
||||
end
|
||||
|
||||
def update_state(update) do
|
||||
GenServer.cast(__MODULE__, {:state, update})
|
||||
end
|
||||
|
||||
## Server callbacks
|
||||
|
||||
@impl GenServer
|
||||
def init({cfg, state}) do
|
||||
state = state || on_init(cfg)
|
||||
{:ok, {cfg, state}}
|
||||
end
|
||||
|
||||
@impl GenServer
|
||||
def handle_call(:cfg, _from, {cfg, state}) do
|
||||
{:reply, cfg, {cfg, state}}
|
||||
end
|
||||
|
||||
@impl GenServer
|
||||
def handle_call(:state, _from, {cfg, state}) do
|
||||
{:reply, state, {cfg, state}}
|
||||
end
|
||||
|
||||
@impl GenServer
|
||||
def handle_cast({:state, update}, {cfg, state}) do
|
||||
{:noreply, {cfg, apply(update, [state])}}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
defmodule Omnibot.Module do
|
||||
defmacro __using__([]) do
|
||||
quote do
|
||||
use Omnibot.Module.Base
|
||||
use Omnibot.Module.Agent
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
defmodule Omnibot.ModuleSupervisor do
|
||||
defmodule Omnibot.PluginSupervisor do
|
||||
@moduledoc false
|
||||
|
||||
use Supervisor
|
||||
|
||||
@@ -16,7 +16,7 @@ defmodule Omnibot.Supervisor do
|
||||
children = [
|
||||
{Task.Supervisor, name: Omnibot.RouterSupervisor, strategy: :one_for_one},
|
||||
{Omnibot.State, cfg: cfg, name: Omnibot.State},
|
||||
{Omnibot.ModuleSupervisor, cfg: cfg, name: Omnibot.ModuleSupervisor},
|
||||
{Omnibot.PluginSupervisor, cfg: cfg, name: Omnibot.PluginSupervisor},
|
||||
] ++ unless IEx.started?(),
|
||||
do: [{Omnibot.Irc, name: Omnibot.Irc}],
|
||||
else: []
|
||||
|
||||
Reference in New Issue
Block a user