]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_channames.cpp
Add the override keyword in places that it is missing.
[user/henk/code/inspircd.git] / src / modules / m_channames.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
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 static std::bitset<256> allowedmap;
23
24 class NewIsChannelHandler : public HandlerBase1<bool, const std::string&>
25 {
26  public:
27         bool Call(const std::string&) CXX11_OVERRIDE;
28 };
29
30 bool NewIsChannelHandler::Call(const std::string& channame)
31 {
32         if (channame.empty() || channame.length() > ServerInstance->Config->Limits.ChanMax || channame[0] != '#')
33                 return false;
34
35         for (std::string::const_iterator c = channame.begin(); c != channame.end(); ++c)
36         {
37                 unsigned int i = *c & 0xFF;
38                 if (!allowedmap[i])
39                         return false;
40         }
41
42         return true;
43 }
44
45 class ModuleChannelNames : public Module
46 {
47         NewIsChannelHandler myhandler;
48         caller1<bool, const std::string&> rememberer;
49         bool badchan;
50         ChanModeReference permchannelmode;
51
52  public:
53         ModuleChannelNames()
54                 : rememberer(ServerInstance->IsChannel)
55                 , badchan(false)
56                 , permchannelmode(this, "permanent")
57         {
58         }
59
60         void init() CXX11_OVERRIDE
61         {
62                 ServerInstance->IsChannel = &myhandler;
63         }
64
65         void ValidateChans()
66         {
67                 Modes::ChangeList removepermchan;
68
69                 badchan = true;
70                 const chan_hash& chans = ServerInstance->GetChans();
71                 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); )
72                 {
73                         Channel* c = i->second;
74                         // Move iterator before we begin kicking
75                         ++i;
76                         if (ServerInstance->IsChannel(c->name))
77                                 continue; // The name of this channel is still valid
78
79                         if (c->IsModeSet(permchannelmode) && c->GetUserCounter())
80                         {
81                                 removepermchan.clear();
82                                 removepermchan.push_remove(*permchannelmode);
83                                 ServerInstance->Modes->Process(ServerInstance->FakeClient, c, NULL, removepermchan);
84                         }
85
86                         Channel::MemberMap& users = c->userlist;
87                         for (Channel::MemberMap::iterator j = users.begin(); j != users.end(); )
88                         {
89                                 if (IS_LOCAL(j->first))
90                                 {
91                                         // KickUser invalidates the iterator
92                                         Channel::MemberMap::iterator it = j++;
93                                         c->KickUser(ServerInstance->FakeClient, it, "Channel name no longer valid");
94                                 }
95                                 else
96                                         ++j;
97                         }
98                 }
99                 badchan = false;
100         }
101
102         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
103         {
104                 ConfigTag* tag = ServerInstance->Config->ConfValue("channames");
105                 std::string denyToken = tag->getString("denyrange");
106                 std::string allowToken = tag->getString("allowrange");
107
108                 if (!denyToken.compare(0, 2, "0-"))
109                         denyToken[0] = '1';
110                 if (!allowToken.compare(0, 2, "0-"))
111                         allowToken[0] = '1';
112
113                 allowedmap.set();
114
115                 irc::portparser denyrange(denyToken, false);
116                 int denyno = -1;
117                 while (0 != (denyno = denyrange.GetToken()))
118                         allowedmap[denyno & 0xFF] = false;
119
120                 irc::portparser allowrange(allowToken, false);
121                 int allowno = -1;
122                 while (0 != (allowno = allowrange.GetToken()))
123                         allowedmap[allowno & 0xFF] = true;
124
125                 allowedmap[0x07] = false; // BEL
126                 allowedmap[0x20] = false; // ' '
127                 allowedmap[0x2C] = false; // ','
128
129                 ValidateChans();
130         }
131
132         void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except_list) CXX11_OVERRIDE
133         {
134                 if (badchan)
135                 {
136                         const Channel::MemberMap& users = memb->chan->GetUsers();
137                         for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
138                                 if (i->first != memb->user)
139                                         except_list.insert(i->first);
140                 }
141         }
142
143         CullResult cull() CXX11_OVERRIDE
144         {
145                 ServerInstance->IsChannel = rememberer;
146                 ValidateChans();
147                 return Module::cull();
148         }
149
150         Version GetVersion() CXX11_OVERRIDE
151         {
152                 return Version("Implements config tags which allow changing characters allowed in channel names", VF_VENDOR);
153         }
154 };
155
156 MODULE_INIT(ModuleChannelNames)