diff options
author | Peter Powell <petpow@saberuk.com> | 2012-09-12 16:27:59 +0100 |
---|---|---|
committer | attilamolnar <attilamolnar@hush.com> | 2012-10-19 17:50:08 +0200 |
commit | 3479532178c73ae47da4729865e5bfc91a299027 (patch) | |
tree | b696e2420e8b7fdefb78fdd9ae62559d0be43c1b | |
parent | aa1f46885810c4779ae539c142fdee15b556206b (diff) |
Fix for #268.
- Move color stripping code to helperfuncs.
- Strip color codes before matching filters.
-rw-r--r-- | docs/conf/inspircd.filter.example | 1 | ||||
-rw-r--r-- | include/inspircd.h | 5 | ||||
-rw-r--r-- | src/helperfuncs.cpp | 27 | ||||
-rw-r--r-- | src/modules/m_filter.cpp | 24 | ||||
-rw-r--r-- | src/modules/m_stripcolor.cpp | 29 |
5 files changed, 54 insertions, 32 deletions
diff --git a/docs/conf/inspircd.filter.example b/docs/conf/inspircd.filter.example index 8200a028f..922f55fb6 100644 --- a/docs/conf/inspircd.filter.example +++ b/docs/conf/inspircd.filter.example @@ -37,6 +37,7 @@ # P: Block part messages # q: Block quit messages # o: Don't match against opers +# c: Strip color codes from text before trying to match # *: Represents all of the above flags # -: Does nothing, a non-op for when you do not want to specify any flags # diff --git a/include/inspircd.h b/include/inspircd.h index 69c8bf47f..cb4f1558f 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -705,6 +705,11 @@ class CoreExport InspIRCd * @return True i the mask is valid */ bool IsValidMask(const std::string &mask); + + /** Strips all color codes from the given string + * @para sentence The string to strip from + */ + static void StripColor(std::string &sentence); /** Rehash the local server */ diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index a6df520c5..3efa58bba 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -196,6 +196,33 @@ bool InspIRCd::IsValidMask(const std::string &mask) return true; } +void InspIRCd::StripColor(std::string &sentence) +{ + /* refactor this completely due to SQUIT bug since the old code would strip last char and replace with \0 --peavey */ + int seq = 0; + + for (std::string::iterator i = sentence.begin(); i != sentence.end();) + { + if (*i == 3) + seq = 1; + else if (seq && (( ((*i >= '0') && (*i <= '9')) || (*i == ',') ) )) + { + seq++; + if ( (seq <= 4) && (*i == ',') ) + seq = 1; + else if (seq > 3) + seq = 0; + } + else + seq = 0; + + if (seq || ((*i == 2) || (*i == 15) || (*i == 22) || (*i == 21) || (*i == 31))) + i = sentence.erase(i); + else + ++i; + } +} + /* true for valid channel name, false else */ bool IsChannelHandler::Call(const char *chname, size_t max) { diff --git a/src/modules/m_filter.cpp b/src/modules/m_filter.cpp index 9e06ca494..89a6bb2ef 100644 --- a/src/modules/m_filter.cpp +++ b/src/modules/m_filter.cpp @@ -50,6 +50,7 @@ class FilterResult bool flag_quit_message; bool flag_privmsg; bool flag_notice; + bool flag_strip_color; FilterResult(const std::string free, const std::string &rea, const std::string &act, long gt, const std::string &fla) : freeform(free), reason(rea), action(act), gline_time(gt), flags(fla) @@ -60,7 +61,8 @@ class FilterResult int FillFlags(const std::string &fl) { flags = fl; - flag_no_opers = flag_part_message = flag_quit_message = flag_privmsg = flag_notice = false; + flag_no_opers = flag_part_message = flag_quit_message = flag_privmsg = + flag_notice = flag_strip_color = false; size_t x = 0; for (std::string::const_iterator n = flags.begin(); n != flags.end(); ++n, ++x) @@ -82,9 +84,12 @@ class FilterResult case 'n': flag_notice = true; break; + case 'c': + flag_strip_color = true; + break; case '*': flag_no_opers = flag_part_message = flag_quit_message = - flag_privmsg = flag_notice = true; + flag_privmsg = flag_notice = flag_strip_color = true; break; default: return x; @@ -539,14 +544,25 @@ ImplFilter::ImplFilter(ModuleFilter* mymodule, const std::string &rea, const std FilterResult* ModuleFilter::FilterMatch(User* user, const std::string &text, int flgs) { + static std::string stripped_text; + stripped_text.clear(); + for (std::vector<ImplFilter>::iterator index = filters.begin(); index != filters.end(); index++) { + FilterResult* filter = dynamic_cast<FilterResult*>(&(*index)); + /* Skip ones that dont apply to us */ - if (!AppliesToMe(user, dynamic_cast<FilterResult*>(&(*index)), flgs)) + if (!AppliesToMe(user, filter, flgs)) continue; + if ((filter->flag_strip_color) && (stripped_text.empty())) + { + stripped_text = text; + InspIRCd::StripColor(stripped_text); + } + //ServerInstance->Logs->Log("m_filter", DEBUG, "Match '%s' against '%s'", text.c_str(), index->freeform.c_str()); - if (index->regex->Matches(text)) + if (index->regex->Matches(filter->flag_strip_color ? stripped_text : text)) { //ServerInstance->Logs->Log("m_filter", DEBUG, "MATCH"); ImplFilter fr = *index; diff --git a/src/modules/m_stripcolor.cpp b/src/modules/m_stripcolor.cpp index 86f307bae..b1cc54580 100644 --- a/src/modules/m_stripcolor.cpp +++ b/src/modules/m_stripcolor.cpp @@ -68,33 +68,6 @@ class ModuleStripColor : public Module ServerInstance->AddExtBanChar('S'); } - virtual void ReplaceLine(std::string &sentence) - { - /* refactor this completely due to SQUIT bug since the old code would strip last char and replace with \0 --peavey */ - int seq = 0; - - for (std::string::iterator i = sentence.begin(); i != sentence.end();) - { - if (*i == 3) - seq = 1; - else if (seq && (( ((*i >= '0') && (*i <= '9')) || (*i == ',') ) )) - { - seq++; - if ( (seq <= 4) && (*i == ',') ) - seq = 1; - else if (seq > 3) - seq = 0; - } - else - seq = 0; - - if (seq || ((*i == 2) || (*i == 15) || (*i == 22) || (*i == 21) || (*i == 31))) - i = sentence.erase(i); - else - ++i; - } - } - virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list) { if (!IS_LOCAL(user)) @@ -119,7 +92,7 @@ class ModuleStripColor : public Module if (active) { - this->ReplaceLine(text); + InspIRCd::StripColor(text); } return MOD_RES_PASSTHRU; |