1
0
mirror of https://github.com/anope/anope.git synced 2026-06-28 07:56:37 +02:00

Cleand up alot of the code in bs_badwords, made it much more C++-ish

git-svn-id: http://anope.svn.sourceforge.net/svnroot/anope/trunk@2654 5417fbe8-f217-4b02-8779-1006273d7864
This commit is contained in:
Adam-
2009-11-17 03:15:31 +00:00
parent 88c0edc8f8
commit 88330c07ad
6 changed files with 207 additions and 207 deletions
+30 -4
View File
@@ -63,8 +63,9 @@ class CoreExport ChannelInfo : public Extensible, public Flags<ChannelInfoFlag>
{
private:
std::map<ChannelModeName, std::string> Params; /* Map of parameters by mode name */
std::vector<ChanAccess *> access; /* List of authorized users */
std::vector<AutoKick *> akick; /* List of users to kickban */
std::vector<ChanAccess *> access; /* List of authorized users */
std::vector<AutoKick *> akick; /* List of users to kickban */
std::vector<BadWord *> badwords; /* List of badwords */
std::bitset<128> mlock_on; /* Modes mlocked on */
std::bitset<128> mlock_off; /* Modes mlocked off */
@@ -112,8 +113,6 @@ class CoreExport ChannelInfo : public Extensible, public Flags<ChannelInfoFlag>
Flags<BotServFlag> botflags;
int16 *ttb; /* Times to ban for each kicker */
uint16 bwcount;
BadWord *badwords; /* For BADWORDS kicker */
int16 capsmin, capspercent; /* For CAPS kicker */
int16 floodlines, floodsecs; /* For FLOOD kicker */
int16 repeattimes; /* For REPEAT kicker */
@@ -209,6 +208,33 @@ class CoreExport ChannelInfo : public Extensible, public Flags<ChannelInfoFlag>
*/
void ClearAkick();
/** Add a badword to the badword list
* @param word The badword
* @param type The type (SINGLE START END)
* @return The badword
*/
BadWord *AddBadWord(const std::string &word, BadWordType type);
/** Get a badword structure by index
* @param index The index
* @return The badword
*/
BadWord *GetBadWord(unsigned index);
/** Get how many badwords are on this channel
* @return The number of badwords in the vector
*/
const unsigned GetBadWordCount() const;
/** Remove a badword
* @param badword The badword
*/
void EraseBadWord(BadWord *badword);
/** Clear all badwords from the channel
*/
void ClearBadWords();
/** Check if a mode is mlocked
* @param Name The mode
* @param status True to check mlock on, false for mlock off
+1 -2
View File
@@ -746,8 +746,7 @@ enum BadWordType
/* Structure used to contain bad words. */
struct BadWord
{
uint16 in_use;
char *word;
std::string word;
BadWordType type;
};
+21 -23
View File
@@ -232,7 +232,6 @@ void botchanmsgs(User * u, ChannelInfo * ci, char *buf)
/* Bad words kicker */
if (ci->botflags.HasFlag(BS_KICK_BADWORDS)) {
int i;
int mustkick = 0;
char *nbuf;
BadWord *bw;
@@ -240,28 +239,27 @@ void botchanmsgs(User * u, ChannelInfo * ci, char *buf)
/* Normalize the buffer */
nbuf = normalizeBuffer(buf);
for (i = 0, bw = ci->badwords; i < ci->bwcount; i++, bw++) {
if (!bw->in_use)
continue;
for (unsigned i = 0; i < ci->GetBadWordCount(); ++i)
{
bw = ci->GetBadWord(i);
if (bw->type == BW_ANY
&& ((BSCaseSensitive && strstr(nbuf, bw->word))
|| (!BSCaseSensitive && stristr(nbuf, bw->word)))) {
&& ((BSCaseSensitive && strstr(nbuf, bw->word.c_str()))
|| (!BSCaseSensitive && stristr(nbuf, bw->word.c_str())))) {
mustkick = 1;
} else if (bw->type == BW_SINGLE) {
int len = strlen(bw->word);
int len = bw->word.length();
if ((BSCaseSensitive && !strcmp(nbuf, bw->word))
if ((BSCaseSensitive && nbuf == bw->word)
|| (!BSCaseSensitive
&& (!stricmp(nbuf, bw->word)))) {
&& (!stricmp(nbuf, bw->word.c_str())))) {
mustkick = 1;
/* two next if are quite odd isn't it? =) */
} else if ((strchr(nbuf, ' ') == nbuf + len)
&&
((BSCaseSensitive
&& !strcmp(nbuf, bw->word))
((BSCaseSensitive && nbuf == bw->word)
|| (!BSCaseSensitive
&& (stristr(nbuf, bw->word) ==
&& (stristr(nbuf, bw->word.c_str()) ==
nbuf)))) {
mustkick = 1;
} else {
@@ -269,10 +267,10 @@ void botchanmsgs(User * u, ChannelInfo * ci, char *buf)
nbuf + strlen(nbuf) - len - 1)
&&
((BSCaseSensitive
&& (strstr(nbuf, bw->word) ==
&& (strstr(nbuf, bw->word.c_str()) ==
nbuf + strlen(nbuf) - len))
|| (!BSCaseSensitive
&& (stristr(nbuf, bw->word) ==
&& (stristr(nbuf, bw->word.c_str()) ==
nbuf + strlen(nbuf) - len)))) {
mustkick = 1;
} else {
@@ -281,7 +279,7 @@ void botchanmsgs(User * u, ChannelInfo * ci, char *buf)
wordbuf[0] = ' ';
wordbuf[len + 1] = ' ';
wordbuf[len + 2] = '\0';
memcpy(wordbuf + 1, bw->word, len);
memcpy(wordbuf + 1, bw->word.c_str(), len);
if ((BSCaseSensitive
&& (strstr(nbuf, wordbuf)))
@@ -295,17 +293,17 @@ void botchanmsgs(User * u, ChannelInfo * ci, char *buf)
}
}
} else if (bw->type == BW_START) {
int len = strlen(bw->word);
int len = bw->word.length();
if ((BSCaseSensitive
&& (!strncmp(nbuf, bw->word, len)))
&& (!strncmp(nbuf, bw->word.c_str(), len)))
|| (!BSCaseSensitive
&& (!strnicmp(nbuf, bw->word, len)))) {
&& (!strnicmp(nbuf, bw->word.c_str(), len)))) {
mustkick = 1;
} else {
char *wordbuf = new char[len + 2];
memcpy(wordbuf + 1, bw->word, len);
memcpy(wordbuf + 1, bw->word.c_str(), len);
wordbuf[0] = ' ';
wordbuf[len + 1] = '\0';
@@ -317,22 +315,22 @@ void botchanmsgs(User * u, ChannelInfo * ci, char *buf)
delete [] wordbuf;
}
} else if (bw->type == BW_END) {
int len = strlen(bw->word);
int len = bw->word.length();
if ((BSCaseSensitive
&&
(!strncmp
(nbuf + strlen(nbuf) - len, bw->word, len)))
(nbuf + strlen(nbuf) - len, bw->word.c_str(), len)))
|| (!BSCaseSensitive
&&
(!strnicmp
(nbuf + strlen(nbuf) - len, bw->word,
(nbuf + strlen(nbuf) - len, bw->word.c_str(),
len)))) {
mustkick = 1;
} else {
char *wordbuf = new char[len + 2];
memcpy(wordbuf, bw->word, len);
memcpy(wordbuf, bw->word.c_str(), len);
wordbuf[len] = ' ';
wordbuf[len + 1] = '\0';
+22 -19
View File
@@ -262,10 +262,7 @@ void get_chanserv_stats(long *nrec, long *memuse)
}
if (ci->ttb)
mem += sizeof(*ci->ttb) * TTB_SIZE;
mem += ci->bwcount * sizeof(BadWord);
for (j = 0; j < ci->bwcount; j++)
if (ci->badwords[j].word)
mem += strlen(ci->badwords[j].word) + 1;
mem += ci->GetBadWordCount() * sizeof(BadWord);
}
}
*nrec = count;
@@ -565,20 +562,21 @@ void load_cs_dbase()
SAFE(read_int16(&tmp16, f));
ci->repeattimes = tmp16;
SAFE(read_int16(&ci->bwcount, f));
if (ci->bwcount) {
ci->badwords = static_cast<BadWord *>(scalloc(ci->bwcount, sizeof(BadWord)));
for (j = 0; j < ci->bwcount; j++) {
SAFE(read_int16(&ci->badwords[j].in_use, f));
if (ci->badwords[j].in_use) {
SAFE(read_string(&ci->badwords[j].word, f));
SAFE(read_int16(&tmp16, f));
if (tmp16) {
for (j = 0; j < tmp16; j++) {
uint16 inuse;
SAFE(read_int16(&inuse, f));
//if (ci->badwords[j].in_use) {
char *badword;
SAFE(read_string(&badword, f));
//SAFE(read_int16(&ci->badwords[j].type, f));
SAFE(read_int16(&tmp16, f));
ci->badwords[j].type = BW_ANY; // for now
}
//ci->badwords[j].type = BW_ANY; // for now
ci->AddBadWord(badword, BW_ANY);
delete [] badword;
// }
}
} else {
ci->badwords = NULL;
}
} /* while (getc_db(f) != 0) */
@@ -749,13 +747,18 @@ void save_cs_dbase()
SAFE(write_int16(ci->floodsecs, f));
SAFE(write_int16(ci->repeattimes, f));
SAFE(write_int16(ci->bwcount, f));
for (j = 0; j < ci->bwcount; j++) {
SAFE(write_int16(ci->badwords[j].in_use, f));
//SAFE(write_int16(ci->bwcount, f));
SAFE(write_int16(ci->GetBadWordCount(), f));
for (j = 0; j < ci->GetBadWordCount(); j++) {
BadWord *bw = ci->GetBadWord(j);
/*SAFE(write_int16(ci->badwords[j].in_use, f));
if (ci->badwords[j].in_use) {
SAFE(write_string(ci->badwords[j].word, f));
SAFE(write_int16(ci->badwords[j].type, f));
}
}*/
SAFE(write_int16(1, f));
SAFE(write_string(bw->word.c_str(), f));
SAFE(write_int16(0, f));
}
} /* for (chanlists[i]) */
+74 -148
View File
@@ -22,29 +22,28 @@ int badwords_list_callback(User * u, int num, va_list args);
class CommandBSBadwords : public Command
{
private:
CommandReturn DoList(User *u, ChannelInfo *ci, const char *word)
CommandReturn DoList(User *u, ChannelInfo *ci, const ci::string &word)
{
int sent_header = 0;
int i = 0;
if (ci->bwcount == 0)
if (!ci->GetBadWordCount())
{
notice_lang(s_BotServ, u, BOT_BADWORDS_LIST_EMPTY, ci->name);
return MOD_CONT;
}
if (word && strspn(word, "1234567890,-") == strlen(word))
if (!word.empty() && strspn(word.c_str(), "1234567890,-") == word.length())
{
process_numlist(word, NULL, badwords_list_callback, u, ci, &sent_header);
process_numlist(word.c_str(), NULL, badwords_list_callback, u, ci, &sent_header);
}
else
{
for (i = 0; i < ci->bwcount; i++)
for (unsigned i = 0; i < ci->GetBadWordCount(); i++)
{
if (!(ci->badwords[i].in_use))
continue;
if (word && ci->badwords[i].word
&& !Anope::Match(ci->badwords[i].word, word, false))
BadWord *badword = ci->GetBadWord(i);
if (!word.empty() && !Anope::Match(badword->word, word.c_str(), false))
continue;
badwords_list(u, i, ci, &sent_header);
}
}
@@ -54,93 +53,65 @@ class CommandBSBadwords : public Command
return MOD_CONT;
}
CommandReturn DoAdd(User *u, ChannelInfo *ci, const char *word)
CommandReturn DoAdd(User *u, ChannelInfo *ci, const ci::string &word)
{
char *opt, *pos;
size_t pos = word.find_last_of(" ");
BadWordType type = BW_ANY;
unsigned i = 0;
BadWord *bw;
ci::string realword = word;
if (readonly)
if (pos != ci::string::npos)
{
notice_lang(s_BotServ, u, BOT_BADWORDS_DISABLED);
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() >= BSBadWordsMax)
{
notice_lang(s_BotServ, u, BOT_BADWORDS_REACHED_LIMIT, BSBadWordsMax);
return MOD_CONT;
}
pos = strrchr(const_cast<char *>(word), ' '); // XXX - Potentially unsafe cast
if (pos)
for (unsigned i = 0; i < ci->GetBadWordCount(); ++i)
{
opt = pos + 1;
if (*opt)
{
if (!stricmp(opt, "SINGLE"))
type = BW_SINGLE;
else if (!stricmp(opt, "START"))
type = BW_START;
else if (!stricmp(opt, "END"))
type = BW_END;
if (type != BW_ANY)
*pos = 0;
}
}
BadWord *bw = ci->GetBadWord(i);
for (bw = ci->badwords, i = 0; i < ci->bwcount; bw++, i++)
{
if (bw->word && ((BSCaseSensitive && (!strcmp(bw->word, word)))
|| (!BSCaseSensitive
&& (!stricmp(bw->word, word)))))
if (!bw->word.empty() && (BSCaseSensitive && !stricmp(bw->word.c_str(), realword.c_str())
|| (!BSCaseSensitive && bw->word == realword.c_str())))
{
notice_lang(s_BotServ, u, BOT_BADWORDS_ALREADY_EXISTS,
bw->word, ci->name);
notice_lang(s_BotServ, u, BOT_BADWORDS_ALREADY_EXISTS, bw->word.c_str(), ci->name);
return MOD_CONT;
}
}
for (i = 0; i < ci->bwcount; i++)
{
if (!ci->badwords[i].in_use)
break;
}
if (i == ci->bwcount)
{
if (i < BSBadWordsMax)
{
ci->bwcount++;
ci->badwords =
static_cast<BadWord *>(srealloc(ci->badwords, sizeof(BadWord) * ci->bwcount));
}
else
{
notice_lang(s_BotServ, u, BOT_BADWORDS_REACHED_LIMIT,
BSBadWordsMax);
return MOD_CONT;
}
}
bw = &ci->badwords[i];
bw->in_use = 1;
bw->word = sstrdup(word);
bw->type = type;
ci->AddBadWord(realword.c_str(), type);
notice_lang(s_BotServ, u, BOT_BADWORDS_ADDED, realword.c_str(), ci->name);
notice_lang(s_BotServ, u, BOT_BADWORDS_ADDED, bw->word, ci->name);
return MOD_CONT;
}
CommandReturn DoDelete(User *u, ChannelInfo *ci, const char *word)
CommandReturn DoDelete(User *u, ChannelInfo *ci, const ci::string &word)
{
int deleted = 0, a, b;
int i;
BadWord *bw;
/* Special case: is it a number/list? Only do search if it isn't. */
if (isdigit(*word) && strspn(word, "1234567890,-") == strlen(word))
if (!word.empty() && isdigit(word[0]) && strspn(word.c_str(), "1234567890,-") == word.length())
{
int count, last = -1;
deleted = process_numlist(word, &count, badwords_del_callback, u, ci, &last);
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(s_BotServ, u, BOT_BADWORDS_NO_SUCH_ENTRY, last, ci->name);
notice_lang(s_BotServ, u, BOT_BADWORDS_NO_SUCH_ENTRY, last, ci->name);
}
else
{
@@ -149,7 +120,7 @@ class CommandBSBadwords : public Command
}
else if (deleted == 1)
{
notice_lang(s_BotServ, u, BOT_BADWORDS_DELETED_ONE, ci->name);
notice_lang(s_BotServ, u, BOT_BADWORDS_DELETED_ONE, ci->name);
}
else
{
@@ -158,74 +129,34 @@ class CommandBSBadwords : public Command
}
else
{
unsigned i;
BadWord *badword;
for (i = 0; i < ci->bwcount; i++)
for (i = 0; i < ci->GetBadWordCount(); ++i)
{
if (ci->badwords[i].in_use && !stricmp(ci->badwords[i].word, word))
badword = ci->GetBadWord(i);
if (badword->word == word)
break;
}
if (i == ci->bwcount)
if (i == ci->GetBadWordCount())
{
notice_lang(s_BotServ, u, BOT_BADWORDS_NOT_FOUND, word, ci->name);
notice_lang(s_BotServ, u, BOT_BADWORDS_NOT_FOUND, word.c_str(), ci->name);
return MOD_CONT;
}
ci->EraseBadWord(badword);
bw = &ci->badwords[i];
notice_lang(s_BotServ, u, BOT_BADWORDS_DELETED, bw->word, ci->name);
if (bw->word)
delete [] bw->word;
bw->word = NULL;
bw->in_use = 0;
deleted = 1;
}
if (deleted) {
/* Reordering - DrStein */
for (b = 0; b < ci->bwcount; b++) {
if (ci->badwords[b].in_use) {
for (a = 0; a < ci->bwcount; a++) {
if (a > b)
break;
if (!(ci->badwords[a].in_use)) {
ci->badwords[a].in_use = ci->badwords[b].in_use;
ci->badwords[a].type = ci->badwords[b].type;
if (ci->badwords[b].word) {
ci->badwords[a].word = sstrdup(ci->badwords[b].word);
delete [] ci->badwords[b].word;
}
ci->badwords[b].word = NULL;
ci->badwords[b].in_use = 0;
break;
}
}
}
}
/* After reordering only the entries at the end could still be empty.
* We ll free the places no longer in use... - Viper */
for (i = ci->bwcount - 1; i >= 0; i--) {
if (ci->badwords[i].in_use)
break;
ci->bwcount--;
}
ci->badwords =
static_cast<BadWord *>(srealloc(ci->badwords,sizeof(BadWord) * ci->bwcount));
notice_lang(s_BotServ, u, BOT_BADWORDS_DELETED, badword->word.c_str(), ci->name);
}
return MOD_CONT;
}
CommandReturn DoClear(User *u, ChannelInfo *ci, const char *word)
CommandReturn DoClear(User *u, ChannelInfo *ci)
{
int i;
for (i = 0; i < ci->bwcount; i++)
if (ci->badwords[i].word)
delete [] ci->badwords[i].word;
free(ci->badwords);
ci->badwords = NULL;
ci->bwcount = 0;
ci->ClearBadWords();
notice_lang(s_BotServ, u, BOT_BADWORDS_CLEAR);
return MOD_CONT;
}
@@ -238,11 +169,11 @@ class CommandBSBadwords : public Command
{
const char *chan = params[0].c_str();
ci::string cmd = params[1];
const char *word = params.size() > 2 ? params[2].c_str() : NULL;
ci::string word = params.size() > 2 ? params[2].c_str() : "";
ChannelInfo *ci;
bool need_args = cmd == "LIST" || cmd == "CLEAR";
if (need_args ? 0 : !word)
if (!need_args && word.empty())
{
this->OnSyntaxError(u, cmd);
return MOD_CONT;
@@ -269,7 +200,7 @@ class CommandBSBadwords : public Command
else if (cmd == "LIST")
return this->DoList(u, ci, word);
else if (cmd == "CLEAR")
return this->DoClear(u, ci, word);
return this->DoClear(u, ci);
else
this->OnSyntaxError(u, "");
@@ -314,37 +245,30 @@ int badwords_del_callback(User * u, int num, va_list args)
*last = num;
if (num < 1 || num > ci->bwcount)
if (num < 1 || num > ci->GetBadWordCount())
return 0;
bw = &ci->badwords[num - 1];
if (bw->word)
delete [] bw->word;
bw->word = NULL;
bw->in_use = 0;
bw = ci->GetBadWord(num - 1);
ci->EraseBadWord(bw);
return 1;
}
int badwords_list(User * u, int index, ChannelInfo * ci, int *sent_header)
{
BadWord *bw = &ci->badwords[index];
BadWord *bw = ci->GetBadWord(index);
if (!bw->in_use)
return 0;
if (!*sent_header) {
if (!*sent_header)
{
notice_lang(s_BotServ, u, BOT_BADWORDS_LIST_HEADER, ci->name);
*sent_header = 1;
}
notice_lang(s_BotServ, u, BOT_BADWORDS_LIST_FORMAT, index + 1,
bw->word,
((bw->type ==
BW_SINGLE) ? "(SINGLE)" : ((bw->type ==
BW_START) ? "(START)"
: ((bw->type ==
BW_END) ? "(END)" : "")))
);
notice_lang(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;
}
@@ -352,8 +276,10 @@ 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->bwcount)
if (num < 1 || num > ci->GetBadWordCount())
return 0;
return badwords_list(u, num - 1, ci, sent_header);
}
+59 -11
View File
@@ -31,8 +31,6 @@ ChannelInfo::ChannelInfo(const std::string &chname)
levels = NULL;
entry_message = NULL;
c = NULL;
bwcount = 0;
badwords = NULL;
capsmin = capspercent = 0;
floodlines = floodsecs = 0;
repeattimes = 0;
@@ -126,15 +124,6 @@ ChannelInfo::~ChannelInfo()
if (this->ttb)
delete [] this->ttb;
for (i = 0; i < this->bwcount; i++)
{
if (this->badwords[i].word)
delete [] this->badwords[i].word;
}
if (this->badwords)
free(this->badwords);
if (this->founder)
this->founder->channelcount--;
}
@@ -333,6 +322,65 @@ void ChannelInfo::ClearAkick()
}
}
/** Add a badword to the badword list
* @param word The badword
* @param type The type (SINGLE START END)
* @return The badword
*/
BadWord *ChannelInfo::AddBadWord(const std::string &word, BadWordType type)
{
BadWord *bw = new BadWord;
bw->word = word;
bw->type = type;
badwords.push_back(bw);
return bw;
}
/** Get a badword structure by index
* @param index The index
* @return The badword
*/
BadWord *ChannelInfo::GetBadWord(unsigned index)
{
if (badwords.empty() || index >= badwords.size())
return NULL;
return badwords[index];
}
/** Get how many badwords are on this channel
* @return The number of badwords in the vector
*/
const unsigned ChannelInfo::GetBadWordCount() const
{
return badwords.empty() ? 0 : badwords.size();
}
/** Remove a badword
* @param badword The badword
*/
void ChannelInfo::EraseBadWord(BadWord *badword)
{
std::vector<BadWord *>::iterator it = std::find(badwords.begin(), badwords.end(), badword);
if (it != badwords.end())
{
delete *it;
badwords.erase(it);
}
}
/** Clear all badwords from the channel
*/
void ChannelInfo::ClearBadWords()
{
for (unsigned i = badwords.size(); i > 0; --i)
{
EraseBadWord(badwords[i - 1]);
}
}
/** Check if a mode is mlocked
* @param Name The mode
* @param status True to check mlock on, false for mlock off