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);
39 snprintf(pfx, MAXBUF, ":%s 961 %s %s", ServerInstance->Config->ServerName.c_str(), user->nick.c_str(), channel->name.c_str());
40 user->SendText(std::string(pfx), items);
41 user->WriteNumeric(960, "%s %s :End of mode list", user->nick.c_str(), channel->name.c_str());
44 class CommandProp : public Command
47 CommandProp(Module* parent) : Command(parent, "PROP", 1)
49 syntax = "<user|channel> {[+-]<mode> [<value>]}*";
52 CmdResult Handle(const std::vector<std::string> ¶meters, User *src)
54 if (parameters.size() == 1)
56 Channel* chan = ServerInstance->FindChan(parameters[0]);
58 DisplayList(src, chan);
62 std::vector<std::string> modes;
63 modes.push_back(parameters[0]);
65 while (i < parameters.size())
67 std::string prop = parameters[i++];
68 bool plus = prop[0] != '-';
69 if (prop[0] == '+' || prop[0] == '-')
70 prop.erase(prop.begin());
72 for(char letter = 'A'; letter <= 'z'; letter++)
74 ModeHandler* mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
75 if (mh && mh->name == prop)
77 modes[1].append((plus ? "+" : "-") + std::string(1, letter));
78 if (mh->GetNumParams(plus))
80 if (i != parameters.size())
81 modes.push_back(parameters[i++]);
86 ServerInstance->SendGlobalMode(modes, src);
91 class DummyZ : public ModeHandler
94 DummyZ(Module* parent) : ModeHandler(parent, "namebase", 'Z', PARAM_ALWAYS, MODETYPE_CHANNEL)
100 class ModuleNamedModes : public Module
105 ModuleNamedModes() : cmd(this), dummyZ(this)
111 ServerInstance->Modules->AddService(cmd);
112 ServerInstance->Modules->AddService(dummyZ);
114 Implementation eventlist[] = { I_OnPreMode };
115 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
120 return Version("Provides the ability to manipulate modes via long names.",VF_VENDOR);
125 ServerInstance->Modules->SetPriority(this, I_OnPreMode, PRIORITY_FIRST);
128 ModResult OnPreMode(User* source, User* dest, Channel* channel, const std::vector<std::string>& parameters)
131 return MOD_RES_PASSTHRU;
132 if (parameters[1].find('Z') == std::string::npos)
133 return MOD_RES_PASSTHRU;
134 if (parameters.size() <= 2)
136 DisplayList(source, channel);
140 std::vector<std::string> newparms;
141 newparms.push_back(parameters[0]);
142 newparms.push_back(parameters[1]);
144 std::string modelist = newparms[1];
146 unsigned int param_at = 2;
147 for(unsigned int i = 0; i < modelist.length(); i++)
149 unsigned char modechar = modelist[i];
150 if (modechar == '+' || modechar == '-')
152 adding = (modechar == '+');
155 ModeHandler *mh = ServerInstance->Modes->FindMode(modechar, MODETYPE_CHANNEL);
159 std::string name, value;
160 if (param_at < parameters.size())
161 name = parameters[param_at++];
162 std::string::size_type eq = name.find('=');
163 if (eq != std::string::npos)
165 value = name.substr(eq + 1);
166 name = name.substr(0, eq);
168 for(char letter = 'A'; modechar == 0 && letter <= 'z'; letter++)
170 mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
171 if (mh && mh->name == name)
173 if (mh->GetNumParams(adding))
177 newparms.push_back(value);
190 modelist[i] = modechar;
192 modelist.erase(i--, 1);
194 else if (mh && mh->GetNumParams(adding) && param_at < parameters.size())
196 newparms.push_back(parameters[param_at++]);
199 newparms[1] = modelist;
200 ServerInstance->Modes->Process(newparms, source, false);
205 MODULE_INIT(ModuleNamedModes)