]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Merge pull request #1018 from SaberUK/insp20+hidekills
[user/henk/code/inspircd.git] / src / wildcard.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
5  *   Copyright (C) 2003, 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 /* $Core */
23
24 #include "inspircd.h"
25 #include "hashcomp.h"
26 #include "inspstring.h"
27
28 static bool match_internal(const unsigned char *str, const unsigned char *mask, unsigned const char *map)
29 {
30         unsigned char *cp = NULL, *mp = NULL;
31         unsigned char* string = (unsigned char*)str;
32         unsigned char* wild = (unsigned char*)mask;
33
34         while ((*string) && (*wild != '*'))
35         {
36                 if ((map[*wild] != map[*string]) && (*wild != '?'))
37                 {
38                         return 0;
39                 }
40                 wild++;
41                 string++;
42         }
43
44         while (*string)
45         {
46                 if (*wild == '*')
47                 {
48                         if (!*++wild)
49                         {
50                                 return 1;
51                         }
52                         mp = wild;
53                         cp = string+1;
54                 }
55                 else
56                         if ((map[*wild] == map[*string]) || (*wild == '?'))
57                         {
58                                 wild++;
59                                 string++;
60                         }
61                         else
62                         {
63                                 wild = mp;
64                                 string = cp++;
65                         }
66
67         }
68
69         while (*wild == '*')
70         {
71                 wild++;
72         }
73
74         return !*wild;
75 }
76
77 /********************************************************************
78  * Below here is all wrappers around match_internal
79  ********************************************************************/
80
81 CoreExport bool InspIRCd::Match(const std::string &str, const std::string &mask, unsigned const char *map)
82 {
83         if (!map)
84                 map = national_case_insensitive_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         if (!map)
92                 map = national_case_insensitive_map;
93         return match_internal((const unsigned char *)str, (const unsigned char *)mask, map);
94 }
95
96 CoreExport bool InspIRCd::MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map)
97 {
98         if (irc::sockets::MatchCIDR(str, mask, true))
99                 return true;
100
101         if (!map)
102                 map = national_case_insensitive_map;
103
104         // Fall back to regular match
105         return InspIRCd::Match(str, mask, map);
106 }
107
108 CoreExport bool InspIRCd::MatchCIDR(const  char *str, const char *mask, unsigned const char *map)
109 {
110         if (irc::sockets::MatchCIDR(str, mask, true))
111                 return true;
112
113         if (!map)
114                 map = national_case_insensitive_map;
115
116         // Fall back to regular match
117         return InspIRCd::Match(str, mask, map);
118 }
119