2020-06-12 17:29:35 -04:00
|
|
|
defmodule Omnibot.Config do
|
2020-07-21 18:09:15 -07:00
|
|
|
@moduledoc """
|
|
|
|
|
The full configuration for an instance of Omnibot.
|
|
|
|
|
|
|
|
|
|
This configuration structure contains both the IRC server connection information, along with all
|
|
|
|
|
plugins and their configuration.
|
|
|
|
|
|
|
|
|
|
## Configuration keys
|
|
|
|
|
|
|
|
|
|
- server: the IRC server to connect to. **(required)**
|
|
|
|
|
- nick: the IRC nickname to use for the bot. **(optional, default "omnibot")**
|
|
|
|
|
- user: the IRC username to use for the bot. **(optional, default "omnibot")**
|
|
|
|
|
- real: the IRC realname to use for the bot. **(optional, default "omnibot")**
|
|
|
|
|
- port: the port to connect to the server on. **(optional, default 6667)**
|
|
|
|
|
- ssl: whether to use SSL or not. Note that IRC usually uses port 6697 for
|
|
|
|
|
SSL. **(optional, default false)**
|
|
|
|
|
- plugins: a list of plugins to load and their configuration. **(optional, default [])**
|
|
|
|
|
- plugin_paths: a list of locations to look for additional plugins. **(optional, default [])**
|
|
|
|
|
|
|
|
|
|
## Plugins configuration
|
|
|
|
|
|
|
|
|
|
Inside the `plugins` key listed above, a list of plugins is expected. Plugins are either a single
|
|
|
|
|
atom, or a tuple of the plugin and a keyword list of configuration.
|
|
|
|
|
|
|
|
|
|
### Plugin channels
|
|
|
|
|
|
|
|
|
|
All plugins have a `channels` configuration value, determining which channels this plugin is
|
|
|
|
|
active in. This value may also be set to the atom value `:all`, which indicates that it is active
|
|
|
|
|
on all channels.
|
|
|
|
|
|
|
|
|
|
At least one plugin in the configuration list must contain a list of channels, so that the bot
|
|
|
|
|
knows which channels to join.
|
|
|
|
|
|
|
|
|
|
## Example configuration
|
|
|
|
|
|
|
|
|
|
iex> %Omnibot.Config {
|
|
|
|
|
...> nick: "omnibot_testing",
|
|
|
|
|
...> server: "irc.bonerjamz.us",
|
|
|
|
|
...> port: 6667,
|
|
|
|
|
...> ssl: false,
|
|
|
|
|
...> plugins: [
|
|
|
|
|
...> {Omnibot.Contrib.OnConnect, commands: [
|
|
|
|
|
...> ["privmsg", "nickserv", "register", "password123", "omnibot@omni.bot"],
|
|
|
|
|
...> ["privmsg", "nickserv", "identify", "password123"]
|
|
|
|
|
...> ]},
|
|
|
|
|
...> {Omnibot.Contrib.Linkbot, channels: :all},
|
|
|
|
|
...> {Omnibot.Contrib.Fortune, channels: :all},
|
|
|
|
|
...> {Omnibot.Contrib.Wordbot, channels: ["#idleville"], ignore: ["username"]},
|
|
|
|
|
...> ],
|
|
|
|
|
...> plugin_paths: [{"plugins", recurse: true}]
|
|
|
|
|
...> }
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2020-06-12 17:29:35 -04:00
|
|
|
alias Omnibot.Irc.Msg
|
|
|
|
|
|
|
|
|
|
@enforce_keys [:server]
|
|
|
|
|
defstruct [
|
|
|
|
|
:server,
|
|
|
|
|
nick: "omnibot",
|
|
|
|
|
user: "omnibot",
|
|
|
|
|
real: "omnibot",
|
|
|
|
|
port: 6667,
|
|
|
|
|
ssl: false,
|
2020-07-02 18:30:46 -07:00
|
|
|
plugins: [],
|
|
|
|
|
plugin_paths: []
|
2020-06-12 17:29:35 -04:00
|
|
|
]
|
|
|
|
|
|
2020-07-21 18:09:15 -07:00
|
|
|
@doc """
|
2020-07-02 18:30:46 -07:00
|
|
|
Gets all channels that the bot should join via its plugins.
|
2020-07-21 18:09:15 -07:00
|
|
|
|
|
|
|
|
## Parameters
|
|
|
|
|
|
|
|
|
|
- cfg: the configuration value to operate on.
|
|
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
|
|
iex> cfg = %Omnibot.Config {
|
|
|
|
|
...> server: "irc.example.com",
|
|
|
|
|
...> plugins: [
|
|
|
|
|
...> {ExamplePlugin, channels: ["#omnibot", "#example"]},
|
|
|
|
|
...> {OtherPlugin, channels: ["#example"]}
|
|
|
|
|
...> ]
|
|
|
|
|
...> }
|
|
|
|
|
iex> Omnibot.Config.all_channels(cfg)
|
|
|
|
|
["#example", "#omnibot"]
|
2020-06-12 17:29:35 -04:00
|
|
|
"""
|
|
|
|
|
def all_channels(cfg) do
|
2020-07-02 18:30:46 -07:00
|
|
|
Enum.flat_map(cfg.plugins, fn
|
2020-06-13 16:04:19 -04:00
|
|
|
{_, [channels: :all]} -> []
|
|
|
|
|
{_, [channels: channels]} -> channels
|
|
|
|
|
end)
|
2020-06-12 17:29:35 -04:00
|
|
|
|> MapSet.new()
|
|
|
|
|
|> MapSet.to_list()
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-21 18:09:15 -07:00
|
|
|
@doc """
|
|
|
|
|
Creates an `Omnibot.Irc.Msg.Prefix` from this configuration to be sent to a server.
|
|
|
|
|
|
|
|
|
|
## Parameters
|
|
|
|
|
|
|
|
|
|
- cfg: the configuration value to operate on.
|
|
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
|
|
iex> cfg = %Omnibot.Config { server: "irc.example.com" }
|
|
|
|
|
iex> Omnibot.Config.msg_prefix(cfg)
|
|
|
|
|
%Omnibot.Irc.Msg.Prefix {:nick => "omnibot", :user => "omnibot", :host => nil}
|
|
|
|
|
"""
|
2020-06-12 17:29:35 -04:00
|
|
|
def msg_prefix(cfg) do
|
|
|
|
|
%Msg.Prefix {
|
|
|
|
|
nick: cfg.nick,
|
|
|
|
|
user: cfg.user,
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-21 18:09:15 -07:00
|
|
|
@doc """
|
2020-06-12 17:29:35 -04:00
|
|
|
Make a new message with the given command and parameters using the given
|
|
|
|
|
configuration to build the prefix.
|
2020-07-21 18:09:15 -07:00
|
|
|
|
|
|
|
|
## Parameters
|
|
|
|
|
|
|
|
|
|
- cfg: the configuration value to operate on.
|
|
|
|
|
- command: the IRC command to send
|
|
|
|
|
- params: a list of parameters to pass to the IRC command.
|
|
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
|
|
iex> cfg = %Omnibot.Config { server: "irc.example.com" }
|
|
|
|
|
iex> Omnibot.Config.msg(cfg, "PRIVMSG", ["#testing", "this is a test message"])
|
|
|
|
|
%Omnibot.Irc.Msg{
|
|
|
|
|
prefix: %Omnibot.Irc.Msg.Prefix {:nick => "omnibot", :user => "omnibot", :host => nil},
|
|
|
|
|
command: "PRIVMSG",
|
|
|
|
|
params: ["#testing", "this is a test message"],
|
|
|
|
|
}
|
2020-06-12 17:29:35 -04:00
|
|
|
"""
|
|
|
|
|
def msg(cfg, command, params \\ []) do
|
|
|
|
|
%Msg {
|
|
|
|
|
prefix: msg_prefix(cfg),
|
|
|
|
|
command: command,
|
|
|
|
|
params: params,
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|