]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_channames.cpp
Replace copyright headers with headers granting specific authors copyright
[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 char*, size_t>
27 {
28  public:
29         NewIsChannelHandler() { }
30         virtual ~NewIsChannelHandler() { }
31         virtual bool Call(const char*, size_t);
32 };
33
34 bool NewIsChannelHandler::Call(const char* c, size_t max)
35 {
36                 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
37                 if (!c || *c++ != '#')
38                         return false;
39
40                 while (*c && --max)
41                 {
42                         unsigned int i = *c++ & 0xFF;
43                         if (!allowedmap[i])
44                                 return false;
45                 }
46                 // a name of exactly max length will have max = 1 here; the null does not trigger --max
47                 return max;
48 }
49
50 class ModuleChannelNames : public Module
51 {
52  private:
53         NewIsChannelHandler myhandler;
54         caller2<bool, const char*, size_t> rememberer;
55         bool badchan;
56
57  public:
58         ModuleChannelNames() : rememberer(ServerInstance->IsChannel)
59         {
60                 ServerInstance->IsChannel = &myhandler;
61                 badchan = false;
62                 Implementation eventlist[] = { I_OnRehash, I_OnUserKick };
63                 ServerInstance->Modules->Attach(eventlist, this, 2);
64                 OnRehash(NULL);
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.c_str(), MAXBUF))
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('P') && c->GetUserCounter())
81                         {
82                                 std::vector<std::string> modes;
83                                 modes.push_back(c->name);
84                                 modes.push_back("-P");
85
86                                 ServerInstance->SendGlobalMode(modes, ServerInstance->FakeClient);
87                         }
88                         const UserMembList* users = c->GetUsers();
89                         for(UserMembCIter j = users->begin(); j != users->end(); ++j)
90                                 if (IS_LOCAL(j->first))
91                                         c->KickUser(ServerInstance->FakeClient, j->first, "Channel name no longer valid");
92                 }
93                 badchan = false;
94         }
95
96         virtual void OnRehash(User* user)
97         {
98                 ConfigReader Conf;
99                 std::string denyToken = Conf.ReadValue("channames", "denyrange", 0);
100                 std::string allowToken = Conf.ReadValue("channames", "allowrange", 0);
101                 allowedmap.set();
102
103                 irc::portparser denyrange(denyToken, false);
104                 int denyno = -1;
105                 while (0 != (denyno = denyrange.GetToken()))
106                         allowedmap[denyno & 0xFF] = false;
107
108                 irc::portparser allowrange(allowToken, false);
109                 int allowno = -1;
110                 while (0 != (allowno = allowrange.GetToken()))
111                         allowedmap[allowno & 0xFF] = true;
112
113                 allowedmap[0x07] = false; // BEL
114                 allowedmap[0x20] = false; // ' '
115                 allowedmap[0x2C] = false; // ','
116
117                 ValidateChans();
118         }
119
120         virtual void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except_list)
121         {
122                 if (badchan)
123                 {
124                         const UserMembList* users = memb->chan->GetUsers();
125                         for(UserMembCIter i = users->begin(); i != users->end(); i++)
126                                 if (i->first != memb->user)
127                                         except_list.insert(i->first);
128                 }
129         }
130
131         virtual ~ModuleChannelNames()
132         {
133                 ServerInstance->IsChannel = rememberer;
134                 ValidateChans();
135         }
136
137         virtual Version GetVersion()
138         {
139                 return Version("Implements config tags which allow changing characters allowed in channel names", VF_VENDOR);
140         }
141 };
142
143 MODULE_INIT(ModuleChannelNames)