2020-06-12 17:29:35 -04:00
|
|
|
defmodule Omnibot.Irc do
|
|
|
|
|
require Logger
|
|
|
|
|
alias Omnibot.Irc.Msg
|
|
|
|
|
alias Omnibot.{Config, State}
|
|
|
|
|
use GenServer
|
|
|
|
|
|
|
|
|
|
## Client API
|
|
|
|
|
|
|
|
|
|
def start_link(opts \\ []) do
|
|
|
|
|
GenServer.start_link(__MODULE__, :ok, opts)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def send_msg(irc, msg) do
|
|
|
|
|
GenServer.cast(irc, {:send_msg, msg})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def send_msg(irc, command, params) when is_list(params) do
|
|
|
|
|
cfg = State.cfg()
|
|
|
|
|
GenServer.cast(irc, {:send_msg, Config.msg(cfg, command, params)})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def send_msg(irc, command, param), do: send_msg(irc, command, [param])
|
|
|
|
|
|
|
|
|
|
def send_to(irc, channel, text), do: send_msg(irc, "PRIVMSG", [channel, text])
|
|
|
|
|
|
|
|
|
|
def join(irc, channel), do: send_msg(irc, "JOIN", channel)
|
|
|
|
|
|
|
|
|
|
def part(irc, channel), do: send_msg(irc, "PART", channel)
|
|
|
|
|
|
2020-06-13 18:11:18 -04:00
|
|
|
defp route_msg(irc, msg) do
|
2020-07-02 18:30:46 -07:00
|
|
|
plugins = Msg.channel(msg) |> State.channel_plugins()
|
2020-06-14 18:00:31 -04:00
|
|
|
|
|
|
|
|
Task.Supervisor.async_stream_nolink(
|
|
|
|
|
Omnibot.RouterSupervisor,
|
2020-07-02 18:30:46 -07:00
|
|
|
plugins,
|
2020-07-12 12:19:14 -07:00
|
|
|
# Spin up tasks to handle messages in case handle_msg() hangs
|
|
|
|
|
fn {plugin, _plug_cfg} -> plugin.handle_msg(irc, msg) end,
|
2020-06-14 18:00:31 -04:00
|
|
|
timeout: 30_000,
|
|
|
|
|
on_timeout: :kill_task
|
|
|
|
|
) |> Stream.run()
|
|
|
|
|
|
2020-06-13 18:11:18 -04:00
|
|
|
end
|
|
|
|
|
|
2020-06-12 17:29:35 -04:00
|
|
|
## Server callbacks
|
|
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
|
def init(:ok) do
|
|
|
|
|
cfg = State.cfg()
|
|
|
|
|
_ssl = cfg.ssl
|
|
|
|
|
|
|
|
|
|
{:ok, socket} =
|
|
|
|
|
:gen_tcp.connect(to_charlist(cfg.server), cfg.port, [:binary, active: false, packet: :line])
|
|
|
|
|
|
|
|
|
|
# Wait for first message
|
|
|
|
|
send_msg(self(), "NICK", cfg.nick)
|
|
|
|
|
send_msg(self(), "USER", [cfg.user, "0", "*", cfg.real])
|
2020-06-13 21:47:46 -04:00
|
|
|
:inet.setopts(socket, active: true)
|
2020-06-12 17:29:35 -04:00
|
|
|
|
|
|
|
|
{:ok, socket}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp write(socket, msg) do
|
|
|
|
|
msg = String.Chars.to_string(msg)
|
|
|
|
|
Logger.debug(">>> #{msg}")
|
|
|
|
|
:gen_tcp.send(socket, "#{msg}\r\n")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
|
def handle_cast({:send_msg, msg}, socket) do
|
|
|
|
|
write(socket, msg)
|
|
|
|
|
{:noreply, socket}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
|
def handle_info({:tcp, _socket, line}, socket) do
|
|
|
|
|
Logger.debug(String.trim(line))
|
|
|
|
|
msg = Msg.parse(line)
|
|
|
|
|
|
|
|
|
|
# Send the message to the router
|
2020-07-16 18:00:43 -07:00
|
|
|
if msg.prefix && (msg.prefix.nick != State.cfg().nick) do
|
|
|
|
|
route_msg(self(), msg)
|
|
|
|
|
end
|
2020-06-12 17:29:35 -04:00
|
|
|
{:noreply, socket}
|
|
|
|
|
end
|
|
|
|
|
end
|