-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFloodControl.cs
96 lines (78 loc) · 2.32 KB
/
FloodControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using MikuBot.Commands;
using MikuBot.Modules;
namespace MikuBot.ExtraPlugins
{
public class FloodControl : MsgCommandModuleBase
{
private class PostList
{
private readonly TimeSpan postExpirationTime = TimeSpan.FromSeconds(20);
private readonly int postIgnoreCount = 10;
private readonly Dictionary<Hostmask, List<DateTime>> postTimes =
new Dictionary<Hostmask, List<DateTime>>(new HostmaskHostnameEqualityComparer());
private bool IsExpired(DateTime time)
{
return (time + postExpirationTime < DateTime.Now);
}
public PostList(IConfig config)
{
ParamIs.NotNull(() => config);
postExpirationTime = TimeSpan.FromSeconds(config.FloodPostExpirationTimeSeconds);
postIgnoreCount = config.FloodPostIgnoreCount;
}
public bool WillBeIgnored(Hostmask hostmask)
{
if (!postTimes.ContainsKey(hostmask))
{
postTimes.Add(hostmask, new List<DateTime>());
}
var entry = postTimes[hostmask];
entry.RemoveAll(IsExpired);
if (entry.Count > postIgnoreCount)
return true;
else
{
entry.Add(DateTime.Now);
return false;
}
}
}
private TimeSpan defaultIgnoreTime = TimeSpan.FromMinutes(2);
private PostList postList;
public override string CommandDescription
{
get { return "Automatically ignores an user who is flooding the bot. Doesn't apply to bot admins."; }
}
public override void HandleCommand(MsgCommand msg, IBotContext bot)
{
if (!msg.HasBotCommand || msg.Sender.IsAuthenticated)
return;
if (postList.WillBeIgnored(msg.SenderHost))
{
bot.IgnoredNickList.Ignore(msg.SenderHost, DateTime.Now + defaultIgnoreTime);
bot.Writer.Msg(msg.ChannelOrSenderNick,
string.Format("{0}: stop harassing me for a while. I'm going to have to ignore you for {1} minutes.",
msg.Sender.Nick, defaultIgnoreTime.Minutes));
}
}
public override void OnLoaded(IBotContext bot, IModuleFile moduleFile)
{
postList = new PostList(bot.Config);
defaultIgnoreTime = TimeSpan.FromMinutes(bot.Config.FloodPostIgnoreTimeMinutes);
}
public override bool IsPassive
{
get { return true; }
}
public override BotUserLevel MinUserLevel
{
get { return BotUserLevel.Unidentified; }
}
public override string Name
{
get { return "FloodControl"; }
}
}
}