]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nationalchars.cpp
Merge branch 'insp20' into master.
[user/henk/code/inspircd.git] / src / modules / m_nationalchars.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2009 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2009 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2009 Robin Burchell <robin+git@viroteck.net>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 /* Contains a code of Unreal IRCd + Bynets patch ( http://www.unrealircd.com/ and http://www.bynets.org/ )
24    Original patch is made by Dmitry "Killer{R}" Kononko. ( http://killprog.com/ )
25    Changed at 2008-06-15 - 2009-02-11
26    by Chernov-Phoenix Alexey (Phoenix@RusNet) mailto:phoenix /email address separator/ pravmail.ru */
27
28 #include "inspircd.h"
29 #include <fstream>
30
31 class lwbNickHandler
32 {
33  public:
34         static bool Call(const std::string&);
35 };
36
37                                                                  /*,m_reverse_additionalUp[256];*/
38 static unsigned char m_reverse_additional[256],m_additionalMB[256],m_additionalUtf8[256],m_additionalUtf8range[256],m_additionalUtf8interval[256];
39
40 char utf8checkrest(unsigned char * mb, unsigned char cnt)
41 {
42         for (unsigned char * tmp=mb; tmp<mb+cnt; tmp++)
43         {
44                 /* & is faster! -- Phoenix (char & b11000000 == b10000000) */
45                 if ((*tmp & 192) != 128)
46                         return -1;
47         }
48         return cnt + 1;
49 }
50
51
52 char utf8size(unsigned char * mb)
53 {
54         if (!*mb)
55                 return -1;
56         if (!(*mb & 128))
57                 return 1;
58         if ((*mb & 224) == 192)
59                 return utf8checkrest(mb + 1,1);
60         if ((*mb & 240) == 224)
61                 return utf8checkrest(mb + 1,2);
62         if ((*mb & 248) == 240)
63                 return utf8checkrest(mb + 1,3);
64         return -1;
65 }
66
67
68 /* Conditions added */
69 bool lwbNickHandler::Call(const std::string& nick)
70 {
71         if (nick.empty())
72                 return false;
73
74         const char* n = nick.c_str();
75         unsigned int p = 0;
76         for (const char* i = n; *i; i++, p++)
77         {
78                 /* 1. Multibyte encodings support:  */
79                 /* 1.1. 16bit char. areas, e.g. chinese:*/
80
81                 /* if current character is the last, we DO NOT check it against multibyte table */
82                 /* if there are mbtable ranges, use ONLY them. No 8bit at all */
83                 if (i[1] && m_additionalMB[0])
84                 {
85                         /* otherwise let's take a look at the current character and the following one */
86                         bool found = false;
87                         for(unsigned char * mb = m_additionalMB; (*mb) && (mb < m_additionalMB + sizeof(m_additionalMB)); mb += 4)
88                         {
89                                 if ( (i[0] >= mb[0]) && (i[0] <= mb[1]) && (i[1] >= mb[2]) && (i[1] <= mb[3]) )
90                                 {
91                                         /* multibyte range character found */
92                                         i++;
93                                         p++;
94                                         found = true;
95                                         break;
96                                 }
97                         }
98                         if (found)
99                                 /* next char! */
100                                 continue;
101                         else
102                                 /* there are ranges, but incorrect char (8bit?) given, sorry */
103                                 return false;
104                 }
105
106                 /* 2. 8bit character support */
107                 if (((*i >= 'A') && (*i <= '}')) || m_reverse_additional[(unsigned char)*i])
108                         /* "A"-"}" can occur anywhere in a nickname */
109                         continue;
110
111                 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
112                         /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
113                         continue;
114
115                 /* 3.1. Check against a simple UTF-8 characters enumeration */
116                 int cursize, cursize2, ncursize = utf8size((unsigned char *)i);
117                 /* do check only if current multibyte character is valid UTF-8 only */
118                 if (ncursize != -1)
119                 {
120                         bool found = false;
121                         for (unsigned char * mb = m_additionalUtf8; (utf8size(mb) != -1) && (mb < m_additionalUtf8 + sizeof(m_additionalUtf8)); mb += cursize)
122                         {
123                                 cursize = utf8size(mb);
124                                 /* Size differs? Pick the next! */
125                                 if (cursize != ncursize)
126                                         continue;
127
128                                 if (!strncmp(i, (char *)mb, cursize))
129                                 {
130                                         i += cursize - 1;
131                                         p += cursize - 1;
132                                         found = true;
133                                         break;
134                                 }
135                         }
136                         if (found)
137                                 continue;
138
139                         /* 3.2. Check against an UTF-8 ranges: <start character> and <length of the range>. */
140                         found = false;
141                         for (unsigned char * mb = m_additionalUtf8range; (utf8size(mb) != -1) && (mb < m_additionalUtf8range + sizeof(m_additionalUtf8range)); mb += cursize + 1)
142                         {
143                                 cursize = utf8size(mb);
144                                 /* Size differs (or lengthbyte is zero)? Pick the next! */
145                                 if ((cursize != ncursize) || (!mb[cursize]))
146                                         continue;
147
148                                 unsigned char uright[5] = {0,0,0,0,0}, range = mb[cursize] - 1;
149                                 strncpy((char* ) uright, (char *) mb, cursize);
150
151                                 for (int temp = cursize - 1; (temp >= 0) && range; --temp)
152                                 {
153                                         /* all but the first char are 64-based */
154                                         if (temp)
155                                         {
156                                                 char part64 = range & 63; /* i.e. % 64 */
157                                                 /* handle carrying over */
158                                                 if (uright[temp] + part64 - 1 > 191)
159                                                 {
160                                                         uright[temp] -= 64;
161                                                         range += 64;
162                                                 }
163                                                 uright[temp] += part64;
164                                                 range >>= 6; /* divide it on a 64 */
165                                         }
166                                         /* the first char of UTF-8 doesn't follow the rule */
167                                         else
168                                         {
169                                                 uright[temp] += range;
170                                         }
171                                 }
172
173                                 if ((strncmp(i, (char *) mb, cursize) >= 0) && (strncmp(i, (char *) uright, cursize) <= 0))
174                                 {
175                                         i += cursize - 1;
176                                         p += cursize - 1;
177                                         found = true;
178                                         break;
179                                 }
180                         }
181                         if (found)
182                                 continue;
183
184                         /* 3.3. Check against an UTF-8 intervals: <start character> and <end character>. */
185                         found = false;
186                         for (unsigned char * mb = m_additionalUtf8interval; (utf8size(mb) != -1) && (utf8size(mb+utf8size(mb)) != -1)
187                                 && (mb < m_additionalUtf8interval + sizeof(m_additionalUtf8interval)); mb += (cursize+cursize2) )
188                         {
189                                 cursize = utf8size(mb);
190                                 cursize2= utf8size(mb+cursize);
191
192                                 int minlen  = cursize  > ncursize ? ncursize : cursize;
193                                 int minlen2 = cursize2 > ncursize ? ncursize : cursize2;
194
195                                 unsigned char* uright = mb + cursize;
196
197                                 if ((strncmp(i, (char *) mb, minlen) >= 0) && (strncmp(i, (char *) uright, minlen2) <= 0))
198                                 {
199                                         i += cursize - 1;
200                                         p += cursize - 1;
201                                         found = true;
202                                         break;
203                                 }
204                         }
205                         if (found)
206                                 continue;
207                 }
208
209                 /* invalid character! abort */
210                 return false;
211         }
212
213         /* too long? or not -- pointer arithmetic rocks */
214         return (p < ServerInstance->Config->Limits.NickMax);
215 }
216
217
218 class ModuleNationalChars : public Module
219 {
220         std::string charset;
221         unsigned char m_additional[256], m_additionalUp[256], m_lower[256], m_upper[256];
222         TR1NS::function<bool(const std::string&)> rememberer;
223         bool forcequit;
224         const unsigned char * lowermap_rememberer;
225         unsigned char prev_map[256];
226
227         template <typename T>
228         void RehashHashmap(T& hashmap)
229         {
230                 T newhash(hashmap.bucket_count());
231                 for (typename T::const_iterator i = hashmap.begin(); i != hashmap.end(); ++i)
232                         newhash.insert(std::make_pair(i->first, i->second));
233                 hashmap.swap(newhash);
234         }
235
236         void CheckRehash()
237         {
238                 // See if anything changed
239                 if (!memcmp(prev_map, national_case_insensitive_map, sizeof(prev_map)))
240                         return;
241
242                 memcpy(prev_map, national_case_insensitive_map, sizeof(prev_map));
243
244                 RehashHashmap(ServerInstance->Users.clientlist);
245                 RehashHashmap(ServerInstance->Users.uuidlist);
246                 RehashHashmap(ServerInstance->chanlist);
247         }
248
249  public:
250         ModuleNationalChars()
251                 : rememberer(ServerInstance->IsNick), lowermap_rememberer(national_case_insensitive_map)
252         {
253                 memcpy(prev_map, national_case_insensitive_map, sizeof(prev_map));
254         }
255
256         void init() CXX11_OVERRIDE
257         {
258                 memcpy(m_lower, rfc_case_insensitive_map, 256);
259                 national_case_insensitive_map = m_lower;
260
261                 ServerInstance->IsNick = &lwbNickHandler::Call;
262         }
263
264         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
265         {
266                 ConfigTag* tag = ServerInstance->Config->ConfValue("nationalchars");
267                 charset = tag->getString("file");
268                 std::string casemapping = tag->getString("casemapping", FileSystem::GetFileName(charset));
269                 if (casemapping.find(' ') != std::string::npos)
270                         throw ModuleException("<nationalchars:casemapping> must not contain any spaces!");
271                 ServerInstance->Config->CaseMapping = casemapping;
272 #if defined _WIN32
273                 if (!FileSystem::StartsWithWindowsDriveLetter(charset))
274                         charset.insert(0, "./locales/");
275 #else
276                 if(charset[0] != '/')
277                         charset.insert(0, "../locales/");
278 #endif
279                 unsigned char * tables[8] = { m_additional, m_additionalMB, m_additionalUp, m_lower, m_upper, m_additionalUtf8, m_additionalUtf8range, m_additionalUtf8interval };
280                 if (!loadtables(charset, tables, 8, 5))
281                         throw ModuleException("The locale file failed to load. Check your log file for more information.");
282                 forcequit = tag->getBool("forcequit");
283                 CheckForceQuit("National character set changed");
284                 CheckRehash();
285         }
286
287         void CheckForceQuit(const char * message)
288         {
289                 if (!forcequit)
290                         return;
291
292                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
293                 for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); )
294                 {
295                         /* Fix by Brain: Dont quit UID users */
296                         // Quitting the user removes it from the list
297                         User* n = *iter;
298                         ++iter;
299                         if (!isdigit(n->nick[0]) && !ServerInstance->IsNick(n->nick))
300                                 ServerInstance->Users->QuitUser(n, message);
301                 }
302         }
303
304         ~ModuleNationalChars()
305         {
306                 ServerInstance->IsNick = rememberer;
307                 national_case_insensitive_map = lowermap_rememberer;
308                 CheckForceQuit("National characters module unloaded");
309                 CheckRehash();
310         }
311
312         Version GetVersion() CXX11_OVERRIDE
313         {
314                 return Version("Provides an ability to have non-RFC1459 nicks & support for national CASEMAPPING", VF_VENDOR | VF_COMMON, charset);
315         }
316
317         /*make an array to check against it 8bit characters a bit faster. Whether allowed or uppercase (for your needs).*/
318         void makereverse(unsigned char * from, unsigned  char * to, unsigned int cnt)
319         {
320                 memset(to, 0, cnt);
321                 for(unsigned char * n=from; (*n) && ((*n)<cnt) && (n<from+cnt); n++)
322                         to[*n] = 1;
323         }
324
325         /*so Bynets Unreal distribution stuff*/
326         bool loadtables(std::string filename, unsigned char ** tables, unsigned char cnt, char faillimit)
327         {
328                 std::ifstream ifs(ServerInstance->Config->Paths.PrependConfig(filename).c_str());
329                 if (ifs.fail())
330                 {
331                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "loadtables() called for missing file: %s", filename.c_str());
332                         return false;
333                 }
334
335                 for (unsigned char n=0; n< cnt; n++)
336                 {
337                         memset(tables[n], 0, 256);
338                 }
339
340                 memcpy(m_lower, rfc_case_insensitive_map, 256);
341
342                 for (unsigned char n = 0; n < cnt; n++)
343                 {
344                         if (loadtable(ifs, tables[n], 255) && (n < faillimit))
345                         {
346                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "loadtables() called for illegal file: %s (line %d)", filename.c_str(), n+1);
347                                 return false;
348                         }
349                 }
350
351                 makereverse(m_additional, m_reverse_additional, sizeof(m_additional));
352                 return true;
353         }
354
355         unsigned char symtoi(const char *t,unsigned char base)
356         /* base = 16 for hexadecimal, 10 for decimal, 8 for octal ;) */
357         {
358                 unsigned char tmp = 0, current;
359                 while ((*t) && (*t !=' ') && (*t != 13) && (*t != 10) && (*t != ','))
360                 {
361                         tmp *= base;
362                         current = ascii_case_insensitive_map[(unsigned char)*t];
363                         if (current >= 'a')
364                                 current = current - 'a' + 10;
365                         else
366                                 current = current - '0';
367                         tmp+=current;
368                         t++;
369                 }
370                 return tmp;
371         }
372
373         int loadtable(std::ifstream &ifs , unsigned char *chartable, unsigned int maxindex)
374         {
375                 std::string buf;
376                 getline(ifs, buf);
377
378                 unsigned int i = 0;
379                 int fail = 0;
380
381                 buf.erase(buf.find_last_not_of("\n") + 1);
382
383                 if (buf[0] == '.')      /* simple plain-text string after dot */
384                 {
385                         i = buf.size() - 1;
386
387                         if (i > (maxindex + 1))
388                                 i = maxindex + 1;
389
390                         memcpy(chartable, buf.c_str() + 1, i);
391                 }
392                 else
393                 {
394                         const char * p = buf.c_str();
395                         while (*p)
396                         {
397                                 if (i > maxindex)
398                                 {
399                                         fail = 1;
400                                         break;
401                                 }
402
403                                 if (*p != '\'')         /* decimal or hexadecimal char code */
404                                 {
405                                         if (*p == '0')
406                                         {
407                                                 if (p[1] == 'x')
408                                                          /* hex with the leading "0x" */
409                                                         chartable[i] = symtoi(p + 2, 16);
410                                                 else
411                                                         chartable[i] = symtoi(p + 1, 8);
412                                         }
413                                         /* hex form */
414                                         else if (*p == 'x')
415                                         {
416                                                 chartable[i] = symtoi(p + 1, 16);
417                                         }else    /* decimal form */
418                                         {
419                                                 chartable[i] = symtoi(p, 10);
420                                         }
421                                 }
422                                 else             /* plain-text char between '' */
423                                 {
424                                         if (*(p + 1) == '\\')
425                                         {
426                                                 chartable[i] = *(p + 2);
427                                                 p += 3;
428                                         }else
429                                         {
430                                                 chartable[i] = *(p + 1);
431                                                 p += 2;
432                                         }
433                                 }
434                                 while (*p && (*p != ',') && (*p != ' ') && (*p != 13) && (*p != 10))
435                                         p++;
436                                 while (*p && ((*p == ',') || (*p == ' ') || (*p == 13) || (*p == 10)))
437                                         p++;
438                                 i++;
439                         }
440                 }
441                 return fail;
442         }
443 };
444
445 MODULE_INIT(ModuleNationalChars)