Explorar el Código

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 hace 4 años
padre
commit
09c8b52cde
Se han modificado 12 ficheros con 87 adiciones y 104 borrados
  1. +4
    -9
      Source/Bot/Data.cs
  2. +9
    -2
      Source/Bot/Module.cs
  3. +5
    -3
      Source/Modules/Mod_Admin.cs
  4. +3
    -1
      Source/Modules/Mod_DiscordLogger.cs
  5. +2
    -0
      Source/Modules/Mod_Fun.cs
  6. +2
    -0
      Source/Modules/Mod_Idgames.cs
  7. +18
    -25
      Source/Modules/Mod_Links.cs
  8. +13
    -27
      Source/Modules/Mod_Memo.cs
  9. +3
    -1
      Source/Modules/Mod_Quote.cs
  10. +20
    -33
      Source/Modules/Mod_Seen.cs
  11. +2
    -0
      Source/Modules/Mod_Shittalk.cs
  12. +6
    -3
      Source/Modules/Mod_Utils.cs

+ 4
- 9
Source/Bot/Data.cs Ver fichero

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


+ 9
- 2
Source/Bot/Module.cs Ver fichero

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


+ 5
- 3
Source/Modules/Mod_Admin.cs Ver fichero

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

//


+ 3
- 1
Source/Modules/Mod_DiscordLogger.cs Ver fichero

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


+ 2
- 0
Source/Modules/Mod_Fun.cs Ver fichero

@@ -107,6 +107,8 @@ namespace ProjectGolan.Vrobot3.Modules
// };

events.onMessage += onMessage;

postSetup();
}

//


+ 2
- 0
Source/Modules/Mod_Idgames.cs Ver fichero

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

postSetup();
}

//


+ 18
- 25
Source/Modules/Mod_Links.cs Ver fichero

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


+ 13
- 27
Source/Modules/Mod_Memo.cs Ver fichero

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


+ 3
- 1
Source/Modules/Mod_Quote.cs Ver fichero

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

//


+ 20
- 33
Source/Modules/Mod_Seen.cs Ver fichero

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

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);
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();

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


+ 2
- 0
Source/Modules/Mod_Shittalk.cs Ver fichero

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

postSetup();
}

//


+ 6
- 3
Source/Modules/Mod_Utils.cs Ver fichero

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


Cargando…
Cancelar
Guardar