]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Fix for #268.
authorPeter Powell <petpow@saberuk.com>
Wed, 12 Sep 2012 15:27:59 +0000 (16:27 +0100)
committerattilamolnar <attilamolnar@hush.com>
Fri, 19 Oct 2012 15:50:08 +0000 (17:50 +0200)
- Move color stripping code to helperfuncs.
- Strip color codes before matching filters.

docs/conf/inspircd.filter.example
include/inspircd.h
src/helperfuncs.cpp
src/modules/m_filter.cpp
src/modules/m_stripcolor.cpp

index 8200a028f0cfa27e594640cbf56f1d71a01cc371..922f55fb699ffc6704213fa6607528571164c724 100644 (file)
@@ -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 
 #
index 69c8bf47f63497015c0c41eb868ee7a9cc0b9c3b..cb4f1558fe051b5a68f79729e07f719b5e777fe4 100644 (file)
@@ -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
         */
index a6df520c5d2d10fddb7a3306fd0ad35e9052d27e..3efa58bba44150c111d2e109ab5f14d6cb6f39e6 100644 (file)
@@ -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)
 {
index 9e06ca494d9c8fa5ccc9a61c089c2d444bad02dc..89a6bb2ef529f744c7285524da72d1442435435f 100644 (file)
@@ -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;
index 86f307bae2ed4e494bccb87555cf75ef3f2f6e1e..b1cc54580560e34fc86533cb43a48c7031d74d61 100644 (file)
@@ -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;