]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nationalchars.cpp
Change IsNickHandler()/IsIdentHandler()/IsChannelHandler() to use C++ strings as...
[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 "caller.h"
30 #include <fstream>
31
32 /* $ModDesc: Provides an ability to have non-RFC1459 nicks & support for national CASEMAPPING */
33
34 class lwbNickHandler : public HandlerBase2<bool, const std::string&, size_t>
35 {
36  public:
37         lwbNickHandler() { }
38         virtual ~lwbNickHandler() { }
39         virtual bool Call(const std::string&, size_t);
40 };
41
42                                                                  /*,m_reverse_additionalUp[256];*/
43 static unsigned char m_reverse_additional[256],m_additionalMB[256],m_additionalUtf8[256],m_additionalUtf8range[256],m_additionalUtf8interval[256];
44
45 char utf8checkrest(unsigned char * mb, unsigned char cnt)
46 {
47         for (unsigned char * tmp=mb; tmp<mb+cnt; tmp++)
48         {
49                 /* & is faster! -- Phoenix (char & b11000000 == b10000000) */
50                 if ((*tmp & 192) != 128)
51                         return -1;
52         }
53         return cnt + 1;
54 }
55
56
57 char utf8size(unsigned char * mb)
58 {
59         if (!*mb)
60                 return -1;
61         if (!(*mb & 128))
62                 return 1;
63         if ((*mb & 224) == 192)
64                 return utf8checkrest(mb + 1,1);
65         if ((*mb & 240) == 224)
66                 return utf8checkrest(mb + 1,2);
67         if ((*mb & 248) == 240)
68                 return utf8checkrest(mb + 1,3);
69         return -1;
70 }
71
72
73 /* Conditions added */
74 bool lwbNickHandler::Call(const std::string& nick, size_t max)
75 {
76         if (nick.empty())
77                 return false;
78
79         const char* n = nick.c_str();
80         unsigned int p = 0;
81         for (const char* i = n; *i; i++, p++)
82         {
83                 /* 1. Multibyte encodings support:  */
84                 /* 1.1. 16bit char. areas, e.g. chinese:*/
85
86                 /* if current character is the last, we DO NOT check it against multibyte table */
87                 /* if there are mbtable ranges, use ONLY them. No 8bit at all */
88                 if (i[1] && m_additionalMB[0])
89                 {
90                         /* otherwise let's take a look at the current character and the following one */
91                         bool found = false;
92                         for(unsigned char * mb = m_additionalMB; (*mb) && (mb < m_additionalMB + sizeof(m_additionalMB)); mb += 4)
93                         {
94                                 if ( (i[0] >= mb[0]) && (i[0] <= mb[1]) && (i[1] >= mb[2]) && (i[1] <= mb[3]) )
95                                 {
96                                         /* multibyte range character found */
97                                         i++;
98                                         p++;
99                                         found = true;
100                                         break;
101                                 }
102                         }
103                         if (found)
104                                 /* next char! */
105                                 continue;
106                         else
107                                 /* there are ranges, but incorrect char (8bit?) given, sorry */
108                                 return false;
109                 }
110
111                 /* 2. 8bit character support */
112                 if (((*i >= 'A') && (*i <= '}')) || m_reverse_additional[(unsigned char)*i])
113                         /* "A"-"}" can occur anywhere in a nickname */
114                         continue;
115
116                 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
117                         /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
118                         continue;
119
120                 /* 3.1. Check against a simple UTF-8 characters enumeration */
121                 int cursize, cursize2, ncursize = utf8size((unsigned char *)i);
122                 /* do check only if current multibyte character is valid UTF-8 only */
123                 if (ncursize != -1)
124                 {
125                         bool found = false;
126                         for (unsigned char * mb = m_additionalUtf8; (utf8size(mb) != -1) && (mb < m_additionalUtf8 + sizeof(m_additionalUtf8)); mb += cursize)
127                         {
128                                 cursize = utf8size(mb);
129                                 /* Size differs? Pick the next! */
130                                 if (cursize != ncursize)
131                                         continue;
132
133                                 if (!strncmp(i, (char *)mb, cursize))
134                                 {
135                                         i += cursize - 1;
136                                         p += cursize - 1;
137                                         found = true;
138                                         break;
139                                 }
140                         }
141                         if (found)
142                                 continue;
143
144                         /* 3.2. Check against an UTF-8 ranges: <start character> and <length of the range>. */
145                         found = false;
146                         for (unsigned char * mb = m_additionalUtf8range; (utf8size(mb) != -1) && (mb < m_additionalUtf8range + sizeof(m_additionalUtf8range)); mb += cursize + 1)
147                         {
148                                 cursize = utf8size(mb);
149                                 /* Size differs (or lengthbyte is zero)? Pick the next! */
150                                 if ((cursize != ncursize) || (!mb[cursize]))
151                                         continue;
152
153                                 unsigned char uright[5] = {0,0,0,0,0}, range = mb[cursize] - 1;
154                                 strncpy((char* ) uright, (char *) mb, cursize);
155
156                                 for (int temp = cursize - 1; (temp >= 0) && range; --temp)
157                                 {
158                                         /* all but the first char are 64-based */
159                                         if (temp)
160                                         {
161                                                 char part64 = range & 63; /* i.e. % 64 */
162                                                 /* handle carrying over */
163                                                 if (uright[temp] + part64 - 1 > 191)
164                                                 {
165                                                         uright[temp] -= 64;
166                                                         range += 64;
167                                                 }
168                                                 uright[temp] += part64;
169                                                 range >>= 6; /* divide it on a 64 */
170                                         }
171                                         /* the first char of UTF-8 doesn't follow the rule */
172                                         else
173                                         {
174                                                 uright[temp] += range;
175                                         }
176                                 }
177
178                                 if ((strncmp(i, (char *) mb, cursize) >= 0) && (strncmp(i, (char *) uright, cursize) <= 0))
179                                 {
180                                         i += cursize - 1;
181                                         p += cursize - 1;
182                                         found = true;
183                                         break;
184                                 }
185                         }
186                         if (found)
187                                 continue;
188
189                         /* 3.3. Check against an UTF-8 intervals: <start character> and <end character>. */
190                         found = false;
191                         for (unsigned char * mb = m_additionalUtf8interval; (utf8size(mb) != -1) && (utf8size(mb+utf8size(mb)) != -1)
192                                 && (mb < m_additionalUtf8interval + sizeof(m_additionalUtf8interval)); mb += (cursize+cursize2) )
193                         {
194                                 cursize = utf8size(mb);
195                                 cursize2= utf8size(mb+cursize);
196
197                                 int minlen  = cursize  > ncursize ? ncursize : cursize;
198                                 int minlen2 = cursize2 > ncursize ? ncursize : cursize2;
199
200                                 unsigned char* uright = mb + cursize;
201
202                                 if ((strncmp(i, (char *) mb, minlen) >= 0) && (strncmp(i, (char *) uright, minlen2) <= 0))
203                                 {
204                                         i += cursize - 1;
205                                         p += cursize - 1;
206                                         found = true;
207                                         break;
208                                 }
209                         }
210                         if (found)
211                                 continue;
212                 }
213
214                 /* invalid character! abort */
215                 return false;
216         }
217
218         /* too long? or not -- pointer arithmetic rocks */
219         return (p < max);
220 }
221
222
223 class ModuleNationalChars : public Module
224 {
225  private:
226         lwbNickHandler myhandler;
227         std::string charset, casemapping;
228         unsigned char m_additional[256], m_additionalUp[256], m_lower[256], m_upper[256];
229         caller2<bool, const std::string&, size_t> rememberer;
230         bool forcequit;
231         const unsigned char * lowermap_rememberer;
232
233  public:
234         ModuleNationalChars()
235                 : rememberer(ServerInstance->IsNick), lowermap_rememberer(national_case_insensitive_map)
236         {
237         }
238
239         void init()
240         {
241                 memcpy(m_lower, rfc_case_insensitive_map, 256);
242                 national_case_insensitive_map = m_lower;
243
244                 ServerInstance->IsNick = &myhandler;
245
246                 Implementation eventlist[] = { I_OnRehash, I_On005Numeric };
247                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
248                 OnRehash(NULL);
249         }
250
251         virtual void On005Numeric(std::string &output)
252         {
253                 std::string tmp(casemapping);
254                 tmp.insert(0, "CASEMAPPING=");
255                 SearchAndReplace(output, std::string("CASEMAPPING=rfc1459"), tmp);
256         }
257
258         virtual void OnRehash(User* user)
259         {
260                 ConfigTag* tag = ServerInstance->Config->ConfValue("nationalchars");
261                 charset = tag->getString("file");
262                 casemapping = tag->getString("casemapping", charset);
263                 if(charset[0] != '/')
264                         charset.insert(0, "../locales/");
265                 unsigned char * tables[8] = { m_additional, m_additionalMB, m_additionalUp, m_lower, m_upper, m_additionalUtf8, m_additionalUtf8range, m_additionalUtf8interval };
266                 loadtables(charset, tables, 8, 5);
267                 forcequit = tag->getBool("forcequit");
268                 CheckForceQuit("National character set changed");
269         }
270
271         void CheckForceQuit(const char * message)
272         {
273                 if (!forcequit)
274                         return;
275
276                 for (LocalUserList::const_iterator iter = ServerInstance->Users->local_users.begin(); iter != ServerInstance->Users->local_users.end(); ++iter)
277                 {
278                         /* Fix by Brain: Dont quit UID users */
279                         User* n = *iter;
280                         if (!isdigit(n->nick[0]) && !ServerInstance->IsNick(n->nick, ServerInstance->Config->Limits.NickMax))
281                                 ServerInstance->Users->QuitUser(n, message);
282                 }
283         }
284
285         virtual ~ModuleNationalChars()
286         {
287                 ServerInstance->IsNick = rememberer;
288                 national_case_insensitive_map = lowermap_rememberer;
289                 CheckForceQuit("National characters module unloaded");
290         }
291
292         virtual Version GetVersion()
293         {
294                 return Version("Provides an ability to have non-RFC1459 nicks & support for national CASEMAPPING", VF_VENDOR | VF_COMMON, charset);
295         }
296
297         /*make an array to check against it 8bit characters a bit faster. Whether allowed or uppercase (for your needs).*/
298         void makereverse(unsigned char * from, unsigned  char * to, unsigned int cnt)
299         {
300                 memset(to, 0, cnt);
301                 for(unsigned char * n=from; (*n) && ((*n)<cnt) && (n<from+cnt); n++)
302                         to[*n] = 1;
303         }
304
305         /*so Bynets Unreal distribution stuff*/
306         void loadtables(std::string filename, unsigned char ** tables, unsigned char cnt, char faillimit)
307         {
308                 std::ifstream ifs(filename.c_str());
309                 if (ifs.fail())
310                 {
311                         ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() called for missing file: %s", filename.c_str());
312                         return;
313                 }
314
315                 for (unsigned char n=0; n< cnt; n++)
316                 {
317                         memset(tables[n], 0, 256);
318                 }
319
320                 memcpy(m_lower, rfc_case_insensitive_map, 256);
321
322                 for (unsigned char n = 0; n < cnt; n++)
323                 {
324                         if (loadtable(ifs, tables[n], 255) && (n < faillimit))
325                         {
326                                 ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() called for illegal file: %s (line %d)", filename.c_str(), n+1);
327                                 return;
328                         }
329                 }
330
331                 makereverse(m_additional, m_reverse_additional, sizeof(m_additional));
332         }
333
334         unsigned char symtoi(const char *t,unsigned char base)
335         /* base = 16 for hexadecimal, 10 for decimal, 8 for octal ;) */
336         {
337                 unsigned char tmp = 0, current;
338                 while ((*t) && (*t !=' ') && (*t != 13) && (*t != 10) && (*t != ','))
339                 {
340                         tmp *= base;
341                         current = ascii_case_insensitive_map[(unsigned char)*t];
342                         if (current >= 'a')
343                                 current = current - 'a' + 10;
344                         else
345                                 current = current - '0';
346                         tmp+=current;
347                         t++;
348                 }
349                 return tmp;
350         }
351
352         int loadtable(std::ifstream &ifs , unsigned char *chartable, unsigned int maxindex)
353         {
354                 std::string buf;
355                 getline(ifs, buf);
356
357                 unsigned int i = 0;
358                 int fail = 0;
359
360                 buf.erase(buf.find_last_not_of("\n") + 1);
361
362                 if (buf[0] == '.')      /* simple plain-text string after dot */
363                 {
364                         i = buf.size() - 1;
365
366                         if (i > (maxindex + 1))
367                                 i = maxindex + 1;
368
369                         memcpy(chartable, buf.c_str() + 1, i);
370                 }
371                 else
372                 {
373                         const char * p = buf.c_str();
374                         while (*p)
375                         {
376                                 if (i > maxindex)
377                                 {
378                                         fail = 1;
379                                         break;
380                                 }
381
382                                 if (*p != '\'')         /* decimal or hexadecimal char code */
383                                 {
384                                         if (*p == '0')
385                                         {
386                                                 if (p[1] == 'x')
387                                                          /* hex with the leading "0x" */
388                                                         chartable[i] = symtoi(p + 2, 16);
389                                                 else
390                                                         chartable[i] = symtoi(p + 1, 8);
391                                         }
392                                         /* hex form */
393                                         else if (*p == 'x')
394                                         {
395                                                 chartable[i] = symtoi(p + 1, 16);
396                                         }else    /* decimal form */
397                                         {
398                                                 chartable[i] = symtoi(p, 10);
399                                         }
400                                 }
401                                 else             /* plain-text char between '' */
402                                 {
403                                         if (*(p + 1) == '\\')
404                                         {
405                                                 chartable[i] = *(p + 2);
406                                                 p += 3;
407                                         }else
408                                         {
409                                                 chartable[i] = *(p + 1);
410                                                 p += 2;
411                                         }
412                                 }
413                                 while (*p && (*p != ',') && (*p != ' ') && (*p != 13) && (*p != 10))
414                                         p++;
415                                 while (*p && ((*p == ',') || (*p == ' ') || (*p == 13) || (*p == 10)))
416                                         p++;
417                                 i++;
418                         }
419                 }
420                 return fail;
421         }
422 };
423
424 MODULE_INIT(ModuleNationalChars)