2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2020 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2014 Googolplexed <googol@googolplexed.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 typedef std::bitset<UCHAR_MAX + 1> AllowedChars;
27 // The characters which are allowed in nicknames.
28 AllowedChars allowedchars;
30 // The characters which are allowed at the front of a nickname.
31 AllowedChars allowedfrontchars;
33 // The mapping of lower case characters to upper case characters.
34 unsigned char casemap[UCHAR_MAX];
36 bool IsValidNick(const std::string& nick)
38 if (nick.empty() || nick.length() > ServerInstance->Config->Limits.NickMax)
41 for (std::string::const_iterator iter = nick.begin(); iter != nick.end(); ++iter)
43 unsigned char chr = static_cast<unsigned char>(*iter);
45 // Check that the character is allowed at the front of the nick.
46 if (iter == nick.begin() && !allowedfrontchars[chr])
49 // Check that the character is allowed in the nick.
50 if (!allowedchars[chr])
62 // The character map which was set before this module was loaded.
63 const unsigned char* origcasemap;
65 // The name of the character map which was set before this module was loaded.
66 const std::string origcasemapname;
68 // The IsNick handler which was set before this module was loaded.
69 const TR1NS::function<bool(const std::string&)> origisnick;
72 void RehashHashmap(T& hashmap)
74 T newhash(hashmap.bucket_count());
75 for (typename T::const_iterator i = hashmap.begin(); i != hashmap.end(); ++i)
76 newhash.insert(std::make_pair(i->first, i->second));
77 hashmap.swap(newhash);
80 void CheckInvalidNick()
82 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
83 for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); ++iter)
85 LocalUser* user = *iter;
86 if (user->nick != user->uuid && !ServerInstance->IsNick(user->nick))
87 user->ChangeNick(user->uuid);
91 void CheckRehash(unsigned char* prevmap)
93 if (!memcmp(prevmap, national_case_insensitive_map, UCHAR_MAX))
96 RehashHashmap(ServerInstance->Users.clientlist);
97 RehashHashmap(ServerInstance->Users.uuidlist);
98 RehashHashmap(ServerInstance->chanlist);
103 : origcasemap(national_case_insensitive_map)
104 , origcasemapname(ServerInstance->Config->CaseMapping)
105 , origisnick(ServerInstance->IsNick)
111 ServerInstance->IsNick = origisnick;
114 ServerInstance->Config->CaseMapping = origcasemapname;
115 national_case_insensitive_map = origcasemap;
116 CheckRehash(casemap);
118 ServerInstance->ISupport.Build();
121 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
123 const std::string name = ServerInstance->Config->ConfValue("codepage")->getString("name");
125 throw ModuleException("<codepage:name> is a required field!");
127 AllowedChars newallowedchars;
128 AllowedChars newallowedfrontchars;
129 ConfigTagList cpchars = ServerInstance->Config->ConfTags("cpchars");
130 for (ConfigIter i = cpchars.first; i != cpchars.second; ++i)
132 ConfigTag* tag = i->second;
134 unsigned char begin = tag->getUInt("begin", tag->getUInt("index", 0), 1, UCHAR_MAX);
136 throw ModuleException("<cpchars> tag without index or begin specified at " + tag->getTagLocation());
138 unsigned char end = tag->getUInt("end", begin, 1, UCHAR_MAX);
140 throw ModuleException("<cpchars:begin> must be lower than <cpchars:end> at " + tag->getTagLocation());
142 bool front = tag->getBool("front", false);
143 for (unsigned short pos = begin; pos <= end; ++pos)
145 if (pos == '\n' || pos == '\r' || pos == ' ')
147 throw ModuleException(InspIRCd::Format("<cpchars> tag contains a forbidden character: %u at %s",
148 pos, tag->getTagLocation().c_str()));
151 if (front && (pos == ':' || isdigit(pos)))
153 throw ModuleException(InspIRCd::Format("<cpchars> tag contains a forbidden front character: %u at %s",
154 pos, tag->getTagLocation().c_str()));
157 newallowedchars.set(pos);
158 newallowedfrontchars.set(pos, front);
159 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Marked %u (%c) as allowed (front: %s)",
160 pos, pos, front ? "yes" : "no");
164 unsigned char newcasemap[UCHAR_MAX];
165 for (size_t i = 0; i < UCHAR_MAX; ++i)
167 ConfigTagList cpcase = ServerInstance->Config->ConfTags("cpcase");
168 for (ConfigIter i = cpcase.first; i != cpcase.second; ++i)
170 ConfigTag* tag = i->second;
172 unsigned char lower = tag->getUInt("lower", 0, 1, UCHAR_MAX);
174 throw ModuleException("<cpcase:lower> is required at " + tag->getTagLocation());
176 unsigned char upper = tag->getUInt("upper", 0, 1, UCHAR_MAX);
178 throw ModuleException("<cpcase:upper> is required at " + tag->getTagLocation());
180 newcasemap[upper] = lower;
181 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Marked %u (%c) as the lower case version of %u (%c)",
182 lower, lower, upper, upper);
185 std::swap(allowedchars, newallowedchars);
186 std::swap(allowedfrontchars, newallowedfrontchars);
187 std::swap(casemap, newcasemap);
189 ServerInstance->IsNick = &IsValidNick;
192 ServerInstance->Config->CaseMapping = name;
193 national_case_insensitive_map = casemap;
194 CheckRehash(newcasemap);
196 ServerInstance->ISupport.Build();
199 Version GetVersion() CXX11_OVERRIDE
201 std::stringstream linkdata;
203 linkdata << "front=";
204 for (size_t i = 0; i < allowedfrontchars.size(); ++i)
205 if (allowedfrontchars[i])
206 linkdata << static_cast<unsigned char>(i);
208 linkdata << "&middle=";
209 for (size_t i = 0; i < allowedchars.size(); ++i)
211 linkdata << static_cast<unsigned char>(i);
214 for (size_t i = 0; i < sizeof(casemap); ++i)
216 linkdata << static_cast<unsigned char>(i) << casemap[i] << ',';
218 return Version("Provides support for custom 8-bit codepages", VF_COMMON | VF_VENDOR, linkdata.str());
221 MODULE_INIT(ModuleCodepage)