2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class WhoisContextImpl : public Whois::Context
26 Events::ModuleEventProvider& lineevprov;
29 WhoisContextImpl(LocalUser* src, User* targ, Events::ModuleEventProvider& evprov)
30 : Whois::Context(src, targ)
35 using Whois::Context::SendLine;
36 void SendLine(Numeric::Numeric& numeric) CXX11_OVERRIDE;
39 void WhoisContextImpl::SendLine(Numeric::Numeric& numeric)
42 FIRST_MOD_RESULT_CUSTOM(lineevprov, Whois::LineEventListener, OnWhoisLine, MOD_RESULT, (*this, numeric));
44 if (MOD_RESULT != MOD_RES_DENY)
45 source->WriteNumeric(numeric);
50 class CommandWhois : public SplitCommand
52 ChanModeReference secretmode;
53 ChanModeReference privatemode;
54 UserModeReference snomaskmode;
55 Events::ModuleEventProvider evprov;
56 Events::ModuleEventProvider lineevprov;
58 void DoWhois(LocalUser* user, User* dest, unsigned long signon, unsigned long idle);
59 void SendChanList(WhoisContextImpl& whois);
62 /** Constructor for whois.
64 CommandWhois(Module* parent)
65 : SplitCommand(parent, "WHOIS", 1)
66 , secretmode(parent, "secret")
67 , privatemode(parent, "private")
68 , snomaskmode(parent, "snomask")
69 , evprov(parent, "event/whois")
70 , lineevprov(parent, "event/whoisline")
73 syntax = "<nick>{,<nick>}";
77 * @param parameters The parameters to the command
78 * @param user The user issuing the command
79 * @return A value from CmdResult to indicate command success or failure.
81 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user);
82 CmdResult HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target);
85 class WhoisNumericSink
87 WhoisContextImpl& whois;
89 WhoisNumericSink(WhoisContextImpl& whoisref)
94 void operator()(Numeric::Numeric& numeric) const
96 whois.SendLine(numeric);
100 class WhoisChanListNumericBuilder : public Numeric::GenericBuilder<' ', false, WhoisNumericSink>
103 WhoisChanListNumericBuilder(WhoisContextImpl& whois)
104 : Numeric::GenericBuilder<' ', false, WhoisNumericSink>(WhoisNumericSink(whois), 319, false, whois.GetSource()->nick.size() + whois.GetTarget()->nick.size() + 1)
106 GetNumeric().push(whois.GetTarget()->nick).push(std::string());
112 const ServerConfig::OperSpyWhoisState spywhois;
113 WhoisChanListNumericBuilder num;
114 WhoisChanListNumericBuilder spynum;
115 std::string prefixstr;
117 void AddMember(Membership* memb, WhoisChanListNumericBuilder& out)
120 const char prefix = memb->GetPrefixChar();
122 prefixstr.push_back(prefix);
123 out.Add(prefixstr, memb->chan->name);
127 WhoisChanList(WhoisContextImpl& whois)
128 : spywhois(whois.GetSource()->HasPrivPermission("users/auspex") ? ServerInstance->Config->OperSpyWhois : ServerConfig::SPYWHOIS_NONE)
134 void AddVisible(Membership* memb)
136 AddMember(memb, num);
139 void AddHidden(Membership* memb)
141 if (spywhois == ServerConfig::SPYWHOIS_NONE)
143 AddMember(memb, (spywhois == ServerConfig::SPYWHOIS_SPLITMSG ? spynum : num));
146 void Flush(WhoisContextImpl& whois)
149 if (!spynum.IsEmpty())
150 whois.SendLine(336, "is on private/secret channels:");
155 void CommandWhois::SendChanList(WhoisContextImpl& whois)
157 WhoisChanList chanlist(whois);
159 User* const target = whois.GetTarget();
160 for (User::ChanList::iterator i = target->chans.begin(); i != target->chans.end(); ++i)
162 Membership* memb = *i;
163 Channel* c = memb->chan;
164 /* If the target is the sender, neither +p nor +s is set, or
165 * the channel contains the user, it is not a spy channel
167 if ((whois.IsSelfWhois()) || ((!c->IsModeSet(privatemode)) && (!c->IsModeSet(secretmode))) || (c->HasUser(whois.GetSource())))
168 chanlist.AddVisible(memb);
170 chanlist.AddHidden(memb);
173 chanlist.Flush(whois);
176 void CommandWhois::DoWhois(LocalUser* user, User* dest, unsigned long signon, unsigned long idle)
178 WhoisContextImpl whois(user, dest, lineevprov);
180 whois.SendLine(311, dest->ident, dest->dhost, '*', dest->fullname);
181 if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
183 whois.SendLine(378, InspIRCd::Format("is connecting from %s@%s %s", dest->ident.c_str(), dest->host.c_str(), dest->GetIPString().c_str()));
188 if (!whois.IsSelfWhois() && !ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
190 whois.SendLine(312, ServerInstance->Config->HideWhoisServer, ServerInstance->Config->Network);
194 whois.SendLine(312, dest->server->GetName(), dest->server->GetDesc());
199 whois.SendLine(301, dest->awaymsg);
204 if (ServerInstance->Config->GenericOper)
205 whois.SendLine(313, "is an IRC operator");
207 whois.SendLine(313, InspIRCd::Format("is %s %s on %s", (strchr("AEIOUaeiou",dest->oper->name[0]) ? "an" : "a"), dest->oper->name.c_str(), ServerInstance->Config->Network.c_str()));
210 if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
212 if (dest->IsModeSet(snomaskmode))
214 whois.SendLine(379, InspIRCd::Format("is using modes +%s %s", dest->FormatModes(), snomaskmode->GetUserParameter(dest).c_str()));
218 whois.SendLine(379, InspIRCd::Format("is using modes +%s", dest->FormatModes()));
222 FOREACH_MOD_CUSTOM(evprov, Whois::EventListener, OnWhois, (whois));
225 * We only send these if we've been provided them. That is, if hidewhois is turned off, and user is local, or
226 * if remote whois is queried, too. This is to keep the user hidden, and also since you can't reliably tell remote time. -- w00t
228 if ((idle) || (signon))
230 whois.SendLine(317, idle, signon, "seconds idle, signon time");
233 whois.SendLine(318, "End of /WHOIS list.");
236 CmdResult CommandWhois::HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target)
238 if (parameters.size() < 2)
241 User* user = ServerInstance->FindUUID(parameters[0]);
245 // User doing the whois must be on this server
246 LocalUser* localuser = IS_LOCAL(user);
250 unsigned long idle = ConvToInt(parameters.back());
251 DoWhois(localuser, target, target->signon, idle);
256 CmdResult CommandWhois::HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
260 unsigned long idle = 0, signon = 0;
262 if (CommandParser::LoopCall(user, this, parameters, 0))
266 * If 2 paramters are specified (/whois nick nick), ignore the first one like spanningtree
267 * does, and use the second one, otherwise, use the only paramter. -- djGrrr
269 if (parameters.size() > 1)
272 dest = ServerInstance->FindNickOnly(parameters[userindex]);
274 if ((dest) && (dest->registered == REG_ALL))
277 * Okay. Umpteenth attempt at doing this, so let's re-comment...
278 * For local users (/w localuser), we show idletime if hidewhois is disabled
279 * For local users (/w localuser localuser), we always show idletime, hence parameters.size() > 1 check.
280 * For remote users (/w remoteuser), we do NOT show idletime
281 * For remote users (/w remoteuser remoteuser), spanningtree will handle calling do_whois, so we can ignore this case.
282 * Thanks to djGrrr for not being impatient while I have a crap day coding. :p -- w00t
284 LocalUser* localuser = IS_LOCAL(dest);
285 if (localuser && (ServerInstance->Config->HideWhoisServer.empty() || parameters.size() > 1))
287 idle = labs((long)((localuser->idle_lastmsg)-ServerInstance->Time()));
288 signon = dest->signon;
291 DoWhois(user,dest,signon,idle);
295 /* no such nick/channel */
296 user->WriteNumeric(Numerics::NoSuchNick(!parameters[userindex].empty() ? parameters[userindex] : "*"));
297 user->WriteNumeric(RPL_ENDOFWHOIS, (!parameters[userindex].empty() ? parameters[userindex] : "*"), "End of /WHOIS list.");
304 COMMAND_INIT(CommandWhois)