]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_namedmodes.cpp
Introduce ModeProcessFlags, can be passed to ModeParser::Process() to indicate local...
[user/henk/code/inspircd.git] / src / modules / m_namedmodes.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 /* $ModDesc: Provides the ability to manipulate modes via long names. */
21
22 #include "inspircd.h"
23
24 static void DisplayList(User* user, Channel* channel)
25 {
26         std::stringstream items;
27         for(char letter = 'A'; letter <= 'z'; letter++)
28         {
29                 ModeHandler* mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
30                 if (!mh || mh->IsListMode())
31                         continue;
32                 if (!channel->IsModeSet(letter))
33                         continue;
34                 items << " +" << mh->name;
35                 if (mh->GetNumParams(true))
36                         items << " " << channel->GetModeParameter(letter);
37         }
38         const std::string line = ":" + ServerInstance->Config->ServerName + " 961 " + user->nick + " " + channel->name;
39         user->SendText(line, items);
40         user->WriteNumeric(960, "%s %s :End of mode list", user->nick.c_str(), channel->name.c_str());
41 }
42
43 class CommandProp : public Command
44 {
45  public:
46         CommandProp(Module* parent) : Command(parent, "PROP", 1)
47         {
48                 syntax = "<user|channel> {[+-]<mode> [<value>]}*";
49         }
50
51         CmdResult Handle(const std::vector<std::string> &parameters, User *src)
52         {
53                 if (parameters.size() == 1)
54                 {
55                         Channel* chan = ServerInstance->FindChan(parameters[0]);
56                         if (chan)
57                                 DisplayList(src, chan);
58                         return CMD_SUCCESS;
59                 }
60                 unsigned int i = 1;
61                 std::vector<std::string> modes;
62                 modes.push_back(parameters[0]);
63                 modes.push_back("");
64                 while (i < parameters.size())
65                 {
66                         std::string prop = parameters[i++];
67                         bool plus = prop[0] != '-';
68                         if (prop[0] == '+' || prop[0] == '-')
69                                 prop.erase(prop.begin());
70
71                         for(char letter = 'A'; letter <= 'z'; letter++)
72                         {
73                                 ModeHandler* mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
74                                 if (mh && mh->name == prop)
75                                 {
76                                         modes[1].append((plus ? "+" : "-") + std::string(1, letter));
77                                         if (mh->GetNumParams(plus))
78                                         {
79                                                 if (i != parameters.size())
80                                                         modes.push_back(parameters[i++]);
81                                         }
82                                 }
83                         }
84                 }
85                 ServerInstance->Modes->Process(modes, src);
86                 return CMD_SUCCESS;
87         }
88 };
89
90 class DummyZ : public ModeHandler
91 {
92  public:
93         DummyZ(Module* parent) : ModeHandler(parent, "namebase", 'Z', PARAM_ALWAYS, MODETYPE_CHANNEL)
94         {
95                 list = true;
96         }
97 };
98
99 class ModuleNamedModes : public Module
100 {
101         CommandProp cmd;
102         DummyZ dummyZ;
103  public:
104         ModuleNamedModes() : cmd(this), dummyZ(this)
105         {
106         }
107
108         void init() CXX11_OVERRIDE
109         {
110                 ServerInstance->Modules->AddService(cmd);
111                 ServerInstance->Modules->AddService(dummyZ);
112
113                 Implementation eventlist[] = { I_OnPreMode };
114                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
115         }
116
117         Version GetVersion() CXX11_OVERRIDE
118         {
119                 return Version("Provides the ability to manipulate modes via long names.",VF_VENDOR);
120         }
121
122         void Prioritize()
123         {
124                 ServerInstance->Modules->SetPriority(this, I_OnPreMode, PRIORITY_FIRST);
125         }
126
127         ModResult OnPreMode(User* source, User* dest, Channel* channel, const std::vector<std::string>& parameters) CXX11_OVERRIDE
128         {
129                 if (!channel)
130                         return MOD_RES_PASSTHRU;
131                 if (parameters[1].find('Z') == std::string::npos)
132                         return MOD_RES_PASSTHRU;
133                 if (parameters.size() <= 2)
134                 {
135                         DisplayList(source, channel);
136                         return MOD_RES_DENY;
137                 }
138
139                 std::vector<std::string> newparms;
140                 newparms.push_back(parameters[0]);
141                 newparms.push_back(parameters[1]);
142
143                 std::string modelist = newparms[1];
144                 bool adding = true;
145                 unsigned int param_at = 2;
146                 for(unsigned int i = 0; i < modelist.length(); i++)
147                 {
148                         unsigned char modechar = modelist[i];
149                         if (modechar == '+' || modechar == '-')
150                         {
151                                 adding = (modechar == '+');
152                                 continue;
153                         }
154                         ModeHandler *mh = ServerInstance->Modes->FindMode(modechar, MODETYPE_CHANNEL);
155                         if (modechar == 'Z')
156                         {
157                                 modechar = 0;
158                                 std::string name, value;
159                                 if (param_at < parameters.size())
160                                         name = parameters[param_at++];
161                                 std::string::size_type eq = name.find('=');
162                                 if (eq != std::string::npos)
163                                 {
164                                         value = name.substr(eq + 1);
165                                         name = name.substr(0, eq);
166                                 }
167                                 for(char letter = 'A'; modechar == 0 && letter <= 'z'; letter++)
168                                 {
169                                         mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
170                                         if (mh && mh->name == name)
171                                         {
172                                                 if (mh->GetNumParams(adding))
173                                                 {
174                                                         if (!value.empty())
175                                                         {
176                                                                 newparms.push_back(value);
177                                                                 modechar = letter;
178                                                                 break;
179                                                         }
180                                                 }
181                                                 else
182                                                 {
183                                                         modechar = letter;
184                                                         break;
185                                                 }
186                                         }
187                                 }
188                                 if (modechar)
189                                         modelist[i] = modechar;
190                                 else
191                                         modelist.erase(i--, 1);
192                         }
193                         else if (mh && mh->GetNumParams(adding) && param_at < parameters.size())
194                         {
195                                 newparms.push_back(parameters[param_at++]);
196                         }
197                 }
198                 newparms[1] = modelist;
199                 ServerInstance->Modes->Process(newparms, source);
200                 return MOD_RES_DENY;
201         }
202 };
203
204 MODULE_INIT(ModuleNamedModes)