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/>.
28 RPL_WHOISOPERATOR = 313,
30 RPL_WHOISCHANNELS = 319,
40 class WhoisContextImpl : public Whois::Context
42 Events::ModuleEventProvider& lineevprov;
45 WhoisContextImpl(LocalUser* src, User* targ, Events::ModuleEventProvider& evprov)
46 : Whois::Context(src, targ)
51 using Whois::Context::SendLine;
52 void SendLine(Numeric::Numeric& numeric) CXX11_OVERRIDE;
55 void WhoisContextImpl::SendLine(Numeric::Numeric& numeric)
58 FIRST_MOD_RESULT_CUSTOM(lineevprov, Whois::LineEventListener, OnWhoisLine, MOD_RESULT, (*this, numeric));
60 if (MOD_RESULT != MOD_RES_DENY)
61 source->WriteNumeric(numeric);
66 class CommandWhois : public SplitCommand
68 ChanModeReference secretmode;
69 ChanModeReference privatemode;
70 UserModeReference snomaskmode;
71 Events::ModuleEventProvider evprov;
72 Events::ModuleEventProvider lineevprov;
74 void DoWhois(LocalUser* user, User* dest, time_t signon, unsigned long idle);
75 void SendChanList(WhoisContextImpl& whois);
78 /** Constructor for whois.
80 CommandWhois(Module* parent)
81 : SplitCommand(parent, "WHOIS", 1)
82 , secretmode(parent, "secret")
83 , privatemode(parent, "private")
84 , snomaskmode(parent, "snomask")
85 , evprov(parent, "event/whois")
86 , lineevprov(parent, "event/whoisline")
89 syntax = "<nick>{,<nick>}";
93 * @param parameters The parameters to the command
94 * @param user The user issuing the command
95 * @return A value from CmdResult to indicate command success or failure.
97 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user) CXX11_OVERRIDE;
98 CmdResult HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target) CXX11_OVERRIDE;
101 class WhoisNumericSink
103 WhoisContextImpl& whois;
105 WhoisNumericSink(WhoisContextImpl& whoisref)
110 void operator()(Numeric::Numeric& numeric) const
112 whois.SendLine(numeric);
116 class WhoisChanListNumericBuilder : public Numeric::GenericBuilder<' ', false, WhoisNumericSink>
119 WhoisChanListNumericBuilder(WhoisContextImpl& whois)
120 : Numeric::GenericBuilder<' ', false, WhoisNumericSink>(WhoisNumericSink(whois), RPL_WHOISCHANNELS, false, whois.GetSource()->nick.size() + whois.GetTarget()->nick.size() + 1)
122 GetNumeric().push(whois.GetTarget()->nick).push(std::string());
128 const ServerConfig::OperSpyWhoisState spywhois;
129 WhoisChanListNumericBuilder num;
130 WhoisChanListNumericBuilder spynum;
131 std::string prefixstr;
133 void AddMember(Membership* memb, WhoisChanListNumericBuilder& out)
136 const char prefix = memb->GetPrefixChar();
138 prefixstr.push_back(prefix);
139 out.Add(prefixstr, memb->chan->name);
143 WhoisChanList(WhoisContextImpl& whois)
144 : spywhois(whois.GetSource()->HasPrivPermission("users/auspex") ? ServerInstance->Config->OperSpyWhois : ServerConfig::SPYWHOIS_NONE)
150 void AddVisible(Membership* memb)
152 AddMember(memb, num);
155 void AddHidden(Membership* memb)
157 if (spywhois == ServerConfig::SPYWHOIS_NONE)
159 AddMember(memb, (spywhois == ServerConfig::SPYWHOIS_SPLITMSG ? spynum : num));
162 void Flush(WhoisContextImpl& whois)
165 if (!spynum.IsEmpty())
166 whois.SendLine(RPL_CHANNELSMSG, "is on private/secret channels:");
171 void CommandWhois::SendChanList(WhoisContextImpl& whois)
173 WhoisChanList chanlist(whois);
175 User* const target = whois.GetTarget();
176 for (User::ChanList::iterator i = target->chans.begin(); i != target->chans.end(); ++i)
178 Membership* memb = *i;
179 Channel* c = memb->chan;
180 /* If the target is the sender, neither +p nor +s is set, or
181 * the channel contains the user, it is not a spy channel
183 if ((whois.IsSelfWhois()) || ((!c->IsModeSet(privatemode)) && (!c->IsModeSet(secretmode))) || (c->HasUser(whois.GetSource())))
184 chanlist.AddVisible(memb);
186 chanlist.AddHidden(memb);
189 chanlist.Flush(whois);
192 void CommandWhois::DoWhois(LocalUser* user, User* dest, time_t signon, unsigned long idle)
194 WhoisContextImpl whois(user, dest, lineevprov);
196 whois.SendLine(RPL_WHOISUSER, dest->ident, dest->GetDisplayedHost(), '*', dest->fullname);
197 if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
199 whois.SendLine(RPL_WHOISHOST, InspIRCd::Format("is connecting from %s@%s %s", dest->ident.c_str(), dest->GetRealHost().c_str(), dest->GetIPString().c_str()));
204 if (!whois.IsSelfWhois() && !ServerInstance->Config->HideServer.empty() && !user->HasPrivPermission("servers/auspex"))
206 whois.SendLine(RPL_WHOISSERVER, ServerInstance->Config->HideServer, ServerInstance->Config->Network);
210 whois.SendLine(RPL_WHOISSERVER, dest->server->GetName(), dest->server->GetDesc());
215 whois.SendLine(RPL_AWAY, dest->awaymsg);
220 if (ServerInstance->Config->GenericOper)
221 whois.SendLine(RPL_WHOISOPERATOR, "is an IRC operator");
223 whois.SendLine(RPL_WHOISOPERATOR, 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()));
226 if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
228 if (dest->IsModeSet(snomaskmode))
230 whois.SendLine(RPL_WHOISMODES, InspIRCd::Format("is using modes %s %s", dest->GetModeLetters().c_str(), snomaskmode->GetUserParameter(dest).c_str()));
234 whois.SendLine(RPL_WHOISMODES, InspIRCd::Format("is using modes %s", dest->GetModeLetters().c_str()));
238 FOREACH_MOD_CUSTOM(evprov, Whois::EventListener, OnWhois, (whois));
241 * We only send these if we've been provided them. That is, if hideserver is turned off, and user is local, or
242 * if remote whois is queried, too. This is to keep the user hidden, and also since you can't reliably tell remote time. -- w00t
244 if ((idle) || (signon))
246 whois.SendLine(RPL_WHOISIDLE, idle, signon, "seconds idle, signon time");
249 whois.SendLine(RPL_ENDOFWHOIS, "End of /WHOIS list.");
252 CmdResult CommandWhois::HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target)
254 if (parameters.size() < 2)
257 User* user = ServerInstance->FindUUID(parameters[0]);
261 // User doing the whois must be on this server
262 LocalUser* localuser = IS_LOCAL(user);
266 unsigned long idle = ConvToNum<unsigned long>(parameters.back());
267 DoWhois(localuser, target, target->signon, idle);
272 CmdResult CommandWhois::HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
275 unsigned int userindex = 0;
276 unsigned long idle = 0;
279 if (CommandParser::LoopCall(user, this, parameters, 0))
283 * If 2 paramters are specified (/whois nick nick), ignore the first one like spanningtree
284 * does, and use the second one, otherwise, use the only paramter. -- djGrrr
286 if (parameters.size() > 1)
289 dest = ServerInstance->FindNickOnly(parameters[userindex]);
291 if ((dest) && (dest->registered == REG_ALL))
294 * Okay. Umpteenth attempt at doing this, so let's re-comment...
295 * For local users (/w localuser), we show idletime if hideserver is disabled
296 * For local users (/w localuser localuser), we always show idletime, hence parameters.size() > 1 check.
297 * For remote users (/w remoteuser), we do NOT show idletime
298 * For remote users (/w remoteuser remoteuser), spanningtree will handle calling do_whois, so we can ignore this case.
299 * Thanks to djGrrr for not being impatient while I have a crap day coding. :p -- w00t
301 LocalUser* localuser = IS_LOCAL(dest);
302 if (localuser && (ServerInstance->Config->HideServer.empty() || parameters.size() > 1))
304 idle = labs((long)((localuser->idle_lastmsg)-ServerInstance->Time()));
305 signon = dest->signon;
308 DoWhois(user,dest,signon,idle);
312 /* no such nick/channel */
313 user->WriteNumeric(Numerics::NoSuchNick(!parameters[userindex].empty() ? parameters[userindex] : "*"));
314 user->WriteNumeric(RPL_ENDOFWHOIS, (!parameters[userindex].empty() ? parameters[userindex] : "*"), "End of /WHOIS list.");
321 COMMAND_INIT(CommandWhois)