1
0
Fork 0

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
master
Graham 2017-02-13 00:13:17 -05:00
parent 509130c66f
commit 09c8b52cde
12 changed files with 86 additions and 103 deletions

View File

@ -15,13 +15,6 @@ using System.Threading.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using CommandFuncDict =
System.Collections.Generic.Dictionary<
System.String,
System.Tuple<
ProjectGolan.Vrobot3.IBotModule,
ProjectGolan.Vrobot3.BotCommandStructure>>;
namespace ProjectGolan.Vrobot3 namespace ProjectGolan.Vrobot3
{ {
// Delegate type for bot commands. // Delegate type for bot commands.
@ -29,14 +22,16 @@ namespace ProjectGolan.Vrobot3
// Dictionary of bot commands. // Dictionary of bot commands.
public class CommandDict : Dictionary<String, BotCommandStructure> {} public class CommandDict : Dictionary<String, BotCommandStructure> {}
public class CommandFuncDict :
Dictionary<String, Tuple<IBotModule, BotCommandStructure>> {}
public partial class Bot public partial class Bot
{ {
private readonly Dictionary<ulong, String> lastLine; private readonly Dictionary<ulong, String> lastLine;
private readonly Client.IChatClient client; private readonly Client.IChatClient client;
public List<IBotModule> modules { get; private set; } public List<IBotModule> modules {get; private set;}
public CommandFuncDict cmdfuncs { get; private set; } public CommandFuncDict cmdfuncs {get; private set;}
public readonly BotInfo info; public readonly BotInfo info;
public Client.ClientInfo clientInfo => client.info; public Client.ClientInfo clientInfo => client.info;

View File

@ -70,12 +70,13 @@ namespace ProjectGolan.Vrobot3
// //
// Used for registering commands in a module. // Used for registering commands in a module.
// //
public struct BotCommandStructure public class BotCommandStructure
{ {
public BotCommand cmd; public BotCommand cmd;
public String help; public String help;
public bool hidden; public bool hidden;
public BotRole role; public BotRole role;
public Type mod;
} }
// //
@ -104,7 +105,13 @@ namespace ProjectGolan.Vrobot3
onSeen?.Invoke(usr, channel); 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 CommandDict commands = new CommandDict();
public Events events; public Events events;

View File

@ -28,14 +28,14 @@ namespace ProjectGolan.Vrobot3.Modules
base(bot) base(bot)
{ {
commands["kill"] = new BotCommandStructure{ commands["kill"] = new BotCommandStructure{
cmd = cmdKill, cmd = cmdKill,
role = BotRole.Admin, role = BotRole.Admin,
help = "Kills all bot instances.\n" + help = "Kills all bot instances.\n" +
"Syntax: %kill" "Syntax: %kill"
}; };
commands["msg"] = new BotCommandStructure{ commands["msg"] = new BotCommandStructure{
cmd = cmdMsg, cmd = cmdMsg,
role = BotRole.Admin, role = BotRole.Admin,
help = "Sends a message.\n" + help = "Sends a message.\n" +
"Syntax: %msg channel, msg\n" + "Syntax: %msg channel, msg\n" +
@ -43,12 +43,14 @@ namespace ProjectGolan.Vrobot3.Modules
}; };
commands["action"] = new BotCommandStructure{ commands["action"] = new BotCommandStructure{
cmd = cmdAction, cmd = cmdAction,
role = BotRole.Admin, role = BotRole.Admin,
help = "Sends an action.\n" + help = "Sends an action.\n" +
"Syntax: %action channel, msg\n" + "Syntax: %action channel, msg\n" +
"Example: %action #general, explodes violently" "Example: %action #general, explodes violently"
}; };
postSetup();
} }
// //

View File

@ -19,7 +19,7 @@ delegate void HaxFn(String name, String vname);
namespace ProjectGolan.Vrobot3.Modules namespace ProjectGolan.Vrobot3.Modules
{ {
[BotModuleDiscord] [BotModuleDiscord, BotModuleDisabled]
public class Mod_DiscordLogger : IBotModule public class Mod_DiscordLogger : IBotModule
{ {
public Mod_DiscordLogger(Bot bot) : base(bot) public Mod_DiscordLogger(Bot bot) : base(bot)
@ -82,6 +82,8 @@ namespace ProjectGolan.Vrobot3.Modules
if(delta != String.Empty) if(delta != String.Empty)
await verMsg(e.Server, "User updated", delta); await verMsg(e.Server, "User updated", delta);
}); });
postSetup();
} }
private EventHandler<T> catchFunc<T>(EventHandler<T> fn) private EventHandler<T> catchFunc<T>(EventHandler<T> fn)

View File

@ -107,6 +107,8 @@ namespace ProjectGolan.Vrobot3.Modules
// }; // };
events.onMessage += onMessage; events.onMessage += onMessage;
postSetup();
} }
// //

