Using /resetbot should use new settings if user changed irc nick, server or port. (Thanks ChrisNetWeb)

This commit is contained in:
UnknownShadow200 2016-05-26 23:36:17 +10:00
parent 0bf0790fa7
commit 76b5c33f1b

View File

@ -41,58 +41,63 @@ namespace MCGalaxy {
this.opchannel = opchannel.Trim(); this.opchannel = opchannel.Trim();
this.nick = nick.Replace(" ", ""); this.nick = nick.Replace(" ", "");
this.server = server; this.server = server;
banCmd = new List<string>() { "resetbot", "resetirc", "oprules", "irccontrollers", "ircctrl" };
if (Server.irc) { if (Server.irc) {
ConnectionArgs con = new ConnectionArgs(nick, server); ConnectionArgs con = new ConnectionArgs(nick, server);
con.Port = Server.ircPort; con.Port = Server.ircPort;
connection = new Connection(new UTF8Encoding(false), con); connection = new Connection(new UTF8Encoding(false), con);
HookEvents();
LoadBannedCommands();
}
}
// Regster events for outgoing void HookEvents() {
Player.PlayerChat += Player_PlayerChat; // Register events for outgoing
Player.PlayerConnect += Player_PlayerConnect; Player.PlayerChat += Player_PlayerChat;
Player.PlayerDisconnect += Player_PlayerDisconnect; Player.PlayerConnect += Player_PlayerConnect;
Player.DoPlayerAction += Player_PlayerAction; Player.PlayerDisconnect += Player_PlayerDisconnect;
Player.DoPlayerAction += Player_PlayerAction;
// Regster events for incoming // Regster events for incoming
connection.Listener.OnNick += Listener_OnNick; connection.Listener.OnNick += Listener_OnNick;
connection.Listener.OnRegistered += Listener_OnRegistered; connection.Listener.OnRegistered += Listener_OnRegistered;
connection.Listener.OnAction += Listener_OnAction; connection.Listener.OnAction += Listener_OnAction;
connection.Listener.OnPublic += Listener_OnPublic; connection.Listener.OnPublic += Listener_OnPublic;
connection.Listener.OnPrivate += Listener_OnPrivate; connection.Listener.OnPrivate += Listener_OnPrivate;
connection.Listener.OnError += Listener_OnError; connection.Listener.OnError += Listener_OnError;
connection.Listener.OnQuit += Listener_OnQuit; connection.Listener.OnQuit += Listener_OnQuit;
connection.Listener.OnJoin += Listener_OnJoin; connection.Listener.OnJoin += Listener_OnJoin;
connection.Listener.OnPart += Listener_OnPart; connection.Listener.OnPart += Listener_OnPart;
connection.Listener.OnDisconnected += Listener_OnDisconnected; connection.Listener.OnDisconnected += Listener_OnDisconnected;
connection.Listener.OnChannelModeChange += Listener_OnChannelModeChange; connection.Listener.OnChannelModeChange += Listener_OnChannelModeChange;
connection.Listener.OnNames += Listener_OnNames; connection.Listener.OnNames += Listener_OnNames;
connection.Listener.OnKick += Listener_OnKick; connection.Listener.OnKick += Listener_OnKick;
connection.Listener.OnKill += Listener_OnKill; connection.Listener.OnKill += Listener_OnKill;
}
// Load banned commands list void LoadBannedCommands() {
if (File.Exists("text/ircbancmd.txt")) { // Backwards compatibility banCmd = new List<string>() { "resetbot", "resetirc", "oprules", "irccontrollers", "ircctrl" };
using (StreamWriter sw = File.CreateText("text/irccmdblacklist.txt")) {
sw.WriteLine("#Here you can put commands that cannot be used from the IRC bot."); if (File.Exists("text/ircbancmd.txt")) { // Backwards compatibility
sw.WriteLine("#Lines starting with \"#\" are ignored."); using (StreamWriter sw = File.CreateText("text/irccmdblacklist.txt")) {
foreach (string line in File.ReadAllLines("text/ircbancmd.txt")) sw.WriteLine("#Here you can put commands that cannot be used from the IRC bot.");
sw.WriteLine(line); sw.WriteLine("#Lines starting with \"#\" are ignored.");
} foreach (string line in File.ReadAllLines("text/ircbancmd.txt"))
File.Delete("text/ircbancmd.txt"); sw.WriteLine(line);
} else {
if (!File.Exists("text/irccmdblacklist.txt"))
File.WriteAllLines("text/irccmdblacklist.txt", new [] {
"#Here you can put commands that cannot be used from the IRC bot.",
"#Lines starting with \"#\" are ignored." });
foreach (string line in File.ReadAllLines("text/irccmdblacklist.txt"))
if (line[0] != '#') banCmd.Add(line);
} }
File.Delete("text/ircbancmd.txt");
} else {
if (!File.Exists("text/irccmdblacklist.txt"))
File.WriteAllLines("text/irccmdblacklist.txt", new [] {
"#Here you can put commands that cannot be used from the IRC bot.",
"#Lines starting with \"#\" are ignored." });
foreach (string line in File.ReadAllLines("text/irccmdblacklist.txt"))
if (line[0] != '#') banCmd.Add(line);
} }
} }
public void Say(string message, bool opchat = false, bool color = true) { public void Say(string message, bool opchat = false, bool color = true) {
if (!Server.irc || !IsConnected()) return; if (!IsConnected()) return;
message = ConvertMessage(message, color); message = ConvertMessage(message, color);
string chan = opchat ? opchannel : channel; string chan = opchat ? opchannel : channel;
@ -101,7 +106,7 @@ namespace MCGalaxy {
} }
public void Pm(string user, string message, bool color = true) { public void Pm(string user, string message, bool color = true) {
if (!Server.irc || !IsConnected()) return; if (!IsConnected()) return;
message = ConvertMessage(message, color); message = ConvertMessage(message, color);
connection.Sender.PrivateMessage(user, message); connection.Sender.PrivateMessage(user, message);
} }
@ -119,6 +124,15 @@ namespace MCGalaxy {
} }
public void Reset() { public void Reset() {
channel = Server.ircChannel.Trim();
opchannel = Server.ircOpChannel.Trim();
nick = Server.ircNick.Replace(" ", "");
server = Server.ircServer;
connection.connectionArgs.Port = Server.ircPort;
connection.connectionArgs.Hostname = Server.ircServer;
connection.connectionArgs.Nick = Server.ircNick;
if (!Server.irc) return; if (!Server.irc) return;
reset = true; reset = true;
retries = 0; retries = 0;
@ -129,45 +143,33 @@ namespace MCGalaxy {
public void Connect() { public void Connect() {
if (!Server.irc || Server.shuttingDown) return; if (!Server.irc || Server.shuttingDown) return;
/*new Thread(new ThreadStart(delegate
{
try { connection.Connect(); }
catch (Exception e)
{
Server.s.Log("Failed to connect to IRC");
Server.ErrorLog(e);
}
})).Start();*/
Server.s.Log("Connecting to IRC..."); Server.s.Log("Connecting to IRC...");
connection.connectionArgs.Nick = nick; connection.connectionArgs.Nick = nick;
try { connection.Connect(); } try {
catch (Exception e) { connection.Connect();
} catch (Exception e) {
Server.s.Log("Failed to connect to IRC!"); Server.s.Log("Failed to connect to IRC!");
Server.ErrorLog(e); Server.ErrorLog(e);
} }
} }
public void Disconnect(string reason) { public void Disconnect(string reason) {
if (IsConnected()) { if (!IsConnected()) return;
connection.Disconnect(reason); connection.Disconnect(reason);
Server.s.Log("Disconnected from IRC!"); Server.s.Log("Disconnected from IRC!");
users.Clear(); users.Clear();
}
} }
public bool IsConnected() { public bool IsConnected() {
if (!Server.irc) return false; return Server.irc && connection != null && connection.Connected;
try { return connection.Connected; }
catch { return false; }
} }
#region In-game event handlers #region In-game event handlers
void Player_PlayerAction(Player p, PlayerAction action, void Player_PlayerAction(Player p, PlayerAction action,
string message, bool stealth) { string message, bool stealth) {
if (!Server.irc || !IsConnected()) return; if (!IsConnected()) return;
string msg = null; string msg = null;
if (action == PlayerAction.AFK) if (action == PlayerAction.AFK)
msg = p.ColoredName + " %Sis AFK " + message; msg = p.ColoredName + " %Sis AFK " + message;
@ -189,7 +191,7 @@ namespace MCGalaxy {
} }
void Player_PlayerDisconnect(Player p, string reason) { void Player_PlayerDisconnect(Player p, string reason) {
if (!Server.irc || !IsConnected()) return; if (!IsConnected()) return;
if (!Server.guestLeaveNotify && p.group.Permission <= LevelPermission.Guest) return; if (!Server.guestLeaveNotify && p.group.Permission <= LevelPermission.Guest) return;
string msg = p.DisplayName + " %Sleft the game (" + reason + ")"; string msg = p.DisplayName + " %Sleft the game (" + reason + ")";
@ -198,7 +200,7 @@ namespace MCGalaxy {
} }
void Player_PlayerConnect(Player p) { void Player_PlayerConnect(Player p) {
if (!Server.irc || !IsConnected()) return; if (!IsConnected()) return;
if (!Server.guestJoinNotify && p.group.Permission <= LevelPermission.Guest) return; if (!Server.guestJoinNotify && p.group.Permission <= LevelPermission.Guest) return;
string msg = p.DisplayName + " %Sjoined the game"; string msg = p.DisplayName + " %Sjoined the game";
@ -212,9 +214,9 @@ namespace MCGalaxy {
return; return;
} }
if (Server.ircColorsEnable && Server.irc && IsConnected()) if (Server.ircColorsEnable && IsConnected())
Say(p.FullName + "%S: " + message, p.opchat); Say(p.FullName + "%S: " + message, p.opchat);
if (!Server.ircColorsEnable && Server.irc && IsConnected()) if (!Server.ircColorsEnable && IsConnected())
Say(p.DisplayName + ": " + message, p.opchat); Say(p.DisplayName + ": " + message, p.opchat);
} }
#endregion #endregion
@ -360,7 +362,7 @@ namespace MCGalaxy {
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;
} }
if (banCmd.Contains(cmdName)) { if (banCmd != null && banCmd.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 false;
} }
return true; return true;