From 09c8b52cde23cf0fb164a3900ca7f5f492388d14 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 13 Feb 2017 00:13:17 -0500 Subject: [PATCH] Bot::Module: Add postSetup to IBotModule for type info Bot::Data: Move using statement to type definition Mod_Utils: Make mod permission checking actually work Mod_Seen: Clean up --- Source/Bot/Data.cs | 13 +++----- Source/Bot/Module.cs | 11 +++++-- Source/Modules/Mod_Admin.cs | 8 +++-- Source/Modules/Mod_DiscordLogger.cs | 4 ++- Source/Modules/Mod_Fun.cs | 2 ++ Source/Modules/Mod_Idgames.cs | 2 ++ Source/Modules/Mod_Links.cs | 43 ++++++++++-------------- Source/Modules/Mod_Memo.cs | 40 ++++++++-------------- Source/Modules/Mod_Quote.cs | 4 ++- Source/Modules/Mod_Seen.cs | 51 +++++++++++------------------ Source/Modules/Mod_Shittalk.cs | 2 ++ Source/Modules/Mod_Utils.cs | 9 +++-- 12 files changed, 86 insertions(+), 103 deletions(-) diff --git a/Source/Bot/Data.cs b/Source/Bot/Data.cs index fb53a57..a8855cd 100644 --- a/Source/Bot/Data.cs +++ b/Source/Bot/Data.cs @@ -15,13 +15,6 @@ using System.Threading.Tasks; using System.Collections.Generic; using System.Linq; -using CommandFuncDict = - System.Collections.Generic.Dictionary< - System.String, - System.Tuple< - ProjectGolan.Vrobot3.IBotModule, - ProjectGolan.Vrobot3.BotCommandStructure>>; - namespace ProjectGolan.Vrobot3 { // Delegate type for bot commands. @@ -29,14 +22,16 @@ namespace ProjectGolan.Vrobot3 // Dictionary of bot commands. public class CommandDict : Dictionary {} + public class CommandFuncDict : + Dictionary> {} public partial class Bot { private readonly Dictionary lastLine; private readonly Client.IChatClient client; - public List modules { get; private set; } - public CommandFuncDict cmdfuncs { get; private set; } + public List modules {get; private set;} + public CommandFuncDict cmdfuncs {get; private set;} public readonly BotInfo info; public Client.ClientInfo clientInfo => client.info; diff --git a/Source/Bot/Module.cs b/Source/Bot/Module.cs index e71c6c3..2f8d404 100644 --- a/Source/Bot/Module.cs +++ b/Source/Bot/Module.cs @@ -70,12 +70,13 @@ namespace ProjectGolan.Vrobot3 // // Used for registering commands in a module. // - public struct BotCommandStructure + public class BotCommandStructure { public BotCommand cmd; public String help; public bool hidden; public BotRole role; + public Type mod; } // @@ -104,7 +105,13 @@ namespace ProjectGolan.Vrobot3 onSeen?.Invoke(usr, channel); } - protected IBotModule(Bot bot) { this.bot = bot; } + protected void postSetup() + { + foreach(var kvp in commands) + kvp.Value.mod = this.GetType(); + } + + protected IBotModule(Bot bot) {this.bot = bot;} public CommandDict commands = new CommandDict(); public Events events; diff --git a/Source/Modules/Mod_Admin.cs b/Source/Modules/Mod_Admin.cs index ea078b3..4efe5b8 100644 --- a/Source/Modules/Mod_Admin.cs +++ b/Source/Modules/Mod_Admin.cs @@ -28,14 +28,14 @@ namespace ProjectGolan.Vrobot3.Modules base(bot) { commands["kill"] = new BotCommandStructure{ - cmd = cmdKill, + cmd = cmdKill, role = BotRole.Admin, help = "Kills all bot instances.\n" + "Syntax: %kill" }; commands["msg"] = new BotCommandStructure{ - cmd = cmdMsg, + cmd = cmdMsg, role = BotRole.Admin, help = "Sends a message.\n" + "Syntax: %msg channel, msg\n" + @@ -43,12 +43,14 @@ namespace ProjectGolan.Vrobot3.Modules }; commands["action"] = new BotCommandStructure{ - cmd = cmdAction, + cmd = cmdAction, role = BotRole.Admin, help = "Sends an action.\n" + "Syntax: %action channel, msg\n" + "Example: %action #general, explodes violently" }; + + postSetup(); } // diff --git a/Source/Modules/Mod_DiscordLogger.cs b/Source/Modules/Mod_DiscordLogger.cs index 2169cea..d8d91e4 100644 --- a/Source/Modules/Mod_DiscordLogger.cs +++ b/Source/Modules/Mod_DiscordLogger.cs @@ -19,7 +19,7 @@ delegate void HaxFn(String name, String vname); namespace ProjectGolan.Vrobot3.Modules { - [BotModuleDiscord] + [BotModuleDiscord, BotModuleDisabled] public class Mod_DiscordLogger : IBotModule { public Mod_DiscordLogger(Bot bot) : base(bot) @@ -82,6 +82,8 @@ namespace ProjectGolan.Vrobot3.Modules if(delta != String.Empty) await verMsg(e.Server, "User updated", delta); }); + + postSetup(); } private EventHandler catchFunc(EventHandler fn) diff --git a/Source/Modules/Mod_Fun.cs b/Source/Modules/Mod_Fun.cs index e484607..53dde81 100644 --- a/Source/Modules/Mod_Fun.cs +++ b/Source/Modules/Mod_Fun.cs @@ -107,6 +107,8 @@ namespace ProjectGolan.Vrobot3.Modules // }; events.onMessage += onMessage; + + postSetup(); } // diff --git a/Source/Modules/Mod_Idgames.cs b/Source/Modules/Mod_Idgames.cs index bbaa4e4..d06bb54 100644 --- a/Source/Modules/Mod_Idgames.cs +++ b/Source/Modules/Mod_Idgames.cs @@ -44,6 +44,8 @@ namespace ProjectGolan.Vrobot3.Modules "Example: .idgames scythe, filename, 4\n" + "Example: .idgames" }; + + postSetup(); } // diff --git a/Source/Modules/Mod_Links.cs b/Source/Modules/Mod_Links.cs index 87acfdc..3352cba 100644 --- a/Source/Modules/Mod_Links.cs +++ b/Source/Modules/Mod_Links.cs @@ -1,8 +1,14 @@ -// -// Mod_Links.cs +//----------------------------------------------------------------------------- // -// Link title capabilities. +// Copyright © 2016 Project Golan // +// See "LICENSE" for more information. +// +//----------------------------------------------------------------------------- +// +// Link expansion. +// +//----------------------------------------------------------------------------- using System; using System.Text.RegularExpressions; @@ -21,42 +27,35 @@ namespace ProjectGolan.Vrobot3 // // Mod_Links // - - public sealed class Mod_Links : IBotModule + public class Mod_Links : IBotModule { // // URI // - private struct URI { public String method, host, path, query, tag, uri; } - // - // Delegates. - private delegate void URIHandler(URI uri, String referer, ref String result); // - // Ctor + // Mod_Links constructor // - public Mod_Links(Bot bot_) : base(bot_) { events.OnMessage += Evt_OnMessage; + + postSetup(); } // // Evt_OnMessage // - public void Evt_OnMessage(UserInfo usr, String channel, String msg, bool iscmd) { - // // Do this asynchronously, we don't want link parsing to block operation. - new Thread(() => { try { @@ -73,7 +72,6 @@ namespace ProjectGolan.Vrobot3 // // GetURITitle // - private Match GetURITitle(URI uri, String referer, int kb = 16) { String rstr = Utils.GetResponseString(uri.uri, 1024 * kb, referer); @@ -87,7 +85,6 @@ namespace ProjectGolan.Vrobot3 // // URI_Default // - private void URI_Default(URI uri, String referer, ref String result) { var req = WebRequest.Create(uri.uri) as HttpWebRequest; @@ -112,9 +109,8 @@ namespace ProjectGolan.Vrobot3 // // URI_Youtube // - // Special fucking flower. + // Special fucking snowflake. // - private void URI_Youtube(URI uri, String referer, ref String result) { var req = WebRequest.Create(uri.uri) as HttpWebRequest; @@ -138,7 +134,6 @@ namespace ProjectGolan.Vrobot3 // // URI_Gelooru // - private void URI_Gelbooru(URI uri, String referer, ref String result) { var match = GetURITitle(uri, referer, 8); // Should be OK to just get the first 8kb here. @@ -155,7 +150,6 @@ namespace ProjectGolan.Vrobot3 // // URI_Hitbox // - private void URI_Hitbox(URI uri, String referer, ref String result) { String name = WebUtility.HtmlEncode(uri.path.TrimStart(new char[]{'/'})); @@ -185,7 +179,6 @@ namespace ProjectGolan.Vrobot3 // // This function is really complicated because of exploits. Fuck exploits. // - private void TryParseURIs(String channel, String msg) { try @@ -260,10 +253,10 @@ namespace ProjectGolan.Vrobot3 referer = uri.method + "://" + uri.host; Dictionary handlers = new Dictionary(){ - { "youtube.com", URI_Youtube }, - { "youtu.be", URI_Youtube }, - { "gelbooru.com", URI_Gelbooru }, - { "hitbox.tv", URI_Hitbox }, + {"youtube.com", URI_Youtube }, + {"youtu.be", URI_Youtube }, + {"gelbooru.com", URI_Gelbooru}, + {"hitbox.tv", URI_Hitbox }, }; String hostst = Regex.Replace(uri.host, @"^www\.", String.Empty, RegexOptions.Multiline); diff --git a/Source/Modules/Mod_Memo.cs b/Source/Modules/Mod_Memo.cs index 5e62740..f33a1e7 100644 --- a/Source/Modules/Mod_Memo.cs +++ b/Source/Modules/Mod_Memo.cs @@ -1,9 +1,15 @@ -// -// Mod_Memo.cs +//----------------------------------------------------------------------------- +// +// Copyright © 2016 Project Golan +// +// See "LICENSE" for more information. +// +//----------------------------------------------------------------------------- // // Memoing capabilities. -// @memocount, .memo, .memoseen +// .memo // +//----------------------------------------------------------------------------- using System; using System.Collections.Generic; @@ -16,13 +22,11 @@ namespace ProjectGolan.Vrobot3.Modules // // Mod_Memo // - - public sealed class Mod_Memo : IBotModule + public class Mod_Memo : IBotModule { // // MemoFlags // - [Flags] enum MemoFlags { @@ -32,33 +36,22 @@ namespace ProjectGolan.Vrobot3.Modules // // MemoInfo // - private struct MemoInfo { - // - // Data. - public String content; public String sender; public DateTime time; public MemoFlags flags; }; - // // MemoDict - // - private class MemoDict : Dictionary> {} - // - // Data. - MemoDict memos = new MemoDict(); // - // Ctor + // Mod_Memo constructor // - public Mod_Memo(Bot bot_) : base(bot_) { @@ -86,12 +79,13 @@ namespace ProjectGolan.Vrobot3.Modules events.OnMessage += Evt_OnMessage; events.OnDisconnected += Evt_OnDisconnected; events.OnSeen += Evt_OnSeen; + + postSetup(); } // // Cmd_MemoCount // - public void Cmd_MemoCount(UserInfo usr, String channel, String msg) { bot.Reply(usr, channel, memos.Count.ToString()); @@ -100,7 +94,6 @@ namespace ProjectGolan.Vrobot3.Modules // // Cmd_Memo // - public void Cmd_Memo(UserInfo usr, String channel, String msg) { String[] args = Utils.GetArguments(msg, commands["memo"].help, 2, 2, ' '); @@ -119,7 +112,6 @@ namespace ProjectGolan.Vrobot3.Modules // // Cmd_MemoSeen // - public void Cmd_MemoSeen(UserInfo usr, String channel, String msg) { String[] args = Utils.GetArguments(msg, commands["memoseen"].help, 2, 2, ' '); @@ -139,7 +131,6 @@ namespace ProjectGolan.Vrobot3.Modules // // AddMemo // - private void AddMemo(String name, MemoInfo memo) { name = name.ToLower(); @@ -155,7 +146,6 @@ namespace ProjectGolan.Vrobot3.Modules // // OutputMemos // - private void OutputMemos(String channel, String realnick, bool onseen) { String nick = realnick.ToLower(); @@ -198,7 +188,6 @@ namespace ProjectGolan.Vrobot3.Modules // // Evt_OnMessage // - public void Evt_OnMessage(UserInfo usr, String channel, String msg, bool iscmd) { OutputMemos(channel, usr.Nick, false); @@ -207,7 +196,6 @@ namespace ProjectGolan.Vrobot3.Modules // // Evt_OnSeen // - public void Evt_OnSeen(UserInfo usr, String channel) { OutputMemos(channel, usr.Nick, true); @@ -216,7 +204,6 @@ namespace ProjectGolan.Vrobot3.Modules // // Evt_OnDisconnected // - public void Evt_OnDisconnected() { WriteMemos(); @@ -225,7 +212,6 @@ namespace ProjectGolan.Vrobot3.Modules // // WriteMemos // - private void WriteMemos() { File.WriteAllText("/srv/irc/vrobot3/data/memos." + bot.n_groupname + ".json", diff --git a/Source/Modules/Mod_Quote.cs b/Source/Modules/Mod_Quote.cs index bed493d..cd7b674 100644 --- a/Source/Modules/Mod_Quote.cs +++ b/Source/Modules/Mod_Quote.cs @@ -40,11 +40,13 @@ namespace ProjectGolan.Vrobot3.Modules base(bot_) { commands["quote"] = new BotCommandStructure{ - cmd = cmdQuote, + cmd = cmdQuote, help = "Get a quote from the Doominati Quote DB.\n" + "Syntax: .quote [id]\n" + "Example: .quote 536" }; + + postSetup(); } // diff --git a/Source/Modules/Mod_Seen.cs b/Source/Modules/Mod_Seen.cs index f61f1b1..8d32ebe 100644 --- a/Source/Modules/Mod_Seen.cs +++ b/Source/Modules/Mod_Seen.cs @@ -1,8 +1,14 @@ -// -// Mod_Seen.cs +//----------------------------------------------------------------------------- +// +// Copyright © 2016 Project Golan +// +// See "LICENSE" for more information. +// +//----------------------------------------------------------------------------- // // .seen // +//----------------------------------------------------------------------------- using System; using System.Collections.Generic; @@ -17,36 +23,27 @@ namespace ProjectGolan.Vrobot3 // // Mod_Seen // - - public sealed class Mod_Seen : IBotModule + public class Mod_Seen : IBotModule { // // SeenName // - private class SeenName { public String real, check; public DateTime time; } - // // SeenDates - // - private class SeenDates : List {} - // - // Data. - private SeenDates seendates = new SeenDates(); private TimeZoneInfo burb; private DateTime lastwrite = DateTime.Now; // - // Ctor + // Mod_Seen constructor // - public Mod_Seen(Bot bot_) : base(bot_) { @@ -55,8 +52,8 @@ namespace ProjectGolan.Vrobot3 bot.n_groupname + ".json")); commands["seen"] = new BotCommandStructure { cmd = Cmd_Seen, - help = "Responds with the last time I saw someone. || " + - "Syntax: .seen person || " + + help = "Responds with the last time I saw someone.\n" + + "Syntax: .seen person\n" + "Example: .seen vrobot3" }; @@ -64,12 +61,13 @@ namespace ProjectGolan.Vrobot3 events.OnDisconnected += Evt_OnDisconnected; burb = TimeZoneInfo.CreateCustomTimeZone("burb", new TimeSpan(10, -30, 0), "burb", "burb"); + + postSetup(); } // // Cmd_Seen // - public void Cmd_Seen(UserInfo usr, String channel, String msg) { if(msg.Length == 0 || msg.Contains(" ")) @@ -79,20 +77,12 @@ namespace ProjectGolan.Vrobot3 var seen = from sdata in seendates where sdata.check == name select sdata; if(seen.Any()) { - var other = seen.First(); - String outp = String.Empty; + var other = seen.First(); + var fuzzy = Utils.FuzzyRelativeDate(other.time, DateTime.Now.FromNtp()); + var time = other.time.ToShortTimeString(); + var pidgeon = TimeZoneInfo.ConvertTime(other.time, TimeZoneInfo.Local, burb).ToShortTimeString(); - outp += "I last saw "; - outp += other.real; - outp += " active "; - outp += Utils.FuzzyRelativeDate(other.time, DateTime.Now.FromNtp()); - outp += ", at "; - outp += other.time.ToShortTimeString(); - outp += " CST ("; - outp += TimeZoneInfo.ConvertTime(other.time, TimeZoneInfo.Local, burb).ToShortTimeString(); - outp += " pidgeon time)."; - - bot.Reply(usr, channel, outp); + bot.Reply(usr, channel, $"I last saw {other.real} active {fuzzy}, at {time} CST ({pidgeon} AEST.)"); } else bot.Reply(usr, channel, "I haven't seen " + msg + " before, sorry."); @@ -103,7 +93,6 @@ namespace ProjectGolan.Vrobot3 // // Evt_OnScreen // - public void Evt_OnSeen(UserInfo usr, String channel) { String name = usr.Nick.ToLower(); @@ -123,7 +112,6 @@ namespace ProjectGolan.Vrobot3 // // Evt_OnDisconnected // - public void Evt_OnDisconnected() { WriteSeenDates(); @@ -132,7 +120,6 @@ namespace ProjectGolan.Vrobot3 // // WriteSeenDates // - private void WriteSeenDates() { File.WriteAllText("/srv/irc/vrobot3/data/seendates." + bot.n_groupname + ".json", diff --git a/Source/Modules/Mod_Shittalk.cs b/Source/Modules/Mod_Shittalk.cs index cfa2551..b2fd3aa 100644 --- a/Source/Modules/Mod_Shittalk.cs +++ b/Source/Modules/Mod_Shittalk.cs @@ -24,6 +24,8 @@ namespace ProjectGolan.Vrobot3.Modules base(bot_) { events.onMessage += onMessage; + + postSetup(); } // diff --git a/Source/Modules/Mod_Utils.cs b/Source/Modules/Mod_Utils.cs index f5d8f4f..381af6b 100644 --- a/Source/Modules/Mod_Utils.cs +++ b/Source/Modules/Mod_Utils.cs @@ -61,6 +61,8 @@ namespace ProjectGolan.Vrobot3.Modules cmd = cmdMystery, help = @"Does nothing. \o/" }; + + postSetup(); } // @@ -117,8 +119,9 @@ namespace ProjectGolan.Vrobot3.Modules let fhidden = kvp.Value.Item2.hidden let fadmin = kvp.Value.Item2.role != BotRole.User where - bot.checkModPermissions(channel, this.GetType()) && - (admin || !fadmin) && !fhidden + bot.checkModPermissions(channel, kvp.Value.Item2.mod) && + ((admin && fadmin) || (!admin && !fadmin)) && + !fhidden orderby kvp.Key select kvp.Key; @@ -196,7 +199,7 @@ namespace ProjectGolan.Vrobot3.Modules "Looks good to me.", "Sure, why not?", "It is certain.", - "Please no. Please no. Please no.", + "pls no", "Yes, please.", "Nah.", "Go for it!",