2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5 * Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
6 * Copyright (C) 2018 Dylan Frank <b00mx0r@aureus.pw>
7 * Copyright (C) 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
8 * Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
9 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
10 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
12 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
13 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
14 * Copyright (C) 2006-2008, 2010 Craig Edwards <brain@inspircd.org>
16 * This file is part of InspIRCd. InspIRCd is free software: you can
17 * redistribute it and/or modify it under the terms of the GNU General Public
18 * License as published by the Free Software Foundation, version 2.
20 * This program is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "modules/whois.h"
37 RPL_WHOISOPERATOR = 313,
39 RPL_WHOISCHANNELS = 319,
51 // Don't split private/secret channels into a separate RPL_WHOISCHANNELS numeric.
54 // Split private/secret channels into a separate RPL_WHOISCHANNELS numeric.
57 // Split private/secret channels into a separate RPL_WHOISCHANNELS numeric with RPL_CHANNELSMSG to explain the split.
61 class WhoisContextImpl : public Whois::Context
63 Events::ModuleEventProvider& lineevprov;
66 WhoisContextImpl(LocalUser* src, User* targ, Events::ModuleEventProvider& evprov)
67 : Whois::Context(src, targ)
72 using Whois::Context::SendLine;
73 void SendLine(Numeric::Numeric& numeric) CXX11_OVERRIDE;
76 void WhoisContextImpl::SendLine(Numeric::Numeric& numeric)
79 FIRST_MOD_RESULT_CUSTOM(lineevprov, Whois::LineEventListener, OnWhoisLine, MOD_RESULT, (*this, numeric));
81 if (MOD_RESULT != MOD_RES_DENY)
82 source->WriteNumeric(numeric);
87 class CommandWhois : public SplitCommand
89 ChanModeReference secretmode;
90 ChanModeReference privatemode;
91 UserModeReference snomaskmode;
92 Events::ModuleEventProvider evprov;
93 Events::ModuleEventProvider lineevprov;
95 void DoWhois(LocalUser* user, User* dest, time_t signon, unsigned long idle);
96 void SendChanList(WhoisContextImpl& whois);
99 /** If true then all opers are shown with a generic 'is a server operator' line rather than the oper type. */
102 /** How to handle private/secret channels in the WHOIS response. */
103 SplitWhoisState splitwhois;
107 /** Constructor for whois.
109 CommandWhois(Module* parent)
110 : SplitCommand(parent, "WHOIS", 1)
111 , secretmode(parent, "secret")
112 , privatemode(parent, "private")
113 , snomaskmode(parent, "snomask")
114 , evprov(parent, "event/whois")
115 , lineevprov(parent, "event/whoisline")
118 syntax = "[<servername>] <nick>[,<nick>]+";
122 * @param parameters The parameters to the command
123 * @param user The user issuing the command
124 * @return A value from CmdResult to indicate command success or failure.
126 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE;
127 CmdResult HandleRemote(RemoteUser* target, const Params& parameters) CXX11_OVERRIDE;
130 class WhoisNumericSink
132 WhoisContextImpl& whois;
134 WhoisNumericSink(WhoisContextImpl& whoisref)
139 void operator()(Numeric::Numeric& numeric) const
141 whois.SendLine(numeric);
145 class WhoisChanListNumericBuilder : public Numeric::GenericBuilder<' ', false, WhoisNumericSink>
148 WhoisChanListNumericBuilder(WhoisContextImpl& whois)
149 : Numeric::GenericBuilder<' ', false, WhoisNumericSink>(WhoisNumericSink(whois), RPL_WHOISCHANNELS, false, whois.GetSource()->nick.size() + whois.GetTarget()->nick.size() + 1)
151 GetNumeric().push(whois.GetTarget()->nick).push(std::string());
157 const SplitWhoisState& splitwhois;
158 WhoisChanListNumericBuilder num;
159 WhoisChanListNumericBuilder secretnum;
160 std::string prefixstr;
162 void AddMember(Membership* memb, WhoisChanListNumericBuilder& out)
165 const char prefix = memb->GetPrefixChar();
167 prefixstr.push_back(prefix);
168 out.Add(prefixstr, memb->chan->name);
172 WhoisChanList(WhoisContextImpl& whois, const SplitWhoisState& sws)
179 void AddVisible(Membership* memb)
181 AddMember(memb, num);
184 void AddHidden(Membership* memb)
186 AddMember(memb, splitwhois == SPLITWHOIS_NONE ? num : secretnum);
189 void Flush(WhoisContextImpl& whois)
192 if (!secretnum.IsEmpty() && splitwhois == SPLITWHOIS_SPLITMSG)
193 whois.SendLine(RPL_CHANNELSMSG, "is on private/secret channels:");
198 void CommandWhois::SendChanList(WhoisContextImpl& whois)
200 WhoisChanList chanlist(whois, splitwhois);
202 User* const target = whois.GetTarget();
203 bool hasoperpriv = whois.GetSource()->HasPrivPermission("users/channel-spy");
204 for (User::ChanList::iterator i = target->chans.begin(); i != target->chans.end(); ++i)
206 Membership* memb = *i;
207 Channel* c = memb->chan;
209 // Anyone can view channels which are not private or secret.
210 if (!c->IsModeSet(privatemode) && !c->IsModeSet(secretmode))
211 chanlist.AddVisible(memb);
213 // Hidden channels are visible when the following conditions are true:
214 // (1) The source user and the target user are the same.
215 // (2) The source user is a member of the hidden channel.
216 // (3) The source user is an oper with the users/channel-spy privilege.
217 else if (whois.IsSelfWhois() || c->HasUser(whois.GetSource()) || hasoperpriv)
218 chanlist.AddHidden(memb);
221 chanlist.Flush(whois);
224 void CommandWhois::DoWhois(LocalUser* user, User* dest, time_t signon, unsigned long idle)
226 WhoisContextImpl whois(user, dest, lineevprov);
228 whois.SendLine(RPL_WHOISUSER, dest->ident, dest->GetDisplayedHost(), '*', dest->GetRealName());
229 if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
231 whois.SendLine(RPL_WHOISHOST, InspIRCd::Format("is connecting from %s@%s %s", dest->ident.c_str(), dest->GetRealHost().c_str(), dest->GetIPString().c_str()));
236 if (!whois.IsSelfWhois() && !ServerInstance->Config->HideServer.empty() && !user->HasPrivPermission("servers/auspex"))
238 whois.SendLine(RPL_WHOISSERVER, ServerInstance->Config->HideServer, ServerInstance->Config->Network);
242 whois.SendLine(RPL_WHOISSERVER, dest->server->GetName(), dest->server->GetDesc());
247 whois.SendLine(RPL_AWAY, dest->awaymsg);
253 whois.SendLine(RPL_WHOISOPERATOR, "is a server operator");
255 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()));
258 if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
260 if (dest->IsModeSet(snomaskmode))
262 whois.SendLine(RPL_WHOISMODES, InspIRCd::Format("is using modes %s %s", dest->GetModeLetters().c_str(), snomaskmode->GetUserParameter(dest).c_str()));
266 whois.SendLine(RPL_WHOISMODES, InspIRCd::Format("is using modes %s", dest->GetModeLetters().c_str()));
270 FOREACH_MOD_CUSTOM(evprov, Whois::EventListener, OnWhois, (whois));
273 * We only send these if we've been provided them. That is, if hideserver is turned off, and user is local, or
274 * if remote whois is queried, too. This is to keep the user hidden, and also since you can't reliably tell remote time. -- w00t
276 if ((idle) || (signon))
278 whois.SendLine(RPL_WHOISIDLE, idle, signon, "seconds idle, signon time");
281 whois.SendLine(RPL_ENDOFWHOIS, "End of /WHOIS list.");
284 CmdResult CommandWhois::HandleRemote(RemoteUser* target, const Params& parameters)
286 if (parameters.size() < 2)
289 User* user = ServerInstance->FindUUID(parameters[0]);
293 // User doing the whois must be on this server
294 LocalUser* localuser = IS_LOCAL(user);
298 unsigned long idle = ConvToNum<unsigned long>(parameters.back());
299 DoWhois(localuser, target, target->signon, idle);
304 CmdResult CommandWhois::HandleLocal(LocalUser* user, const Params& parameters)
307 unsigned int userindex = 0;
308 unsigned long idle = 0;
311 if (CommandParser::LoopCall(user, this, parameters, 0))
315 * If 2 parameters are specified (/whois nick nick), ignore the first one like spanningtree
316 * does, and use the second one, otherwise, use the only parameter. -- djGrrr
318 if (parameters.size() > 1)
321 dest = ServerInstance->FindNickOnly(parameters[userindex]);
323 if ((dest) && (dest->registered == REG_ALL))
326 * Okay. Umpteenth attempt at doing this, so let's re-comment...
327 * For local users (/w localuser), we show idletime if hideserver is disabled
328 * For local users (/w localuser localuser), we always show idletime, hence parameters.size() > 1 check.
329 * For remote users (/w remoteuser), we do NOT show idletime
330 * For remote users (/w remoteuser remoteuser), spanningtree will handle calling do_whois, so we can ignore this case.
331 * Thanks to djGrrr for not being impatient while I have a crap day coding. :p -- w00t
333 LocalUser* localuser = IS_LOCAL(dest);
334 if (localuser && (ServerInstance->Config->HideServer.empty() || parameters.size() > 1))
336 idle = labs((long)((localuser->idle_lastmsg)-ServerInstance->Time()));
337 signon = dest->signon;
340 DoWhois(user,dest,signon,idle);
344 /* no such nick/channel */
345 user->WriteNumeric(Numerics::NoSuchNick(!parameters[userindex].empty() ? parameters[userindex] : "*"));
346 user->WriteNumeric(RPL_ENDOFWHOIS, (!parameters[userindex].empty() ? parameters[userindex] : "*"), "End of /WHOIS list.");
353 class CoreModWhois : public Module
364 void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
366 ConfigTag* tag = ServerInstance->Config->ConfValue("options");
367 const std::string splitwhois = tag->getString("splitwhois", "no", 1);
368 SplitWhoisState newsplitstate;
369 if (stdalgo::string::equalsci(splitwhois, "no"))
370 newsplitstate = SPLITWHOIS_NONE;
371 else if (stdalgo::string::equalsci(splitwhois, "split"))
372 newsplitstate = SPLITWHOIS_SPLIT;
373 else if (stdalgo::string::equalsci(splitwhois, "splitmsg"))
374 newsplitstate = SPLITWHOIS_SPLITMSG;
376 throw ModuleException(splitwhois + " is an invalid <options:splitwhois> value, at " + tag->getTagLocation());
378 ConfigTag* security = ServerInstance->Config->ConfValue("security");
379 cmd.genericoper = security->getBool("genericoper");
380 cmd.splitwhois = newsplitstate;
383 Version GetVersion() CXX11_OVERRIDE
385 return Version("Provides the WHOIS command", VF_VENDOR|VF_CORE);
389 MODULE_INIT(CoreModWhois)