]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/hashcomp.h
Add an oper only parameter to Simple{Channel,User}ModeHandler.
[user/henk/code/inspircd.git] / include / hashcomp.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
10  *
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.
14  *
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
18  * details.
19  *
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/>.
22  */
23
24
25 #pragma once
26
27 #include <cstring>
28 #include <string>
29 #include <vector>
30 #include <deque>
31 #include <map>
32 #include <set>
33 #include "inspircd.h"
34 #include "convto.h"
35
36 /*******************************************************
37  * This file contains classes and templates that deal
38  * with the comparison and hashing of 'irc strings'.
39  * An 'irc string' is a string which compares in a
40  * case insensitive manner, and as per RFC 1459 will
41  * treat [ identical to {, ] identical to }, and \
42  * as identical to |.
43  *
44  * There are functors that accept std::string and
45  * compare/hash them as type irc::string by using
46  * mapping arrays internally.
47  *******************************************************/
48
49 /** Seperate from the other casemap tables so that code *can* still exclusively rely on RFC casemapping
50  * if it must.
51  *
52  * This is provided as a pointer so that modules can change it to their custom mapping tables,
53  * e.g. for national character support.
54  */
55 CoreExport extern unsigned const char *national_case_insensitive_map;
56
57 /** A mapping of uppercase to lowercase, including scandinavian
58  * 'oddities' as specified by RFC1459, e.g. { -> [, and | -> \
59  */
60 CoreExport extern unsigned const char rfc_case_insensitive_map[256];
61
62 /** Case insensitive map, ASCII rules.
63  * That is;
64  * [ != {, but A == a.
65  */
66 CoreExport extern unsigned const char ascii_case_insensitive_map[256];
67
68 /** The irc namespace contains a number of helper classes.
69  */
70 namespace irc
71 {
72         /** Check if two IRC object (e.g. nick or channel) names are equal.
73          * This function uses national_case_insensitive_map to determine equality, which, by default does comparison
74          * according to RFC 1459, treating certain otherwise non-identical characters as identical.
75          * @param s1 First string to compare
76          * @param s2 Second string to compare
77          * @return True if the two names are equal, false otherwise
78          */
79         CoreExport bool equals(const std::string& s1, const std::string& s2);
80
81         /** Check whether \p needle exists within \p haystack.
82          * @param haystack The string to search within.
83          * @param needle The string to search for.
84          * @return Either the index at which \p needle was found or std::string::npos.
85          */
86         CoreExport size_t find(const std::string& haystack, const std::string& needle);
87
88         /** This class returns true if two strings match.
89          * Case sensitivity is ignored, and the RFC 'character set'
90          * is adhered to
91          */
92         struct StrHashComp
93         {
94                 /** The operator () does the actual comparison in hash_map
95                  */
96                 bool operator()(const std::string& s1, const std::string& s2) const
97                 {
98                         return equals(s1, s2);
99                 }
100         };
101
102         struct insensitive
103         {
104                 size_t CoreExport operator()(const std::string &s) const;
105         };
106
107         struct insensitive_swo
108         {
109                 bool CoreExport operator()(const std::string& a, const std::string& b) const;
110         };
111
112         /** irc::sepstream allows for splitting token seperated lists.
113          * Each successive call to sepstream::GetToken() returns
114          * the next token, until none remain, at which point the method returns
115          * false.
116          */
117         class CoreExport sepstream
118         {
119          protected:
120                 /** Original string.
121                  */
122                 std::string tokens;
123                 /** Separator value
124                  */
125                 char sep;
126                 /** Current string position
127                  */
128                 size_t pos;
129                 /** If set then GetToken() can return an empty string
130                  */
131                 bool allow_empty;
132          public:
133                 /** Create a sepstream and fill it with the provided data
134                  */
135                 sepstream(const std::string &source, char separator, bool allowempty = false);
136
137                 /** Fetch the next token from the stream
138                  * @param token The next token from the stream is placed here
139                  * @return True if tokens still remain, false if there are none left
140                  */
141                 bool GetToken(std::string& token);
142
143                 /** Fetch the next numeric token from the stream
144                  * @param token The next token from the stream is placed here
145                  * @return True if tokens still remain, false if there are none left
146                  */
147                 template<typename Numeric>
148                 bool GetNumericToken(Numeric& token)
149                 {
150                         std::string str;
151                         if (!GetToken(str))
152                                 return false;
153
154                         token = ConvToNum<Numeric>(str);
155                         return true;
156                 }
157
158                 /** Fetch the entire remaining stream, without tokenizing
159                  * @return The remaining part of the stream
160                  */
161                 const std::string GetRemaining();
162
163                 /** Returns true if the end of the stream has been reached
164                  * @return True if the end of the stream has been reached, otherwise false
165                  */
166                 bool StreamEnd();
167
168                 /** Returns true if the specified value exists in the stream
169                  * @param value The value to search for
170                  * @return True if the value was found, False otherwise
171                  */
172                 bool Contains(const std::string& value);
173         };
174
175         /** A derived form of sepstream, which seperates on commas
176          */
177         class CoreExport commasepstream : public sepstream
178         {
179          public:
180                 /** Initialize with comma separator
181                  */
182                 commasepstream(const std::string &source, bool allowempty = false) : sepstream(source, ',', allowempty)
183                 {
184                 }
185         };
186
187         /** A derived form of sepstream, which seperates on spaces
188          */
189         class CoreExport spacesepstream : public sepstream
190         {
191          public:
192                 /** Initialize with space separator
193                  */
194                 spacesepstream(const std::string &source, bool allowempty = false) : sepstream(source, ' ', allowempty)
195                 {
196                 }
197         };
198
199         /** irc::tokenstream reads a string formatted as per RFC1459 and RFC2812.
200          * It will split the string into 'tokens' each containing one parameter
201          * from the string.
202          * For instance, if it is instantiated with the string:
203          * "PRIVMSG #test :foo bar baz qux"
204          * then each successive call to tokenstream::GetToken() will return
205          * "PRIVMSG", "#test", "foo bar baz qux", "".
206          * Note that if the whole string starts with a colon this is not taken
207          * to mean the string is all one parameter, and the first item in the
208          * list will be ":item". This is to allow for parsing 'source' fields
209          * from data.
210          */
211         class CoreExport tokenstream
212         {
213         private:
214                 /** The message we are parsing tokens from. */
215                 std::string message;
216
217                 /** The current position within the message. */
218                 size_t position;
219
220          public:
221                 /** Create a tokenstream and fill it with the provided data. */
222                 tokenstream(const std::string& msg, size_t start = 0, size_t end = std::string::npos);
223
224                 /** Retrieves the underlying message. */
225                 std::string& GetMessage() { return message; }
226
227                 /** Retrieve the next \<middle> token in the token stream.
228                  * @param token The next token available, or an empty string if none remain.
229                  * @return True if tokens are left to be read, false if the last token was just retrieved.
230                  */
231                 bool GetMiddle(std::string& token);
232
233                 /** Retrieve the next \<trailing> token in the token stream.
234                  * @param token The next token available, or an empty string if none remain.
235                  * @return True if tokens are left to be read, false if the last token was just retrieved.
236                  */
237                 bool GetTrailing(std::string& token);
238         };
239
240         /** The portparser class seperates out a port range into integers.
241          * A port range may be specified in the input string in the form
242          * "6660,6661,6662-6669,7020". The end of the stream is indicated by
243          * a return value of 0 from portparser::GetToken(). If you attempt
244          * to specify an illegal range (e.g. one where start >= end, or
245          * start or end < 0) then GetToken() will return the first element
246          * of the pair of numbers.
247          */
248         class CoreExport portparser
249         {
250          private:
251
252                 /** Used to split on commas
253                  */
254                 commasepstream sep;
255
256                 /** Current position in a range of ports
257                  */
258                 long in_range;
259
260                 /** Starting port in a range of ports
261                  */
262                 long range_begin;
263
264                 /** Ending port in a range of ports
265                  */
266                 long range_end;
267
268                 /** Allow overlapped port ranges
269                  */
270                 bool overlapped;
271
272                 /** Used to determine overlapping of ports
273                  * without O(n) algorithm being used
274                  */
275                 std::set<long> overlap_set;
276
277                 /** Returns true if val overlaps an existing range
278                  */
279                 bool Overlaps(long val);
280          public:
281
282                 /** Create a portparser and fill it with the provided data
283                  * @param source The source text to parse from
284                  * @param allow_overlapped Allow overlapped ranges
285                  */
286                 portparser(const std::string &source, bool allow_overlapped = true);
287
288                 /** Fetch the next token from the stream
289                  * @return The next port number is returned, or 0 if none remain
290                  */
291                 long GetToken();
292         };
293 }