]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/wildcard.cpp
Patch from eLement notifying the user who sent a NICKLOCK that the target was locked...
[user/henk/code/inspircd.git] / src / wildcard.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 static bool match_internal(const unsigned char *str, const unsigned char *mask, unsigned const char *map)
21 {
22         unsigned char *cp = NULL, *mp = NULL;
23         unsigned char* string = (unsigned char*)str;
24         unsigned char* wild = (unsigned char*)mask;
25
26         while ((*string) && (*wild != '*'))
27         {
28                 if ((map[*wild] != map[*string]) && (*wild != '?'))
29                 {
30                         return 0;
31                 }
32                 wild++;
33                 string++;
34         }
35
36         while (*string)
37         {
38                 if (*wild == '*')
39                 {
40                         if (!*++wild)
41                         {
42                                 return 1;
43                         }
44                         mp = wild;
45                         cp = string+1;
46                 }
47                 else
48                         if ((map[*wild] == map[*string]) || (*wild == '?'))
49                         {
50                                 wild++;
51                                 string++;
52                         }
53                         else
54                         {
55                                 wild = mp;
56                                 string = cp++;
57                         }
58
59         }
60
61         while (*wild == '*')
62         {
63                 wild++;
64         }
65
66         return !*wild;
67 }
68
69 /********************************************************************
70  * Below here is all wrappers around match_internal
71  ********************************************************************/
72
73 CoreExport bool InspIRCd::Match(const std::string &str, const std::string &mask, unsigned const char *map)
74 {
75         if (!map)
76                 map = national_case_insensitive_map;
77
78         return match_internal((const unsigned char *)str.c_str(), (const unsigned char *)mask.c_str(), map);
79 }
80
81 CoreExport bool InspIRCd::Match(const  char *str, const char *mask, unsigned const char *map)
82 {
83         if (!map)
84                 map = national_case_insensitive_map;
85         return match_internal((const unsigned char *)str, (const unsigned char *)mask, map);
86 }
87
88 CoreExport bool InspIRCd::MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map)
89 {
90         if (irc::sockets::MatchCIDR(str, mask, true))
91                 return true;
92
93         if (!map)
94                 map = national_case_insensitive_map;
95
96         // Fall back to regular match
97         return InspIRCd::Match(str, mask, map);
98 }
99
100 CoreExport bool InspIRCd::MatchCIDR(const  char *str, const char *mask, unsigned const char *map)
101 {
102         if (irc::sockets::MatchCIDR(str, mask, true))
103                 return true;
104
105         if (!map)
106                 map = national_case_insensitive_map;
107
108         // Fall back to regular match
109         return InspIRCd::Match(str, mask, map);
110 }
111