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 DEFINE_HANDLER2(lwbNickHandler, bool, const char*, size_t);
26 /*,m_reverse_additionalUp[256];*/
27 static unsigned char m_reverse_additional[256],m_additionalMB[256],m_additionalUtf8[256],m_additionalUtf8range[256];
29 char utf8checkrest(unsigned char * mb, unsigned char cnt)
31 for (unsigned char * tmp=mb; tmp<mb+cnt; tmp++)
33 if ((*tmp<128)||(*tmp>191))
40 char utf8size(unsigned char * mb)
47 return utf8checkrest(mb+1,1);
49 return utf8checkrest(mb+1,2);
51 return utf8checkrest(mb+1,3);
56 /* Conditions added */
57 bool lwbNickHandler::Call(const char* n, size_t max)
63 for (const char* i = n; *i; i++, p++)
65 /* 1. Multibyte encodings support: */
66 /* 1.1. 16bit char. areas, e.g. chinese:*/
68 /* if current character is the last, we DO NOT check it against multibyte table */
69 /* if there are mbtable ranges, use ONLY them. No 8bit at all */
70 if (i[1] && m_additionalMB[0])
72 /* otherwise let's take a look at the current character and the following one */
74 for(unsigned char * mb=m_additionalMB; (*mb) && (mb<m_additionalMB+sizeof(m_additionalMB)); mb+=4)
76 if ( (i[0]>=mb[0]) && (i[0]<=mb[1]) && (i[1]>=mb[2]) && (i[1]<=mb[3]) )
78 /* multibyte range character found */
88 /* there are ranges, but incorrect char (8bit?) given, sorry */
92 /* 2. 8bit character support */
94 if ( ((*i >= 'A') && (*i <= '}'))
95 || m_reverse_additional[(unsigned char)*i])
97 /* "A"-"}" can occur anywhere in a nickname */
101 if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
103 /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
107 /* 3.1. Check against a simple UTF-8 characters enumeration */
108 int cursize,ncursize; /*size of a current character*/
109 ncursize=utf8size((unsigned char *)i);
110 /* do check only if current multibyte character is valid UTF-8 only */
114 for(unsigned char * mb=m_additionalUtf8;
115 (utf8size(mb)!=-1) && (mb<m_additionalUtf8+sizeof(m_additionalUtf8));
118 cursize=utf8size(mb);
119 /* Size differs? Pick the next! */
120 if (cursize!=ncursize)
122 if (!strncmp(i,(char *)mb,cursize))
132 /* 3.2. Check against an UTF-8 ranges: <start character> and <lenght of the range>.
133 Also char. is to be checked if it is a valid UTF-8 one */
136 for(unsigned char * mb=m_additionalUtf8range;
137 (utf8size(mb)!=-1) && (mb<m_additionalUtf8range+sizeof(m_additionalUtf8range));
140 cursize=utf8size(mb);
141 /* Size differs? Pick the next! */
142 if ((cursize!=ncursize)||(!mb[cursize]))
145 unsigned char uright[5]={0,0,0,0,0};
146 strncpy((char *)uright,(char *)mb, cursize);
148 if((uright[cursize-1]+mb[cursize]-1>0xff) && (cursize!=1))
150 uright[cursize-2]+=1;
152 uright[cursize-1]=(uright[cursize-1]+mb[cursize]-1) % 0x100;
154 if ((strncmp(i,(char *)mb,cursize)>=0) && (strncmp(i,(char *)uright,cursize)<=0))
166 /* invalid character! abort */
170 /* too long? or not -- pointer arithmetic rocks */
175 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;
186 ModuleNationalChars(InspIRCd* Me)
189 rememberer=(caller2<bool, const char*, size_t> *) malloc(sizeof(rememberer));
190 lowermap_rememberer=national_case_insensitive_map;
191 memcpy(m_lower,rfc_case_insensitive_map,256);
192 national_case_insensitive_map=m_lower;
195 *rememberer=ServerInstance->IsNick;
196 myhandler=new lwbNickHandler(ServerInstance);
197 ServerInstance->IsNick=myhandler;
198 Implementation eventlist[] = { I_OnRehash, I_On005Numeric };
199 ServerInstance->Modules->Attach(eventlist, this, 2);
203 virtual void On005Numeric(std::string &output)
205 std::string tmp(casemapping);
206 tmp.insert(0,"CASEMAPPING=");
207 SearchAndReplace(output,"CASEMAPPING=rfc1459",tmp);
210 virtual void OnRehash(User* user, const std::string ¶meter)
212 ConfigReader* conf = new ConfigReader(ServerInstance);
213 charset = conf->ReadValue("nationalchars", "file", 0);
214 casemapping = conf->ReadValue("nationalchars", "casemapping", charset, 0, false);
215 charset.insert(0,"../locales/");
216 unsigned char * tables[7]=
218 m_additional,m_additionalMB,m_additionalUp,m_lower,m_upper,
219 m_additionalUtf8,m_additionalUtf8range
221 loadtables(charset,tables,7,5);
222 forcequit = conf->ReadFlag("nationalchars", "forcequit", 0);
223 CheckForceQuit("National character set changed");
227 void CheckForceQuit(const char * message)
232 std::vector<User*> purge;
233 std::vector<User*>::iterator iter;
235 for (iter=ServerInstance->Users->local_users.begin();iter!=ServerInstance->Users->local_users.end();++iter)
237 if (!ServerInstance->IsNick((*iter)->nick.c_str(), ServerInstance->Config->Limits.NickMax))
238 purge.push_back(*iter);
240 for (iter=purge.begin();iter!=purge.end();++iter)
242 ServerInstance->Users->QuitUser((*iter), message);
245 virtual ~ModuleNationalChars()
248 ServerInstance->IsNick= *rememberer;
250 national_case_insensitive_map=lowermap_rememberer;
251 CheckForceQuit("National characters module unloaded");
254 virtual Version GetVersion()
256 return Version("$Id: m_nationalchars.cpp 0 2008-12-15 14:24:12SAMT phoenix $",VF_COMMON,API_VERSION);
259 /*make an array to check against it 8bit characters a bit faster. Whether allowed or uppercase (for your needs).*/
261 void makereverse(unsigned char * from, unsigned char * to, unsigned int cnt)
264 for(unsigned char * n=from; (*n) && ((*n)<cnt) && (n<from+cnt); n++)
270 /*so Bynets Unreal distribution stuff*/
271 void loadtables(std::string filename, unsigned char ** tables, unsigned char cnt, char faillimit)
273 std::ifstream ifs(filename.c_str());
276 ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() called for missing file: %s", filename.c_str());
283 memset(tables[n], 0, 256);
286 memcpy(m_lower,rfc_case_insensitive_map,256);
290 if (loadtable(ifs, tables[n], 255) && (n<faillimit))
292 ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() called for illegal file: %s (line %d)", filename.c_str(), n+1);
296 /* ServerInstance->Logs->Log("m_nationalchars",DEFAULT,"loadtables() : %s", ((char *)national_case_insensitive_map)+1);*/
298 makereverse(m_additional, m_reverse_additional, sizeof(m_additional));
299 /* Do you need faster access to additional 8bit uppercase table? No? Oh, sorry :( Let's comment this out */
300 /* makereverse(m_additionalUp, m_reverse_additionalUp, sizeof(m_additional)); */
303 unsigned char symtoi(const char *t,unsigned char base)
304 /* base = 16 for hexadecimal, 10 for decimal, 8 for octal ;) */
306 unsigned char tmp=0,current;
307 while ((*t)&&(*t!=' ')&&(*t!=13)&&(*t!=10)&&(*t!=','))
310 current=ascii_case_insensitive_map[(unsigned char)*t];
312 current=current-'a'+10;
321 int loadtable(std::ifstream &ifs , unsigned char *chartable, unsigned int maxindex)
329 buf.erase(buf.find_last_not_of("\n")+1);
331 if (buf[0]=='.') /* simple plain-text string after dot */
334 if (i>(maxindex+1)) i=maxindex+1;
335 memcpy(chartable,buf.c_str()+1,i);
339 const char * p=buf.c_str();
348 if (*p!='\'')/* decimal or hexadecimal char code */
353 /* hex with the leading "0x" */
354 chartable[i] = symtoi(p+2,16);
356 chartable[i] = symtoi(p+1,8);
361 chartable[i] = symtoi(p+1,16);
362 }else /* decimal form */
364 chartable[i] = symtoi(p,10);
366 } else /* plain-text char between '' */
370 chartable[i] = *(p+2);
374 chartable[i] = *(p+1);
379 while (*p&& (*p!=',')&&(*p!=' ')&&(*p!=13)&&(*p!=10) ) p++;
380 while (*p&&((*p==',')||(*p==' ')||(*p==13)||(*p==10))) p++;
392 MODULE_INIT(ModuleNationalChars)