2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2020 Sadie Powell <sadie@witchery.services>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 typedef std::bitset<UCHAR_MAX + 1> AllowedChars;
26 // The characters which are allowed in nicknames.
27 AllowedChars allowedchars;
29 // The characters which are allowed at the front of a nickname.
30 AllowedChars allowedfrontchars;
32 // The mapping of lower case characters to upper case characters.
33 unsigned char casemap[UCHAR_MAX];
35 bool IsValidNick(const std::string& nick)
37 if (nick.empty() || nick.length() > ServerInstance->Config->Limits.NickMax)
40 for (std::string::const_iterator iter = nick.begin(); iter != nick.end(); ++iter)
42 unsigned char chr = static_cast<unsigned char>(*iter);
44 // Check that the character is allowed at the front of the nick.
45 if (iter == nick.begin() && !allowedfrontchars[chr])
48 // Check that the character is allowed in the nick.
49 if (!allowedchars[chr])
61 // The character map which was set before this module was loaded.
62 const unsigned char* origcasemap;
64 // The name of the character map which was set before this module was loaded.
65 const std::string origcasemapname;
67 // The IsNick handler which was set before this module was loaded.
68 const TR1NS::function<bool(const std::string&)> origisnick;
71 void RehashHashmap(T& hashmap)
73 T newhash(hashmap.bucket_count());
74 for (typename T::const_iterator i = hashmap.begin(); i != hashmap.end(); ++i)
75 newhash.insert(std::make_pair(i->first, i->second));
76 hashmap.swap(newhash);
79 void CheckDuplicateNick()
81 insp::flat_set<std::string, irc::insensitive_swo> duplicates;
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 && !duplicates.insert(user->nick).second)
88 user->WriteNumeric(RPL_SAVENICK, user->uuid, "Your nickname is no longer available.");
89 user->ChangeNick(user->uuid);
94 void CheckInvalidNick()
96 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
97 for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); ++iter)
99 LocalUser* user = *iter;
100 if (user->nick != user->uuid && !ServerInstance->IsNick(user->nick))
102 user->WriteNumeric(RPL_SAVENICK, user->uuid, "Your nickname is no longer valid.");
103 user->ChangeNick(user->uuid);
108 void CheckRehash(unsigned char* prevmap)
110 if (!memcmp(prevmap, national_case_insensitive_map, UCHAR_MAX))
113 RehashHashmap(ServerInstance->Users.clientlist);
114 RehashHashmap(ServerInstance->Users.uuidlist);
115 RehashHashmap(ServerInstance->chanlist);
120 : origcasemap(national_case_insensitive_map)
121 , origcasemapname(ServerInstance->Config->CaseMapping)
122 , origisnick(ServerInstance->IsNick)
128 ServerInstance->IsNick = origisnick;
131 ServerInstance->Config->CaseMapping = origcasemapname;
132 national_case_insensitive_map = origcasemap;
133 CheckDuplicateNick();
134 CheckRehash(casemap);
136 ServerInstance->ISupport.Build();
139 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
141 const std::string name = ServerInstance->Config->ConfValue("codepage")->getString("name");
143 throw ModuleException("<codepage:name> is a required field!");
145 AllowedChars newallowedchars;
146 AllowedChars newallowedfrontchars;
147 ConfigTagList cpchars = ServerInstance->Config->ConfTags("cpchars");
148 for (ConfigIter i = cpchars.first; i != cpchars.second; ++i)
150 ConfigTag* tag = i->second;
152 unsigned char begin = tag->getUInt("begin", tag->getUInt("index", 0), 1, UCHAR_MAX);
154 throw ModuleException("<cpchars> tag without index or begin specified at " + tag->getTagLocation());
156 unsigned char end = tag->getUInt("end", begin, 1, UCHAR_MAX);
158 throw ModuleException("<cpchars:begin> must be lower than <cpchars:end> at " + tag->getTagLocation());
160 bool front = tag->getBool("front", false);
161 for (unsigned short pos = begin; pos <= end; ++pos)
163 if (pos == '\n' || pos == '\r' || pos == ' ')
165 throw ModuleException(InspIRCd::Format("<cpchars> tag contains a forbidden character: %u at %s",
166 pos, tag->getTagLocation().c_str()));
169 if (front && (pos == ':' || isdigit(pos)))
171 throw ModuleException(InspIRCd::Format("<cpchars> tag contains a forbidden front character: %u at %s",
172 pos, tag->getTagLocation().c_str()));
175 newallowedchars.set(pos);
176 newallowedfrontchars.set(pos, front);
177 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Marked %u (%c) as allowed (front: %s)",
178 pos, pos, front ? "yes" : "no");
182 unsigned char newcasemap[UCHAR_MAX];
183 for (size_t i = 0; i < UCHAR_MAX; ++i)
185 ConfigTagList cpcase = ServerInstance->Config->ConfTags("cpcase");
186 for (ConfigIter i = cpcase.first; i != cpcase.second; ++i)
188 ConfigTag* tag = i->second;
190 unsigned char lower = tag->getUInt("lower", 0, 1, UCHAR_MAX);
192 throw ModuleException("<cpcase:lower> is required at " + tag->getTagLocation());
194 unsigned char upper = tag->getUInt("upper", 0, 1, UCHAR_MAX);
196 throw ModuleException("<cpcase:upper> is required at " + tag->getTagLocation());
198 newcasemap[upper] = lower;
199 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Marked %u (%c) as the lower case version of %u (%c)",
200 lower, lower, upper, upper);
203 std::swap(allowedchars, newallowedchars);
204 std::swap(allowedfrontchars, newallowedfrontchars);
205 std::swap(casemap, newcasemap);
207 ServerInstance->IsNick = &IsValidNick;
210 ServerInstance->Config->CaseMapping = name;
211 national_case_insensitive_map = casemap;
212 CheckRehash(newcasemap);
214 ServerInstance->ISupport.Build();
217 Version GetVersion() CXX11_OVERRIDE
219 std::stringstream linkdata;
221 linkdata << "front=";
222 for (size_t i = 0; i < allowedfrontchars.size(); ++i)
223 if (allowedfrontchars[i])
224 linkdata << static_cast<unsigned char>(i);
226 linkdata << "&middle=";
227 for (size_t i = 0; i < allowedchars.size(); ++i)
229 linkdata << static_cast<unsigned char>(i);
232 for (size_t i = 0; i < sizeof(casemap); ++i)
234 linkdata << static_cast<unsigned char>(i) << casemap[i] << ',';
236 return Version("Allows the server administrator to define what characters are allowed in nicknames and how characters should be compared in a case insensitive way.", VF_COMMON | VF_VENDOR, linkdata.str());
239 MODULE_INIT(ModuleCodepage)