2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
24 #include "inspstring.h"
26 static bool match_internal(const unsigned char *str, const unsigned char *mask, unsigned const char *map)
28 unsigned char *cp = NULL, *mp = NULL;
29 unsigned char* string = (unsigned char*)str;
30 unsigned char* wild = (unsigned char*)mask;
32 while ((*string) && (*wild != '*'))
34 if ((map[*wild] != map[*string]) && (*wild != '?'))
54 if ((map[*wild] == map[*string]) || (*wild == '?'))
75 /********************************************************************
76 * Below here is all wrappers around match_internal
77 ********************************************************************/
79 bool InspIRCd::Match(const std::string &str, const std::string &mask, unsigned const char *map)
82 map = national_case_insensitive_map;
84 return match_internal((const unsigned char *)str.c_str(), (const unsigned char *)mask.c_str(), map);
87 bool InspIRCd::Match(const char *str, const char *mask, unsigned const char *map)
90 map = national_case_insensitive_map;
91 return match_internal((const unsigned char *)str, (const unsigned char *)mask, map);
94 bool InspIRCd::MatchCIDR(const std::string &str, const std::string &mask, unsigned const char *map)
96 if (irc::sockets::MatchCIDR(str, mask, true))
100 map = national_case_insensitive_map;
102 // Fall back to regular match
103 return InspIRCd::Match(str, mask, map);
106 bool InspIRCd::MatchCIDR(const char *str, const char *mask, unsigned const char *map)
108 if (irc::sockets::MatchCIDR(str, mask, true))
112 map = national_case_insensitive_map;
114 // Fall back to regular match
115 return InspIRCd::Match(str, mask, map);
118 bool InspIRCd::MatchMask(const std::string& masks, const std::string& hostname, const std::string& ipaddr)
120 std::stringstream masklist(masks);
122 while (masklist >> mask)
124 if (InspIRCd::Match(hostname, mask, ascii_case_insensitive_map) ||
125 InspIRCd::MatchCIDR(ipaddr, mask, ascii_case_insensitive_map))