Compare commits

...

2 Commits

Author SHA1 Message Date
Rachel Fae Fox 501a26fb13 fix warnings where possible. 2022-11-10 13:10:04 -05:00
Rachel Fae Fox bd396dc3d4 Removed Supervisor.Spec
Supervisor.Spec is deprecated so moved to the modern format.
2022-11-10 13:08:14 -05:00
6 changed files with 101 additions and 96 deletions

View File

@ -23,7 +23,7 @@ defmodule Discordirc.DiscordHandler do
{:ok, webhook} ->
webhook.user.id == Nostrum.Snowflake.dump(me.id)
{:error, e} ->
{:error, _e} ->
false
end
@ -33,8 +33,6 @@ defmodule Discordirc.DiscordHandler do
end
def handle_event({:MESSAGE_CREATE, msg, _ws_state}) do
{:ok, me} = Api.get_current_user()
unless is_me_or_my_webhook(msg) do
case ChannelMap.irc(msg.channel_id) do
{:ok, net, _} ->

View File

@ -4,23 +4,17 @@ defmodule Discordirc do
"""
use Application
alias Discordirc.IRC
alias Discordirc.IrcNetworkSupervisor
alias Discordirc.DiscordHandler
alias Discordirc.WebhookService
def start(_type, _args) do
import Supervisor.Spec
children = [
{DiscordHandler, []},
{WebhookService, []},
{IrcNetworkSupervisor, []}
]
ircnets =
Application.get_env(:discordirc, :networks)
|> Enum.map(fn net -> worker(IRC, [net], id: net.network) end)
children =
ircnets ++
[
Discordirc.DiscordHandler,
Discordirc.WebhookService
]
options = [strategy: :one_for_one, name: Discordirc.Supervisor]
Supervisor.start_link(children, options)
Supervisor.start_link(children, strategy: :one_for_one)
end
end

View File

@ -2,12 +2,11 @@ defmodule Discordirc.Formatter do
@moduledoc """
Transforms messages to/from discord from/to irc
"""
alias Nostrum.Api, as: DiscordAPI
alias Discordirc.DiscordInfo
@discordcdn "https://cdn.discordapp.com/"
def from_irc(nick, msg, ctcp \\ false) do
def from_irc(_nick, msg, ctcp \\ false) do
# strip or replace IRC formatting.
fmsg =
msg

View File

@ -34,7 +34,6 @@ defmodule Discordirc.IRC do
alias ExIRC.Whois
alias Discordirc.ChannelMap
alias Discordirc.Formatter
alias Nostrum.Api, as: DiscordAPI
def start_link(%{:network => network} = params) when is_map(params) do
state = %State{State.from_params(params) | :channels => ChannelMap.getircchannels(network)}
@ -68,6 +67,67 @@ defmodule Discordirc.IRC do
{:noreply, state}
end
def handle_info({:connected, server, port}, state) do
Logger.debug("Connected to #{server}:#{port}")
Logger.debug("Logging to #{server}:#{port} as #{state.nick}..")
Client.logon(state.client, state.pass, state.nick, state.user, state.name)
{:noreply, state}
end
def handle_info({:received, msg, %SenderInfo{:nick => nick}, channel}, state) do
discordid = ChannelMap.discord(state.network, channel)
fmsg = Formatter.from_irc(nick, msg, false)
case discordid do
{:ok, x} ->
send(
:WebhookService,
{:irc, %{channel_id: x, nick: "#{nick}@#{state.network}", content: fmsg}}
)
end
{:noreply, state}
end
def handle_info({:whois, whois = %Whois{:hostname => host}}, state) do
case whois do
%Whois{nick: n, user: user} when n == state.nick ->
me = "#{state.nick}!#{user}@#{host}"
Logger.debug("Setting host to #{me} #{inspect(whois)}")
{:noreply, %State{state | :me => me}}
_ ->
{:noreply, state}
end
end
def handle_info({:unrecognized, "396", %{args: _args}}, state) do
Logger.debug("Received UnrealIRCD host change notification, double checking host")
Client.whois(state.client, state.nick)
{:noreply, state}
end
def handle_info({:me, msg, %SenderInfo{:nick => nick}, channel}, state) do
discordid = ChannelMap.discord(state.network, channel)
fmsg = Formatter.from_irc(nick, msg, true)
case discordid do
{:ok, x} ->
send(
:WebhookService,
{:irc, %{channel_id: x, nick: "#{nick}@#{state.network}", content: fmsg}}
)
end
{:noreply, state}
end
# lets try using the supervisor instead...
def handle_info(:disconnected, state) do
Logger.debug("Disconnected, throwing self to hell.")
{:stop, "disconnection", state}
end
def handle_info({:discordmsg, msg}, state) do
channel = ChannelMap.irc(msg.channel_id)
@ -112,89 +172,23 @@ defmodule Discordirc.IRC do
{:noreply, state}
end
def handle_info({:discord_cmd, :kick, users}) do
end
def handle_info({:discord_cmd, :ban, users}) do
end
def handle_info({:discord_cmd, :mode, modestr}) do
end
def handle_info({:discord_cmd, :topic, topic}) do
end
def handle_info({:connected, server, port}, state) do
Logger.debug("Connected to #{server}:#{port}")
Logger.debug("Logging to #{server}:#{port} as #{state.nick}..")
Client.logon(state.client, state.pass, state.nick, state.user, state.name)
def handle_info({:discord_cmd, :kick, _users}, state) do
{:noreply, state}
end
def handle_info({:received, msg, %SenderInfo{:nick => nick}, channel}, state) do
discordid = ChannelMap.discord(state.network, channel)
fmsg = Formatter.from_irc(nick, msg, false)
case discordid do
{:ok, x} ->
send(
:WebhookService,
{:irc, %{channel_id: x, nick: "#{nick}@#{state.network}", content: fmsg}}
)
end
def handle_info({:discord_cmd, :ban, _users}, state) do
{:noreply, state}
end
def handle_info({:whois, whois = %Whois{:hostname => host}}, state) do
case whois do
%Whois{nick: n, user: user} when n == state.nick ->
me = "#{state.nick}!#{user}@#{host}"
Logger.debug("Setting host to #{me} #{inspect(whois)}")
{:noreply, %State{state | :me => me}}
_ ->
{:noreply, state}
end
end
def handle_info({:unrecognized, "396", %{args: args}}, state) do
Logger.debug("Received UnrealIRCD host change notification, double checking host")
Client.whois(state.client, state.nick)
def handle_info({:discord_cmd, :mode, _modestr}, state) do
{:noreply, state}
end
def handle_info({:me, msg, %SenderInfo{:nick => nick}, channel}, state) do
discordid = ChannelMap.discord(state.network, channel)
fmsg = Formatter.from_irc(nick, msg, true)
case discordid do
{:ok, x} ->
send(
:WebhookService,
{:irc, %{channel_id: x, nick: "#{nick}@#{state.network}", content: fmsg}}
)
end
def handle_info({:discord_cmd, :topic, _topic}, state) do
{:noreply, state}
end
# lets try using the supervisor instead...
def handle_info(:disconnected, state) do
Logger.debug("Disconnected, throwing self to hell.")
{:stop, "disconnection", state}
end
# def handle_info(:disconnected, state) do
# if state.ssl? do
# Client.connect_ssl!(state.client, state.server, state.port)
# else
# Client.connect!(state.client, state.server, state.port)
# end
#
# {:noreply, state}
# end
# this MUST be the last handle_info.
def handle_info(event, state) do
Logger.debug("unknown event: inspect output: " <> inspect(event))
{:noreply, state}

22
lib/irc_supervisor.ex Executable file
View File

@ -0,0 +1,22 @@
defmodule Discordirc.IrcNetworkSupervisor do
@moduledoc """
Supervises all of the IRC networks.
"""
use Supervisor
alias Discordirc.IRC
def start_link(init_arg) do
Supervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end
@impl true
def init(_init_arg) do
networks =
Application.get_env(:discordirc, :networks)
|> Enum.map(&%{start: {IRC, :start_link, [&1]}, id: &1.network})
Supervisor.init(networks, strategy: :one_for_one)
end
end

View File

@ -6,8 +6,6 @@ defmodule Discordirc.WebhookService do
use GenServer
require Logger
alias Nostrum.Api, as: DiscordAPI
alias Nostrum.Cache, as: DiscordCache
alias Nostrum.Error.ApiError
defmodule State do
defstruct hooks: nil
@ -94,10 +92,10 @@ defmodule Discordirc.WebhookService do
DiscordAPI.execute_webhook(wh.id, wh.token, args)
rescue
e in MatchError ->
Logger.warn("MatchError from nostrum workaround in place.")
Logger.warn("MatchError from nostrum workaround in place. e: #{inspect(e)}")
e in FunctionClauseError ->
Logger.warn("FunctionClauseError from nostrum workaround in place.")
Logger.warn("FunctionClauseError from nostrum workaround in place. #{inspect(e)}")
end
{:noreply, state}