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,
42 // Don't split private/secret channels into a separate RPL_WHOISCHANNELS numeric.
45 // Split private/secret channels into a separate RPL_WHOISCHANNELS numeric.
48 // Split private/secret channels into a separate RPL_WHOISCHANNELS numeric with RPL_CHANNELSMSG to explain the split.
52 class WhoisContextImpl : public Whois::Context
54 Events::ModuleEventProvider& lineevprov;
57 WhoisContextImpl(LocalUser* src, User* targ, Events::ModuleEventProvider& evprov)
58 : Whois::Context(src, targ)
63 using Whois::Context::SendLine;
64 void SendLine(Numeric::Numeric& numeric) CXX11_OVERRIDE;
67 void WhoisContextImpl::SendLine(Numeric::Numeric& numeric)
70 FIRST_MOD_RESULT_CUSTOM(lineevprov, Whois::LineEventListener, OnWhoisLine, MOD_RESULT, (*this, numeric));
72 if (MOD_RESULT != MOD_RES_DENY)
73 source->WriteNumeric(numeric);
78 class CommandWhois : public SplitCommand
80 ChanModeReference secretmode;
81 ChanModeReference privatemode;
82 UserModeReference snomaskmode;
83 Events::ModuleEventProvider evprov;
84 Events::ModuleEventProvider lineevprov;
86 void DoWhois(LocalUser* user, User* dest, time_t signon, unsigned long idle);
87 void SendChanList(WhoisContextImpl& whois);
90 SplitWhoisState splitwhois;
92 /** Constructor for whois.
94 CommandWhois(Module* parent)
95 : SplitCommand(parent, "WHOIS", 1)
96 , secretmode(parent, "secret")
97 , privatemode(parent, "private")
98 , snomaskmode(parent, "snomask")
99 , evprov(parent, "event/whois")
100 , lineevprov(parent, "event/whoisline")
103 syntax = "<nick>{,<nick>}";
107 * @param parameters The parameters to the command
108 * @param user The user issuing the command
109 * @return A value from CmdResult to indicate command success or failure.
111 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user) CXX11_OVERRIDE;
112 CmdResult HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target) CXX11_OVERRIDE;
115 class WhoisNumericSink
117 WhoisContextImpl& whois;
119 WhoisNumericSink(WhoisContextImpl& whoisref)
124 void operator()(Numeric::Numeric& numeric) const
126 whois.SendLine(numeric);
130 class WhoisChanListNumericBuilder : public Numeric::GenericBuilder<' ', false, WhoisNumericSink>
133 WhoisChanListNumericBuilder(WhoisContextImpl& whois)
134 : Numeric::GenericBuilder<' ', false, WhoisNumericSink>(WhoisNumericSink(whois), RPL_WHOISCHANNELS, false, whois.GetSource()->nick.size() + whois.GetTarget()->nick.size() + 1)
136 GetNumeric().push(whois.GetTarget()->nick).push(std::string());
142 const SplitWhoisState& splitwhois;
143 WhoisChanListNumericBuilder num;
144 WhoisChanListNumericBuilder secretnum;
145 std::string prefixstr;
147 void AddMember(Membership* memb, WhoisChanListNumericBuilder& out)
150 const char prefix = memb->GetPrefixChar();
152 prefixstr.push_back(prefix);
153 out.Add(prefixstr, memb->chan->name);
157 WhoisChanList(WhoisContextImpl& whois, const SplitWhoisState& sws)
164 void AddVisible(Membership* memb)
166 AddMember(memb, num);
169 void AddHidden(Membership* memb)
171 AddMember(memb, splitwhois == SPLITWHOIS_NONE ? num : secretnum);
174 void Flush(WhoisContextImpl& whois)
177 if (!secretnum.IsEmpty() && splitwhois == SPLITWHOIS_SPLITMSG)
178 whois.SendLine(RPL_CHANNELSMSG, "is on private/secret channels:");
183 void CommandWhois::SendChanList(WhoisContextImpl& whois)
185 WhoisChanList chanlist(whois, splitwhois);
187 User* const target = whois.GetTarget();
188 bool hasoperpriv = whois.GetSource()->HasPrivPermission("users/channel-spy");
189 for (User::ChanList::iterator i = target->chans.begin(); i != target->chans.end(); ++i)
191 Membership* memb = *i;
192 Channel* c = memb->chan;
194 // Anyone can view channels which are not private or secret.
195 if (!c->IsModeSet(privatemode) && !c->IsModeSet(secretmode))
196 chanlist.AddVisible(memb);
198 // Hidden channels are visible when the following conditions are true:
199 // (1) The source user and the target user are the same.
200 // (2) The source user is a member of the hidden channel.
201 // (3) The source user is an oper with the users/channel-spy privilege.
202 else if (whois.IsSelfWhois() || c->HasUser(whois.GetSource()) || hasoperpriv)
203 chanlist.AddHidden(memb);
206 chanlist.Flush(whois);
209 void CommandWhois::DoWhois(LocalUser* user, User* dest, time_t signon, unsigned long idle)
211 WhoisContextImpl whois(user, dest, lineevprov);
213 whois.SendLine(RPL_WHOISUSER, dest->ident, dest->GetDisplayedHost(), '*', dest->fullname);
214 if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
216 whois.SendLine(RPL_WHOISHOST, InspIRCd::Format("is connecting from %s@%s %s", dest->ident.c_str(), dest->GetRealHost().c_str(), dest->GetIPString().c_str()));
221 if (!whois.IsSelfWhois() && !ServerInstance->Config->HideServer.empty() && !user->HasPrivPermission("servers/auspex"))
223 whois.SendLine(RPL_WHOISSERVER, ServerInstance->Config->HideServer, ServerInstance->Config->Network);
227 whois.SendLine(RPL_WHOISSERVER, dest->server->GetName(), dest->server->GetDesc());
232 whois.SendLine(RPL_AWAY, dest->awaymsg);
237 if (ServerInstance->Config->GenericOper)
238 whois.SendLine(RPL_WHOISOPERATOR, "is an IRC operator");
240 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()));
243 if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
245 if (dest->IsModeSet(snomaskmode))
247 whois.SendLine(RPL_WHOISMODES, InspIRCd::Format("is using modes %s %s", dest->GetModeLetters().c_str(), snomaskmode->GetUserParameter(dest).c_str()));
251 whois.SendLine(RPL_WHOISMODES, InspIRCd::Format("is using modes %s", dest->GetModeLetters().c_str()));
255 FOREACH_MOD_CUSTOM(evprov, Whois::EventListener, OnWhois, (whois));
258 * We only send these if we've been provided them. That is, if hideserver is turned off, and user is local, or
259 * if remote whois is queried, too. This is to keep the user hidden, and also since you can't reliably tell remote time. -- w00t
261 if ((idle) || (signon))
263 whois.SendLine(RPL_WHOISIDLE, idle, signon, "seconds idle, signon time");
266 whois.SendLine(RPL_ENDOFWHOIS, "End of /WHOIS list.");
269 CmdResult CommandWhois::HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target)
271 if (parameters.size() < 2)
274 User* user = ServerInstance->FindUUID(parameters[0]);
278 // User doing the whois must be on this server
279 LocalUser* localuser = IS_LOCAL(user);
283 unsigned long idle = ConvToNum<unsigned long>(parameters.back());
284 DoWhois(localuser, target, target->signon, idle);
289 CmdResult CommandWhois::HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
292 unsigned int userindex = 0;
293 unsigned long idle = 0;
296 if (CommandParser::LoopCall(user, this, parameters, 0))
300 * If 2 paramters are specified (/whois nick nick), ignore the first one like spanningtree
301 * does, and use the second one, otherwise, use the only paramter. -- djGrrr
303 if (parameters.size() > 1)
306 dest = ServerInstance->FindNickOnly(parameters[userindex]);
308 if ((dest) && (dest->registered == REG_ALL))
311 * Okay. Umpteenth attempt at doing this, so let's re-comment...
312 * For local users (/w localuser), we show idletime if hideserver is disabled
313 * For local users (/w localuser localuser), we always show idletime, hence parameters.size() > 1 check.
314 * For remote users (/w remoteuser), we do NOT show idletime
315 * For remote users (/w remoteuser remoteuser), spanningtree will handle calling do_whois, so we can ignore this case.
316 * Thanks to djGrrr for not being impatient while I have a crap day coding. :p -- w00t
318 LocalUser* localuser = IS_LOCAL(dest);
319 if (localuser && (ServerInstance->Config->HideServer.empty() || parameters.size() > 1))
321 idle = labs((long)((localuser->idle_lastmsg)-ServerInstance->Time()));
322 signon = dest->signon;
325 DoWhois(user,dest,signon,idle);
329 /* no such nick/channel */
330 user->WriteNumeric(Numerics::NoSuchNick(!parameters[userindex].empty() ? parameters[userindex] : "*"));
331 user->WriteNumeric(RPL_ENDOFWHOIS, (!parameters[userindex].empty() ? parameters[userindex] : "*"), "End of /WHOIS list.");
338 class CoreModWhois : public Module
349 void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
351 ConfigTag* tag = ServerInstance->Config->ConfValue("options");
352 const std::string splitwhois = tag->getString("splitwhois", "no");
353 if (stdalgo::string::equalsci(splitwhois, "no"))
354 cmd.splitwhois = SPLITWHOIS_NONE;
355 else if (stdalgo::string::equalsci(splitwhois, "split"))
356 cmd.splitwhois = SPLITWHOIS_SPLIT;
357 else if (stdalgo::string::equalsci(splitwhois, "splitmsg"))
358 cmd.splitwhois = SPLITWHOIS_SPLITMSG;
360 throw ModuleException(splitwhois + " is an invalid <security:splitwhois> value, at " + tag->getTagLocation());
363 Version GetVersion() CXX11_OVERRIDE
365 return Version("Provides the WHOIS command", VF_VENDOR|VF_CORE);
369 MODULE_INIT(CoreModWhois)