2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
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 */
32 /* $ModDesc: Provides an ability to have non-RFC1459 nicks & support for national CASEMAPPING */
34 class lwbNickHandler : public HandlerBase2<bool, const char*, size_t>
38 virtual ~lwbNickHandler() { }
39 virtual bool Call(const char*, size_t);
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];
45 char utf8checkrest(unsigned char * mb, unsigned char cnt)
47 for (unsigned char * tmp=mb; tmp<mb+cnt; tmp++)
49 /* & is faster! -- Phoenix (char & b11000000 == b10000000) */
50 if ((*tmp & 192) != 128)
57 char utf8size(unsigned char * mb)
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);
73 /* Conditions added */
74 bool lwbNickHandler::Call(const char* n, size_t max)
80 for (const char* i = n; *i; i++, p++)
82 /* 1. Multibyte encodings support: */
83 /* 1.1. 16bit char. areas, e.g. chinese:*/
85 /* if current character is the last, we DO NOT check it against multibyte table */
86 /* if there are mbtable ranges, use ONLY them. No 8bit at all */
87 if (i[1] && m_additionalMB[0])
89 /* otherwise let's take a look at the current character and the following one */
91 for(unsigned char * mb = m_additionalMB; (*mb) && (mb < m_additionalMB + sizeof(m_additionalMB)); mb += 4)
93 if ( (i[0] >= mb[0]) && (i[0] <= mb[1]) && (i[1] >= mb[2]) && (i[1] <= mb[3]) )
95 /* multibyte range character found */
106 /* there are ranges, but incorrect char (8bit?) given, sorry */
110 /* 2. 8bit character support */
111 if (((*i >= 'A') && (*i <= '}')) || m_reverse_additional[(unsigned char)*i])
112 /* "A"-"}" can occur anywhere in a nickname */
115 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
116 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
119 /* 3.1. Check against a simple UTF-8 characters enumeration */
120 int cursize, cursize2, ncursize = utf8size((unsigned char *)i);
121 /* do check only if current multibyte character is valid UTF-8 only */
125 for (unsigned char * mb = m_additionalUtf8; (utf8size(mb) != -1) && (mb < m_additionalUtf8 + sizeof(m_additionalUtf8)); mb += cursize)
127 cursize = utf8size(mb);
128 /* Size differs? Pick the next! */
129 if (cursize != ncursize)
132 if (!strncmp(i, (char *)mb, cursize))
143 /* 3.2. Check against an UTF-8 ranges: <start character> and <length of the range>. */
145 for (unsigned char * mb = m_additionalUtf8range; (utf8size(mb) != -1) && (mb < m_additionalUtf8range + sizeof(m_additionalUtf8range)); mb += cursize + 1)
147 cursize = utf8size(mb);
148 /* Size differs (or lengthbyte is zero)? Pick the next! */
149 if ((cursize != ncursize) || (!mb[cursize]))
152 unsigned char uright[5] = {0,0,0,0,0}, range = mb[cursize] - 1;
153 strncpy((char* ) uright, (char *) mb, cursize);
155 for (int temp = cursize - 1; (temp >= 0) && range; --temp)
157 /* all but the first char are 64-based */
160 char part64 = range & 63; /* i.e. % 64 */
161 /* handle carrying over */
162 if (uright[temp] + part64 - 1 > 191)
167 uright[temp] += part64;
168 range >>= 6; /* divide it on a 64 */
170 /* the first char of UTF-8 doesn't follow the rule */
173 uright[temp] += range;
177 if ((strncmp(i, (char *) mb, cursize) >= 0) && (strncmp(i, (char *) uright, cursize) <= 0))
188 /* 3.3. Check against an UTF-8 intervals: <start character> and <end character>. */
190 for (unsigned char * mb = m_additionalUtf8interval; (utf8size(mb) != -1) && (utf8size(mb+utf8size(mb)) != -1)
191 && (mb < m_additionalUtf8interval + sizeof(m_additionalUtf8interval)); mb += (cursize+cursize2) )
193 cursize = utf8size(mb);
194 cursize2= utf8size(mb+cursize);
196 int minlen = cursize > ncursize ? ncursize : cursize;
197 int minlen2 = cursize2 > ncursize ? ncursize : cursize2;
199 unsigned char* uright = mb + cursize;
201 if ((strncmp(i, (char *) mb, minlen) >= 0) && (strncmp(i, (char *) uright, minlen2) <= 0))
213 /* invalid character! abort */
217 /* too long? or not -- pointer arithmetic rocks */
222 class ModuleNationalChars : public Module
225 lwbNickHandler myhandler;
226 std::string charset, casemapping;
227 unsigned char m_additional[256], m_additionalUp[256], m_lower[256], m_upper[256];
228 caller2<bool, const char*, size_t> rememberer;
230 const unsigned char * lowermap_rememberer;
233 ModuleNationalChars() : rememberer(ServerInstance->IsNick)
235 lowermap_rememberer = national_case_insensitive_map;
236 memcpy(m_lower, rfc_case_insensitive_map, 256);
237 national_case_insensitive_map = m_lower;
239 ServerInstance->IsNick = &myhandler;
241 Implementation eventlist[] = { I_OnRehash, I_On005Numeric };
242 ServerInstance->Modules->Attach(eventlist, this, 2);
246 virtual void On005Numeric(std::string &output)
248 std::string tmp(casemapping);
249 tmp.insert(0, "CASEMAPPING=");
250 SearchAndReplace(output, std::string("CASEMAPPING=rfc1459"), tmp);
253 virtual void OnRehash(User* user)
255 ConfigTag* tag = ServerInstance->Config->ConfValue("nationalchars");
256 charset = tag->getString("file");
257 casemapping = tag->getString("casemapping", charset);
258 if(charset[0] != '/')
259 charset.insert(0, "../locales/");
260 unsigned char * tables[8] = { m_additional, m_additionalMB, m_additionalUp, m_lower, m_upper, m_additionalUtf8, m_additionalUtf8range, m_additionalUtf8interval };
261 loadtables(charset, tables, 8, 5);
262 forcequit = tag->getBool("forcequit");
263 CheckForceQuit("National character set changed");
266 void CheckForceQuit(const char * message)
271 for (std::vector<LocalUser*>::iterator iter = ServerInstance->Users->local_users.begin(); iter != ServerInstance->Users->local_users.end(); ++iter)
273 /* Fix by Brain: Dont quit UID users */
275 if (!isdigit(n->nick[0]) && !ServerInstance->IsNick(n->nick.c_str(), ServerInstance->Config->Limits.NickMax))
276 ServerInstance->Users->QuitUser(n, message);
280 virtual ~ModuleNationalChars()
282 ServerInstance->IsNick = rememberer;
283 national_case_insensitive_map = lowermap_rememberer;
284 CheckForceQuit("National characters module unloaded");
287 virtual Version GetVersion()
289 return Version("Provides an ability to have non-RFC1459 nicks & support for national CASEMAPPING", VF_VENDOR | VF_COMMON, charset);
292 /*make an array to check against it 8bit characters a bit faster. Whether allowed or uppercase (for your needs).*/
293 void makereverse(unsigned char * from, unsigned char * to, unsigned int cnt)
296 for(unsigned char * n=from; (*n) && ((*n)<cnt) && (n<from+cnt); n++)
300 /*so Bynets Unreal distribution stuff*/
301 void loadtables(std::string filename, unsigned char ** tables, unsigned char cnt, char faillimit)
303 std::ifstream ifs(filename.c_str());
306 ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() called for missing file: %s", filename.c_str());
310 for (unsigned char n=0; n< cnt; n++)
312 memset(tables[n], 0, 256);
315 memcpy(m_lower, rfc_case_insensitive_map, 256);
317 for (unsigned char n = 0; n < cnt; n++)
319 if (loadtable(ifs, tables[n], 255) && (n < faillimit))
321 ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() called for illegal file: %s (line %d)", filename.c_str(), n+1);
326 makereverse(m_additional, m_reverse_additional, sizeof(m_additional));
329 unsigned char symtoi(const char *t,unsigned char base)
330 /* base = 16 for hexadecimal, 10 for decimal, 8 for octal ;) */
332 unsigned char tmp = 0, current;
333 while ((*t) && (*t !=' ') && (*t != 13) && (*t != 10) && (*t != ','))
336 current = ascii_case_insensitive_map[(unsigned char)*t];
338 current = current - 'a' + 10;
340 current = current - '0';
347 int loadtable(std::ifstream &ifs , unsigned char *chartable, unsigned int maxindex)
355 buf.erase(buf.find_last_not_of("\n") + 1);
357 if (buf[0] == '.') /* simple plain-text string after dot */
361 if (i > (maxindex + 1))
364 memcpy(chartable, buf.c_str() + 1, i);
368 const char * p = buf.c_str();
377 if (*p != '\'') /* decimal or hexadecimal char code */
382 /* hex with the leading "0x" */
383 chartable[i] = symtoi(p + 2, 16);
385 chartable[i] = symtoi(p + 1, 8);
390 chartable[i] = symtoi(p + 1, 16);
391 }else /* decimal form */
393 chartable[i] = symtoi(p, 10);
396 else /* plain-text char between '' */
398 if (*(p + 1) == '\\')
400 chartable[i] = *(p + 2);
404 chartable[i] = *(p + 1);
408 while (*p && (*p != ',') && (*p != ' ') && (*p != 13) && (*p != 10))
410 while (*p && ((*p == ',') || (*p == ' ') || (*p == 13) || (*p == 10)))
419 MODULE_INIT(ModuleNationalChars)