Remove Omnibot.State.cfg/0 from Omnibot.Irc module
Deprecating Omnibot.State: IRC module holds its own configuration now. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -6,8 +6,9 @@ defmodule Omnibot.Irc do
|
|||||||
|
|
||||||
## Client API
|
## Client API
|
||||||
|
|
||||||
def start_link(opts \\ []) do
|
def start_link(opts) do
|
||||||
GenServer.start_link(__MODULE__, :ok, opts)
|
{cfg, opts} = Keyword.pop!(opts, :cfg)
|
||||||
|
GenServer.start_link(__MODULE__, cfg, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_msg(irc, msg) do
|
def send_msg(irc, msg) do
|
||||||
@@ -15,8 +16,7 @@ defmodule Omnibot.Irc do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def send_msg(irc, command, params) when is_list(params) do
|
def send_msg(irc, command, params) when is_list(params) do
|
||||||
cfg = State.cfg()
|
GenServer.cast(irc, {:send_msg, command, params})
|
||||||
GenServer.cast(irc, {:send_msg, Config.msg(cfg, command, params)})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def send_msg(irc, command, param), do: send_msg(irc, command, [param])
|
def send_msg(irc, command, param), do: send_msg(irc, command, [param])
|
||||||
@@ -27,6 +27,8 @@ defmodule Omnibot.Irc do
|
|||||||
|
|
||||||
def part(irc, channel), do: send_msg(irc, "PART", channel)
|
def part(irc, channel), do: send_msg(irc, "PART", channel)
|
||||||
|
|
||||||
|
def cfg(irc), do: GenServer.call(irc, :cfg)
|
||||||
|
|
||||||
defp route_msg(irc, msg) do
|
defp route_msg(irc, msg) do
|
||||||
plugins = Msg.channel(msg) |> State.channel_plugins()
|
plugins = Msg.channel(msg) |> State.channel_plugins()
|
||||||
|
|
||||||
@@ -44,8 +46,7 @@ defmodule Omnibot.Irc do
|
|||||||
## Server callbacks
|
## Server callbacks
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def init(:ok) do
|
def init(cfg) do
|
||||||
cfg = State.cfg()
|
|
||||||
_ssl = cfg.ssl
|
_ssl = cfg.ssl
|
||||||
|
|
||||||
{:ok, socket} =
|
{:ok, socket} =
|
||||||
@@ -56,7 +57,7 @@ defmodule Omnibot.Irc do
|
|||||||
send_msg(self(), "USER", [cfg.user, "0", "*", cfg.real])
|
send_msg(self(), "USER", [cfg.user, "0", "*", cfg.real])
|
||||||
:inet.setopts(socket, active: true)
|
:inet.setopts(socket, active: true)
|
||||||
|
|
||||||
{:ok, socket}
|
{:ok, {socket, cfg}}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp write(socket, msg) do
|
defp write(socket, msg) do
|
||||||
@@ -66,19 +67,31 @@ defmodule Omnibot.Irc do
|
|||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_cast({:send_msg, msg}, socket) do
|
def handle_cast({:send_msg, command, params}, state = {socket, cfg}) do
|
||||||
|
msg = Config.msg(cfg, command, params)
|
||||||
write(socket, msg)
|
write(socket, msg)
|
||||||
{:noreply, socket}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_info({:tcp, _socket, line}, socket) do
|
def handle_cast({:send_msg, msg}, state = {socket, _cfg}) do
|
||||||
|
write(socket, msg)
|
||||||
|
{:noreply, state}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_call(:cfg, _from, state = {_socket, cfg}) do
|
||||||
|
{:reply, cfg, state}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_info({:tcp, _socket, line}, state) do
|
||||||
Logger.debug(String.trim(line))
|
Logger.debug(String.trim(line))
|
||||||
msg = Msg.parse(line)
|
msg = Msg.parse(line)
|
||||||
|
|
||||||
# Send the message to the router
|
# Send the message to the router
|
||||||
route_msg(self(), msg)
|
route_msg(self(), msg)
|
||||||
|
|
||||||
{:noreply, socket}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -15,16 +15,15 @@ defmodule Omnibot.Supervisor do
|
|||||||
|> Code.eval_file()
|
|> Code.eval_file()
|
||||||
cfg = bindings[:config]
|
cfg = bindings[:config]
|
||||||
|
|
||||||
|
# TODO : move cfg to its own process so reloading it is as simple as killing the process
|
||||||
children = [
|
children = [
|
||||||
{Task.Supervisor, name: Omnibot.RouterSupervisor, strategy: :one_for_one},
|
{Task.Supervisor, name: Omnibot.RouterSupervisor, strategy: :one_for_one},
|
||||||
{Omnibot.State, cfg: cfg, name: Omnibot.State},
|
{Omnibot.State, cfg: cfg, name: Omnibot.State},
|
||||||
{Omnibot.PluginSupervisor, cfg: cfg, name: Omnibot.PluginSupervisor},
|
{Omnibot.PluginManager, cfg: cfg, name: Omnibot.PluginManager},
|
||||||
] ++ unless IEx.started?(),
|
] ++ unless IEx.started?(),
|
||||||
do: [{Omnibot.Irc, name: Omnibot.Irc}],
|
do: [{Omnibot.Irc, cfg: cfg, name: Omnibot.Irc}],
|
||||||
else: []
|
else: []
|
||||||
|
|
||||||
# TODO : how to handle config reloading?
|
|
||||||
|
|
||||||
# :one_for_all here because the RouterSupervisor and IRC server are co-dependent
|
# :one_for_all here because the RouterSupervisor and IRC server are co-dependent
|
||||||
Supervisor.init(children, strategy: :one_for_all)
|
Supervisor.init(children, strategy: :one_for_all)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user