2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
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.
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
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/>.
20 /* $ModDesc: Provides the ability to manipulate modes via long names. */
24 static void DisplayList(User* user, Channel* channel)
26 std::stringstream items;
27 for(char letter = 'A'; letter <= 'z'; letter++)
29 ModeHandler* mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
30 if (!mh || mh->IsListMode())
32 if (!channel->IsModeSet(letter))
34 items << " +" << mh->name;
35 if (mh->GetNumParams(true))
36 items << " " << channel->GetModeParameter(letter);
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());
43 class CommandProp : public Command
46 CommandProp(Module* parent) : Command(parent, "PROP", 1)
48 syntax = "<user|channel> {[+-]<mode> [<value>]}*";
51 CmdResult Handle(const std::vector<std::string> ¶meters, User *src)
53 if (parameters.size() == 1)
55 Channel* chan = ServerInstance->FindChan(parameters[0]);
57 DisplayList(src, chan);
61 std::vector<std::string> modes;
62 modes.push_back(parameters[0]);
64 while (i < parameters.size())
66 std::string prop = parameters[i++];
67 bool plus = prop[0] != '-';
68 if (prop[0] == '+' || prop[0] == '-')
69 prop.erase(prop.begin());
71 for(char letter = 'A'; letter <= 'z'; letter++)
73 ModeHandler* mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
74 if (mh && mh->name == prop)
76 modes[1].append((plus ? "+" : "-") + std::string(1, letter));
77 if (mh->GetNumParams(plus))
79 if (i != parameters.size())
80 modes.push_back(parameters[i++]);
85 ServerInstance->SendGlobalMode(modes, src);
90 class DummyZ : public ModeHandler
93 DummyZ(Module* parent) : ModeHandler(parent, "namebase", 'Z', PARAM_ALWAYS, MODETYPE_CHANNEL)
99 class ModuleNamedModes : public Module
104 ModuleNamedModes() : cmd(this), dummyZ(this)
110 ServerInstance->Modules->AddService(cmd);
111 ServerInstance->Modules->AddService(dummyZ);
113 Implementation eventlist[] = { I_OnPreMode };
114 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
119 return Version("Provides the ability to manipulate modes via long names.",VF_VENDOR);
124 ServerInstance->Modules->SetPriority(this, I_OnPreMode, PRIORITY_FIRST);
127 ModResult OnPreMode(User* source, User* dest, Channel* channel, const std::vector<std::string>& parameters)
130 return MOD_RES_PASSTHRU;
131 if (parameters[1].find('Z') == std::string::npos)
132 return MOD_RES_PASSTHRU;
133 if (parameters.size() <= 2)
135 DisplayList(source, channel);
139 std::vector<std::string> newparms;
140 newparms.push_back(parameters[0]);
141 newparms.push_back(parameters[1]);
143 std::string modelist = newparms[1];
145 unsigned int param_at = 2;
146 for(unsigned int i = 0; i < modelist.length(); i++)
148 unsigned char modechar = modelist[i];
149 if (modechar == '+' || modechar == '-')
151 adding = (modechar == '+');
154 ModeHandler *mh = ServerInstance->Modes->FindMode(modechar, MODETYPE_CHANNEL);
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)
164 value = name.substr(eq + 1);
165 name = name.substr(0, eq);
167 for(char letter = 'A'; modechar == 0 && letter <= 'z'; letter++)
169 mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
170 if (mh && mh->name == name)
172 if (mh->GetNumParams(adding))
176 newparms.push_back(value);
189 modelist[i] = modechar;
191 modelist.erase(i--, 1);
193 else if (mh && mh->GetNumParams(adding) && param_at < parameters.size())
195 newparms.push_back(parameters[param_at++]);
198 newparms[1] = modelist;
199 ServerInstance->Modes->Process(newparms, source, false);
204 MODULE_INIT(ModuleNamedModes)