1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* Contains a code of Unreal IRCd + Bynets patch ( http://www.unrealircd.com/ and http://www.bynets.org/ )
15 Original patch is made by Dmitry "Killer{R}" Kononko. ( http://killprog.com/ )
16 Changed at 2008-06-15 - 2009-02-11
17 by Chernov-Phoenix Alexey (Phoenix@RusNet) mailto:phoenix /email address separator/ pravmail.ru */
23 /* $ModDesc: Provides an ability to have non-RFC1459 nicks & support for national CASEMAPPING */
25 class lwbNickHandler : public HandlerBase2<bool, const char*, size_t>
29 virtual ~lwbNickHandler() { }
30 virtual bool Call(const char*, size_t);
33 /*,m_reverse_additionalUp[256];*/
34 static unsigned char m_reverse_additional[256],m_additionalMB[256],m_additionalUtf8[256],m_additionalUtf8range[256],m_additionalUtf8interval[256];
36 char utf8checkrest(unsigned char * mb, unsigned char cnt)
38 for (unsigned char * tmp=mb; tmp<mb+cnt; tmp++)
40 /* & is faster! -- Phoenix (char & b11000000 == b10000000) */
41 if ((*tmp & 192) != 128)
48 char utf8size(unsigned char * mb)
54 if ((*mb & 224) == 192)
55 return utf8checkrest(mb + 1,1);
56 if ((*mb & 240) == 224)
57 return utf8checkrest(mb + 1,2);
58 if ((*mb & 248) == 240)
59 return utf8checkrest(mb + 1,3);
64 /* Conditions added */
65 bool lwbNickHandler::Call(const char* n, size_t max)
71 for (const char* i = n; *i; i++, p++)
73 /* 1. Multibyte encodings support: */
74 /* 1.1. 16bit char. areas, e.g. chinese:*/
76 /* if current character is the last, we DO NOT check it against multibyte table */
77 /* if there are mbtable ranges, use ONLY them. No 8bit at all */
78 if (i[1] && m_additionalMB[0])
80 /* otherwise let's take a look at the current character and the following one */
82 for(unsigned char * mb = m_additionalMB; (*mb) && (mb < m_additionalMB + sizeof(m_additionalMB)); mb += 4)
84 if ( (i[0] >= mb[0]) && (i[0] <= mb[1]) && (i[1] >= mb[2]) && (i[1] <= mb[3]) )
86 /* multibyte range character found */
97 /* there are ranges, but incorrect char (8bit?) given, sorry */
101 /* 2. 8bit character support */
102 if (((*i >= 'A') && (*i <= '}')) || m_reverse_additional[(unsigned char)*i])
103 /* "A"-"}" can occur anywhere in a nickname */
106 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
107 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
110 /* 3.1. Check against a simple UTF-8 characters enumeration */
111 int cursize, cursize2, ncursize = utf8size((unsigned char *)i);
112 /* do check only if current multibyte character is valid UTF-8 only */
116 for (unsigned char * mb = m_additionalUtf8; (utf8size(mb) != -1) && (mb < m_additionalUtf8 + sizeof(m_additionalUtf8)); mb += cursize)
118 cursize = utf8size(mb);
119 /* Size differs? Pick the next! */
120 if (cursize != ncursize)
123 if (!strncmp(i, (char *)mb, cursize))
134 /* 3.2. Check against an UTF-8 ranges: <start character> and <length of the range>. */
136 for (unsigned char * mb = m_additionalUtf8range; (utf8size(mb) != -1) && (mb < m_additionalUtf8range + sizeof(m_additionalUtf8range)); mb += cursize + 1)
138 cursize = utf8size(mb);
139 /* Size differs (or lengthbyte is zero)? Pick the next! */
140 if ((cursize != ncursize) || (!mb[cursize]))
143 unsigned char uright[5] = {0,0,0,0,0}, range = mb[cursize] - 1;
144 strncpy((char* ) uright, (char *) mb, cursize);
146 for (int temp = cursize - 1; (temp >= 0) && range; --temp)
148 /* all but the first char are 64-based */
151 char part64 = range & 63; /* i.e. % 64 */
152 /* handle carrying over */
153 if (uright[temp] + part64 - 1 > 191)
158 uright[temp] += part64;
159 range >>= 6; /* divide it on a 64 */
161 /* the first char of UTF-8 doesn't follow the rule */
164 uright[temp] += range;
168 if ((strncmp(i, (char *) mb, cursize) >= 0) && (strncmp(i, (char *) uright, cursize) <= 0))
179 /* 3.3. Check against an UTF-8 intervals: <start character> and <end character>. */
181 for (unsigned char * mb = m_additionalUtf8interval; (utf8size(mb) != -1) && (utf8size(mb+utf8size(mb)) != -1)
182 && (mb < m_additionalUtf8interval + sizeof(m_additionalUtf8interval)); mb += (cursize+cursize2) )
184 cursize = utf8size(mb);
185 cursize2= utf8size(mb+cursize);
187 int minlen = cursize > ncursize ? ncursize : cursize;
188 int minlen2 = cursize2 > ncursize ? ncursize : cursize2;
190 unsigned char* uright = mb + cursize;
192 if ((strncmp(i, (char *) mb, minlen) >= 0) && (strncmp(i, (char *) uright, minlen2) <= 0))
204 /* invalid character! abort */
208 /* too long? or not -- pointer arithmetic rocks */
213 class ModuleNationalChars : public Module
216 lwbNickHandler myhandler;
217 std::string charset, casemapping;
218 unsigned char m_additional[256], m_additionalUp[256], m_lower[256], m_upper[256];
219 caller2<bool, const char*, size_t> rememberer;
221 const unsigned char * lowermap_rememberer;
224 ModuleNationalChars() : rememberer(ServerInstance->IsNick)
226 lowermap_rememberer = national_case_insensitive_map;
227 memcpy(m_lower, rfc_case_insensitive_map, 256);
228 national_case_insensitive_map = m_lower;
230 ServerInstance->IsNick = &myhandler;
232 Implementation eventlist[] = { I_OnRehash, I_On005Numeric };
233 ServerInstance->Modules->Attach(eventlist, this, 2);
237 virtual void On005Numeric(std::string &output)
239 std::string tmp(casemapping);
240 tmp.insert(0, "CASEMAPPING=");
241 SearchAndReplace(output, std::string("CASEMAPPING=rfc1459"), tmp);
244 virtual void OnRehash(User* user)
247 charset = conf.ReadValue("nationalchars", "file", 0);
248 casemapping = conf.ReadValue("nationalchars", "casemapping", charset, 0, false);
249 charset.insert(0, "../locales/");
250 unsigned char * tables[8] = { m_additional, m_additionalMB, m_additionalUp, m_lower, m_upper, m_additionalUtf8, m_additionalUtf8range, m_additionalUtf8interval };
251 loadtables(charset, tables, 8, 5);
252 forcequit = conf.ReadFlag("nationalchars", "forcequit", 0);
253 CheckForceQuit("National character set changed");
256 void CheckForceQuit(const char * message)
261 for (std::vector<LocalUser*>::iterator iter = ServerInstance->Users->local_users.begin(); iter != ServerInstance->Users->local_users.end(); ++iter)
263 /* Fix by Brain: Dont quit UID users */
265 if (!isdigit(n->nick[0]) && !ServerInstance->IsNick(n->nick.c_str(), ServerInstance->Config->Limits.NickMax))
266 ServerInstance->Users->QuitUser(n, message);
270 virtual ~ModuleNationalChars()
272 ServerInstance->IsNick = rememberer;
273 national_case_insensitive_map = lowermap_rememberer;
274 CheckForceQuit("National characters module unloaded");
277 virtual Version GetVersion()
279 return Version("Provides an ability to have non-RFC1459 nicks & support for national CASEMAPPING",VF_COMMON);
282 /*make an array to check against it 8bit characters a bit faster. Whether allowed or uppercase (for your needs).*/
283 void makereverse(unsigned char * from, unsigned char * to, unsigned int cnt)
286 for(unsigned char * n=from; (*n) && ((*n)<cnt) && (n<from+cnt); n++)
290 /*so Bynets Unreal distribution stuff*/
291 void loadtables(std::string filename, unsigned char ** tables, unsigned char cnt, char faillimit)
293 std::ifstream ifs(filename.c_str());
296 ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() called for missing file: %s", filename.c_str());
300 for (unsigned char n=0; n< cnt; n++)
302 memset(tables[n], 0, 256);
305 memcpy(m_lower, rfc_case_insensitive_map, 256);
307 for (unsigned char n = 0; n < cnt; n++)
309 if (loadtable(ifs, tables[n], 255) && (n < faillimit))
311 ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() called for illegal file: %s (line %d)", filename.c_str(), n+1);
316 makereverse(m_additional, m_reverse_additional, sizeof(m_additional));
319 unsigned char symtoi(const char *t,unsigned char base)
320 /* base = 16 for hexadecimal, 10 for decimal, 8 for octal ;) */
322 unsigned char tmp = 0, current;
323 while ((*t) && (*t !=' ') && (*t != 13) && (*t != 10) && (*t != ','))
326 current = ascii_case_insensitive_map[(unsigned char)*t];
328 current = current - 'a' + 10;
330 current = current - '0';
337 int loadtable(std::ifstream &ifs , unsigned char *chartable, unsigned int maxindex)
345 buf.erase(buf.find_last_not_of("\n") + 1);
347 if (buf[0] == '.') /* simple plain-text string after dot */
351 if (i > (maxindex + 1))
354 memcpy(chartable, buf.c_str() + 1, i);
358 const char * p = buf.c_str();
367 if (*p != '\'') /* decimal or hexadecimal char code */
372 /* hex with the leading "0x" */
373 chartable[i] = symtoi(p + 2, 16);
375 chartable[i] = symtoi(p + 1, 8);
380 chartable[i] = symtoi(p + 1, 16);
381 }else /* decimal form */
383 chartable[i] = symtoi(p, 10);
386 else /* plain-text char between '' */
388 if (*(p + 1) == '\\')
390 chartable[i] = *(p + 2);
394 chartable[i] = *(p + 1);
398 while (*p && (*p != ',') && (*p != ' ') && (*p != 13) && (*p != 10))
400 while (*p && ((*p == ',') || (*p == ' ') || (*p == 13) || (*p == 10)))
409 MODULE_INIT(ModuleNationalChars)