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