2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5 * Copyright (C) 2013, 2015, 2017, 2020-2021 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8 * Copyright (C) 2011 jackmcbarn <jackmcbarn@inspircd.org>
9 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10 * Copyright (C) 2009-2010 Craig Edwards <brain@inspircd.org>
11 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
12 * Copyright (C) 2009 Robin Burchell <robin+git@viroteck.net>
13 * Copyright (C) 2009 Dennis Friis <peavey@inspircd.org>
15 * This file is part of InspIRCd. InspIRCd is free software: you can
16 * redistribute it and/or modify it under the terms of the GNU General Public
17 * License as published by the Free Software Foundation, version 2.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /* Contains a code of Unreal IRCd + Bynets patch (https://www.unrealircd.org and https://bynets.org)
30 Original patch is made by Dmitry "Killer{R}" Kononko. (http://killprog.com)
31 Changed at 2008-06-15 - 2009-02-11
32 by Chernov-Phoenix Alexey (Phoenix@RusNet) mailto:phoenix /email address separator/ pravmail.ru */
40 static bool Call(const std::string&);
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 std::string& nick)
76 if (nick.empty() || isdigit(nick[0]))
79 const char* n = nick.c_str();
81 for (const char* i = n; *i; i++, p++)
83 /* 1. Multibyte encodings support: */
84 /* 1.1. 16bit char. areas, e.g. chinese:*/
86 /* if current character is the last, we DO NOT check it against multibyte table */
87 /* if there are mbtable ranges, use ONLY them. No 8bit at all */
88 if (i[1] && m_additionalMB[0])
90 /* otherwise let's take a look at the current character and the following one */
92 for(unsigned char * mb = m_additionalMB; (*mb) && (mb < m_additionalMB + sizeof(m_additionalMB)); mb += 4)
94 if ( (i[0] >= mb[0]) && (i[0] <= mb[1]) && (i[1] >= mb[2]) && (i[1] <= mb[3]) )
96 /* multibyte range character found */
107 /* there are ranges, but incorrect char (8bit?) given, sorry */
111 /* 2. 8bit character support */
112 if (((*i >= 'A') && (*i <= '}')) || m_reverse_additional[(unsigned char)*i])
113 /* "A"-"}" can occur anywhere in a nickname */
116 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
117 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
120 /* 3.1. Check against a simple UTF-8 characters enumeration */
121 int cursize, cursize2, ncursize = utf8size((unsigned char *)i);
122 /* do check only if current multibyte character is valid UTF-8 only */
126 for (unsigned char * mb = m_additionalUtf8; (utf8size(mb) != -1) && (mb < m_additionalUtf8 + sizeof(m_additionalUtf8)); mb += cursize)
128 cursize = utf8size(mb);
129 /* Size differs? Pick the next! */
130 if (cursize != ncursize)
133 if (!strncmp(i, (char *)mb, cursize))
144 /* 3.2. Check against an UTF-8 ranges: <start character> and <length of the range>. */
146 for (unsigned char * mb = m_additionalUtf8range; (utf8size(mb) != -1) && (mb < m_additionalUtf8range + sizeof(m_additionalUtf8range)); mb += cursize + 1)
148 cursize = utf8size(mb);
149 /* Size differs (or lengthbyte is zero)? Pick the next! */
150 if ((cursize != ncursize) || (!mb[cursize]))
153 unsigned char uright[5] = {0,0,0,0,0}, range = mb[cursize] - 1;
154 strncpy((char* ) uright, (char *) mb, cursize);
156 for (int temp = cursize - 1; (temp >= 0) && range; --temp)
158 /* all but the first char are 64-based */
161 char part64 = range & 63; /* i.e. % 64 */
162 /* handle carrying over */
163 if (uright[temp] + part64 - 1 > 191)
168 uright[temp] += part64;
169 range >>= 6; /* divide it on a 64 */
171 /* the first char of UTF-8 doesn't follow the rule */
174 uright[temp] += range;
178 if ((strncmp(i, (char *) mb, cursize) >= 0) && (strncmp(i, (char *) uright, cursize) <= 0))
189 /* 3.3. Check against an UTF-8 intervals: <start character> and <end character>. */
191 for (unsigned char * mb = m_additionalUtf8interval; (utf8size(mb) != -1) && (utf8size(mb+utf8size(mb)) != -1)
192 && (mb < m_additionalUtf8interval + sizeof(m_additionalUtf8interval)); mb += (cursize+cursize2) )
194 cursize = utf8size(mb);
195 cursize2= utf8size(mb+cursize);
197 int minlen = cursize > ncursize ? ncursize : cursize;
198 int minlen2 = cursize2 > ncursize ? ncursize : cursize2;
200 unsigned char* uright = mb + cursize;
202 if ((strncmp(i, (char *) mb, minlen) >= 0) && (strncmp(i, (char *) uright, minlen2) <= 0))
214 /* invalid character! abort */
218 /* too long? or not -- pointer arithmetic rocks */
219 return (p < ServerInstance->Config->Limits.NickMax);
223 class ModuleNationalChars : public Module
226 unsigned char m_additional[256], m_additionalUp[256], m_lower[256], m_upper[256];
227 TR1NS::function<bool(const std::string&)> rememberer;
229 const unsigned char * lowermap_rememberer;
230 std::string casemapping_rememberer;
231 unsigned char prev_map[256];
233 template <typename T>
234 void RehashHashmap(T& hashmap)
236 T newhash(hashmap.bucket_count());
237 for (typename T::const_iterator i = hashmap.begin(); i != hashmap.end(); ++i)
238 newhash.insert(std::make_pair(i->first, i->second));
239 hashmap.swap(newhash);
244 // See if anything changed
245 if (!memcmp(prev_map, national_case_insensitive_map, sizeof(prev_map)))
248 memcpy(prev_map, national_case_insensitive_map, sizeof(prev_map));
250 RehashHashmap(ServerInstance->Users.clientlist);
251 RehashHashmap(ServerInstance->Users.uuidlist);
252 RehashHashmap(ServerInstance->chanlist);
256 ModuleNationalChars()
257 : rememberer(ServerInstance->IsNick)
258 , lowermap_rememberer(national_case_insensitive_map)
259 , casemapping_rememberer(ServerInstance->Config->CaseMapping)
261 memcpy(prev_map, national_case_insensitive_map, sizeof(prev_map));
264 void init() CXX11_OVERRIDE
266 memcpy(m_lower, rfc_case_insensitive_map, 256);
267 national_case_insensitive_map = m_lower;
269 ServerInstance->IsNick = &lwbNickHandler::Call;
272 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
274 ConfigTag* tag = ServerInstance->Config->ConfValue("nationalchars");
275 charset = tag->getString("file");
276 std::string casemapping = tag->getString("casemapping", FileSystem::GetFileName(charset), 1);
277 if (casemapping.find(' ') != std::string::npos)
278 throw ModuleException("<nationalchars:casemapping> must not contain any spaces!");
279 ServerInstance->Config->CaseMapping = casemapping;
281 if (!FileSystem::StartsWithWindowsDriveLetter(charset))
282 charset.insert(0, "./locales/");
284 if(charset[0] != '/')
285 charset.insert(0, "../locales/");
287 unsigned char * tables[8] = { m_additional, m_additionalMB, m_additionalUp, m_lower, m_upper, m_additionalUtf8, m_additionalUtf8range, m_additionalUtf8interval };
288 if (!loadtables(charset, tables, 8, 5))
289 throw ModuleException("The locale file failed to load. Check your log file for more information.");
290 forcequit = tag->getBool("forcequit");
291 CheckForceQuit("National character set changed");
295 void CheckForceQuit(const char * message)
300 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
301 for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); )
303 /* Fix by Brain: Dont quit UID users */
304 // Quitting the user removes it from the list
307 if (!isdigit(n->nick[0]) && !ServerInstance->IsNick(n->nick))
308 ServerInstance->Users->QuitUser(n, message);
312 ~ModuleNationalChars()
314 ServerInstance->IsNick = rememberer;
315 national_case_insensitive_map = lowermap_rememberer;
316 ServerInstance->Config->CaseMapping = casemapping_rememberer;
317 // The core rebuilds ISupport on module unload, but before the dtor.
318 ServerInstance->ISupport.Build();
319 CheckForceQuit("National characters module unloaded");
323 Version GetVersion() CXX11_OVERRIDE
325 return Version("Allows the server administrator to define what characters are allowed in nicknames and channel names and how those characters should be compared in a case insensitive way.", VF_VENDOR | VF_COMMON);
328 /*make an array to check against it 8bit characters a bit faster. Whether allowed or uppercase (for your needs).*/
329 void makereverse(unsigned char * from, unsigned char * to, unsigned int cnt)
332 for(unsigned char * n=from; (*n) && ((*n)<cnt) && (n<from+cnt); n++)
336 /*so Bynets Unreal distribution stuff*/
337 bool loadtables(std::string filename, unsigned char ** tables, unsigned char cnt, char faillimit)
339 std::ifstream ifs(ServerInstance->Config->Paths.PrependConfig(filename).c_str());
342 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "loadtables() called for missing file: %s", filename.c_str());
346 for (unsigned char n=0; n< cnt; n++)
348 memset(tables[n], 0, 256);
351 memcpy(m_lower, rfc_case_insensitive_map, 256);
353 for (unsigned char n = 0; n < cnt; n++)
355 if (loadtable(ifs, tables[n], 255) && (n < faillimit))
357 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "loadtables() called for illegal file: %s (line %d)", filename.c_str(), n+1);
362 makereverse(m_additional, m_reverse_additional, sizeof(m_additional));
366 unsigned char symtoi(const char *t,unsigned char base)
367 /* base = 16 for hexadecimal, 10 for decimal, 8 for octal ;) */
369 unsigned char tmp = 0, current;
370 while ((*t) && (*t !=' ') && (*t != 13) && (*t != 10) && (*t != ','))
373 current = ascii_case_insensitive_map[(unsigned char)*t];
375 current = current - 'a' + 10;
377 current = current - '0';
384 int loadtable(std::ifstream &ifs , unsigned char *chartable, unsigned int maxindex)
392 buf.erase(buf.find_last_not_of("\n") + 1);
394 if (buf[0] == '.') /* simple plain-text string after dot */
398 if (i > (maxindex + 1))
401 memcpy(chartable, buf.c_str() + 1, i);
405 const char * p = buf.c_str();
414 if (*p != '\'') /* decimal or hexadecimal char code */
419 /* hex with the leading "0x" */
420 chartable[i] = symtoi(p + 2, 16);
422 chartable[i] = symtoi(p + 1, 8);
427 chartable[i] = symtoi(p + 1, 16);
429 else /* decimal form */
431 chartable[i] = symtoi(p, 10);
434 else /* plain-text char between '' */
436 if (*(p + 1) == '\\')
438 chartable[i] = *(p + 2);
442 chartable[i] = *(p + 1);
446 while (*p && (*p != ',') && (*p != ' ') && (*p != 13) && (*p != 10))
448 while (*p && ((*p == ',') || (*p == ' ') || (*p == 13) || (*p == 10)))
457 MODULE_INIT(ModuleNationalChars)