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