-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathchat_spam_throttle.sp
129 lines (110 loc) · 4.04 KB
/
chat_spam_throttle.sp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma semicolon 1
#pragma newdecls required
#define MAXMSGLEN 192
Handle g_hCvarDebug = INVALID_HANDLE;
Handle g_hCvarExpireTime = INVALID_HANDLE;
Handle g_hCvarCheckSender = INVALID_HANDLE;
ArrayList g_hMsgTime;
ArrayList g_hMsgSender;
ArrayList g_hMsgText;
int g_iMsgCount = 0;
public Plugin myinfo = {
name = "Chat Spam Throttle",
author = "devilesk",
description = "Chat filter to prevent spamming the same message too often.",
version = "0.3.0",
url = "https://github.com/devilesk/rl4d2l-plugins"
}
public void OnPluginStart() {
g_hCvarDebug = CreateConVar("chat_spam_throttle_debug", "0", "Chat Spam Throttle debug mode", 0, true, 0.0, true, 1.0);
g_hCvarExpireTime = CreateConVar("chat_spam_throttle_time", "20", "Time in seconds before a message can be repeated.", 0, true, 0.0);
g_hCvarCheckSender = CreateConVar("chat_spam_throttle_check_sender", "1", "Allow repeating messages sent by someone else.", 0);
g_hMsgTime = new ArrayList(32);
g_hMsgSender = new ArrayList(32);
g_hMsgText = new ArrayList(MAXMSGLEN);
}
public void OnMapStart() {
g_hMsgTime.Clear();
g_hMsgSender.Clear();
g_hMsgText.Clear();
g_iMsgCount = 0;
}
public Action OnClientSayCommand(int client, const char[] command, const char[] args) {
char message[MAXMSGLEN];
PrintDebug("[OnClientSayCommand] command: %s, args: %s", command, args);
if (args[0] == '!' || args[0] == '/') return Plugin_Continue;
strcopy(message, MAXMSGLEN, args);
FilterMessage(message);
if(FindMessage(client, message) != -1) {
PrintToChat(client, "You are sending that message too often.");
return Plugin_Handled;
}
return Plugin_Continue;
}
public int FindMessage(int client, const char[] message) {
int expireTime = GetTime() - GetConVarInt(g_hCvarExpireTime);
bool bCheckSender = GetConVarBool(g_hCvarCheckSender);
char msg[MAXMSGLEN];
for (int i = g_iMsgCount - 1; i >= 0; i--) {
if (g_hMsgTime.Get(i) < expireTime) {
UntrackMessage(i);
continue;
}
if (bCheckSender && client != g_hMsgSender.Get(i)) continue;
g_hMsgText.GetString(i, msg, sizeof(msg));
if (!StrEqual(message, msg, false)) continue;
PrintDebug("[FindMessage] match %s, %s", message, msg);
return i;
}
PrintDebug("[FindMessage] no match %s, %s", message, msg);
TrackMessage(client, message);
return -1;
}
public void UntrackMessage(int index) {
g_hMsgTime.Erase(index);
g_hMsgSender.Erase(index);
g_hMsgText.Erase(index);
g_iMsgCount--;
}
public void TrackMessage(int client, const char[] message) {
g_hMsgTime.Push(GetTime());
g_hMsgSender.Push(client);
g_hMsgText.PushString(message);
g_iMsgCount++;
}
// Based on Unicode Name Filter https://forums.alliedmods.net/showthread.php?p=2207177?p=2207177
public void FilterMessage(char[] message) {
TrimString(message);
int charMax = strlen(message);
int charIndex;
int copyPos = 0;
char strippedString[MAXMSGLEN];
for (charIndex = 0; charIndex < charMax; charIndex++) {
// Reach end of string. Break.
if (message[copyPos] == 0) {
strippedString[copyPos] = 0;
break;
}
if (GetCharBytes(message[charIndex]) > 1) continue;
if (IsAlphaNumeric(message[charIndex]) || IsCharSpace(message[charIndex])) {
strippedString[copyPos] = message[charIndex];
copyPos++;
continue;
}
}
// Copy back to passing parameter.
strcopy(message, MAXMSGLEN, strippedString);
PrintDebug("[FilterMessage] message: %s", message);
}
public bool IsAlphaNumeric(int characterNum) {
return ((characterNum >= 48 && characterNum <= 57)
|| (characterNum >= 65 && characterNum <= 90)
|| (characterNum >= 97 && characterNum <= 122));
}
stock void PrintDebug(const char[] Message, any ...) {
if (GetConVarBool(g_hCvarDebug)) {
char DebugBuff[256];
VFormat(DebugBuff, sizeof(DebugBuff), Message, 2);
LogMessage(DebugBuff);
}
}