]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_codepage.cpp
Update copyright headers.
[user/henk/code/inspircd.git] / src / modules / m_codepage.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2020 Sadie Powell <sadie@witchery.services>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19
20 #include "inspircd.h"
21
22 typedef std::bitset<UCHAR_MAX + 1> AllowedChars;
23
24 namespace
25 {
26         // The characters which are allowed in nicknames.
27         AllowedChars allowedchars;
28
29         // The characters which are allowed at the front of a nickname.
30         AllowedChars allowedfrontchars;
31
32         // The mapping of lower case characters to upper case characters.
33         unsigned char casemap[UCHAR_MAX];
34
35         bool IsValidNick(const std::string& nick)
36         {
37                 if (nick.empty() || nick.length() > ServerInstance->Config->Limits.NickMax)
38                         return false;
39
40                 for (std::string::const_iterator iter = nick.begin(); iter != nick.end(); ++iter)
41                 {
42                         unsigned char chr = static_cast<unsigned char>(*iter);
43
44                         // Check that the character is allowed at the front of the nick.
45                         if (iter == nick.begin() && !allowedfrontchars[chr])
46                                 return false;
47
48                         // Check that the character is allowed in the nick.
49                         if (!allowedchars[chr])
50                                 return false;
51                 }
52
53                 return true;
54         }
55 }
56
57 class ModuleCodepage
58         : public Module
59 {
60  private:
61         // The character map which was set before this module was loaded.
62         const unsigned char* origcasemap;
63
64         // The name of the character map which was set before this module was loaded.
65         const std::string origcasemapname;
66
67         // The IsNick handler which was set before this module was loaded.
68         const TR1NS::function<bool(const std::string&)> origisnick;
69
70         template <typename T>
71         void RehashHashmap(T& hashmap)
72         {
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);
77         }
78
79         void CheckInvalidNick()
80         {
81                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
82                 for (UserManager::LocalList::const_iterator iter = list.begin(); iter != list.end(); ++iter)
83                 {
84                         LocalUser* user = *iter;
85                         if (user->nick != user->uuid && !ServerInstance->IsNick(user->nick))
86                                 user->ChangeNick(user->uuid);
87                 }
88         }
89
90         void CheckRehash(unsigned char* prevmap)
91         {
92                 if (!memcmp(prevmap, national_case_insensitive_map, UCHAR_MAX))
93                         return;
94
95                 RehashHashmap(ServerInstance->Users.clientlist);
96                 RehashHashmap(ServerInstance->Users.uuidlist);
97                 RehashHashmap(ServerInstance->chanlist);
98         }
99
100  public:
101         ModuleCodepage()
102                 : origcasemap(national_case_insensitive_map)
103                 , origcasemapname(ServerInstance->Config->CaseMapping)
104                 , origisnick(ServerInstance->IsNick)
105         {
106         }
107
108         ~ModuleCodepage()
109         {
110                 ServerInstance->IsNick = origisnick;
111                 CheckInvalidNick();
112
113                 ServerInstance->Config->CaseMapping = origcasemapname;
114                 national_case_insensitive_map = origcasemap;
115                 CheckRehash(casemap);
116
117                 ServerInstance->ISupport.Build();
118         }
119
120         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
121         {
122                 const std::string name = ServerInstance->Config->ConfValue("codepage")->getString("name");
123                 if (name.empty())
124                         throw ModuleException("<codepage:name> is a required field!");
125
126                 AllowedChars newallowedchars;
127                 AllowedChars newallowedfrontchars;
128                 ConfigTagList cpchars = ServerInstance->Config->ConfTags("cpchars");
129                 for (ConfigIter i = cpchars.first; i != cpchars.second; ++i)
130                 {
131                         ConfigTag* tag = i->second;
132
133                         unsigned char begin = tag->getUInt("begin", tag->getUInt("index", 0), 1, UCHAR_MAX);
134                         if (!begin)
135                                 throw ModuleException("<cpchars> tag without index or begin specified at " + tag->getTagLocation());
136
137                         unsigned char end = tag->getUInt("end", begin, 1, UCHAR_MAX);
138                         if (begin > end)
139                                 throw ModuleException("<cpchars:begin> must be lower than <cpchars:end> at " + tag->getTagLocation());
140
141                         bool front = tag->getBool("front", false);
142                         for (unsigned short pos = begin; pos <= end; ++pos)
143                         {
144                                 if (pos == '\n' || pos == '\r' || pos == ' ')
145                                 {
146                                         throw ModuleException(InspIRCd::Format("<cpchars> tag contains a forbidden character: %u at %s",
147                                                 pos, tag->getTagLocation().c_str()));
148                                 }
149
150                                 if (front && (pos == ':' || isdigit(pos)))
151                                 {
152                                         throw ModuleException(InspIRCd::Format("<cpchars> tag contains a forbidden front character: %u at %s",
153                                                 pos, tag->getTagLocation().c_str()));
154                                 }
155
156                                 newallowedchars.set(pos);
157                                 newallowedfrontchars.set(pos, front);
158                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Marked %u (%c) as allowed (front: %s)",
159                                         pos, pos, front ? "yes" : "no");
160                         }
161                 }
162
163                 unsigned char newcasemap[UCHAR_MAX];
164                 for (size_t i = 0; i < UCHAR_MAX; ++i)
165                         newcasemap[i] = i;
166                 ConfigTagList cpcase = ServerInstance->Config->ConfTags("cpcase");
167                 for (ConfigIter i = cpcase.first; i != cpcase.second; ++i)
168                 {
169                         ConfigTag* tag = i->second;
170
171                         unsigned char lower = tag->getUInt("lower", 0, 1, UCHAR_MAX);
172                         if (!lower)
173                                 throw ModuleException("<cpcase:lower> is required at " + tag->getTagLocation());
174
175                         unsigned char upper = tag->getUInt("upper", 0, 1, UCHAR_MAX);
176                         if (!upper)
177                                 throw ModuleException("<cpcase:upper> is required at " + tag->getTagLocation());
178
179                         newcasemap[upper] = lower;
180                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Marked %u (%c) as the lower case version of %u (%c)",
181                                 lower, lower, upper, upper);
182                 }
183
184                 std::swap(allowedchars, newallowedchars);
185                 std::swap(allowedfrontchars, newallowedfrontchars);
186                 std::swap(casemap, newcasemap);
187
188                 ServerInstance->IsNick = &IsValidNick;
189                 CheckInvalidNick();
190
191                 ServerInstance->Config->CaseMapping = name;
192                 national_case_insensitive_map = casemap;
193                 CheckRehash(newcasemap);
194
195                 ServerInstance->ISupport.Build();
196         }
197
198         Version GetVersion() CXX11_OVERRIDE
199         {
200                 std::stringstream linkdata;
201
202                 linkdata << "front=";
203                 for (size_t i = 0; i < allowedfrontchars.size(); ++i)
204                         if (allowedfrontchars[i])
205                                 linkdata << static_cast<unsigned char>(i);
206
207                 linkdata << "&middle=";
208                 for (size_t i = 0; i < allowedchars.size(); ++i)
209                         if (allowedchars[i])
210                                 linkdata << static_cast<unsigned char>(i);
211
212                 linkdata << "&map=";
213                 for (size_t i = 0; i < sizeof(casemap); ++i)
214                         if (casemap[i] != i)
215                                 linkdata << static_cast<unsigned char>(i) << casemap[i] << ',';
216
217                 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());
218         }
219 };
220 MODULE_INIT(ModuleCodepage)