]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/wildcard.cpp
Braunvieh!
[user/henk/code/inspircd.git] / src / wildcard.cpp
index 41070d14506c8f30fdae2f48902a09406760e4c7..63a28b8cb797fe2e25bb9e4d8e94847c530752ec 100644 (file)
-#include <string>
-#include "inspircd_config.h"
+/*       +------------------------------------+
+ *       | Inspire Internet Relay Chat Daemon |
+ *       +------------------------------------+
+ *
+ *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
+ * This program is free but copyrighted software; see
+ *            the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
+/* $Core */
+
 #include "inspircd.h"
+#include "hashcomp.h"
+#include "inspstring.h"
 
-void Delete(char* str,int pos)
+static bool match_internal(const unsigned char *str, const unsigned char *mask, unsigned const char *map)
 {
-       char moo[MAXBUF];
-       strcpy(moo,str);
-       moo[pos] = '\0';
-       strcpy(str,moo);
-       strcat(str,moo+pos+1);
-}
+        unsigned char *cp = NULL, *mp = NULL;
+        unsigned char* string = (unsigned char*)str;
+        unsigned char* wild = (unsigned char*)mask;
 
-void Insert(char* substr,char* str,int pos)
-{
-       std::string a = str;
-       a.insert(pos,substr);
-       strcpy(str,a.c_str());
+       if (!map)
+               map = rfc_case_insensitive_map;
+
+        while ((*string) && (*wild != '*'))
+        {
+                if ((map[*wild] != map[*string]) && (*wild != '?'))
+                {
+                        return 0;
+                }
+                wild++;
+                string++;
+        }
+
+        while (*string)
+        {
+                if (*wild == '*')
+                {
+                        if (!*++wild)
+                        {
+                                return 1;
+                        }
+                        mp = wild;
+                        cp = string+1;
+                }
+                else
+                if ((map[*wild] == map[*string]) || (*wild == '?'))
+                {
+                        wild++;
+                        string++;
+                }
+                else
+                {
+                        wild = mp;
+                        string = cp++;
+                }
+
+        }
+
+        while (*wild == '*')
+        {
+                wild++;
+        }
+
+        return !*wild;
 }
 
+/********************************************************************
+ * Below here is all wrappers around match_internal
+ ********************************************************************/
 
-int MWC = 0;
+CoreExport bool InspIRCd::Match(const std::string &str, const std::string &mask, unsigned const char *map)
+{
+       return match_internal((const unsigned char *)str.c_str(), (const unsigned char *)mask.c_str(), map);
+}
 
-bool match2(char* literal,char* mask)
+CoreExport bool InspIRCd::Match(const  char *str, const char *mask, unsigned const char *map)
 {
+       return match_internal((const unsigned char *)str, (const unsigned char *)mask, map);
+}
 
-char OldM[MAXBUF];
-int I,I2;
-
-if (MWC)
-       return true;
-
-if ((strstr(mask,"*")==0) && (strlen(literal) != strlen(mask)))
-       return 0;
- I=0;
- I2=0;
- while (I < strlen(mask))
- {
-   if (I2 >= strlen(literal))
-          return 0;
-   if ((mask[I]=='*') && (MWC==0))
-   {
-     strcpy(OldM,mask);
-     
-     Delete(mask,I);
-     
-     while (strlen(mask)<255)
-     {
-       match2(literal,mask);
-       if (MWC==2)
-              return 1;
-
-       Insert("?",mask,I);
-     }
-     strcpy(mask,OldM);
-     Delete(mask,I);
-     Insert("?",mask,I);
-   }
-   if (mask[I]=='?')
-   {
-     I++;
-     I2++;
-     continue;
-   }
-   if (mask[I] != literal[I2])
-          return 0;
-   if (MWC)
-          return 1;
-   I++;
-   I2++;
- }
- if (strlen(literal)==strlen(mask))
-                MWC=2;
+CoreExport bool InspIRCd::MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map)
+{
+       if (irc::sockets::MatchCIDR(str, mask, true))
+               return true;
 
+       // Fall back to regular match
+       return InspIRCd::Match(str, mask, NULL);
 }
 
-bool match(char* literal, char* mask)
+CoreExport bool InspIRCd::MatchCIDR(const  char *str, const char *mask, unsigned const char *map)
 {
-       char L[10240];
-       char M[10240];
-       MWC = 0;
-       strncpy(L,literal,10240);
-       strncpy(M,mask,10240);
-       strlower(L);
-       strlower(M);
-       match2(L,M);
-       return (MWC == 2);
+       if (irc::sockets::MatchCIDR(str, mask, true))
+               return true;
+
+       // Fall back to regular match
+       return InspIRCd::Match(str, mask, NULL);
 }