mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-23 12:42:22 -04:00
IRC bot can now join multiple channels and opchannels.
This commit is contained in:
parent
69695f268d
commit
02e694649c
@ -30,7 +30,7 @@ namespace MCGalaxy {
|
|||||||
public sealed class IRCBot {
|
public sealed class IRCBot {
|
||||||
public const string ResetSignal = "\x0F\x03";
|
public const string ResetSignal = "\x0F\x03";
|
||||||
internal Connection connection;
|
internal Connection connection;
|
||||||
internal string channel, opchannel;
|
internal string[] channels, opchannels;
|
||||||
internal string nick, server;
|
internal string nick, server;
|
||||||
internal bool reset = false;
|
internal bool reset = false;
|
||||||
internal byte retries = 0;
|
internal byte retries = 0;
|
||||||
@ -51,8 +51,10 @@ namespace MCGalaxy {
|
|||||||
|
|
||||||
/// <summary> Sends an IRC message to either the normal or operator IRC channel. </summary>
|
/// <summary> Sends an IRC message to either the normal or operator IRC channel. </summary>
|
||||||
public void Say(string message, bool opchat = false, bool color = true) {
|
public void Say(string message, bool opchat = false, bool color = true) {
|
||||||
string chan = opchat ? opchannel : channel;
|
string[] chans = opchat ? opchannels : channels;
|
||||||
if (!String.IsNullOrEmpty(chan)) Message(chan, message, color);
|
foreach (string chan in channels) {
|
||||||
|
Message(chan, message, color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Sends an IRC private message to the given user. </summary>
|
/// <summary> Sends an IRC private message to the given user. </summary>
|
||||||
@ -138,8 +140,8 @@ namespace MCGalaxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void UpdateState() {
|
void UpdateState() {
|
||||||
channel = Server.ircChannel.Trim();
|
channels = GetChannels(Server.ircChannel);
|
||||||
opchannel = Server.ircOpChannel.Trim();
|
opchannels = GetChannels(Server.ircOpChannel);
|
||||||
nick = Server.ircNick.Replace(" ", "");
|
nick = Server.ircNick.Replace(" ", "");
|
||||||
server = Server.ircServer;
|
server = Server.ircServer;
|
||||||
|
|
||||||
@ -149,6 +151,12 @@ namespace MCGalaxy {
|
|||||||
args.ServerPassword = Server.ircIdentify && Server.ircPassword != "" ? Server.ircPassword : "*";
|
args.ServerPassword = Server.ircIdentify && Server.ircPassword != "" ? Server.ircPassword : "*";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static string[] GetChannels(string names) {
|
||||||
|
names = names.Trim().Replace(" ", "");
|
||||||
|
if (names.Length == 0) return new string[0];
|
||||||
|
return names.Split(',');
|
||||||
|
}
|
||||||
|
|
||||||
void SetDefaultBannedCommands() {
|
void SetDefaultBannedCommands() {
|
||||||
BannedCommands = new List<string>() { "resetbot", "resetirc", "oprules", "irccontrollers", "ircctrl" };
|
BannedCommands = new List<string>() { "resetbot", "resetirc", "oprules", "irccontrollers", "ircctrl" };
|
||||||
}
|
}
|
||||||
@ -170,7 +178,7 @@ namespace MCGalaxy {
|
|||||||
"#Here you can put commands that cannot be used from the IRC bot.",
|
"#Here you can put commands that cannot be used from the IRC bot.",
|
||||||
"#Lines starting with \"#\" are ignored." });
|
"#Lines starting with \"#\" are ignored." });
|
||||||
foreach (string line in File.ReadAllLines("text/irccmdblacklist.txt")) {
|
foreach (string line in File.ReadAllLines("text/irccmdblacklist.txt")) {
|
||||||
if (line[0] != '#') BannedCommands.Add(line);
|
if (line.Length > 0 && line[0] != '#') BannedCommands.Add(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,7 +153,7 @@ namespace MCGalaxy.Network {
|
|||||||
|
|
||||||
void DoJoinLeaveMessage(string who, string verb, string channel) {
|
void DoJoinLeaveMessage(string who, string verb, string channel) {
|
||||||
Server.s.Log(String.Format("{0} {1} channel {2}", who, verb, channel));
|
Server.s.Log(String.Format("{0} {1} channel {2}", who, verb, channel));
|
||||||
string which = channel.CaselessEq(bot.opchannel) ? " operator" : "";
|
string which = bot.opchannels.CaselessContains(channel) ? " operator" : "";
|
||||||
Player.GlobalIRCMessage(String.Format("%I(IRC) {0} {1} the{2} channel", who, verb, which));
|
Player.GlobalIRCMessage(String.Format("%I(IRC) {0} {1} the{2} channel", who, verb, which));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,8 +184,7 @@ namespace MCGalaxy.Network {
|
|||||||
Command.Search(ref cmdName, ref cmdArgs);
|
Command.Search(ref cmdName, ref cmdArgs);
|
||||||
|
|
||||||
string error;
|
string error;
|
||||||
string chan = String.IsNullOrEmpty(bot.channel) ? bot.opchannel : bot.channel;
|
if (!CheckIRCCommand(user, cmdName, out error)) {
|
||||||
if (!CheckIRCCommand(user, cmdName, chan, out error)) {
|
|
||||||
if (error != null) bot.Pm(user.Nick, error);
|
if (error != null) bot.Pm(user.Nick, error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -197,7 +196,7 @@ namespace MCGalaxy.Network {
|
|||||||
void Listener_OnPublic(UserInfo user, string channel, string message) {
|
void Listener_OnPublic(UserInfo user, string channel, string message) {
|
||||||
message = message.TrimEnd();
|
message = message.TrimEnd();
|
||||||
if (message.Length == 0) return;
|
if (message.Length == 0) return;
|
||||||
bool opchat = channel.CaselessEq(bot.opchannel);
|
bool opchat = bot.opchannels.CaselessContains(channel);
|
||||||
|
|
||||||
message = Colors.IrcToMinecraftColors(message);
|
message = Colors.IrcToMinecraftColors(message);
|
||||||
string[] parts = message.SplitSpaces(3);
|
string[] parts = message.SplitSpaces(3);
|
||||||
@ -206,7 +205,7 @@ namespace MCGalaxy.Network {
|
|||||||
|
|
||||||
if (ircCmd == Server.ircCommandPrefix && !HandleChannelCommand(user, channel, message, parts)) return;
|
if (ircCmd == Server.ircCommandPrefix && !HandleChannelCommand(user, channel, message, parts)) return;
|
||||||
|
|
||||||
if (channel.CaselessEq(bot.opchannel)) {
|
if (opchat) {
|
||||||
Server.s.Log(String.Format("(OPs): (IRC) {0}: {1}", user.Nick, message));
|
Server.s.Log(String.Format("(OPs): (IRC) {0}: {1}", user.Nick, message));
|
||||||
Chat.MessageOps(String.Format("To Ops &f-%I(IRC) {0}&f- {1}", user.Nick,
|
Chat.MessageOps(String.Format("To Ops &f-%I(IRC) {0}&f- {1}", user.Nick,
|
||||||
Server.profanityFilter ? ProfanityFilter.Parse(message) : message));
|
Server.profanityFilter ? ProfanityFilter.Parse(message) : message));
|
||||||
@ -223,8 +222,8 @@ namespace MCGalaxy.Network {
|
|||||||
Command.Search(ref cmdName, ref cmdArgs);
|
Command.Search(ref cmdName, ref cmdArgs);
|
||||||
|
|
||||||
string error;
|
string error;
|
||||||
if (!CheckIRCCommand(user, cmdName, channel, out error)) {
|
if (!CheckIRCCommand(user, cmdName, out error)) {
|
||||||
if (error != null) bot.Message(error, channel);
|
if (error != null) bot.Message(channel, error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,24 +266,25 @@ namespace MCGalaxy.Network {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CheckIRCCommand(UserInfo user, string cmdName, string channel, out string error) {
|
bool CheckIRCCommand(UserInfo user, string cmdName, out string error) {
|
||||||
List<string> chanNicks;
|
|
||||||
error = null;
|
error = null;
|
||||||
if (!Server.ircControllers.Contains(user.Nick))
|
if (!Server.ircControllers.Contains(user.Nick)) return false;
|
||||||
return false;
|
|
||||||
if (!userMap.TryGetValue(channel, out chanNicks))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
int index = GetNickIndex(user.Nick, chanNicks);
|
bool foundAtAll = false;
|
||||||
if (index < 0) {
|
foreach (string chan in bot.channels) {
|
||||||
|
if (VerifyNick(chan, user.Nick, ref error, ref foundAtAll)) return true;
|
||||||
|
}
|
||||||
|
foreach (string chan in bot.opchannels) {
|
||||||
|
if (VerifyNick(chan, user.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;
|
error = "You are not on the bot's list of users for some reason, please leave and rejoin."; return false;
|
||||||
}
|
}
|
||||||
if (!VerifyNick(chanNicks[index], ref error)) return false;
|
|
||||||
|
|
||||||
if (bot.BannedCommands.Contains(cmdName)) {
|
if (bot.BannedCommands.Contains(cmdName)) {
|
||||||
error = "You are not allowed to use this command from IRC."; return false;
|
error = "You are not allowed to use this command from IRC.";
|
||||||
}
|
}
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -313,8 +313,12 @@ namespace MCGalaxy.Network {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void JoinChannels() {
|
void JoinChannels() {
|
||||||
bot.Join(bot.channel);
|
foreach (string chan in bot.channels) {
|
||||||
bot.Join(bot.opchannel);
|
bot.Join(chan);
|
||||||
|
}
|
||||||
|
foreach (string chan in bot.opchannels) {
|
||||||
|
bot.Join(chan);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Listener_OnPrivateNotice(UserInfo user, string notice) {
|
void Listener_OnPrivateNotice(UserInfo user, string notice) {
|
||||||
@ -438,24 +442,31 @@ namespace MCGalaxy.Network {
|
|||||||
c == '[' || c == ']' || c == '{' || c == '}' || c == '^' || c == '`' || c == '_' || c == '|';
|
c == '[' || c == ']' || c == '{' || c == '}' || c == '^' || c == '`' || c == '_' || c == '|';
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VerifyNick(string nick, ref string error) {
|
bool VerifyNick(string channel, string userNick, ref string error, ref bool foundAtAll) {
|
||||||
|
List<string> chanNicks = null;
|
||||||
|
if (!userMap.TryGetValue(channel, out chanNicks)) return false;
|
||||||
|
|
||||||
|
int index = GetNickIndex(userNick, chanNicks);
|
||||||
|
if (index == -1) return false;
|
||||||
|
foundAtAll = true;
|
||||||
|
|
||||||
IRCControllerVerify verify = Server.IRCVerify;
|
IRCControllerVerify verify = Server.IRCVerify;
|
||||||
if (verify == IRCControllerVerify.None) return true;
|
if (verify == IRCControllerVerify.None) return true;
|
||||||
|
|
||||||
if (verify == IRCControllerVerify.HalfOp) {
|
if (verify == IRCControllerVerify.HalfOp) {
|
||||||
string prefix = GetPrefix(nick);
|
string prefix = GetPrefix(chanNicks[index]);
|
||||||
if (prefix == "" || prefix == "+") {
|
if (prefix == "" || prefix == "+") {
|
||||||
error = "You must be at least a half-op on the channel to use commands from IRC."; return false;
|
error = "You must be at least a half-op on the channel to use commands from IRC."; return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
List<string> chanNicks = null;
|
foreach (string chan in bot.opchannels) {
|
||||||
userMap.TryGetValue(bot.opchannel, out chanNicks);
|
if (!userMap.TryGetValue(chan, out chanNicks)) continue;
|
||||||
int index = GetNickIndex(nick, chanNicks);
|
|
||||||
if (index == -1) {
|
index = GetNickIndex(userNick, chanNicks);
|
||||||
error = "You must have joined the opchannel to use commands from IRC."; return false;
|
if (index != -1) return true;
|
||||||
}
|
}
|
||||||
return true;
|
error = "You must have joined the opchannel to use commands from IRC."; return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,6 +115,13 @@ namespace MCGalaxy {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool CaselessContains(this string[] items, string value) {
|
||||||
|
for (int i = 0; i < items.Length; i++) {
|
||||||
|
if (items[i].Equals(value, comp)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public static bool CaselessRemove(this List<string> items, string value) {
|
public static bool CaselessRemove(this List<string> items, string value) {
|
||||||
for (int i = 0; i < items.Count; i++) {
|
for (int i = 0; i < items.Count; i++) {
|
||||||
if (!items[i].Equals(value, comp)) continue;
|
if (!items[i].Equals(value, comp)) continue;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user