Skip to content

Commit

Permalink
The last remnants of the old IRCHandlers have been swept away
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Apr 18, 2021
1 parent 1b7883f commit 1555ee7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 127 deletions.
5 changes: 2 additions & 3 deletions MCGalaxy/Modules/Relay/Discord/DiscordBot.cs
Expand Up @@ -100,9 +100,8 @@ public sealed class DiscordBot : RelayBot {

RelayUser user = new RelayUser();
user.Nick = (string)author["username"];
user.UserID = (string)author["id"];

HandleChannelMessage(user, channel, message, false);
user.UserID = (string)author["id"];
HandleChannelMessage(user, channel, message);
}

string GetStatus() {
Expand Down
198 changes: 84 additions & 114 deletions MCGalaxy/Modules/Relay/IRC/IRCBot.cs
Expand Up @@ -16,13 +16,9 @@
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using MCGalaxy.Events.PlayerEvents;
using MCGalaxy.Events.ServerEvents;
using MCGalaxy.Modules.Relay;
using MCGalaxy.Network;
using Sharkbite.Irc;

namespace MCGalaxy {
Expand All @@ -42,11 +38,8 @@ public sealed class IRCBot : RelayBot {

public IRCBot() {
nicks = new IRCNickList();
nicks.bot = this;

SetDefaultBannedCommands();
nicks.bot = this;
UpdateState();
InitConnectionState();
}


Expand All @@ -61,20 +54,20 @@ public sealed class IRCBot : RelayBot {
message = ConvertMessage(message);
connection.Sender.PublicMessage(channel, message);
}

public void Join(string channel) {
if (String.IsNullOrEmpty(channel)) return;
connection.Sender.Join(channel);
}

public void Raw(string message) {
if (!Enabled || !Connected) return;
connection.Sender.Raw(message);
}

void Join(string channel) {
if (String.IsNullOrEmpty(channel)) return;
connection.Sender.Join(channel);
}


protected override void DoConnect() {
InitConnectionState();
if (connection == null) connection = new Connection(new UTF8Encoding(false), args);
Hook();
UpdateState();

Expand All @@ -89,12 +82,6 @@ public sealed class IRCBot : RelayBot {
}


void InitConnectionState() {
if (!Server.Config.UseIRC || connection != null) return;
connection = new Connection(new UTF8Encoding(false), args);
LoadBannedCommands();
}

void UpdateState() {
Channels = Server.Config.IRCChannels.SplitComma();
OpChannels = Server.Config.IRCOpChannels.SplitComma();
Expand Down Expand Up @@ -179,96 +166,97 @@ public sealed class IRCBot : RelayBot {


protected override bool CanUseCommands(RelayUser user, string cmdName, out string error) {
return CheckIRCCommand(user.Nick, cmdName, out error);
}

// TODO remove
public void HandlePublic(string nick, string channel, string message, bool opchat) {
RelayUser user = new RelayUser();
user.Nick = nick;
HandleChannelMessage(user, channel, message, opchat);
}

public void HandlePrivate(string nick, string message) {
RelayUser user = new RelayUser();
user.Nick = nick;
HandleUserMessage(user, message);
error = null;
string nick = user.Nick;
if (!Server.ircControllers.Contains(nick)) return false;

bool foundAtAll = false;
foreach (string chan in Channels) {
if (nicks.VerifyNick(chan, nick, ref error, ref foundAtAll)) return true;
}
foreach (string chan in OpChannels) {
if (nicks.VerifyNick(chan, nick, ref error, ref foundAtAll)) return true;
}

if (!foundAtAll) {
error = "You are not on the bot's list of users for some reason, please leave and rejoin."; return false;
}
if (BannedCommands.CaselessContains(cmdName)) {
error = "You are not allowed to use this command from IRC.";
}
return false;
}


volatile bool hookedEvents = false;

/// <summary> Hooks IRC events so they are handled. </summary>
void Hook() {
if (hookedEvents) return;
hookedEvents = true;
HookEvents();

// Regster events for incoming
connection.Listener.OnNick += Listener_OnNick;
connection.Listener.OnRegistered += Listener_OnRegistered;
connection.Listener.OnAction += Listener_OnAction;
connection.Listener.OnPublic += Listener_OnPublic;
connection.Listener.OnPrivate += Listener_OnPrivate;
connection.Listener.OnError += Listener_OnError;
connection.Listener.OnQuit += Listener_OnQuit;
connection.Listener.OnJoin += Listener_OnJoin;
connection.Listener.OnPart += Listener_OnPart;
connection.Listener.OnDisconnected += Listener_OnDisconnected;
connection.Listener.OnChannelModeChange += Listener_OnChannelModeChange;
connection.Listener.OnNames += Listener_OnNames;
connection.Listener.OnKick += Listener_OnKick;
connection.Listener.OnKill += Listener_OnKill;
connection.Listener.OnPrivateNotice += Listener_OnPrivateNotice;
connection.Listener.OnNick += OnNick;
connection.Listener.OnRegistered += OnRegistered;
connection.Listener.OnAction += OnAction;
connection.Listener.OnPublic += OnPublic;
connection.Listener.OnPrivate += OnPrivate;
connection.Listener.OnError += OnError;
connection.Listener.OnQuit += OnQuit;
connection.Listener.OnJoin += OnJoin;
connection.Listener.OnPart += OnPart;
connection.Listener.OnDisconnected += OnDisconnected;
connection.Listener.OnChannelModeChange += OnChannelModeChange;
connection.Listener.OnNames += OnNames;
connection.Listener.OnKick += OnKick;
connection.Listener.OnKill += OnKill;
connection.Listener.OnPrivateNotice += OnPrivateNotice;
}

/// <summary> Unhooks IRC events so they are no longer handled. </summary>
void Unhook() {
if (!hookedEvents) return;
hookedEvents = false;
UnhookEvents();

// Regster events for incoming
connection.Listener.OnNick -= Listener_OnNick;
connection.Listener.OnRegistered -= Listener_OnRegistered;
connection.Listener.OnAction -= Listener_OnAction;
connection.Listener.OnPublic -= Listener_OnPublic;
connection.Listener.OnPrivate -= Listener_OnPrivate;
connection.Listener.OnError -= Listener_OnError;
connection.Listener.OnQuit -= Listener_OnQuit;
connection.Listener.OnJoin -= Listener_OnJoin;
connection.Listener.OnPart -= Listener_OnPart;
connection.Listener.OnDisconnected -= Listener_OnDisconnected;
connection.Listener.OnChannelModeChange -= Listener_OnChannelModeChange;
connection.Listener.OnNames -= Listener_OnNames;
connection.Listener.OnKick -= Listener_OnKick;
connection.Listener.OnKill -= Listener_OnKill;
connection.Listener.OnPrivateNotice -= Listener_OnPrivateNotice;
connection.Listener.OnNick -= OnNick;
connection.Listener.OnRegistered -= OnRegistered;
connection.Listener.OnAction -= OnAction;
connection.Listener.OnPublic -= OnPublic;
connection.Listener.OnPrivate -= OnPrivate;
connection.Listener.OnError -= OnError;
connection.Listener.OnQuit -= OnQuit;
connection.Listener.OnJoin -= OnJoin;
connection.Listener.OnPart -= OnPart;
connection.Listener.OnDisconnected -= OnDisconnected;
connection.Listener.OnChannelModeChange -= OnChannelModeChange;
connection.Listener.OnNames -= OnNames;
connection.Listener.OnKick -= OnKick;
connection.Listener.OnKill -= OnKill;
connection.Listener.OnPrivateNotice -= OnPrivateNotice;
}


void Listener_OnAction(UserInfo user, string channel, string description) {
void OnAction(UserInfo user, string channel, string description) {
MessageInGame(user.Nick, string.Format("&I(IRC) * {0} {1}", user.Nick, description));
}

void Listener_OnJoin(UserInfo user, string channel) {
void OnJoin(UserInfo user, string channel) {
connection.Sender.Names(channel);
DoJoinLeaveMessage(user.Nick, "joined", channel);
AnnounceJoinLeave(user.Nick, "joined", channel);
}

void Listener_OnPart(UserInfo user, string channel, string reason) {
void OnPart(UserInfo user, string channel, string reason) {
nicks.OnLeftChannel(user, channel);
if (user.Nick == nick) return;
DoJoinLeaveMessage(user.Nick, "left", channel);
AnnounceJoinLeave(user.Nick, "left", channel);
}

void DoJoinLeaveMessage(string nick, string verb, string channel) {
void AnnounceJoinLeave(string nick, string verb, string channel) {
Logger.Log(LogType.RelayActivity, "{0} {1} channel {2}", nick, verb, channel);
string which = OpChannels.CaselessContains(channel) ? " operator" : "";
MessageInGame(nick, string.Format("&I(IRC) {0} {1} the{2} channel", nick, verb, which));
}

void Listener_OnQuit(UserInfo user, string reason) {
void OnQuit(UserInfo user, string reason) {
// Old bot was disconnected, try to reclaim it
if (user.Nick == nick) connection.Sender.Nick(nick);
nicks.OnLeft(user);
Expand All @@ -278,50 +266,32 @@ public sealed class IRCBot : RelayBot {
MessageInGame(user.Nick, "&I(IRC) " + user.Nick + " left");
}

void Listener_OnError(ReplyCode code, string message) {
void OnError(ReplyCode code, string message) {
Logger.Log(LogType.RelayActivity, "IRC Error: " + message);
}

void Listener_OnPrivate(UserInfo user, string message) {
message = IRCBot.ParseMessage(message);
HandlePrivate(user.Nick, message);
void OnPrivate(UserInfo user, string message) {
RelayUser rUser = new RelayUser();
rUser.Nick = user.Nick;

message = ParseMessage(message);
HandleUserMessage(rUser, message);
}

void Listener_OnPublic(UserInfo user, string channel, string message) {
void OnPublic(UserInfo user, string channel, string message) {
message = message.TrimEnd();
if (message.Length == 0) return;
bool opchat = OpChannels.CaselessContains(channel);

message = IRCBot.ParseMessage(message);
HandlePublic(user.Nick, channel, message, opchat);
}

public bool CheckIRCCommand(string nick, string cmdName, out string error) {
error = null;
if (!Server.ircControllers.Contains(nick)) return false;

bool foundAtAll = false;
foreach (string chan in Channels) {
if (nicks.VerifyNick(chan, nick, ref error, ref foundAtAll)) return true;
}
foreach (string chan in OpChannels) {
if (nicks.VerifyNick(chan, nick, ref error, ref foundAtAll)) return true;
}
message = ParseMessage(message);

if (!foundAtAll) {
error = "You are not on the bot's list of users for some reason, please leave and rejoin."; return false;
}
if (BannedCommands.CaselessContains(cmdName)) {
error = "You are not allowed to use this command from IRC.";
}
return false;
RelayUser rUser = new RelayUser();
rUser.Nick = nick;
HandleChannelMessage(rUser, channel, message);
}


void Listener_OnRegistered() {
void OnRegistered() {
Logger.Log(LogType.RelayActivity, "Connected to IRC!");
resetting = false;
retries = 0;
retries = 0;

Authenticate();
JoinChannels();
Expand All @@ -333,7 +303,7 @@ public sealed class IRCBot : RelayBot {
foreach (string chan in OpChannels) { Join(chan); }
}

void Listener_OnPrivateNotice(UserInfo user, string notice) {
void OnPrivateNotice(UserInfo user, string notice) {
if (!notice.CaselessStarts("You are now identified")) return;
JoinChannels();
}
Expand All @@ -348,11 +318,11 @@ public sealed class IRCBot : RelayBot {
}
}

void Listener_OnDisconnected() {
void OnDisconnected() {
if (!resetting && retries < 3) { retries++; Connect(); }
}

void Listener_OnNick(UserInfo user, string newNick) {
void OnNick(UserInfo user, string newNick) {
//Chat.MessageGlobal(Server.IRCColor + "(IRC) " + user.Nick + " changed nick to " + newNick);
// We have successfully reclaimed our nick, so try to sign in again.
if (newNick == nick) Authenticate();
Expand All @@ -362,23 +332,23 @@ public sealed class IRCBot : RelayBot {
MessageInGame(user.Nick, "&I(IRC) " + user.Nick + " &Sis now known as &I" + newNick);
}

void Listener_OnNames(string channel, string[] _nicks, bool last) {
void OnNames(string channel, string[] _nicks, bool last) {
nicks.UpdateFor(channel, _nicks);
}

void Listener_OnChannelModeChange(UserInfo who, string channel) {
void OnChannelModeChange(UserInfo who, string channel) {
connection.Sender.Names(channel);
}

void Listener_OnKick(UserInfo user, string channel, string kickee, string reason) {
void OnKick(UserInfo user, string channel, string kickee, string reason) {
nicks.OnLeftChannel(user, channel);

if (reason.Length > 0) reason = " (" + reason + ")";
Logger.Log(LogType.RelayActivity, "{0} kicked {1} from IRC{2}", user.Nick, kickee, reason);
MessageInGame(user.Nick, "&I(IRC) " + user.Nick + " kicked " + kickee + reason);
}

void Listener_OnKill(UserInfo user, string nick, string reason) {
void OnKill(UserInfo user, string nick, string reason) {
nicks.OnLeft(user);
}
}
Expand Down
17 changes: 7 additions & 10 deletions MCGalaxy/Modules/RelayBot.cs
Expand Up @@ -82,6 +82,7 @@ public abstract class RelayBot {
Logger.Log(LogType.RelayActivity, "Connecting to {0}...", RelayName);

try {
LoadBannedCommands();
DoConnect();
} catch (Exception e) {
Logger.Log(LogType.RelayActivity, "Failed to connect to {0}!", RelayName);
Expand All @@ -103,15 +104,10 @@ public abstract class RelayBot {
}

protected abstract void DoConnect();
protected abstract void DoDisconnect(string reason);


protected void SetDefaultBannedCommands() {
protected abstract void DoDisconnect(string reason);

void LoadBannedCommands() {
BannedCommands = new List<string>() { "resetbot", "resetirc", "oprules", "irccontrollers", "ircctrl" };
}

protected void LoadBannedCommands() {
SetDefaultBannedCommands();

if (!File.Exists("text/irccmdblacklist.txt")) {
File.WriteAllLines("text/irccmdblacklist.txt", new string[] {
Expand Down Expand Up @@ -207,11 +203,12 @@ public abstract class RelayBot {
HandleRelayCommand(user, null, cmdName, cmdArgs);
}

protected void HandleChannelMessage(RelayUser user, string channel, string message, bool opchat) {
protected void HandleChannelMessage(RelayUser user, string channel, string message) {
string[] parts = message.SplitSpaces(3);
string rawCmd = parts[0].ToLower();
if (HandleWhoCommand(user, channel, rawCmd, opchat)) return;
bool opchat = OpChannels.CaselessContains(channel);

if (HandleWhoCommand(user, channel, rawCmd, opchat)) return;
if (rawCmd.CaselessEq(Server.Config.IRCCommandPrefix)) {
if (!HandleChannelCommand(user, channel, message, parts)) return;
}
Expand Down

0 comments on commit 1555ee7

Please sign in to comment.