diff options
author | Adam- <Adam-@5417fbe8-f217-4b02-8779-1006273d7864> | 2009-11-17 03:15:31 +0000 |
---|---|---|
committer | Adam- <Adam-@5417fbe8-f217-4b02-8779-1006273d7864> | 2009-11-17 03:15:31 +0000 |
commit | 88330c07adc5bb3b085cf6ede1d30c0c48afcfb6 (patch) | |
tree | 3effa4cdbc3c0be79bd812580d77c3884636b6ec /src | |
parent | 88c0edc8f8f2f3aad67e69593b107b7bd0a56a35 (diff) |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/botserv.c | 44 | ||||
-rw-r--r-- | src/chanserv.c | 41 | ||||
-rw-r--r-- | src/core/bs_badwords.c | 212 | ||||
-rw-r--r-- | src/regchannel.cpp | 70 |
4 files changed, 171 insertions, 196 deletions
diff --git a/src/botserv.c b/src/botserv.c index c9e1f3ed7..e56e75d40 100644 --- a/src/botserv.c +++ b/src/botserv.c @@ -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'; diff --git a/src/chanserv.c b/src/chanserv.c index e961921b1..d7b2a3bfa 100644 --- a/src/chanserv.c +++ b/src/chanserv.c @@ -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]) */ diff --git a/src/core/bs_badwords.c b/src/core/bs_badwords.c index 3041ced7b..f8f98be09 100644 --- a/src/core/bs_badwords.c +++ b/src/core/bs_badwords.c @@ -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; - - if (readonly) - { - notice_lang(s_BotServ, u, BOT_BADWORDS_DISABLED); - return MOD_CONT; - } + ci::string realword = word; - pos = strrchr(const_cast<char *>(word), ' '); // XXX - Potentially unsafe cast - if (pos) + if (pos != ci::string::npos) { - opt = pos + 1; - if (*opt) + ci::string opt = ci::string(word, pos + 1); + if (!opt.empty()) { - if (!stricmp(opt, "SINGLE")) + if (opt == "SINGLE") type = BW_SINGLE; - else if (!stricmp(opt, "START")) + else if (opt == "START") type = BW_START; - else if (!stricmp(opt, "END")) + else if (opt == "END") type = BW_END; - if (type != BW_ANY) - *pos = 0; } + realword = ci::string(word, 0, pos); } - for (bw = ci->badwords, i = 0; i < ci->bwcount; bw++, i++) + if (ci->GetBadWordCount() >= BSBadWordsMax) { - if (bw->word && ((BSCaseSensitive && (!strcmp(bw->word, word))) - || (!BSCaseSensitive - && (!stricmp(bw->word, word))))) - { - notice_lang(s_BotServ, u, BOT_BADWORDS_ALREADY_EXISTS, - bw->word, ci->name); - return MOD_CONT; - } + notice_lang(s_BotServ, u, BOT_BADWORDS_REACHED_LIMIT, BSBadWordsMax); + return MOD_CONT; } - for (i = 0; i < ci->bwcount; i++) + for (unsigned i = 0; i < ci->GetBadWordCount(); ++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 + BadWord *bw = ci->GetBadWord(i); + + 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_REACHED_LIMIT, - BSBadWordsMax); + notice_lang(s_BotServ, u, BOT_BADWORDS_ALREADY_EXISTS, bw->word.c_str(), ci->name); return MOD_CONT; } } - bw = &ci->badwords[i]; - bw->in_use = 1; - bw->word = sstrdup(word); - bw->type = type; - notice_lang(s_BotServ, u, BOT_BADWORDS_ADDED, bw->word, ci->name); + ci->AddBadWord(realword.c_str(), type); + + notice_lang(s_BotServ, u, BOT_BADWORDS_ADDED, realword.c_str(), 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); } diff --git a/src/regchannel.cpp b/src/regchannel.cpp index db3850d6c..2b1dee411 100644 --- a/src/regchannel.cpp +++ b/src/regchannel.cpp @@ -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 |