2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2011 Adam <Adam@anope.org>
5 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6 * Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
7 * Copyright (C) 2005-2009 Craig Edwards <craigedwards@brainbox.cc>
8 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
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/>.
35 /*******************************************************
36 * This file contains classes and templates that deal
37 * with the comparison and hashing of 'irc strings'.
38 * An 'irc string' is a string which compares in a
39 * case insensitive manner, and as per RFC 1459 will
40 * treat [ identical to {, ] identical to }, and \
43 * Our hashing functions are designed to accept
44 * std::string and compare/hash them as type irc::string
45 * by converting them internally. This makes them
46 * backwards compatible with other code which is not
47 * aware of irc::string.
48 *******************************************************/
50 /** Seperate from the other casemap tables so that code *can* still exclusively rely on RFC casemapping
53 * This is provided as a pointer so that modules can change it to their custom mapping tables,
54 * e.g. for national character support.
56 CoreExport extern unsigned const char *national_case_insensitive_map;
58 /** A mapping of uppercase to lowercase, including scandinavian
59 * 'oddities' as specified by RFC1459, e.g. { -> [, and | -> \
61 CoreExport extern unsigned const char rfc_case_insensitive_map[256];
63 /** Case insensitive map, ASCII rules.
67 CoreExport extern unsigned const char ascii_case_insensitive_map[256];
69 /** Case sensitive (identity) map.
71 CoreExport extern unsigned const char rfc_case_sensitive_map[256];
73 /** The irc namespace contains a number of helper classes.
78 /** Check if two IRC object (e.g. nick or channel) names are equal.
79 * This function uses national_case_insensitive_map to determine equality, which, by default does comparison
80 * according to RFC 1459, treating certain otherwise non-identical characters as identical.
81 * @param s1 First string to compare
82 * @param s2 Second string to compare
83 * @return True if the two names are equal, false otherwise
85 CoreExport bool equals(const std::string& s1, const std::string& s2);
87 /** This class returns true if two strings match.
88 * Case sensitivity is ignored, and the RFC 'character set'
93 /** The operator () does the actual comparison in hash_map
95 bool operator()(const std::string& s1, const std::string& s2) const
97 return equals(s1, s2);
103 size_t CoreExport operator()(const std::string &s) const;
106 struct insensitive_swo
108 bool CoreExport operator()(const std::string& a, const std::string& b) const;
111 /** The irc_char_traits class is used for RFC-style comparison of strings.
112 * This class is used to implement irc::string, a case-insensitive, RFC-
113 * comparing string class.
115 struct CoreExport irc_char_traits : public std::char_traits<char>
117 /** Check if two chars match.
118 * @param c1st First character
119 * @param c2nd Second character
120 * @return true if the characters are equal
122 static bool eq(char c1st, char c2nd);
124 /** Check if two chars do NOT match.
125 * @param c1st First character
126 * @param c2nd Second character
127 * @return true if the characters are unequal
129 static bool ne(char c1st, char c2nd);
131 /** Check if one char is less than another.
132 * @param c1st First character
133 * @param c2nd Second character
134 * @return true if c1st is less than c2nd
136 static bool lt(char c1st, char c2nd);
138 /** Compare two strings of size n.
139 * @param str1 First string
140 * @param str2 Second string
141 * @param n Length to compare to
142 * @return similar to strcmp, zero for equal, less than zero for str1
143 * being less and greater than zero for str1 being greater than str2.
145 static int compare(const char* str1, const char* str2, size_t n);
147 /** Find a char within a string up to position n.
148 * @param s1 String to find in
149 * @param n Position to search up to
150 * @param c Character to search for
151 * @return Pointer to the first occurance of c in s1
153 static const char* find(const char* s1, int n, char c);
156 /** This typedef declares irc::string based upon irc_char_traits.
158 typedef std::basic_string<char, irc_char_traits, std::allocator<char> > string;
160 /** Joins the contents of a vector to a string.
161 * @param sequence Zero or more items to join.
162 * @separator The character to place between the items.
164 std::string CoreExport stringjoiner(const std::vector<std::string>& sequence, char separator = ' ');
166 /** irc::sepstream allows for splitting token seperated lists.
167 * Each successive call to sepstream::GetToken() returns
168 * the next token, until none remain, at which point the method returns
171 class CoreExport sepstream
180 /** Current string position
183 /** If set then GetToken() can return an empty string
187 /** Create a sepstream and fill it with the provided data
189 sepstream(const std::string &source, char separator, bool allowempty = false);
191 /** Fetch the next token from the stream
192 * @param token The next token from the stream is placed here
193 * @return True if tokens still remain, false if there are none left
195 bool GetToken(std::string& token);
197 /** Fetch the entire remaining stream, without tokenizing
198 * @return The remaining part of the stream
200 const std::string GetRemaining();
202 /** Returns true if the end of the stream has been reached
203 * @return True if the end of the stream has been reached, otherwise false
208 /** A derived form of sepstream, which seperates on commas
210 class CoreExport commasepstream : public sepstream
213 /** Initialize with comma separator
215 commasepstream(const std::string &source, bool allowempty = false) : sepstream(source, ',', allowempty)
220 /** A derived form of sepstream, which seperates on spaces
222 class CoreExport spacesepstream : public sepstream
225 /** Initialize with space separator
227 spacesepstream(const std::string &source, bool allowempty = false) : sepstream(source, ' ', allowempty)
232 /** irc::tokenstream reads a string formatted as per RFC1459 and RFC2812.
233 * It will split the string into 'tokens' each containing one parameter
235 * For instance, if it is instantiated with the string:
236 * "PRIVMSG #test :foo bar baz qux"
237 * then each successive call to tokenstream::GetToken() will return
238 * "PRIVMSG", "#test", "foo bar baz qux", "".
239 * Note that if the whole string starts with a colon this is not taken
240 * to mean the string is all one parameter, and the first item in the
241 * list will be ":item". This is to allow for parsing 'source' fields
244 class CoreExport tokenstream : private spacesepstream
247 /** Create a tokenstream and fill it with the provided data
249 tokenstream(const std::string &source);
251 /** Fetch the next token from the stream as a std::string
252 * @param token The next token available, or an empty string if none remain
253 * @return True if tokens are left to be read, false if the last token was just retrieved.
255 bool GetToken(std::string &token);
257 /** Fetch the next token from the stream as an integer
258 * @param token The next token available, or undefined if none remain
259 * @return True if tokens are left to be read, false if the last token was just retrieved.
261 bool GetToken(int &token);
263 /** Fetch the next token from the stream as a long integer
264 * @param token The next token available, or undefined if none remain
265 * @return True if tokens are left to be read, false if the last token was just retrieved.
267 bool GetToken(long &token);
270 /** The portparser class seperates out a port range into integers.
271 * A port range may be specified in the input string in the form
272 * "6660,6661,6662-6669,7020". The end of the stream is indicated by
273 * a return value of 0 from portparser::GetToken(). If you attempt
274 * to specify an illegal range (e.g. one where start >= end, or
275 * start or end < 0) then GetToken() will return the first element
276 * of the pair of numbers.
278 class CoreExport portparser
282 /** Used to split on commas
286 /** Current position in a range of ports
290 /** Starting port in a range of ports
294 /** Ending port in a range of ports
298 /** Allow overlapped port ranges
302 /** Used to determine overlapping of ports
303 * without O(n) algorithm being used
305 std::set<long> overlap_set;
307 /** Returns true if val overlaps an existing range
309 bool Overlaps(long val);
312 /** Create a portparser and fill it with the provided data
313 * @param source The source text to parse from
314 * @param allow_overlapped Allow overlapped ranges
316 portparser(const std::string &source, bool allow_overlapped = true);
318 /** Fetch the next token from the stream
319 * @return The next port number is returned, or 0 if none remain