View File

@ -44,6 +44,8 @@ namespace ProjectGolan.Vrobot3.Modules
"Example: .idgames scythe, filename, 4\n" + "Example: .idgames scythe, filename, 4\n" +
"Example: .idgames" "Example: .idgames"
}; };
postSetup();
} }
// //

View File

@ -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;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -21,42 +27,35 @@ namespace ProjectGolan.Vrobot3
// //
// Mod_Links // Mod_Links
// //
public class Mod_Links : IBotModule
public sealed class Mod_Links : IBotModule
{ {
// //
// URI // URI
// //
private struct URI private struct URI
{ {
public String method, host, path, query, tag, uri; public String method, host, path, query, tag, uri;
} }
//
// Delegates.
private delegate void URIHandler(URI uri, String referer, ref String result); private delegate void URIHandler(URI uri, String referer, ref String result);
// //
// Ctor // Mod_Links constructor
// //
public Mod_Links(Bot bot_) : public Mod_Links(Bot bot_) :
base(bot_) base(bot_)
{ {
events.OnMessage += Evt_OnMessage; events.OnMessage += Evt_OnMessage;
postSetup();
} }
// //
// Evt_OnMessage // Evt_OnMessage
// //
public void Evt_OnMessage(UserInfo usr, String channel, String msg, bool iscmd) public void Evt_OnMessage(UserInfo usr, String channel, String msg, bool iscmd)
{ {
//
// Do this asynchronously, we don't want link parsing to block operation. // Do this asynchronously, we don't want link parsing to block operation.
new Thread(() => { new Thread(() => {
try try
{ {
@ -73,7 +72,6 @@ namespace ProjectGolan.Vrobot3
// //
// GetURITitle // GetURITitle
// //
private Match GetURITitle(URI uri, String referer, int kb = 16) private Match GetURITitle(URI uri, String referer, int kb = 16)
{ {
String rstr = Utils.GetResponseString(uri.uri, 1024 * kb, referer); String rstr = Utils.GetResponseString(uri.uri, 1024 * kb, referer);
@ -87,7 +85,6 @@ namespace ProjectGolan.Vrobot3
// //
// URI_Default // URI_Default
// //
private void URI_Default(URI uri, String referer, ref String result) private void URI_Default(URI uri, String referer, ref String result)
{ {
var req = WebRequest.Create(uri.uri) as HttpWebRequest; var req = WebRequest.Create(uri.uri) as HttpWebRequest;
@ -112,9 +109,8 @@ namespace ProjectGolan.Vrobot3
// //
// URI_Youtube // URI_Youtube
// //
// Special fucking flower. // Special fucking snowflake.
// //
private void URI_Youtube(URI uri, String referer, ref String result) private void URI_Youtube(URI uri, String referer, ref String result)
{ {
var req = WebRequest.Create(uri.uri) as HttpWebRequest; var req = WebRequest.Create(uri.uri) as HttpWebRequest;
@ -138,7 +134,6 @@ namespace ProjectGolan.Vrobot3
// //
// URI_Gelooru // URI_Gelooru
// //
private void URI_Gelbooru(URI uri, String referer, ref String result) 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. 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 // URI_Hitbox
// //
private void URI_Hitbox(URI uri, String referer, ref String result) private void URI_Hitbox(URI uri, String referer, ref String result)
{ {
String name = WebUtility.HtmlEncode(uri.path.TrimStart(new char[]{'/'})); 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. // This function is really complicated because of exploits. Fuck exploits.
// //
private void TryParseURIs(String channel, String msg) private void TryParseURIs(String channel, String msg)
{ {
try try
@ -260,10 +253,10 @@ namespace ProjectGolan.Vrobot3
referer = uri.method + "://" + uri.host; referer = uri.method + "://" + uri.host;
Dictionary<String, URIHandler> handlers = new Dictionary<String, URIHandler>(){ Dictionary<String, URIHandler> handlers = new Dictionary<String, URIHandler>(){
{ "youtube.com", URI_Youtube }, {"youtube.com", URI_Youtube },
{ "youtu.be", URI_Youtube }, {"youtu.be", URI_Youtube },
{ "gelbooru.com", URI_Gelbooru }, {"gelbooru.com", URI_Gelbooru},
{ "hitbox.tv", URI_Hitbox }, {"hitbox.tv", URI_Hitbox },
}; };
String hostst = Regex.Replace(uri.host, @"^www\.", String.Empty, RegexOptions.Multiline); String hostst = Regex.Replace(uri.host, @"^www\.", String.Empty, RegexOptions.Multiline);

View File

@ -1,9 +1,15 @@
// //-----------------------------------------------------------------------------
// Mod_Memo.cs //
// Copyright © 2016 Project Golan
//
// See "LICENSE" for more information.
//
//-----------------------------------------------------------------------------
// //
// Memoing capabilities. // Memoing capabilities.
// @memocount, .memo, .memoseen // .memo
// //
//-----------------------------------------------------------------------------
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -16,13 +22,11 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// Mod_Memo // Mod_Memo
// //
public class Mod_Memo : IBotModule
public sealed class Mod_Memo : IBotModule
{ {
// //
// MemoFlags // MemoFlags
// //
[Flags] [Flags]
enum MemoFlags enum MemoFlags
{ {
@ -32,33 +36,22 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// MemoInfo // MemoInfo
// //
private struct MemoInfo private struct MemoInfo
{ {
//
// Data.
public String content; public String content;
public String sender; public String sender;
public DateTime time; public DateTime time;
public MemoFlags flags; public MemoFlags flags;
}; };
//
// MemoDict // MemoDict
//
private class MemoDict : Dictionary<String, List<MemoInfo>> {} private class MemoDict : Dictionary<String, List<MemoInfo>> {}
//
// Data.
MemoDict memos = new MemoDict(); MemoDict memos = new MemoDict();
// //
// Ctor // Mod_Memo constructor
// //
public Mod_Memo(Bot bot_) : public Mod_Memo(Bot bot_) :
base(bot_) base(bot_)
{ {
@ -86,12 +79,13 @@ namespace ProjectGolan.Vrobot3.Modules
events.OnMessage += Evt_OnMessage; events.OnMessage += Evt_OnMessage;
events.OnDisconnected += Evt_OnDisconnected; events.OnDisconnected += Evt_OnDisconnected;
events.OnSeen += Evt_OnSeen; events.OnSeen += Evt_OnSeen;
postSetup();
} }
// //
// Cmd_MemoCount // Cmd_MemoCount
// //
public void Cmd_MemoCount(UserInfo usr, String channel, String msg) public void Cmd_MemoCount(UserInfo usr, String channel, String msg)
{ {
bot.Reply(usr, channel, memos.Count.ToString()); bot.Reply(usr, channel, memos.Count.ToString());
@ -100,7 +94,6 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// Cmd_Memo // Cmd_Memo
// //
public void Cmd_Memo(UserInfo usr, String channel, String msg) public void Cmd_Memo(UserInfo usr, String channel, String msg)
{ {
String[] args = Utils.GetArguments(msg, commands["memo"].help, 2, 2, ' '); String[] args = Utils.GetArguments(msg, commands["memo"].help, 2, 2, ' ');
@ -119,7 +112,6 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// Cmd_MemoSeen // Cmd_MemoSeen
// //
public void Cmd_MemoSeen(UserInfo usr, String channel, String msg) public void Cmd_MemoSeen(UserInfo usr, String channel, String msg)
{ {
String[] args = Utils.GetArguments(msg, commands["memoseen"].help, 2, 2, ' '); String[] args = Utils.GetArguments(msg, commands["memoseen"].help, 2, 2, ' ');
@ -139,7 +131,6 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// AddMemo // AddMemo
// //
private void AddMemo(String name, MemoInfo memo) private void AddMemo(String name, MemoInfo memo)
{ {
name = name.ToLower(); name = name.ToLower();
@ -155,7 +146,6 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// OutputMemos // OutputMemos
// //
private void OutputMemos(String channel, String realnick, bool onseen) private void OutputMemos(String channel, String realnick, bool onseen)
{ {
String nick = realnick.ToLower(); String nick = realnick.ToLower();
@ -198,7 +188,6 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// Evt_OnMessage // Evt_OnMessage
// //
public void Evt_OnMessage(UserInfo usr, String channel, String msg, bool iscmd) public void Evt_OnMessage(UserInfo usr, String channel, String msg, bool iscmd)
{ {
OutputMemos(channel, usr.Nick, false); OutputMemos(channel, usr.Nick, false);
@ -207,7 +196,6 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// Evt_OnSeen // Evt_OnSeen
// //
public void Evt_OnSeen(UserInfo usr, String channel) public void Evt_OnSeen(UserInfo usr, String channel)
{ {
OutputMemos(channel, usr.Nick, true); OutputMemos(channel, usr.Nick, true);
@ -216,7 +204,6 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// Evt_OnDisconnected // Evt_OnDisconnected
// //
public void Evt_OnDisconnected() public void Evt_OnDisconnected()
{ {
WriteMemos(); WriteMemos();
@ -225,7 +212,6 @@ namespace ProjectGolan.Vrobot3.Modules
// //
// WriteMemos // WriteMemos
// //
private void WriteMemos() private void WriteMemos()
{ {
File.WriteAllText("/srv/irc/vrobot3/data/memos." + bot.n_groupname + ".json", File.WriteAllText("/srv/irc/vrobot3/data/memos." + bot.n_groupname + ".json",

View File

@ -40,11 +40,13 @@ namespace ProjectGolan.Vrobot3.Modules
base(bot_) base(bot_)
{ {
commands["quote"] = new BotCommandStructure{ commands["quote"] = new BotCommandStructure{
cmd = cmdQuote, cmd = cmdQuote,
help = "Get a quote from the Doominati Quote DB.\n" + help = "Get a quote from the Doominati Quote DB.\n" +
"Syntax: .quote [id]\n" + "Syntax: .quote [id]\n" +
"Example: .quote 536" "Example: .quote 536"
}; };
postSetup();
} }
// //

View File

@ -1,8 +1,14 @@
// //-----------------------------------------------------------------------------
// Mod_Seen.cs //
// Copyright © 2016 Project Golan
//
// See "LICENSE" for more information.
//
//-----------------------------------------------------------------------------
// //
// .seen // .seen
// //
//-----------------------------------------------------------------------------
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -17,36 +23,27 @@ namespace ProjectGolan.Vrobot3
// //
// Mod_Seen // Mod_Seen
// //
public class Mod_Seen : IBotModule
public sealed class Mod_Seen : IBotModule
{ {
// //
// SeenName // SeenName
// //
private class SeenName private class SeenName
{ {
public String real, check; public String real, check;
public DateTime time; public DateTime time;
} }
//
// SeenDates // SeenDates
//
private class SeenDates : List<SeenName> {} private class SeenDates : List<SeenName> {}
//
// Data.
private SeenDates seendates = new SeenDates(); private SeenDates seendates = new SeenDates();
private TimeZoneInfo burb; private TimeZoneInfo burb;
private DateTime lastwrite = DateTime.Now; private DateTime lastwrite = DateTime.Now;
// //
// Ctor // Mod_Seen constructor
// //
public Mod_Seen(Bot bot_) : public Mod_Seen(Bot bot_) :
base(bot_) base(bot_)
{ {
@ -55,8 +52,8 @@ namespace ProjectGolan.Vrobot3
bot.n_groupname + ".json")); bot.n_groupname + ".json"));
commands["seen"] = new BotCommandStructure { cmd = Cmd_Seen, commands["seen"] = new BotCommandStructure { cmd = Cmd_Seen,
help = "Responds with the last time I saw someone. || " + help = "Responds with the last time I saw someone.\n" +
"Syntax: .seen person || " + "Syntax: .seen person\n" +
"Example: .seen vrobot3" "Example: .seen vrobot3"
}; };
@ -64,12 +61,13 @@ namespace ProjectGolan.Vrobot3
events.OnDisconnected += Evt_OnDisconnected; events.OnDisconnected += Evt_OnDisconnected;
burb = TimeZoneInfo.CreateCustomTimeZone("burb", new TimeSpan(10, -30, 0), "burb", "burb"); burb = TimeZoneInfo.CreateCustomTimeZone("burb", new TimeSpan(10, -30, 0), "burb", "burb");
postSetup();
} }
// //
// Cmd_Seen // Cmd_Seen
// //
public void Cmd_Seen(UserInfo usr, String channel, String msg) public void Cmd_Seen(UserInfo usr, String channel, String msg)
{ {
if(msg.Length == 0 || msg.Contains(" ")) 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; var seen = from sdata in seendates where sdata.check == name select sdata;
if(seen.Any()) if(seen.Any())
{ {
var other = seen.First(); var other = seen.First();
String outp = String.Empty; 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 "; bot.Reply(usr, channel, $"I last saw {other.real} active {fuzzy}, at {time} CST ({pidgeon} AEST.)");
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);
} }
else else
bot.Reply(usr, channel, "I haven't seen " + msg + " before, sorry."); bot.Reply(usr, channel, "I haven't seen " + msg + " before, sorry.");
@ -103,7 +93,6 @@ namespace ProjectGolan.Vrobot3
// //
// Evt_OnScreen // Evt_OnScreen
// //
public void Evt_OnSeen(UserInfo usr, String channel) public void Evt_OnSeen(UserInfo usr, String channel)
{ {
String name = usr.Nick.ToLower(); String name = usr.Nick.ToLower();
@ -123,7 +112,6 @@ namespace ProjectGolan.Vrobot3
// //
// Evt_OnDisconnected // Evt_OnDisconnected
// //
public void Evt_OnDisconnected() public void Evt_OnDisconnected()
{ {
WriteSeenDates(); WriteSeenDates();
@ -132,7 +120,6 @@ namespace ProjectGolan.Vrobot3
// //
// WriteSeenDates // WriteSeenDates
// //
private void WriteSeenDates() private void WriteSeenDates()
{ {
File.WriteAllText("/srv/irc/vrobot3/data/seendates." + bot.n_groupname + ".json", File.WriteAllText("/srv/irc/vrobot3/data/seendates." + bot.n_groupname + ".json",

View File

@ -24,6 +24,8 @@ namespace ProjectGolan.Vrobot3.Modules
base(bot_) base(bot_)
{ {
events.onMessage += onMessage; events.onMessage += onMessage;
postSetup();
} }
// //

View File

@ -61,6 +61,8 @@ namespace ProjectGolan.Vrobot3.Modules
cmd = cmdMystery, cmd = cmdMystery,
help = @"Does nothing. \o/" help = @"Does nothing. \o/"
}; };
postSetup();
} }
// //
@ -117,8 +119,9 @@ namespace ProjectGolan.Vrobot3.Modules
let fhidden = kvp.Value.Item2.hidden let fhidden = kvp.Value.Item2.hidden
let fadmin = kvp.Value.Item2.role != BotRole.User let fadmin = kvp.Value.Item2.role != BotRole.User
where where
bot.checkModPermissions(channel, this.GetType()) && bot.checkModPermissions(channel, kvp.Value.Item2.mod) &&
(admin || !fadmin) && !fhidden ((admin && fadmin) || (!admin && !fadmin)) &&
!fhidden
orderby kvp.Key orderby kvp.Key
select kvp.Key; select kvp.Key;
@ -196,7 +199,7 @@ namespace ProjectGolan.Vrobot3.Modules
"Looks good to me.", "Looks good to me.",
"Sure, why not?", "Sure, why not?",
"It is certain.", "It is certain.",
"Please no. Please no. Please no.", "pls no",
"Yes, please.", "Yes, please.",
"Nah.", "Nah.",
"Go for it!", "Go for it!",