]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nationalchars.cpp
Grr fucking windows grrrr fuckity fuckity
[user/henk/code/inspircd.git] / src / modules / m_nationalchars.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* Contains a code of Unreal IRCd + Bynets patch ( http://www.unrealircd.com/ and http://www.bynets.org/ )
15    Changed at 2008-06-15 - 2008-12-15
16    by Chernov-Phoenix Alexey (Phoenix@RusNet) mailto:phoenix /email address separator/ pravmail.ru */
17
18 #include "inspircd.h"
19 #include "caller.h"
20 #include <fstream>
21
22 /* $ModDesc: Provides an ability to have non-RFC1459 nicks & support for national CASEMAPPING */
23
24 class lwbNickHandler : public HandlerBase2<bool, const char*, size_t>
25 {
26         InspIRCd* Server;
27  public:
28         lwbNickHandler(InspIRCd* Srv) : Server(Srv) { }
29         virtual ~lwbNickHandler() { }
30         virtual bool Call(const char*, size_t);
31 };
32
33                                                                  /*,m_reverse_additionalUp[256];*/
34 static unsigned char m_reverse_additional[256],m_additionalMB[256],m_additionalUtf8[256],m_additionalUtf8range[256];
35
36 char utf8checkrest(unsigned char * mb, unsigned char cnt)
37 {
38         for (unsigned char * tmp=mb; tmp<mb+cnt; tmp++)
39         {
40                 if ((*tmp < 128) || (*tmp > 191))
41                         return -1;
42         }
43         return cnt + 1;
44 }
45
46
47 char utf8size(unsigned char * mb)
48 {
49         if (!*mb)
50                 return -1;
51         if (!(*mb & 128))
52                 return 1;
53         if ((*mb & 224) == 192)
54                 return utf8checkrest(mb + 1,1);
55         if ((*mb & 240) == 224)
56                 return utf8checkrest(mb + 1,2);
57         if ((*mb & 248) == 240)
58                 return utf8checkrest(mb + 1,3);
59         return -1;
60 }
61
62
63 /* Conditions added */
64 bool lwbNickHandler::Call(const char* n, size_t max)
65 {
66         if (!n || !*n)
67                 return false;
68
69         unsigned int p = 0;
70         for (const char* i = n; *i; i++, p++)
71         {
72                 /* 1. Multibyte encodings support:  */
73                 /* 1.1. 16bit char. areas, e.g. chinese:*/
74
75                 /* if current character is the last, we DO NOT check it against multibyte table */
76                 /* if there are mbtable ranges, use ONLY them. No 8bit at all */
77                 if (i[1] && m_additionalMB[0])
78                 {
79                         /* otherwise let's take a look at the current character and the following one */
80                         bool found = false;
81                         for(unsigned char * mb = m_additionalMB; (*mb) && (mb < m_additionalMB + sizeof(m_additionalMB)); mb += 4)
82                         {
83                                 if ( (i[0] >= mb[0]) && (i[0] <= mb[1]) && (i[1] >= mb[2]) && (i[1] <= mb[3]) )
84                                 {
85                                         /* multibyte range character found */
86                                         i++;
87                                         p++;
88                                         found = true;
89                                         break;
90                                 }
91                         }
92                         if (found)
93                                 /* next char! */
94                                 continue;
95                         else
96                                 /* there are ranges, but incorrect char (8bit?) given, sorry */
97                                 return false;
98                 }
99
100                 /* 2. 8bit character support */
101                 if (((*i >= 'A') && (*i <= '}')) || m_reverse_additional[(unsigned char)*i])
102                         /* "A"-"}" can occur anywhere in a nickname */
103                         continue;
104
105                 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
106                         /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
107                         continue;
108
109                 /* 3.1. Check against a simple UTF-8 characters enumeration */
110                 int cursize, ncursize = utf8size((unsigned char *)i);
111                 /* do check only if current multibyte character is valid UTF-8 only */
112                 if (ncursize != -1)
113                 {
114                         bool found = false;
115                         for (unsigned char * mb = m_additionalUtf8; (utf8size(mb) != -1) && (mb < m_additionalUtf8 + sizeof(m_additionalUtf8)); mb += cursize)
116                         {
117                                 cursize = utf8size(mb);
118                                 /* Size differs? Pick the next! */
119                                 if (cursize != ncursize)
120                                         continue;
121
122                                 if (!strncmp(i, (char *)mb, cursize))
123                                 {
124                                         i += cursize - 1;
125                                         p += cursize - 1;
126                                         found = true;
127                                         break;
128                                 }
129                         }
130                         if (found)
131                                 continue;
132
133                         /* 3.2. Check against an UTF-8 ranges: <start character> and <lenght of the range>.
134                         Also char. is to be checked if it is a valid UTF-8 one */
135                         found = false;
136                         for (unsigned char * mb = m_additionalUtf8range; (utf8size(mb) != -1) && (mb < m_additionalUtf8range + sizeof(m_additionalUtf8range)); mb += cursize + 1)
137                         {
138                                 cursize = utf8size(mb);
139                                 /* Size differs? Pick the next! */
140                                 if ((cursize != ncursize) || (!mb[cursize]))
141                                         continue;
142
143                                 unsigned char uright[5] = {0,0,0,0,0};
144
145                                 strncpy((char* ) uright, (char *) mb, cursize);
146
147                                 if ((uright[cursize-1] + mb[cursize]-1>0xff) && (cursize != 1))
148                                 {
149                                         uright[cursize - 2]+=1;
150                                 }
151                                 uright[cursize - 1] = (uright[cursize - 1]+mb[cursize] - 1) % 0x100;
152
153                                 if ((strncmp(i, (char *) mb, cursize) >= 0) && (strncmp(i, (char *) uright, cursize) <= 0))
154                                 {
155                                         i += cursize - 1;
156                                         p += cursize - 1;
157                                         found = true;
158                                         break;
159                                 }
160                         }
161                         if (found)
162                                 continue;
163                 }
164
165                 /* invalid character! abort */
166                 return false;
167         }
168
169         /* too long? or not -- pointer arithmetic rocks */
170         return (p < max);
171 }
172
173
174 class ModuleNationalChars : public Module
175 {
176  private:
177
178         InspIRCd* ServerInstance;
179         lwbNickHandler* myhandler;
180         std::string charset, casemapping;
181         unsigned char m_additional[256], m_additionalUp[256], m_lower[256], m_upper[256];
182         caller2<bool, const char*, size_t> * rememberer;
183         bool forcequit;
184         const unsigned char * lowermap_rememberer;
185
186  public:
187         ModuleNationalChars(InspIRCd* Me) : Module(Me)
188         {
189                 rememberer = (caller2<bool, const char*, size_t> *) malloc(sizeof(rememberer));
190
191                 lowermap_rememberer = national_case_insensitive_map;
192                 memcpy(m_lower, rfc_case_insensitive_map, 256);
193                 national_case_insensitive_map = m_lower;
194
195                 ServerInstance = Me;
196
197                 *rememberer = ServerInstance->IsNick;
198                 myhandler = new lwbNickHandler(ServerInstance);
199                 ServerInstance->IsNick = myhandler;
200
201                 Implementation eventlist[] = { I_OnRehash, I_On005Numeric };
202                 ServerInstance->Modules->Attach(eventlist, this, 2);
203                 OnRehash(NULL, "");
204         }
205
206         virtual void On005Numeric(std::string &output)
207         {
208                 std::string tmp(casemapping);
209                 tmp.insert(0, "CASEMAPPING=");
210                 SearchAndReplace(output, std::string("CASEMAPPING=rfc1459"), tmp);
211         }
212
213         virtual void OnRehash(User* user, const std::string &parameter)
214         {
215                 ConfigReader* conf = new ConfigReader(ServerInstance);
216                 charset = conf->ReadValue("nationalchars", "file", 0);
217                 casemapping = conf->ReadValue("nationalchars", "casemapping", charset, 0, false);
218                 charset.insert(0, "../locales/");
219                 unsigned char * tables[7] = { m_additional, m_additionalMB, m_additionalUp, m_lower, m_upper, m_additionalUtf8, m_additionalUtf8range };
220                 loadtables(charset, tables, 7, 5);
221                 forcequit = conf->ReadFlag("nationalchars", "forcequit", 0);
222                 CheckForceQuit("National character set changed");
223                 delete conf;
224         }
225
226         void CheckForceQuit(const char * message)
227         {
228                 if (!forcequit)
229                         return;
230
231                 for (std::vector<User*>::iterator iter = ServerInstance->Users->local_users.begin(); iter != ServerInstance->Users->local_users.end(); ++iter)
232                 {
233                         /* Fix by Brain: Dont quit UID users */
234                         User* n = *iter;
235                         if (!isdigit(n->nick[0]) && !ServerInstance->IsNick(n->nick.c_str(), ServerInstance->Config->Limits.NickMax))
236                                 ServerInstance->Users->QuitUser(n, message);
237                 }
238         }
239
240         virtual ~ModuleNationalChars()
241         {
242                 delete myhandler;
243                 ServerInstance->IsNick = *rememberer;
244                 free(rememberer);
245                 national_case_insensitive_map = lowermap_rememberer;
246                 CheckForceQuit("National characters module unloaded");
247         }
248
249         virtual Version GetVersion()
250         {
251                 return Version("$Id: m_nationalchars.cpp 0 2008-12-15 14:24:12SAMT phoenix $",VF_COMMON,API_VERSION);
252         }
253
254         /*make an array to check against it 8bit characters a bit faster. Whether allowed or uppercase (for your needs).*/
255         void makereverse(unsigned char * from, unsigned  char * to, unsigned int cnt)
256         {
257                 memset(to, 0, cnt);
258                 for(unsigned char * n=from; (*n) && ((*n)<cnt) && (n<from+cnt); n++)
259                         to[*n] = 1;
260         }
261
262         /*so Bynets Unreal distribution stuff*/
263         void loadtables(std::string filename, unsigned char ** tables, unsigned char cnt, char faillimit)
264         {
265                 std::ifstream ifs(filename.c_str());
266                 if (ifs.fail())
267                 {
268                         ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() called for missing file: %s", filename.c_str());
269                         return;
270                 }
271
272                 for (unsigned char n=0; n< cnt; n++)
273                 {
274                         memset(tables[n], 0, 256);
275                 }
276
277                 memcpy(m_lower, rfc_case_insensitive_map, 256);
278
279                 for (unsigned char n = 0; n < cnt; n++)
280                 {
281                         if (loadtable(ifs, tables[n], 255) && (n < faillimit))
282                         {
283                                 ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() called for illegal file: %s (line %d)", filename.c_str(), n+1);
284                                 return;
285                         }
286                 }
287
288                 makereverse(m_additional, m_reverse_additional, sizeof(m_additional));
289         }
290
291         unsigned char symtoi(const char *t,unsigned char base)
292         /* base = 16 for hexadecimal, 10 for decimal, 8 for octal ;) */
293         {
294                 unsigned char tmp = 0, current;
295                 while ((*t) && (*t !=' ') && (*t != 13) && (*t != 10) && (*t != ','))
296                 {
297                         tmp *= base;
298                         current = ascii_case_insensitive_map[(unsigned char)*t];
299                         if (current >= 'a')
300                                 current = current - 'a' + 10;
301                         else
302                                 current = current - '0';
303                         tmp+=current;
304                         t++;
305                 }
306                 return tmp;
307         }
308
309         int loadtable(std::ifstream &ifs , unsigned char *chartable, unsigned int maxindex)
310         {
311                 std::string buf;
312                 getline(ifs, buf);
313
314                 unsigned int i = 0;
315                 int fail = 0;
316
317                 buf.erase(buf.find_last_not_of("\n") + 1);
318
319                 if (buf[0] == '.')      /* simple plain-text string after dot */
320                 {
321                         i = buf.size() - 1;
322         
323                         if (i > (maxindex + 1))
324                                 i = maxindex + 1;
325
326                         memcpy(chartable, buf.c_str() + 1, i);
327                 }
328                 else
329                 {
330                         const char * p = buf.c_str();
331                         while (*p)
332                         {
333                                 if (i > maxindex)
334                                 {
335                                         fail = 1;
336                                         break;
337                                 }
338
339                                 if (*p != '\'')         /* decimal or hexadecimal char code */
340                                 {
341                                         if (*p == '0')
342                                         {
343                                                 if (p[1] == 'x')
344                                                          /* hex with the leading "0x" */
345                                                         chartable[i] = symtoi(p + 2, 16);
346                                                 else
347                                                         chartable[i] = symtoi(p + 1, 8);
348                                         }
349                                         /* hex form */
350                                         else if (*p == 'x')
351                                         {
352                                                 chartable[i] = symtoi(p + 1, 16);
353                                         }else    /* decimal form */
354                                         {
355                                                 chartable[i] = symtoi(p, 10);
356                                         }
357                                 }
358                                 else             /* plain-text char between '' */
359                                 {
360                                         if (*(p + 1) == '\\')
361                                         {
362                                                 chartable[i] = *(p + 2);
363                                                 p += 3;
364                                         }else
365                                         {
366                                                 chartable[i] = *(p + 1);
367                                                 p += 2;
368                                         }
369                                 }
370                                 while (*p && (*p != ',') && (*p != ' ') && (*p != 13) && (*p != 10))
371                                         p++;
372                                 while (*p && ((*p == ',') || (*p == ' ') || (*p == 13) || (*p == 10)))
373                                         p++;
374                                 i++;
375                         }
376                 }
377                 return fail;
378         }
379 };
380
381 MODULE_INIT(ModuleNationalChars)