2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5 * Copyright (C) 2013, 2015, 2017, 2020 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 /*,m_reverse_additionalUp[256];*/
44 static unsigned char m_reverse_additional[256],m_additionalMB[256],m_additionalUtf8[256],m_additionalUtf8range[256],m_additionalUtf8interval[256];
46 char utf8checkrest(unsigned char * mb, unsigned char cnt)
48 for (unsigned char * tmp=mb; tmp<mb+cnt; tmp++)
50 /* & is faster! -- Phoenix (char & b11000000 == b10000000) */
51 if ((*tmp & 192) != 128)
58 char utf8size(unsigned char * mb)
64 if ((*mb & 224) == 192)
65 return utf8checkrest(mb + 1,1);
66 if ((*mb & 240) == 224)
67 return utf8checkrest(mb + 1,2);
68 if ((*mb & 248) == 240)
69 return utf8checkrest(mb + 1,3);
74 /* Conditions added */
75 bool lwbNickHandler::Call(const std::string& nick)
77 if (nick.empty() || isdigit(nick[0]))
80 const char* n = nick.c_str();
82 for (const char* i = n; *i; i++, p++)
84 /* 1. Multibyte encodings support: */
85 /* 1.1. 16bit char. areas, e.g. chinese:*/
87 /* if current character is the last, we DO NOT check it against multibyte table */
88 /* if there are mbtable ranges, use ONLY them. No 8bit at all */
89 if (i[1] && m_additionalMB[0])
91 /* otherwise let's take a look at the current character and the following one */
93 for(unsigned char * mb = m_additionalMB; (*mb) && (mb < m_additionalMB + sizeof(m_additionalMB)); mb += 4)
95 if ( (i[0] >= mb[0]) && (i[0] <= mb[1]) && (i[1] >= mb[2]) && (i[1] <= mb[3]) )
97 /* multibyte range character found */
108 /* there are ranges, but incorrect char (8bit?) given, sorry */
112 /* 2. 8bit character support */
113 if (((*i >= 'A') && (*i <= '}')) || m_reverse_additional[(unsigned char)*i])
114 /* "A"-"}" can occur anywhere in a nickname */
117 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
118 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
121 /* 3.1. Check against a simple UTF-8 characters enumeration */
122 int cursize, cursize2, ncursize = utf8size((unsigned char *)i);
123 /* do check only if current multibyte character is valid UTF-8 only */
127 for (unsigned char * mb = m_additionalUtf8; (utf8size(mb) != -1) && (mb < m_additionalUtf8 + sizeof(m_additionalUtf8)); mb += cursize)
129 cursize = utf8size(mb);
130 /* Size differs? Pick the next! */
131 if (cursize != ncursize)
134 if (!strncmp(i, (char *)mb, cursize))
145 /* 3.2. Check against an UTF-8 ranges: <start character> and <length of the range>. */
147 for (unsigned char * mb = m_additionalUtf8range; (utf8size(mb) != -1) && (mb < m_additionalUtf8range + sizeof(m_additionalUtf8range)); mb += cursize + 1)
149 cursize = utf8size(mb);
150 /* Size differs (or lengthbyte is zero)? Pick the next! */
151 if ((cursize != ncursize) || (!mb[cursize]))
154 unsigned char uright[5] = {0,0,0,0,0}, range = mb[cursize] - 1;
155 strncpy((char* ) uright, (char *) mb, cursize);
157 for (int temp = cursize - 1; (temp >= 0) && range; --temp)
159 /* all but the first char are 64-based */
162 char part64 = range & 63; /* i.e. % 64 */
163 /* handle carrying over */
164 if (uright[temp] + part64 - 1 > 191)
169 uright[temp] += part64;
170 range >>= 6; /* divide it on a 64 */
172 /* the first char of UTF-8 doesn't follow the rule */
175 uright[temp] += range;
179 if ((strncmp(i, (char *) mb, cursize) >= 0) && (strncmp(i, (char *) uright, cursize) <= 0))
190 /* 3.3. Check against an UTF-8 intervals: <start character> and <end character>. */
192 for (unsigned char * mb = m_additionalUtf8interval; (utf8size(mb) != -1) && (utf8size(mb+utf8size(mb)) != -1)
193 && (mb < m_additionalUtf8interval + sizeof(m_additionalUtf8interval)); mb += (cursize+cursize2) )
195 cursize = utf8size(mb);
196 cursize2= utf8size(mb+cursize);
198 int minlen = cursize > ncursize ? ncursize : cursize;
199 int minlen2 = cursize2 > ncursize ? ncursize : cursize2;
201 unsigned char* uright = mb + cursize;
203 if ((strncmp(i, (char *) mb, minlen) >= 0) && (strncmp(i, (char *) uright, minlen2) <= 0))
215 /* invalid character! abort */
219 /* too long? or not -- pointer arithmetic rocks */
220 return (p < ServerInstance->Config->Limits.NickMax);
224 class ModuleNationalChars : public Module
227 unsigned char m_additional[256], m_additionalUp[256], m_lower[256], m_upper[256];
228 TR1NS::function<bool(const std::string&)> rememberer;
230 const unsigned char * lowermap_rememberer;
231 std::string casemapping_rememberer;
232 unsigned char prev_map[256];
234 template <typename T>
235 void RehashHashmap(T& hashmap)
237 T newhash(hashmap.bucket_count());
238 for (typename T::const_iterator i = hashmap.begin(); i != hashmap.end(); ++i)
239 newhash.insert(std::make_pair(i->first, i->second));
240 hashmap.swap(newhash);
245 // See if anything changed
246 if (!memcmp(prev_map, national_case_insensitive_map, sizeof(prev_map)))
249 memcpy(prev_map, national_case_insensitive_map, sizeof(prev_map));
251 RehashHashmap(ServerInstance->Users.clientlist);
252 RehashHashmap(ServerInstance->Users.uuidlist);
253 RehashHashmap(ServerInstance->chanlist);
257 ModuleNationalChars()
258 : rememberer(ServerInstance->IsNick)
259 , lowermap_rememberer(national_case_insensitive_map)
260 , casemapping_rememberer(ServerInstance->Config->CaseMapping)
262 memcpy(prev_map, national_case_insensitive_map, sizeof(prev_map));
265 void init() CXX11_OVERRIDE
267 memcpy(m_lower, rfc_case_insensitive_map, 256);
268 national_case_insensitive_map = m_lower;
270 ServerInstance->IsNick = &lwbNickHandler::Call;
273 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
275 ConfigTag* tag = ServerInstance->Config->ConfValue("nationalchars");
276 charset = tag->getString("file");
277 std::string casemapping = tag->getString("casemapping", FileSystem::GetFileName(charset), 1);
278 if (casemapping.find(' ') != std::string::npos)
279 throw ModuleException("<nationalchars:casemapping> must not contain any spaces!");
280 ServerInstance->Config->CaseMapping = casemapping;
282 if (!FileSystem::StartsWithWindowsDriveLetter(charset))
283 charset.insert(0, "./locales/");
285 if(charset[0] != '/')
286 charset.insert(0, "../locales/");
288 unsigned char * tables[8] = { m_additional, m_additionalMB, m_additionalUp, m_lower, m_upper, m_additionalUtf8, m_additionalUtf8range, m_additionalUtf8interval };
289 if (!loadtables(charset, tables, 8, 5))
290 throw ModuleException("The locale file failed to load. Check your log file for more information.");
291 forcequit = tag->getBool("forcequit");
292 CheckForceQuit("National character set changed");
296 void CheckForceQuit(const char * message)
301 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
302 for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); )
304 /* Fix by Brain: Dont quit UID users */
305 // Quitting the user removes it from the list
308 if (!isdigit(n->nick[0]) && !ServerInstance->IsNick(n->nick))
309 ServerInstance->Users->QuitUser(n, message);
313 ~ModuleNationalChars()
315 ServerInstance->IsNick = rememberer;
316 national_case_insensitive_map = lowermap_rememberer;
317 ServerInstance->Config->CaseMapping = casemapping_rememberer;
318 // The core rebuilds ISupport on module unload, but before the dtor.
319 ServerInstance->ISupport.Build();
320 CheckForceQuit("National characters module unloaded");
324 Version GetVersion() CXX11_OVERRIDE
326 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);
329 /*make an array to check against it 8bit characters a bit faster. Whether allowed or uppercase (for your needs).*/
330 void makereverse(unsigned char * from, unsigned char * to, unsigned int cnt)
333 for(unsigned char * n=from; (*n) && ((*n)<cnt) && (n<from+cnt); n++)
337 /*so Bynets Unreal distribution stuff*/
338 bool loadtables(std::string filename, unsigned char ** tables, unsigned char cnt, char faillimit)
340 std::ifstream ifs(ServerInstance->Config->Paths.PrependConfig(filename).c_str());
343 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "loadtables() called for missing file: %s", filename.c_str());
347 for (unsigned char n=0; n< cnt; n++)
349 memset(tables[n], 0, 256);
352 memcpy(m_lower, rfc_case_insensitive_map, 256);
354 for (unsigned char n = 0; n < cnt; n++)
356 if (loadtable(ifs, tables[n], 255) && (n < faillimit))
358 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "loadtables() called for illegal file: %s (line %d)", filename.c_str(), n+1);
363 makereverse(m_additional, m_reverse_additional, sizeof(m_additional));
367 unsigned char symtoi(const char *t,unsigned char base)
368 /* base = 16 for hexadecimal, 10 for decimal, 8 for octal ;) */
370 unsigned char tmp = 0, current;
371 while ((*t) && (*t !=' ') && (*t != 13) && (*t != 10) && (*t != ','))
374 current = ascii_case_insensitive_map[(unsigned char)*t];
376 current = current - 'a' + 10;
378 current = current - '0';
385 int loadtable(std::ifstream &ifs , unsigned char *chartable, unsigned int maxindex)
393 buf.erase(buf.find_last_not_of("\n") + 1);
395 if (buf[0] == '.') /* simple plain-text string after dot */
399 if (i > (maxindex + 1))
402 memcpy(chartable, buf.c_str() + 1, i);
406 const char * p = buf.c_str();
415 if (*p != '\'') /* decimal or hexadecimal char code */
420 /* hex with the leading "0x" */
421 chartable[i] = symtoi(p + 2, 16);
423 chartable[i] = symtoi(p + 1, 8);
428 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)