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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/* BotServ core functions
*
* (C) 2003-2010 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*
* $Id$
*
*/
/*************************************************************************/
#include "module.h"
int badwords_del_callback(User * u, int num, va_list args);
int badwords_list(User * u, int index, ChannelInfo * ci, int *sent_header);
int badwords_list_callback(User * u, int num, va_list args);
class CommandBSBadwords : public Command
{
private:
CommandReturn DoList(User *u, ChannelInfo *ci, const ci::string &word)
{
int sent_header = 0;
if (!ci->GetBadWordCount())
{
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_LIST_EMPTY, ci->name.c_str());
return MOD_CONT;
}
if (!word.empty() && strspn(word.c_str(), "1234567890,-") == word.length())
{
process_numlist(word.c_str(), NULL, badwords_list_callback, u, ci, &sent_header);
}
else
{
for (unsigned i = 0; i < ci->GetBadWordCount(); i++)
{
BadWord *badword = ci->GetBadWord(i);
if (!word.empty() && !Anope::Match(badword->word, word.c_str(), false))
continue;
badwords_list(u, i, ci, &sent_header);
}
}
if (!sent_header)
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_NO_MATCH, ci->name.c_str());
return MOD_CONT;
}
CommandReturn DoAdd(User *u, ChannelInfo *ci, const ci::string &word)
{
size_t pos = word.find_last_of(" ");
BadWordType type = BW_ANY;
ci::string realword = word;
if (pos != ci::string::npos)
{
ci::string opt = ci::string(word, pos + 1);
if (!opt.empty())
{
if (opt == "SINGLE")
type = BW_SINGLE;
else if (opt == "START")
type = BW_START;
else if (opt == "END")
type = BW_END;
}
realword = ci::string(word, 0, pos);
}
if (ci->GetBadWordCount() >= Config.BSBadWordsMax)
{
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_REACHED_LIMIT, Config.BSBadWordsMax);
return MOD_CONT;
}
for (unsigned i = 0; i < ci->GetBadWordCount(); ++i)
{
BadWord *bw = ci->GetBadWord(i);
if (!bw->word.empty() && (Config.BSCaseSensitive && !stricmp(bw->word.c_str(), realword.c_str())
|| (!Config.BSCaseSensitive && bw->word == realword.c_str())))
{
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_ALREADY_EXISTS, bw->word.c_str(), ci->name.c_str());
return MOD_CONT;
}
}
ci->AddBadWord(realword.c_str(), type);
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_ADDED, realword.c_str(), ci->name.c_str());
return MOD_CONT;
}
CommandReturn DoDelete(User *u, ChannelInfo *ci, const ci::string &word)
{
/* Special case: is it a number/list? Only do search if it isn't. */
if (!word.empty() && isdigit(word[0]) && strspn(word.c_str(), "1234567890,-") == word.length())
{
int count, deleted, last = -1;
deleted = process_numlist(word.c_str(), &count, badwords_del_callback, u, ci, &last);
if (!deleted)
{
if (count == 1)
{
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_NO_SUCH_ENTRY, last, ci->name.c_str());
}
else
{
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_NO_MATCH, ci->name.c_str());
}
}
else if (deleted == 1)
{
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_DELETED_ONE, ci->name.c_str());
}
else
{
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_DELETED_SEVERAL, deleted, ci->name.c_str());
}
if (deleted)
ci->CleanBadWords();
}
else
{
unsigned i;
BadWord *badword;
for (i = 0; i < ci->GetBadWordCount(); ++i)
{
badword = ci->GetBadWord(i);
if (badword->word == word)
break;
}
if (i == ci->GetBadWordCount())
{
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_NOT_FOUND, word.c_str(), ci->name.c_str());
return MOD_CONT;
}
ci->EraseBadWord(badword);
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_DELETED, badword->word.c_str(), ci->name.c_str());
}
return MOD_CONT;
}
CommandReturn DoClear(User *u, ChannelInfo *ci)
{
ci->ClearBadWords();
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_CLEAR);
return MOD_CONT;
}
public:
CommandBSBadwords() : Command("BADWORDS", 2, 3)
{
}
CommandReturn Execute(User *u, const std::vector<ci::string> ¶ms)
{
const char *chan = params[0].c_str();
ci::string cmd = params[1];
ci::string word = params.size() > 2 ? params[2].c_str() : "";
ChannelInfo *ci;
bool need_args = cmd == "LIST" || cmd == "CLEAR";
if (!need_args && word.empty())
{
this->OnSyntaxError(u, cmd);
return MOD_CONT;
}
ci = cs_findchan(chan);
if (!check_access(u, ci, CA_BADWORDS) && (!need_args || !u->Account()->HasPriv("botserv/administration")))
{
notice_lang(Config.s_BotServ, u, ACCESS_DENIED);
return MOD_CONT;
}
if (readonly)
{
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_DISABLED);
return MOD_CONT;
}
if (cmd == "ADD")
return this->DoAdd(u, ci, word);
else if (cmd == "DEL")
return this->DoDelete(u, ci, word);
else if (cmd == "LIST")
return this->DoList(u, ci, word);
else if (cmd == "CLEAR")
return this->DoClear(u, ci);
else
this->OnSyntaxError(u, "");
return MOD_CONT;
}
bool OnHelp(User *u, const ci::string &subcommand)
{
notice_help(Config.s_BotServ, u, BOT_HELP_BADWORDS);
return true;
}
void OnSyntaxError(User *u, const ci::string &subcommand)
{
syntax_error(Config.s_BotServ, u, "BADWORDS", BOT_BADWORDS_SYNTAX);
}
};
class BSBadwords : public Module
{
public:
BSBadwords(const std::string &modname, const std::string &creator) : Module(modname, creator)
{
this->SetAuthor("Anope");
this->SetVersion("$Id$");
this->SetType(CORE);
this->AddCommand(BOTSERV, new CommandBSBadwords);
ModuleManager::Attach(I_OnBotServHelp, this);
}
void OnBotServHelp(User *u)
{
notice_lang(Config.s_BotServ, u, BOT_HELP_CMD_BADWORDS);
}
};
int badwords_del_callback(User * u, int num, va_list args)
{
ChannelInfo *ci = va_arg(args, ChannelInfo *);
int *last = va_arg(args, int *);
*last = num;
if (num < 1 || num > ci->GetBadWordCount())
return 0;
ci->GetBadWord(num - 1)->InUse = false;
return 1;
}
int badwords_list(User * u, int index, ChannelInfo * ci, int *sent_header)
{
BadWord *bw = ci->GetBadWord(index);
if (!*sent_header)
{
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_LIST_HEADER, ci->name.c_str());
*sent_header = 1;
}
notice_lang(Config.s_BotServ, u, BOT_BADWORDS_LIST_FORMAT, index + 1, bw->word.c_str(),
((bw->type == BW_SINGLE) ? "(SINGLE)" : ((bw->type == BW_START) ? "(START)"
: ((bw->type == BW_END) ? "(END)" : "")))
);
return 1;
}
int badwords_list_callback(User * u, int num, va_list args)
{
ChannelInfo *ci = va_arg(args, ChannelInfo *);
int *sent_header = va_arg(args, int *);
if (num < 1 || num > ci->GetBadWordCount())
return 0;
return badwords_list(u, num - 1, ci, sent_header);
}
MODULE_INIT(BSBadwords)
|