Browse Source

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
Graham 2 years ago
parent
commit
09c8b52cde

+ 4
- 9
Source/Bot/Data.cs View File

@@ -15,13 +15,6 @@ using System.Threading.Tasks;
15 15
 using System.Collections.Generic;
16 16
 using System.Linq;
17 17
 
18
-using CommandFuncDict =
19
-   System.Collections.Generic.Dictionary<
20
-      System.String,
21
-      System.Tuple<
22
-         ProjectGolan.Vrobot3.IBotModule,
23
-         ProjectGolan.Vrobot3.BotCommandStructure>>;
24
-
25 18
 namespace ProjectGolan.Vrobot3
26 19
 {
27 20
    // Delegate type for bot commands.
@@ -29,14 +22,16 @@ namespace ProjectGolan.Vrobot3
29 22
 
30 23
    // Dictionary of bot commands.
31 24
    public class CommandDict : Dictionary<String, BotCommandStructure> {}
25
+   public class CommandFuncDict :
26
+      Dictionary<String, Tuple<IBotModule, BotCommandStructure>> {}
32 27
 
33 28
    public partial class Bot
34 29
    {
35 30
       private readonly Dictionary<ulong, String> lastLine;
36 31
       private readonly Client.IChatClient        client;
37 32
 
38
-      public List<IBotModule> modules  { get; private set; }
39
-      public CommandFuncDict  cmdfuncs { get; private set; }
33
+      public List<IBotModule> modules  {get; private set;}
34
+      public CommandFuncDict  cmdfuncs {get; private set;}
40 35
       public readonly BotInfo info;
41 36
 
42 37
       public Client.ClientInfo  clientInfo  => client.info;

+ 9
- 2
Source/Bot/Module.cs View File

@@ -70,12 +70,13 @@ namespace ProjectGolan.Vrobot3
70 70
    //
71 71
    // Used for registering commands in a module.
72 72
    //
73
-   public struct BotCommandStructure
73
+   public class BotCommandStructure
74 74
    {
75 75
       public BotCommand cmd;
76 76
       public String     help;
77 77
       public bool       hidden;
78 78
       public BotRole    role;
79
+      public Type       mod;
79 80
    }
80 81
 
81 82
    //
@@ -104,7 +105,13 @@ namespace ProjectGolan.Vrobot3
104 105
             onSeen?.Invoke(usr, channel);
105 106
       }
106 107
 
107
-      protected IBotModule(Bot bot) { this.bot = bot; }
108
+      protected void postSetup()
109
+      {
110
+         foreach(var kvp in commands)
111
+            kvp.Value.mod = this.GetType();
112
+      }
113
+
114
+      protected IBotModule(Bot bot) {this.bot = bot;}
108 115
 
109 116
       public    CommandDict commands = new CommandDict();
110 117
       public    Events      events;

+ 5
- 3
Source/Modules/Mod_Admin.cs View File

@@ -28,14 +28,14 @@ namespace ProjectGolan.Vrobot3.Modules
28 28
          base(bot)
29 29
       {
30 30
          commands["kill"] = new BotCommandStructure{
31
-            cmd = cmdKill,
31
+            cmd  = cmdKill,
32 32
             role = BotRole.Admin,
33 33
             help = "Kills all bot instances.\n" +
34 34
                    "Syntax: %kill"
35 35
          };
36 36
 
37 37
          commands["msg"] = new BotCommandStructure{
38
-            cmd = cmdMsg,
38
+            cmd  = cmdMsg,
39 39
             role = BotRole.Admin,
40 40
             help = "Sends a message.\n" +
41 41
                    "Syntax: %msg channel, msg\n" +
@@ -43,12 +43,14 @@ namespace ProjectGolan.Vrobot3.Modules
43 43
          };
44 44
 
45 45
          commands["action"] = new BotCommandStructure{
46
-            cmd = cmdAction,
46
+            cmd  = cmdAction,
47 47
             role = BotRole.Admin,
48 48
             help = "Sends an action.\n" +
49 49
                    "Syntax: %action channel, msg\n" +
50 50
                    "Example: %action #general, explodes violently"
51 51
          };
52
+
53
+         postSetup();
52 54
       }
53 55
 
54 56
       //

+ 3
- 1
Source/Modules/Mod_DiscordLogger.cs View File

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

+ 2
- 0
Source/Modules/Mod_Fun.cs View File

@@ -107,6 +107,8 @@ namespace ProjectGolan.Vrobot3.Modules
107 107
 //       };
108 108
 
109 109
          events.onMessage += onMessage;
110
+
111
+         postSetup();
110 112
       }
