1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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 Changed at 2008-06-15 - 2008-12-15
16 by Chernov-Phoenix Alexey (Phoenix@RusNet) mailto:phoenix /email address separator/ pravmail.ru */
22 /* $ModDesc: Provides an ability to have non-RFC1459 nicks & support for national CASEMAPPING */
24 class lwbNickHandler : public HandlerBase2<bool, const char*, size_t>
28 lwbNickHandler(InspIRCd* Srv) : Server(Srv) { }
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];
36 char utf8checkrest(unsigned char * mb, unsigned char cnt)
38 for (unsigned char * tmp=mb; tmp<mb+cnt; tmp++)
40 if ((*tmp < 128) || (*tmp > 191))
47 char utf8size(unsigned char * mb)
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);
63 /* Conditions added */
64 bool lwbNickHandler::Call(const char* n, size_t max)
70 for (const char* i = n; *i; i++, p++)
72 /* 1. Multibyte encodings support: */
73 /* 1.1. 16bit char. areas, e.g. chinese:*/
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])
79 /* otherwise let's take a look at the current character and the following one */
81 for(unsigned char * mb = m_additionalMB; (*mb) && (mb < m_additionalMB + sizeof(m_additionalMB)); mb += 4)
83 if ( (i[0] >= mb[0]) && (i[0] <= mb[1]) && (i[1] >= mb[2]) && (i[1] <= mb[3]) )
85 /* multibyte range character found */
96 /* there are ranges, but incorrect char (8bit?) given, sorry */
100 /* 2. 8bit character support */
101 if (((*i >= 'A') && (*i <= '}')) || m_reverse_additional[(unsigned char)*i])
102 /* "A"-"}" can occur anywhere in a nickname */
105 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
106 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
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 */
115 for (unsigned char * mb = m_additionalUtf8; (utf8size(mb) != -1) && (mb < m_additionalUtf8 + sizeof(m_additionalUtf8)); mb += cursize)
117 cursize = utf8size(mb);
118 /* Size differs? Pick the next! */
119 if (cursize != ncursize)
122 if (!strncmp(i, (char *)mb, cursize))
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 */
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? Pick the next! */
140 if ((cursize != ncursize) || (!mb[cursize]))
143 unsigned char uright[5] = {0,0,0,0,0};
145 strncpy((char* ) uright, (char *) mb, cursize);
147 if ((uright[cursize-1] + mb[cursize]-1>0xff) && (cursize != 1))
149 uright[cursize - 2]+=1;
151 uright[cursize - 1] = (uright[cursize - 1]+mb[cursize] - 1) % 0x100;
153 if ((strncmp(i, (char *) mb, cursize) >= 0) && (strncmp(i, (char *) uright, cursize) <= 0))
165 /* invalid character! abort */
169 /* too long? or not -- pointer arithmetic rocks */
174 class ModuleNationalChars : public Module
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;
184 const unsigned char * lowermap_rememberer;
187 ModuleNationalChars(InspIRCd* Me) : Module(Me)
189 rememberer = (caller2<bool, const char*, size_t> *) malloc(sizeof(rememberer));
191 lowermap_rememberer = national_case_insensitive_map;
192 memcpy(m_lower, rfc_case_insensitive_map, 256);
193 national_case_insensitive_map = m_lower;
197 *rememberer = ServerInstance->IsNick;
198 myhandler = new lwbNickHandler(ServerInstance);
199 ServerInstance->IsNick = myhandler;
201 Implementation eventlist[] = { I_OnRehash, I_On005Numeric };
202 ServerInstance->Modules->Attach(eventlist, this, 2);
206 virtual void On005Numeric(std::string &output)
208 std::string tmp(casemapping);
209 tmp.insert(0, "CASEMAPPING=");
210 SearchAndReplace(output, std::string("CASEMAPPING=rfc1459"), tmp);
213 virtual void OnRehash(User* user, const std::string ¶meter)
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");
226 void CheckForceQuit(const char * message)
231 for (std::vector<User*>::iterator iter = ServerInstance->Users->local_users.begin(); iter != ServerInstance->Users->local_users.end(); ++iter)
233 /* Fix by Brain: Dont quit UID users */
235 if (!isdigit(n->nick[0]) && !ServerInstance->IsNick(n->nick.c_str(), ServerInstance->Config->Limits.NickMax))
236 ServerInstance->Users->QuitUser(n, message);
240 virtual ~ModuleNationalChars()
243 ServerInstance->IsNick = *rememberer;
245 national_case_insensitive_map = lowermap_rememberer;
246 CheckForceQuit("National characters module unloaded");
249 virtual Version GetVersion()
251 return Version("$Id: m_nationalchars.cpp 0 2008-12-15 14:24:12SAMT phoenix $",VF_COMMON,API_VERSION);
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)
258 for(unsigned char * n=from; (*n) && ((*n)<cnt) && (n<from+cnt); n++)
262 /*so Bynets Unreal distribution stuff*/
263 void loadtables(std::string filename, unsigned char ** tables, unsigned char cnt, char faillimit)
265 std::ifstream ifs(filename.c_str());
268 ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() called for missing file: %s", filename.c_str());
272 for (unsigned char n=0; n< cnt; n++)
274 memset(tables[n], 0, 256);
277 memcpy(m_lower, rfc_case_insensitive_map, 256);
279 for (unsigned char n = 0; n < cnt; n++)
281 if (loadtable(ifs, tables[n], 255) && (n < faillimit))
283 ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() called for illegal file: %s (line %d)", filename.c_str(), n+1);
288 makereverse(m_additional, m_reverse_additional, sizeof(m_additional));
291 unsigned char symtoi(const char *t,unsigned char base)
292 /* base = 16 for hexadecimal, 10 for decimal, 8 for octal ;) */
294 unsigned char tmp = 0, current;
295 while ((*t) && (*t !=' ') && (*t != 13) && (*t != 10) && (*t != ','))
298 current = ascii_case_insensitive_map[(unsigned char)*t];
300 current = current - 'a' + 10;
302 current = current - '0';
309 int loadtable(std::ifstream &ifs , unsigned char *chartable, unsigned int maxindex)
317 buf.erase(buf.find_last_not_of("\n") + 1);
319 if (buf[0] == '.') /* simple plain-text string after dot */
323 if (i > (maxindex + 1))
326 memcpy(chartable, buf.c_str() + 1, i);
330 const char * p = buf.c_str();
339 if (*p != '\'') /* decimal or hexadecimal char code */
344 /* hex with the leading "0x" */
345 chartable[i] = symtoi(p + 2, 16);
347 chartable[i] = symtoi(p + 1, 8);
352 chartable[i] = symtoi(p + 1, 16);
353 }else /* decimal form */
355 chartable[i] = symtoi(p, 10);
358 else /* plain-text char between '' */
360 if (*(p + 1) == '\\')
362 chartable[i] = *(p + 2);
366 chartable[i] = *(p + 1);
370 while (*p && (*p != ',') && (*p != ' ') && (*p != 13) && (*p != 10))
372 while (*p && ((*p == ',') || (*p == ' ') || (*p == 13) || (*p == 10)))
381 MODULE_INIT(ModuleNationalChars)