2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2017 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
6 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
7 * Copyright (C) 2008, 2010 Craig Edwards <brain@inspircd.org>
8 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9 * Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
11 * This file is part of InspIRCd. InspIRCd is free software: you can
12 * redistribute it and/or modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation, version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 static bool MatchInternal(const unsigned char* str, const unsigned char* mask, unsigned const char* map)
29 unsigned char* cp = NULL;
30 unsigned char* mp = NULL;
31 unsigned char* string = (unsigned char*)str;
32 unsigned char* wild = (unsigned char*)mask;
34 while ((*string) && (*wild != '*'))
36 if ((map[*wild] != map[*string]) && (*wild != '?'))
56 if ((map[*wild] == map[*string]) || (*wild == '?'))
77 // Below here is all wrappers around MatchInternal
79 bool InspIRCd::Match(const std::string& str, const std::string& mask, unsigned const char* map)
82 map = national_case_insensitive_map;
84 return MatchInternal((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;
92 return MatchInternal((const unsigned char*)str, (const unsigned char*)mask, map);
95 bool InspIRCd::MatchCIDR(const std::string& str, const std::string& mask, unsigned const char* map)
97 if (irc::sockets::MatchCIDR(str, mask, true))
100 // Fall back to regular match
101 return InspIRCd::Match(str, mask, map);
104 bool InspIRCd::MatchCIDR(const char* str, const char* mask, unsigned const char* map)
106 if (irc::sockets::MatchCIDR(str, mask, true))
109 // Fall back to regular match
110 return InspIRCd::Match(str, mask, map);
113 bool InspIRCd::MatchMask(const std::string& masks, const std::string& hostname, const std::string& ipaddr)
115 irc::spacesepstream masklist(masks);
117 while (masklist.GetToken(mask))
119 if (InspIRCd::Match(hostname, mask, ascii_case_insensitive_map) ||
120 InspIRCd::MatchCIDR(ipaddr, mask, ascii_case_insensitive_map))