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 (https://www.unrealircd.org and https://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 */
34 static bool Call(const std::string&);
37 /*,m_reverse_additionalUp[256];*/
38 static unsigned char m_reverse_additional[256],m_additionalMB[256],m_additionalUtf8[256],m_additionalUtf8range[256],m_additionalUtf8interval[256];
40 char utf8checkrest(unsigned char * mb, unsigned char cnt)
42 for (unsigned char * tmp=mb; tmp<mb+cnt; tmp++)
44 /* & is faster! -- Phoenix (char & b11000000 == b10000000) */
45 if ((*tmp & 192) != 128)
52 char utf8size(unsigned char * mb)
58 if ((*mb & 224) == 192)
59 return utf8checkrest(mb + 1,1);
60 if ((*mb & 240) == 224)
61 return utf8checkrest(mb + 1,2);
62 if ((*mb & 248) == 240)
63 return utf8checkrest(mb + 1,3);
68 /* Conditions added */
69 bool lwbNickHandler::Call(const std::string& nick)
74 const char* n = nick.c_str();
76 for (const char* i = n; *i; i++, p++)
78 /* 1. Multibyte encodings support: */
79 /* 1.1. 16bit char. areas, e.g. chinese:*/
81 /* if current character is the last, we DO NOT check it against multibyte table */
82 /* if there are mbtable ranges, use ONLY them. No 8bit at all */
83 if (i[1] && m_additionalMB[0])
85 /* otherwise let's take a look at the current character and the following one */
87 for(unsigned char * mb = m_additionalMB; (*mb) && (mb < m_additionalMB + sizeof(m_additionalMB)); mb += 4)
89 if ( (i[0] >= mb[0]) && (i[0] <= mb[1]) && (i[1] >= mb[2]) && (i[1] <= mb[3]) )
91 /* multibyte range character found */
102 /* there are ranges, but incorrect char (8bit?) given, sorry */
106 /* 2. 8bit character support */
107 if (((*i >= 'A') && (*i <= '}')) || m_reverse_additional[(unsigned char)*i])
108 /* "A"-"}" can occur anywhere in a nickname */
111 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
112 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
115 /* 3.1. Check against a simple UTF-8 characters enumeration */
116 int cursize, cursize2, ncursize = utf8size((unsigned char *)i);
117 /* do check only if current multibyte character is valid UTF-8 only */
121 for (unsigned char * mb = m_additionalUtf8; (utf8size(mb) != -1) && (mb < m_additionalUtf8 + sizeof(m_additionalUtf8)); mb += cursize)
123 cursize = utf8size(mb);
124 /* Size differs? Pick the next! */
125 if (cursize != ncursize)
128 if (!strncmp(i, (char *)mb, cursize))
139 /* 3.2. Check against an UTF-8 ranges: <start character> and <length of the range>. */
141 for (unsigned char * mb = m_additionalUtf8range; (utf8size(mb) != -1) && (mb < m_additionalUtf8range + sizeof(m_additionalUtf8range)); mb += cursize + 1)
143 cursize = utf8size(mb);
144 /* Size differs (or lengthbyte is zero)? Pick the next! */
145 if ((cursize != ncursize) || (!mb[cursize]))
148 unsigned char uright[5] = {0,0,0,0,0}, range = mb[cursize] - 1;
149 strncpy((char* ) uright, (char *) mb, cursize);
151 for (int temp = cursize - 1; (temp >= 0) && range; --temp)
153 /* all but the first char are 64-based */
156 char part64 = range & 63; /* i.e. % 64 */
157 /* handle carrying over */
158 if (uright[temp] + part64 - 1 > 191)
163 uright[temp] += part64;
164 range >>= 6; /* divide it on a 64 */
166 /* the first char of UTF-8 doesn't follow the rule */
169 uright[temp] += range;
173 if ((strncmp(i, (char *) mb, cursize) >= 0) && (strncmp(i, (char *) uright, cursize) <= 0))
184 /* 3.3. Check against an UTF-8 intervals: <start character> and <end character>. */
186 for (unsigned char * mb = m_additionalUtf8interval; (utf8size(mb) != -1) && (utf8size(mb+utf8size(mb)) != -1)
187 && (mb < m_additionalUtf8interval + sizeof(m_additionalUtf8interval)); mb += (cursize+cursize2) )
189 cursize = utf8size(mb);
190 cursize2= utf8size(mb+cursize);
192 int minlen = cursize > ncursize ? ncursize : cursize;
193 int minlen2 = cursize2 > ncursize ? ncursize : cursize2;
195 unsigned char* uright = mb + cursize;
197 if ((strncmp(i, (char *) mb, minlen) >= 0) && (strncmp(i, (char *) uright, minlen2) <= 0))
209 /* invalid character! abort */
213 /* too long? or not -- pointer arithmetic rocks */
214 return (p < ServerInstance->Config->Limits.NickMax);
218 class ModuleNationalChars : public Module
221 unsigned char m_additional[256], m_additionalUp[256], m_lower[256], m_upper[256];
222 TR1NS::function<bool(const std::string&)> rememberer;
224 const unsigned char * lowermap_rememberer;
225 std::string casemapping_rememberer;
226 unsigned char prev_map[256];
228 template <typename T>
229 void RehashHashmap(T& hashmap)
231 T newhash(hashmap.bucket_count());
232 for (typename T::const_iterator i = hashmap.begin(); i != hashmap.end(); ++i)
233 newhash.insert(std::make_pair(i->first, i->second));
234 hashmap.swap(newhash);
239 // See if anything changed
240 if (!memcmp(prev_map, national_case_insensitive_map, sizeof(prev_map)))
243 memcpy(prev_map, national_case_insensitive_map, sizeof(prev_map));
245 RehashHashmap(ServerInstance->Users.clientlist);
246 RehashHashmap(ServerInstance->Users.uuidlist);
247 RehashHashmap(ServerInstance->chanlist);
251 ModuleNationalChars()
252 : rememberer(ServerInstance->IsNick)
253 , lowermap_rememberer(national_case_insensitive_map)
254 , casemapping_rememberer(ServerInstance->Config->CaseMapping)
256 memcpy(prev_map, national_case_insensitive_map, sizeof(prev_map));
259 void init() CXX11_OVERRIDE
261 memcpy(m_lower, rfc_case_insensitive_map, 256);
262 national_case_insensitive_map = m_lower;
264 ServerInstance->IsNick = &lwbNickHandler::Call;
267 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
269 ConfigTag* tag = ServerInstance->Config->ConfValue("nationalchars");
270 charset = tag->getString("file");
271 std::string casemapping = tag->getString("casemapping", FileSystem::GetFileName(charset));
272 if (casemapping.find(' ') != std::string::npos)
273 throw ModuleException("<nationalchars:casemapping> must not contain any spaces!");
274 ServerInstance->Config->CaseMapping = casemapping;
276 if (!FileSystem::StartsWithWindowsDriveLetter(charset))
277 charset.insert(0, "./locales/");
279 if(charset[0] != '/')
280 charset.insert(0, "../locales/");
282 unsigned char * tables[8] = { m_additional, m_additionalMB, m_additionalUp, m_lower, m_upper, m_additionalUtf8, m_additionalUtf8range, m_additionalUtf8interval };
283 if (!loadtables(charset, tables, 8, 5))
284 throw ModuleException("The locale file failed to load. Check your log file for more information.");
285 forcequit = tag->getBool("forcequit");
286 CheckForceQuit("National character set changed");
290 void CheckForceQuit(const char * message)
295 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
296 for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); )
298 /* Fix by Brain: Dont quit UID users */
299 // Quitting the user removes it from the list
302 if (!isdigit(n->nick[0]) && !ServerInstance->IsNick(n->nick))
303 ServerInstance->Users->QuitUser(n, message);
307 ~ModuleNationalChars()
309 ServerInstance->IsNick = rememberer;
310 national_case_insensitive_map = lowermap_rememberer;
311 ServerInstance->Config->CaseMapping = casemapping_rememberer;
312 // The core rebuilds ISupport on module unload, but before the dtor.
313 ServerInstance->ISupport.Build();
314 CheckForceQuit("National characters module unloaded");
318 Version GetVersion() CXX11_OVERRIDE
320 return Version("Provides an ability to have non-RFC1459 nicks & support for national CASEMAPPING", VF_VENDOR | VF_COMMON, charset);
323 /*make an array to check against it 8bit characters a bit faster. Whether allowed or uppercase (for your needs).*/
324 void makereverse(unsigned char * from, unsigned char * to, unsigned int cnt)
327 for(unsigned char * n=from; (*n) && ((*n)<cnt) && (n<from+cnt); n++)
331 /*so Bynets Unreal distribution stuff*/
332 bool loadtables(std::string filename, unsigned char ** tables, unsigned char cnt, char faillimit)
334 std::ifstream ifs(ServerInstance->Config->Paths.PrependConfig(filename).c_str());
337 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "loadtables() called for missing file: %s", filename.c_str());
341 for (unsigned char n=0; n< cnt; n++)
343 memset(tables[n], 0, 256);
346 memcpy(m_lower, rfc_case_insensitive_map, 256);
348 for (unsigned char n = 0; n < cnt; n++)
350 if (loadtable(ifs, tables[n], 255) && (n < faillimit))
352 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "loadtables() called for illegal file: %s (line %d)", filename.c_str(), n+1);
357 makereverse(m_additional, m_reverse_additional, sizeof(m_additional));
361 unsigned char symtoi(const char *t,unsigned char base)
362 /* base = 16 for hexadecimal, 10 for decimal, 8 for octal ;) */
364 unsigned char tmp = 0, current;
365 while ((*t) && (*t !=' ') && (*t != 13) && (*t != 10) && (*t != ','))
368 current = ascii_case_insensitive_map[(unsigned char)*t];
370 current = current - 'a' + 10;
372 current = current - '0';
379 int loadtable(std::ifstream &ifs , unsigned char *chartable, unsigned int maxindex)
387 buf.erase(buf.find_last_not_of("\n") + 1);
389 if (buf[0] == '.') /* simple plain-text string after dot */
393 if (i > (maxindex + 1))
396 memcpy(chartable, buf.c_str() + 1, i);
400 const char * p = buf.c_str();
409 if (*p != '\'') /* decimal or hexadecimal char code */
414 /* hex with the leading "0x" */
415 chartable[i] = symtoi(p + 2, 16);
417 chartable[i] = symtoi(p + 1, 8);
422 chartable[i] = symtoi(p + 1, 16);
423 }else /* decimal form */
425 chartable[i] = symtoi(p, 10);
428 else /* plain-text char between '' */
430 if (*(p + 1) == '\\')
432 chartable[i] = *(p + 2);
436 chartable[i] = *(p + 1);
440 while (*p && (*p != ',') && (*p != ' ') && (*p != 13) && (*p != 10))
442 while (*p && ((*p == ',') || (*p == ' ') || (*p == 13) || (*p == 10)))
451 MODULE_INIT(ModuleNationalChars)