2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com>
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/>.
23 class CommandClearChan : public Command
28 CommandClearChan(Module* Creator)
29 : Command(Creator, "CLEARCHAN", 1, 3)
31 syntax = "<channel> [<KILL|KICK|G|Z>] [<reason>]";
34 // Stop the linking mod from forwarding ENCAP'd CLEARCHAN commands, see below why
35 force_manual_route = true;
38 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
40 Channel* chan = activechan = ServerInstance->FindChan(parameters[0]);
43 user->WriteNotice("The channel " + parameters[0] + " does not exist.");
47 // See what method the oper wants to use, default to KILL
48 std::string method("KILL");
49 if (parameters.size() > 1)
51 method = parameters[1];
52 std::transform(method.begin(), method.end(), method.begin(), ::toupper);
55 XLineFactory* xlf = NULL;
56 bool kick = (method == "KICK");
57 if ((!kick) && (method != "KILL"))
59 if ((method != "Z") && (method != "G"))
61 user->WriteNotice("Invalid method for clearing " + chan->name);
65 xlf = ServerInstance->XLines->GetFactory(method);
70 const std::string reason = parameters.size() > 2 ? parameters.back() : "Clearing " + chan->name;
72 if (!user->server->IsSilentULine())
73 ServerInstance->SNO->WriteToSnoMask((IS_LOCAL(user) ? 'a' : 'A'), user->nick + " has cleared \002" + chan->name + "\002 (" + method + "): " + reason);
75 user->WriteNotice("Clearing \002" + chan->name + "\002 (" + method + "): " + reason);
78 // Route this command manually so it is sent before the QUITs we are about to generate.
79 // The idea is that by the time our QUITs reach the next hop, it has already removed all their
80 // clients from the channel, meaning victims on other servers won't see the victims on this
82 CommandBase::Params eparams;
83 eparams.push_back(chan->name);
84 eparams.push_back(method);
85 eparams.push_back(":");
86 eparams.back().append(reason);
87 ServerInstance->PI->BroadcastEncap(this->name, eparams, user, user);
90 // Attach to the appropriate hook so we're able to hide the QUIT/KICK messages
91 Implementation hook = (kick ? I_OnUserKick : I_OnBuildNeighborList);
92 ServerInstance->Modules->Attach(hook, creator);
95 // Now remove all local non-opers from the channel
96 Channel::MemberMap& users = chan->userlist;
97 for (Channel::MemberMap::iterator i = users.begin(); i != users.end(); )
99 User* curr = i->first;
100 const Channel::MemberMap::iterator currit = i;
103 if (!IS_LOCAL(curr) || curr->IsOper())
106 // If kicking users, remove them and skip the QuitUser()
109 chan->KickUser(ServerInstance->FakeClient, currit, reason);
113 // If we are banning users then create the XLine and add it
119 mask = ((method[0] == 'Z') ? curr->GetIPString() : "*@" + curr->GetRealHost());
120 xline = xlf->Generate(ServerInstance->Time(), 60*60, user->nick, reason, mask);
122 catch (ModuleException&)
124 // Nothing, move on to the next user
128 if (!ServerInstance->XLines->AddLine(xline, user))
132 ServerInstance->Users->QuitUser(curr, reason);
135 ServerInstance->Modules->Detach(hook, creator);
137 ServerInstance->XLines->ApplyLines();
143 class ModuleClearChan : public Module
145 CommandClearChan cmd;
153 void init() CXX11_OVERRIDE
155 // Only attached while we are working; don't react to events otherwise
156 ServerInstance->Modules->DetachAll(this);
159 void OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception) CXX11_OVERRIDE
162 for (IncludeChanList::iterator i = include.begin(); i != include.end(); ++i)
164 if ((*i)->chan == cmd.activechan)
166 // Don't show the QUIT to anyone in the channel by default
173 const Channel::MemberMap& users = cmd.activechan->GetUsers();
174 for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
176 LocalUser* curr = IS_LOCAL(i->first);
182 // If another module has removed the channel we're working on from the list of channels
183 // to consider for sending the QUIT to then don't add exceptions for opers, because the
184 // module before us doesn't want them to see it or added the exceptions already.
185 // If there is a value for this oper in excepts already, this won't overwrite it.
187 exception.insert(std::make_pair(curr, true));
190 else if (!include.empty() && curr->chans.size() > 1)
192 // This is a victim and potentially has another common channel with the user quitting,
193 // add a negative exception overwriting the previous value, if any.
194 exception[curr] = false;
199 void OnUserKick(User* source, Membership* memb, const std::string& reason, CUList& excepts) CXX11_OVERRIDE
201 // Hide the KICK from all non-opers
202 User* leaving = memb->user;
203 const Channel::MemberMap& users = memb->chan->GetUsers();
204 for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
206 User* curr = i->first;
207 if ((IS_LOCAL(curr)) && (!curr->IsOper()) && (curr != leaving))
208 excepts.insert(curr);
212 Version GetVersion() CXX11_OVERRIDE
214 return Version("Adds /CLEARCHAN that allows opers to masskick, masskill or mass G/Z-line users on a channel.", VF_VENDOR|VF_OPTCOMMON);
218 MODULE_INIT(ModuleClearChan)