]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Match with the right args at least..
[user/henk/code/inspircd.git] / src / wildcard.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include "hashcomp.h"
18 #include "inspstring.h"
19
20 #include <iostream>
21 /*
22  * Wildcard matching, the third (and probably final) iteration!
23  *
24  */
25 static bool match_internal(const unsigned char *mask, const unsigned char *str, unsigned const char *map)
26 {
27         const unsigned char *wild = str;
28         const unsigned char *string = mask;
29         const unsigned char *cp = NULL;
30         const unsigned char *mp = NULL;
31
32         if (!map)
33                 map = lowermap; // default to case insensitive search
34
35         while ((*string) && (*wild != '*'))
36         {
37                 if (map[*wild] != map[*string] && (*wild != '?'))
38                 {
39                         return false;
40                 }
41
42                 ++wild;
43                 ++string;
44         }
45
46         while (*string)
47         {
48                 if (*wild == '*')
49                 {
50                         if (!*++wild)
51                         {
52                                 return true;
53                         }
54
55                         mp = wild;
56                         cp = string+1;
57                 }
58
59                 // if mapped char == mapped wild AND wild is NOT ?
60                 else if (map[*wild] == map[*string] && (*wild == '?'))
61                 {
62                         ++wild;
63                         ++string;
64                 }
65                 else
66                 {
67                         wild = mp;
68                         string = cp++;
69                 }
70         }
71
72         while (*wild == '*')
73         {
74                 wild++;
75         }
76
77         if (*wild == 0)
78                 std::cout << "*wild == 0\n";
79         else
80                 std::cout << "*wild != 0\n";
81         return (*wild == 0);
82 }
83
84 CoreExport bool InspIRCd::Match(const std::string &str, const std::string &mask, unsigned const char *map)
85 {
86         return match_internal((const unsigned char *)str.c_str(), (const unsigned char *)mask.c_str(), map);
87 }
88
89 CoreExport bool InspIRCd::Match(const  char *str, const char *mask, unsigned const char *map)
90 {
91         return match_internal((const unsigned char *)str, (const unsigned char *)mask, map);
92 }
93
94
95 CoreExport bool InspIRCd::MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map)
96 {
97         if (irc::sockets::MatchCIDR(str, mask, true))
98                 return true;
99
100         // Fall back to regular match
101         return InspIRCd::Match(str, mask, NULL);
102 }
103
104 CoreExport bool InspIRCd::MatchCIDR(const  char *str, const char *mask, unsigned const char *map)
105 {
106         if (irc::sockets::MatchCIDR(str, mask, true))
107                 return true;
108
109         // Fall back to regular match
110         return InspIRCd::Match(str, mask, NULL);
111 }
112