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