Compare commits
2 Commits
4fac5895db
...
501a26fb13
Author | SHA1 | Date |
---|---|---|
Rachel Fae Fox | 501a26fb13 | |
Rachel Fae Fox | bd396dc3d4 |
|
@ -23,7 +23,7 @@ defmodule Discordirc.DiscordHandler do
|
||||||
{:ok, webhook} ->
|
{:ok, webhook} ->
|
||||||
webhook.user.id == Nostrum.Snowflake.dump(me.id)
|
webhook.user.id == Nostrum.Snowflake.dump(me.id)
|
||||||
|
|
||||||
{:error, e} ->
|
{:error, _e} ->
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -33,8 +33,6 @@ defmodule Discordirc.DiscordHandler do
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event({:MESSAGE_CREATE, msg, _ws_state}) do
|
def handle_event({:MESSAGE_CREATE, msg, _ws_state}) do
|
||||||
{:ok, me} = Api.get_current_user()
|
|
||||||
|
|
||||||
unless is_me_or_my_webhook(msg) do
|
unless is_me_or_my_webhook(msg) do
|
||||||
case ChannelMap.irc(msg.channel_id) do
|
case ChannelMap.irc(msg.channel_id) do
|
||||||
{:ok, net, _} ->
|
{:ok, net, _} ->
|
||||||
|
|
|
@ -4,23 +4,17 @@ defmodule Discordirc do
|
||||||
"""
|
"""
|
||||||
use Application
|
use Application
|
||||||
|
|
||||||
alias Discordirc.IRC
|
alias Discordirc.IrcNetworkSupervisor
|
||||||
|
alias Discordirc.DiscordHandler
|
||||||
|
alias Discordirc.WebhookService
|
||||||
|
|
||||||
def start(_type, _args) do
|
def start(_type, _args) do
|
||||||
import Supervisor.Spec
|
children = [
|
||||||
|
{DiscordHandler, []},
|
||||||
|
{WebhookService, []},
|
||||||
|
{IrcNetworkSupervisor, []}
|
||||||
|
]
|
||||||
|
|
||||||
ircnets =
|
Supervisor.start_link(children, strategy: :one_for_one)
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,12 +2,11 @@ defmodule Discordirc.Formatter do
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
Transforms messages to/from discord from/to irc
|
Transforms messages to/from discord from/to irc
|
||||||
"""
|
"""
|
||||||
alias Nostrum.Api, as: DiscordAPI
|
|
||||||
alias Discordirc.DiscordInfo
|
alias Discordirc.DiscordInfo
|
||||||
|
|
||||||
@discordcdn "https://cdn.discordapp.com/"
|
@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.
|
# strip or replace IRC formatting.
|
||||||
fmsg =
|
fmsg =
|
||||||
msg
|
msg
|
||||||
|
|
138
lib/irc_bot.ex
138
lib/irc_bot.ex
|
@ -34,7 +34,6 @@ defmodule Discordirc.IRC do
|
||||||
alias ExIRC.Whois
|
alias ExIRC.Whois
|
||||||
alias Discordirc.ChannelMap
|
alias Discordirc.ChannelMap
|
||||||
alias Discordirc.Formatter
|
alias Discordirc.Formatter
|
||||||
alias Nostrum.Api, as: DiscordAPI
|
|
||||||
|
|
||||||
def start_link(%{:network => network} = params) when is_map(params) do
|
def start_link(%{:network => network} = params) when is_map(params) do
|
||||||
state = %State{State.from_params(params) | :channels => ChannelMap.getircchannels(network)}
|
state = %State{State.from_params(params) | :channels => ChannelMap.getircchannels(network)}
|
||||||
|
@ -68,6 +67,67 @@ defmodule Discordirc.IRC do
|
||||||
{:noreply, state}
|
{:noreply, state}
|
||||||
end
|
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
|
def handle_info({:discordmsg, msg}, state) do
|
||||||
channel = ChannelMap.irc(msg.channel_id)
|
channel = ChannelMap.irc(msg.channel_id)
|
||||||
|
|
||||||
|
@ -112,89 +172,23 @@ defmodule Discordirc.IRC do
|
||||||
{:noreply, state}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_info({:discord_cmd, :kick, users}) do
|
def handle_info({:discord_cmd, :kick, _users}, state) 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)
|
|
||||||
{:noreply, state}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_info({:received, msg, %SenderInfo{:nick => nick}, channel}, state) do
|
def handle_info({:discord_cmd, :ban, _users}, 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}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_info({:whois, whois = %Whois{:hostname => host}}, state) do
|
def handle_info({:discord_cmd, :mode, _modestr}, 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}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_info({:me, msg, %SenderInfo{:nick => nick}, channel}, state) do
|
def handle_info({:discord_cmd, :topic, _topic}, 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}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
# lets try using the supervisor instead...
|
# this MUST be the last handle_info.
|
||||||
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
|
|
||||||
|
|
||||||
def handle_info(event, state) do
|
def handle_info(event, state) do
|
||||||
Logger.debug("unknown event: inspect output: " <> inspect(event))
|
Logger.debug("unknown event: inspect output: " <> inspect(event))
|
||||||
{:noreply, state}
|
{:noreply, state}
|
||||||
|
|
|
@ -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
|
|
@ -6,8 +6,6 @@ defmodule Discordirc.WebhookService do
|
||||||
use GenServer
|
use GenServer
|
||||||
require Logger
|
require Logger
|
||||||
alias Nostrum.Api, as: DiscordAPI
|
alias Nostrum.Api, as: DiscordAPI
|
||||||
alias Nostrum.Cache, as: DiscordCache
|
|
||||||
alias Nostrum.Error.ApiError
|
|
||||||
|
|
||||||
defmodule State do
|
defmodule State do
|
||||||
defstruct hooks: nil
|
defstruct hooks: nil
|
||||||
|
@ -94,10 +92,10 @@ defmodule Discordirc.WebhookService do
|
||||||
DiscordAPI.execute_webhook(wh.id, wh.token, args)
|
DiscordAPI.execute_webhook(wh.id, wh.token, args)
|
||||||
rescue
|
rescue
|
||||||
e in MatchError ->
|
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 ->
|
e in FunctionClauseError ->
|
||||||
Logger.warn("FunctionClauseError from nostrum workaround in place.")
|
Logger.warn("FunctionClauseError from nostrum workaround in place. #{inspect(e)}")
|
||||||
end
|
end
|
||||||
|
|
||||||
{:noreply, state}
|
{:noreply, state}
|
||||||
|
|
Loading…
Reference in New Issue