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