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