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