111 113
 
112 114
       //

+ 2
- 0
Source/Modules/Mod_Idgames.cs View File

@@ -44,6 +44,8 @@ namespace ProjectGolan.Vrobot3.Modules
44 44
                    "Example: .idgames scythe, filename, 4\n" +
45 45
                    "Example: .idgames"
46 46
          };
47
+
48
+         postSetup();
47 49
       }
48 50
 
49 51
       //

+ 18
- 25
Source/Modules/Mod_Links.cs View File

@@ -1,8 +1,14 @@
1
-//
2
-// Mod_Links.cs
1
+//-----------------------------------------------------------------------------
3 2
 //
4
-// Link title capabilities.
3
+// Copyright © 2016 Project Golan
5 4
 //
5
+// See "LICENSE" for more information.
6
+//
7
+//-----------------------------------------------------------------------------
8
+//
9
+// Link expansion.
10
+//
11
+//-----------------------------------------------------------------------------
6 12
 
7 13
 using System;
8 14
 using System.Text.RegularExpressions;
@@ -21,42 +27,35 @@ namespace ProjectGolan.Vrobot3
21 27
    //
22 28
    // Mod_Links
23 29
    //
24
-
25
-   public sealed class Mod_Links : IBotModule
30
+   public class Mod_Links : IBotModule
26 31
    {
27 32
       //
28 33
       // URI
29 34
       //
30
-
31 35
       private struct URI
32 36
       {
33 37
          public String method, host, path, query, tag, uri;
34 38
       }
35 39
 
36
-      //
37
-      // Delegates.
38
-
39 40
       private delegate void URIHandler(URI uri, String referer, ref String result);
40 41
 
41 42
       //
42
-      // Ctor
43
+      // Mod_Links constructor
43 44
       //
44
-
45 45
       public Mod_Links(Bot bot_) :
46 46
          base(bot_)
47 47
       {
48 48
          events.OnMessage += Evt_OnMessage;
49
+
50
+         postSetup();
49 51
       }
50 52
 
51 53
       //
52 54
       // Evt_OnMessage
53 55
       //
54
-
55 56
       public void Evt_OnMessage(UserInfo usr, String channel, String msg, bool iscmd)
56 57
       {
57
-         //
58 58
          // Do this asynchronously, we don't want link parsing to block operation.
59
-
60 59
          new Thread(() => {
61 60
             try
62 61
             {
@@ -73,7 +72,6 @@ namespace ProjectGolan.Vrobot3
73 72
       //
74 73
       // GetURITitle
75 74
       //
76
-
77 75
       private Match GetURITitle(URI uri, String referer, int kb = 16)
78 76
       {
79 77
          String rstr = Utils.GetResponseString(uri.uri, 1024 * kb, referer);
@@ -87,7 +85,6 @@ namespace ProjectGolan.Vrobot3
87 85
       //
88 86
       // URI_Default
89 87
       //
90
-
91 88
       private void URI_Default(URI uri, String referer, ref String result)
92 89
       {
93 90
          var req = WebRequest.Create(uri.uri) as HttpWebRequest;
@@ -112,9 +109,8 @@ namespace ProjectGolan.Vrobot3
112 109
       //
113 110
       // URI_Youtube
114 111
       //
115
-      // Special fucking flower.
112
+      // Special fucking snowflake.
116 113
       //
117
-
118 114
       private void URI_Youtube(URI uri, String referer, ref String result)
119 115
       {
120 116
          var req = WebRequest.Create(uri.uri) as HttpWebRequest;
@@ -138,7 +134,6 @@ namespace ProjectGolan.Vrobot3
138 134
       //
139 135
       // URI_Gelooru
140 136
       //
141
-
142 137
       private void URI_Gelbooru(URI uri, String referer, ref String result)
143 138
       {
144 139
          var match = GetURITitle(uri, referer, 8); // Should be OK to just get the first 8kb here.
@@ -155,7 +150,6 @@ namespace ProjectGolan.Vrobot3
155 150
       //
156 151
       // URI_Hitbox
157 152
       //
158
-
159 153
       private void URI_Hitbox(URI uri, String referer, ref String result)
160 154
       {
161 155
          String name = WebUtility.HtmlEncode(uri.path.TrimStart(new char[]{'/'}));
@@ -185,7 +179,6 @@ namespace ProjectGolan.Vrobot3
185 179
       //
186 180
       // This function is really complicated because of exploits. Fuck exploits.
187 181
       //
188
-
189 182
       private void TryParseURIs(String channel, String msg)
190 183
       {
191 184
          try
@@ -260,10 +253,10 @@ namespace ProjectGolan.Vrobot3
260 253
                      referer = uri.method + "://" + uri.host;
261 254
 
262 255
                      Dictionary<String, URIHandler> handlers = new Dictionary<String, URIHandler>(){
263
-                        { "youtube.com",  URI_Youtube  },
264
-                        { "youtu.be",     URI_Youtube  },
265
-                        { "gelbooru.com", URI_Gelbooru },
266
-                        { "hitbox.tv",    URI_Hitbox   },
256
+                        {"youtube.com",  URI_Youtube },
257
+                        {"youtu.be",     URI_Youtube },
258
+                        {"gelbooru.com", URI_Gelbooru},
259
+                        {"hitbox.tv",    URI_Hitbox  },
267 260
                      };
268 261
 
269 262
                      String hostst = Regex.Replace(uri.host, @"^www\.", String.Empty, RegexOptions.Multiline);

+ 13
- 27
Source/Modules/Mod_Memo.cs View File

@@ -1,9 +1,15 @@
1
-//
2
-// Mod_Memo.cs
1
+//-----------------------------------------------------------------------------
2
+//
3
+// Copyright © 2016 Project Golan
4
+//
5
+// See "LICENSE" for more information.
6
+//
7
+//-----------------------------------------------------------------------------
3 8
 //
4 9
 // Memoing capabilities.
5
-// @memocount, .memo, .memoseen
10
+// .memo
6 11
 //
12
+//-----------------------------------------------------------------------------
7 13
 
8 14
 using System;
9 15
 using System.Collections.Generic;
@@ -16,13 +22,11 @@ namespace ProjectGolan.Vrobot3.Modules
16 22
    //
17 23
    // Mod_Memo
18 24
    //
19
-
20
-   public sealed class Mod_Memo : IBotModule
25
+   public class Mod_Memo : IBotModule
21 26
    {
22 27
       //
23 28
       // MemoFlags
24 29
       //
25
-
26 30
       [Flags]
27 31
       enum MemoFlags
28 32
       {
@@ -32,33 +36,22 @@ namespace ProjectGolan.Vrobot3.Modules
32 36
       //
33 37
       // MemoInfo
34 38
       //
35
-
36 39
       private struct MemoInfo
37 40
       {
38
-         //
39
-         // Data.
40
-
41 41
          public String content;
42 42
          public String sender;
43 43
          public DateTime time;
44 44
          public MemoFlags flags;
45 45
       };
46 46
 
47
-      //
48 47
       // MemoDict
49
-      //
50
-
51 48
       private class MemoDict : Dictionary<String, List<MemoInfo>> {}
52 49
 
53
-      //
54
-      // Data.
55
-
56 50
       MemoDict memos = new MemoDict();
57 51
 
58 52
       //
59
-      // Ctor
53
+      // Mod_Memo constructor
60 54
       //
61
-
62 55
       public Mod_Memo(Bot bot_) :
63 56
          base(bot_)
64 57
       {
@@ -86,12 +79,13 @@ namespace ProjectGolan.Vrobot3.Modules
86 79
          events.OnMessage += Evt_OnMessage;
87 80
          events.OnDisconnected += Evt_OnDisconnected;
88 81
          events.OnSeen += Evt_OnSeen;
82
+
83
+         postSetup();
89 84
       }
90 85
 
91 86
       //
92 87
       // Cmd_MemoCount
93 88
       //
94
-
95 89
       public void Cmd_MemoCount(UserInfo usr, String channel, String msg)
96 90
       {
97 91
          bot.Reply(usr, channel, memos.Count.ToString());
@@ -100,7 +94,6 @@ namespace ProjectGolan.Vrobot3.Modules
100 94
       //
101 95
       // Cmd_Memo
102 96
       //
103
-
104 97
       public void Cmd_Memo(UserInfo usr, String channel, String msg)
105 98
       {
106 99
          String[] args = Utils.GetArguments(msg, commands["memo"].help, 2, 2, ' ');
@@ -119,7 +112,6 @@ namespace ProjectGolan.Vrobot3.Modules
119 112
       //
120 113
       // Cmd_MemoSeen
121 114
       //
122
-
123 115
       public void Cmd_MemoSeen(UserInfo usr, String channel, String msg)
124 116
       {
125 117
          String[] args = Utils.GetArguments(msg, commands["memoseen"].help, 2, 2, ' ');
@@ -139,7 +131,6 @@ namespace ProjectGolan.Vrobot3.Modules
139 131
       //
140 132
       // AddMemo
141 133
       //
142
-
143 134
       private void AddMemo(String name, MemoInfo memo)
144 135
       {
145 136
          name = name.ToLower();
@@ -155,7 +146,6 @@ namespace ProjectGolan.Vrobot3.Modules
155 146
       //
156 147
       // OutputMemos
157 148
       //
158
-
159 149
       private void OutputMemos(String channel, String realnick, bool onseen)
160 150
       {
161 151
          String nick = realnick.ToLower();
@@ -198,7 +188,6 @@ namespace ProjectGolan.Vrobot3.Modules
198 188
       //
199 189
       // Evt_OnMessage
200 190
       //
201
-
202 191
       public void Evt_OnMessage(UserInfo usr, String channel, String msg, bool iscmd)
203 192
       {
204 193
          OutputMemos(channel, usr.Nick, false);
@@ -207,7 +196,6 @@ namespace ProjectGolan.Vrobot3.Modules
207 196
       //
208 197
       // Evt_OnSeen
209 198
       //
210
-
211 199
       public void Evt_OnSeen(UserInfo usr, String channel)
212 200
       {
213 201
          OutputMemos(channel, usr.Nick, true);
@@ -216,7 +204,6 @@ namespace ProjectGolan.Vrobot3.Modules
216 204
       //
217 205
       // Evt_OnDisconnected
218 206
       //
219
-
220 207
       public void Evt_OnDisconnected()
221 208
       {
222 209
          WriteMemos();
@@ -225,7 +212,6 @@ namespace ProjectGolan.Vrobot3.Modules
225 212
       //
226 213
       // WriteMemos
227 214
       //
228
-
229 215
       private void WriteMemos()
230 216
       {
231 217
          File.WriteAllText("/srv/irc/vrobot3/data/memos." + bot.n_groupname + ".json",

+ 3
- 1
Source/Modules/Mod_Quote.cs View File

@@ -40,11 +40,13 @@ namespace ProjectGolan.Vrobot3.Modules
40 40
          base(bot_)
41 41
       {
42 42
          commands["quote"] = new BotCommandStructure{
43
-            cmd = cmdQuote,
43
+            cmd  = cmdQuote,
44 44
             help = "Get a quote from the Doominati Quote DB.\n" +
45 45
                    "Syntax: .quote [id]\n" +
46 46
                    "Example: .quote 536"
47 47
          };
48
+
49
+         postSetup();
48 50
       }
49 51
 
50 52
       //

+ 20
- 33
Source/Modules/Mod_Seen.cs View File

@@ -1,8 +1,14 @@
1
-//
2
-// Mod_Seen.cs
1
+//-----------------------------------------------------------------------------
2
+//
3
+// Copyright © 2016 Project Golan
4
+//
5
+// See "LICENSE" for more information.
6
+//
7
+//-----------------------------------------------------------------------------
3 8
 //
4 9
 // .seen
5 10
 //
11
+//-----------------------------------------------------------------------------
6 12
 
7 13
 using System;
8 14
 using System.Collections.Generic;
@@ -17,36 +23,27 @@ namespace ProjectGolan.Vrobot3
17 23
    //
18 24
    // Mod_Seen
19 25
    //
20
-
21
-   public sealed class Mod_Seen : IBotModule
26
+   public class Mod_Seen : IBotModule
22 27
    {
23 28
       //
24 29
       // SeenName
25 30
       //
26
-
27 31
       private class SeenName
28 32
       {
29 33
          public String real, check;
30 34
          public DateTime time;
31 35
       }
32 36
 
33
-      //
34 37
       // SeenDates
35
-      //
36
-
37 38
       private class SeenDates : List<SeenName> {}
38 39
 
39
-      //
40
-      // Data.
41
-
42 40
       private SeenDates seendates = new SeenDates();
43 41
       private TimeZoneInfo burb;
44 42
       private DateTime lastwrite = DateTime.Now;
45 43
 
46 44
       //
47
-      // Ctor
45
+      // Mod_Seen constructor
48 46
       //
49
-
50 47
       public Mod_Seen(Bot bot_) :
51 48
          base(bot_)
52 49
       {
@@ -55,8 +52,8 @@ namespace ProjectGolan.Vrobot3
55 52
                                                                                   bot.n_groupname + ".json"));
56 53
 
57 54
          commands["seen"] = new BotCommandStructure { cmd = Cmd_Seen,
58
-            help = "Responds with the last time I saw someone. || " +
59
-                   "Syntax: .seen person || " +
55
+            help = "Responds with the last time I saw someone.\n" +
56
+                   "Syntax: .seen person\n" +
60 57
                    "Example: .seen vrobot3"
61 58
          };
62 59
          
@@ -64,12 +61,13 @@ namespace ProjectGolan.Vrobot3
64 61
          events.OnDisconnected += Evt_OnDisconnected;
65 62
 
66 63
          burb = TimeZoneInfo.CreateCustomTimeZone("burb", new TimeSpan(10, -30, 0), "burb", "burb");
64
+
65
+         postSetup();
67 66
       }
68 67
 
69 68
       //
70 69
       // Cmd_Seen
71 70
       //
72
-
73 71
       public void Cmd_Seen(UserInfo usr, String channel, String msg)
74 72
       {
75 73
          if(msg.Length == 0 || msg.Contains(" "))
@@ -79,20 +77,12 @@ namespace ProjectGolan.Vrobot3
79 77
          var seen = from sdata in seendates where sdata.check == name select sdata;
80 78
          if(seen.Any())
81 79
          {
82
-            var other = seen.First();
83
-            String outp = String.Empty;
84
-
85
-            outp += "I last saw ";
86
-            outp += other.real;
87
-            outp += " active ";
88
-            outp += Utils.FuzzyRelativeDate(other.time, DateTime.Now.FromNtp());
89
-            outp += ", at ";
90
-            outp += other.time.ToShortTimeString();
91
-            outp += " CST (";
92
-            outp += TimeZoneInfo.ConvertTime(other.time, TimeZoneInfo.Local, burb).ToShortTimeString();
93
-            outp += " pidgeon time).";
94
-
95
-            bot.Reply(usr, channel, outp);
80
+            var other   = seen.First();
81
+            var fuzzy   = Utils.FuzzyRelativeDate(other.time, DateTime.Now.FromNtp());
82
+            var time    = other.time.ToShortTimeString();
83
+            var pidgeon = TimeZoneInfo.ConvertTime(other.time, TimeZoneInfo.Local, burb).ToShortTimeString();
84
+
85
+            bot.Reply(usr, channel, $"I last saw {other.real} active {fuzzy}, at {time} CST ({pidgeon} AEST.)");
96 86
          }
97 87
          else
98 88
             bot.Reply(usr, channel, "I haven't seen " + msg + " before, sorry.");
@@ -103,7 +93,6 @@ namespace ProjectGolan.Vrobot3
103 93
       //
104 94
       // Evt_OnScreen
105 95
       //
106
-
107 96
       public void Evt_OnSeen(UserInfo usr, String channel)
108 97
       {
109 98
          String name = usr.Nick.ToLower();
@@ -123,7 +112,6 @@ namespace ProjectGolan.Vrobot3
123 112
       //
124 113
       // Evt_OnDisconnected
125 114
       //
126
-
127 115
       public void Evt_OnDisconnected()
128 116
       {
129 117
          WriteSeenDates();
@@ -132,7 +120,6 @@ namespace ProjectGolan.Vrobot3
132 120
       //
133 121
       // WriteSeenDates
134 122
       //
135
-
136 123
       private void WriteSeenDates()
137 124
       {
138 125
          File.WriteAllText("/srv/irc/vrobot3/data/seendates." + bot.n_groupname + ".json",

+ 2
- 0
Source/Modules/Mod_Shittalk.cs View File

@@ -24,6 +24,8 @@ namespace ProjectGolan.Vrobot3.Modules
24 24
          base(bot_)
25 25
       {
26 26
          events.onMessage += onMessage;
27
+
28
+         postSetup();
27 29
       }
28 30
 
29 31
       //

+ 6
- 3
Source/Modules/Mod_Utils.cs View File

@@ -61,6 +61,8 @@ namespace ProjectGolan.Vrobot3.Modules
61 61
             cmd = cmdMystery,
62 62
             help = @"Does nothing. \o/"
63 63
          };
64
+
65
+         postSetup();
64 66
       }
65 67
 
66 68
       //
@@ -117,8 +119,9 @@ namespace ProjectGolan.Vrobot3.Modules
117 119
                let fhidden = kvp.Value.Item2.hidden
118 120
                let fadmin  = kvp.Value.Item2.role != BotRole.User
119 121
                where
120
-                  bot.checkModPermissions(channel, this.GetType()) &&
121
-                  (admin || !fadmin) && !fhidden
122
+                  bot.checkModPermissions(channel, kvp.Value.Item2.mod) &&
123
+                  ((admin && fadmin) || (!admin && !fadmin)) &&
124
+                  !fhidden
122 125
                orderby kvp.Key
123 126
                select kvp.Key;
124 127
 
@@ -196,7 +199,7 @@ namespace ProjectGolan.Vrobot3.Modules
196 199
             "Looks good to me.",
197 200
             "Sure, why not?",
198 201
             "It is certain.",
199
-            "Please no. Please no. Please no.",
202
+            "pls no",
200 203
             "Yes, please.",
201 204
             "Nah.",
202 205
             "Go for it!",

Loading…
Cancel
Save