1
0
Förgrening 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
förälder 509130c66f
incheckning 09c8b52cde
12 ändrade filer med 86 tillägg och 103 borttagningar

Visa fil

@ -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<String, BotCommandStructure> {}
public class CommandFuncDict :
Dictionary<String, Tuple<IBotModule, BotCommandStructure>> {}
public partial class Bot
{
private readonly Dictionary<ulong, String> lastLine;
private readonly Client.IChatClient client;
public List<IBotModule> modules { get; private set; }
public CommandFuncDict cmdfuncs { get; private set; }
public List<IBotModule> modules {get; private set;}
public CommandFuncDict cmdfuncs {get; private set;}
public readonly BotInfo info;
public Client.ClientInfo clientInfo => client.info;

Visa fil

@ -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;

Visa fil

@ -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();
}
//

Visa fil

@ -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<T> catchFunc<T>(EventHandler<T> fn)

Visa fil

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

Visa fil

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

Visa fil

@ -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<String, URIHandler> handlers = new Dictionary<String, URIHandler>(){
{ "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);

Visa fil

@ -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<String, List<MemoInfo>> {}
//
// 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",

Visa fil

@ -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();
}
//

Visa fil

@ -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<SeenName> {}
//
// 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",

Visa fil

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

Visa fil

@ -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!",