]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Remove $Core and $Mod* comments apart from $ModDep.
[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 #include "inspircd.h"
23 #include "hashcomp.h"
24 #include "inspstring.h"
25
26 static bool match_internal(const unsigned char *str, const unsigned char *mask, unsigned const char *map)
27 {
28         unsigned char *cp = NULL, *mp = NULL;
29         unsigned char* string = (unsigned char*)str;
30         unsigned char* wild = (unsigned char*)mask;
31
32         while ((*string) && (*wild != '*'))
33         {
34                 if ((map[*wild] != map[*string]) && (*wild != '?'))
35                 {
36                         return 0;
37                 }
38                 wild++;
39                 string++;
40         }
41
42         while (*string)
43         {
44                 if (*wild == '*')
45                 {
46                         if (!*++wild)
47                         {
48                                 return 1;
49                         }
50                         mp = wild;
51                         cp = string+1;
52                 }
53                 else
54                         if ((map[*wild] == map[*string]) || (*wild == '?'))
55                         {
56                                 wild++;
57                                 string++;
58                         }
59                         else
60                         {
61                                 wild = mp;
62                                 string = cp++;
63                         }
64
65         }
66
67         while (*wild == '*')
68         {
69                 wild++;
70         }
71
72         return !*wild;
73 }
74
75 /********************************************************************
76  * Below here is all wrappers around match_internal
77  ********************************************************************/
78
79 bool InspIRCd::Match(const std::string &str, const std::string &mask, unsigned const char *map)
80 {
81         if (!map)
82                 map = national_case_insensitive_map;
83
84         return match_internal((const unsigned char *)str.c_str(), (const unsigned char *)mask.c_str(), map);
85 }
86
87 bool InspIRCd::Match(const char *str, const char *mask, unsigned const char *map)
88 {
89         if (!map)
90                 map = national_case_insensitive_map;
91         return match_internal((const unsigned char *)str, (const unsigned char *)mask, map);
92 }
93
94 bool InspIRCd::MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map)
95 {
96         if (irc::sockets::MatchCIDR(str, mask, true))
97                 return true;
98
99         if (!map)
100                 map = national_case_insensitive_map;
101
102         // Fall back to regular match
103         return InspIRCd::Match(str, mask, map);
104 }
105
106 bool InspIRCd::MatchCIDR(const char *str, const char *mask, unsigned const char *map)
107 {
108         if (irc::sockets::MatchCIDR(str, mask, true))
109                 return true;
110
111         if (!map)
112                 map = national_case_insensitive_map;
113
114         // Fall back to regular match
115         return InspIRCd::Match(str, mask, map);
116 }
117
118 bool InspIRCd::MatchMask(const std::string& masks, const std::string& hostname, const std::string& ipaddr)
119 {
120         std::stringstream masklist(masks);
121         std::string mask;
122         while (masklist >> mask)
123         {
124                 if (InspIRCd::Match(hostname, mask, ascii_case_insensitive_map) || 
125                         InspIRCd::MatchCIDR(ipaddr, mask, ascii_case_insensitive_map))
126                 {
127                         return true;
128                 }
129         }
130         return false;
131 }