]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_channames.cpp
Fix negative array access in channame
[user/henk/code/inspircd.git] / src / modules / m_channames.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Implements config tags which allow changing characters allowed in channel names */
17
18 static std::bitset<256> allowedmap;
19
20 class NewIsChannelHandler : public HandlerBase2<bool, const char*, size_t>
21 {
22  public:
23         NewIsChannelHandler() { }
24         virtual ~NewIsChannelHandler() { }
25         virtual bool Call(const char*, size_t);
26 };
27
28 bool NewIsChannelHandler::Call(const char* c, size_t max)
29 {
30                 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
31                 if (!c || *c++ != '#')
32                         return false;
33
34                 while (*c && --max)
35                 {
36                         unsigned int i = *c++ & 0xFF;
37                         if (!allowedmap[i])
38                                 return false;
39                 }
40                 // a name of exactly max length will have max = 1 here; the null does not trigger --max
41                 return max;
42 }
43
44 class ModuleChannelNames : public Module
45 {
46  private:
47         NewIsChannelHandler myhandler;
48         caller2<bool, const char*, size_t> rememberer;
49         bool badchan;
50
51  public:
52         ModuleChannelNames() : rememberer(ServerInstance->IsChannel)
53         {
54                 ServerInstance->IsChannel = &myhandler;
55                 badchan = false;
56                 Implementation eventlist[] = { I_OnRehash, I_OnUserKick };
57                 ServerInstance->Modules->Attach(eventlist, this, 2);
58                 OnRehash(NULL);
59         }
60
61         void ValidateChans()
62         {
63                 badchan = true;
64                 std::vector<Channel*> chanvec;
65                 for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); ++i)
66                 {
67                         if (!ServerInstance->IsChannel(i->second->name.c_str(), MAXBUF))
68                                 chanvec.push_back(i->second);
69                 }
70                 std::vector<Channel*>::reverse_iterator c2 = chanvec.rbegin();
71                 while (c2 != chanvec.rend())
72                 {
73                         Channel* c = *c2++;
74                         if (c->IsModeSet('P') && c->GetUserCounter())
75                         {
76                                 std::vector<std::string> modes;
77                                 modes.push_back(c->name);
78                                 modes.push_back("-P");
79
80                                 ServerInstance->SendGlobalMode(modes, ServerInstance->FakeClient);
81                         }
82                         const UserMembList* users = c->GetUsers();
83                         for(UserMembCIter j = users->begin(); j != users->end(); ++j)
84                                 if (IS_LOCAL(j->first))
85                                         c->KickUser(ServerInstance->FakeClient, j->first, "Channel name no longer valid");
86                 }
87                 badchan = false;
88         }
89
90         virtual void OnRehash(User* user)
91         {
92                 ConfigReader Conf;
93                 std::string denyToken = Conf.ReadValue("channames", "denyrange", 0);
94                 std::string allowToken = Conf.ReadValue("channames", "allowrange", 0);
95                 allowedmap.set();
96
97                 irc::portparser denyrange(denyToken, false);
98                 int denyno = -1;
99                 while (0 != (denyno = denyrange.GetToken()))
100                         allowedmap[denyno & 0xFF] = false;
101
102                 irc::portparser allowrange(allowToken, false);
103                 int allowno = -1;
104                 while (0 != (allowno = allowrange.GetToken()))
105                         allowedmap[allowno & 0xFF] = true;
106
107                 allowedmap[0x07] = false; // BEL
108                 allowedmap[0x20] = false; // ' '
109                 allowedmap[0x2C] = false; // ','
110
111                 ValidateChans();
112         }
113
114         virtual void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except_list)
115         {
116                 if (badchan)
117                 {
118                         const UserMembList* users = memb->chan->GetUsers();
119                         for(UserMembCIter i = users->begin(); i != users->end(); i++)
120                                 if (i->first != memb->user)
121                                         except_list.insert(i->first);
122                 }
123         }
124
125         virtual ~ModuleChannelNames()
126         {
127                 ServerInstance->IsChannel = rememberer;
128                 ValidateChans();
129         }
130
131         virtual Version GetVersion()
132         {
133                 return Version("Implements config tags which allow changing characters allowed in channel names", VF_VENDOR);
134         }
135 };
136
137 MODULE_INIT(ModuleChannelNames)