2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018 Peter Powell <petpow@saberuk.com>
5 * Copyright (C) 2014 Adam <Adam@anope.org>
6 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
7 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "modules/account.h"
38 // The flags for matching users to include.
39 std::bitset<UCHAR_MAX> flags;
41 // Whether we are matching using a wildcard or a flag.
44 // The text to match against.
45 std::string matchtext;
47 // The WHO/WHOX responses we will send to the source.
48 std::vector<Numeric::Numeric> results;
50 // Whether the source requested a WHOX response.
53 // The fields to include in the WHOX response.
54 std::bitset<UCHAR_MAX> whox_fields;
56 // A user specified label for the WHOX response.
57 std::string whox_querytype;
59 WhoData(const CommandBase::Params& parameters)
62 // Find the matchtext and swap the 0 for a * so we can use InspIRCd::Match on it.
63 matchtext = parameters.size() > 2 ? parameters[2] : parameters[0];
67 // Fuzzy matches are when the source has not specified a specific user.
68 fuzzy_match = (parameters.size() > 1) || (matchtext.find_first_of("*?.") != std::string::npos);
70 // If flags have been specified by the source.
71 if (parameters.size() > 1)
73 std::bitset<UCHAR_MAX>* current_bitset = &flags;
74 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
76 unsigned char chr = static_cast<unsigned char>(*iter);
78 // If the source specifies a percentage the rest of the flags are WHOX fields.
82 current_bitset = &whox_fields;
86 // If we are in WHOX mode and the source specifies a comma
87 // the rest of the parameter is the query type.
88 if (whox && chr == ',')
90 whox_querytype.assign(++iter, parameters[1].end());
94 // The source specified a matching flag.
95 current_bitset->set(chr);
101 class CommandWho : public SplitCommand
104 ChanModeReference secretmode;
105 ChanModeReference privatemode;
106 UserModeReference hidechansmode;
107 UserModeReference invisiblemode;
109 /** Determines whether a user can view the users of a channel. */
110 bool CanView(Channel* chan, User* user)
112 // If we are in a channel we can view all users in it.
113 if (chan->HasUser(user))
116 // Opers with the users/auspex priv can see everything.
117 if (user->HasPrivPermission("users/auspex"))
120 // You can see inside a channel from outside unless it is secret or private.
121 return !chan->IsModeSet(secretmode) && !chan->IsModeSet(privatemode);
124 /** Gets the first channel which is visible between the source and the target users. */
125 Membership* GetFirstVisibleChannel(LocalUser* source, User* user)
127 for (User::ChanList::iterator iter = user->chans.begin(); iter != user->chans.end(); ++iter)
129 Membership* memb = *iter;
131 // TODO: move the +I check into m_hidechans.
132 bool has_modes = memb->chan->IsModeSet(secretmode) || memb->chan->IsModeSet(privatemode) || user->IsModeSet(hidechansmode);
133 if (source == user || !has_modes || memb->chan->HasUser(source))
139 /** Determines whether WHO flags match a specific channel user. */
140 static bool MatchChannel(LocalUser* source, Membership* memb, WhoData& data);
142 /** Determines whether WHO flags match a specific user. */
143 static bool MatchUser(LocalUser* source, User* target, WhoData& data);
145 /** Performs a WHO request on a channel. */
146 void WhoChannel(LocalUser* source, const std::vector<std::string>& parameters, Channel* c, WhoData& data);
148 /** Template for getting a user from various types of collection. */
150 static User* GetUser(T& t);
152 /** Performs a WHO request on a list of users. */
154 void WhoUsers(LocalUser* source, const std::vector<std::string>& parameters, const T& users, WhoData& data);
157 CommandWho(Module* parent)
158 : SplitCommand(parent, "WHO", 1, 3)
159 , secretmode(parent, "secret")
160 , privatemode(parent, "private")
161 , hidechansmode(parent, "hidechans")
162 , invisiblemode(parent, "invisible")
164 allow_empty_last_param = false;
165 syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [[Aafhilmnoprstux][%acdfhilnorstu] <server>|<nickname>|<channel>|<realname>|<host>|0]";
168 /** Sends a WHO reply to a user. */
169 void SendWhoLine(LocalUser* user, const std::vector<std::string>& parameters, Membership* memb, User* u, WhoData& data);
171 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE;
174 template<> User* CommandWho::GetUser(UserManager::OperList::const_iterator& t) { return *t; }
175 template<> User* CommandWho::GetUser(user_hash::const_iterator& t) { return t->second; }
177 bool CommandWho::MatchChannel(LocalUser* source, Membership* memb, WhoData& data)
179 bool source_has_users_auspex = source->HasPrivPermission("users/auspex");
180 bool source_can_see_server = ServerInstance->Config->HideServer.empty() || source_has_users_auspex;
182 // The source only wants remote users. This user is eligible if:
183 // (1) The source can't see server information.
184 // (2) The source is not local to the current server.
185 LocalUser* lu = IS_LOCAL(memb->user);
186 if (data.flags['f'] && source_can_see_server && lu)
189 // The source only wants local users. This user is eligible if:
190 // (1) The source can't see server information.
191 // (2) The source is local to the current server.
192 if (data.flags['l'] && source_can_see_server && !lu)
195 // Only show operators if the oper flag has been specified.
196 if (data.flags['o'] && !memb->user->IsOper())
199 // All other flags are ignored for channels.
203 bool CommandWho::MatchUser(LocalUser* source, User* user, WhoData& data)
205 // Users who are not fully registered can never match.
206 if (user->registered != REG_ALL)
209 bool source_has_users_auspex = source->HasPrivPermission("users/auspex");
210 bool source_can_see_target = source == user || source_has_users_auspex;
211 bool source_can_see_server = ServerInstance->Config->HideServer.empty() || source_has_users_auspex;
213 // The source only wants remote users. This user is eligible if:
214 // (1) The source can't see server information.
215 // (2) The source is not local to the current server.
216 LocalUser* lu = IS_LOCAL(user);
217 if (data.flags['f'] && source_can_see_server && lu)
220 // The source only wants local users. This user is eligible if:
221 // (1) The source can't see server information.
222 // (2) The source is local to the current server.
223 if (data.flags['l'] && source_can_see_server && !lu)
226 // The source wants to match against users' away messages.
229 match = user->IsAway() && InspIRCd::Match(user->awaymsg, data.matchtext, ascii_case_insensitive_map);
231 // The source wants to match against users' account names.
232 else if (data.flags['a'])
234 const AccountExtItem* accountext = GetAccountExtItem();
235 const std::string* account = accountext ? accountext->get(user) : NULL;
236 match = account && InspIRCd::Match(*account, data.matchtext);
239 // The source wants to match against users' hostnames.
240 else if (data.flags['h'])
242 const std::string host = user->GetHost(source_can_see_target && data.flags['x']);
243 match = InspIRCd::Match(host, data.matchtext, ascii_case_insensitive_map);
246 // The source wants to match against users' IP addresses.
247 else if (data.flags['i'])
248 match = source_can_see_target && InspIRCd::MatchCIDR(user->GetIPString(), data.matchtext, ascii_case_insensitive_map);
250 // The source wants to match against users' modes.
251 else if (data.flags['m'])
253 if (source_can_see_target)
256 for (std::string::const_iterator iter = data.matchtext.begin(); iter != data.matchtext.end(); ++iter)
258 unsigned char chr = static_cast<unsigned char>(*iter);
261 // The following user modes should be set.
266 // The following user modes should be unset.
272 if (user->IsModeSet(chr) != set)
278 // All of the modes matched.
283 // The source wants to match against users' nicks.
284 else if (data.flags['n'])
285 match = InspIRCd::Match(user->nick, data.matchtext);
287 // The source wants to match against users' connection ports.
288 else if (data.flags['p'])
290 if (source_can_see_target && lu)
292 irc::portparser portrange(data.matchtext, false);
294 while ((port = portrange.GetToken()))
296 if (port == lu->GetServerPort())
305 // The source wants to match against users' real names.
306 else if (data.flags['r'])
307 match = InspIRCd::Match(user->GetRealName(), data.matchtext, ascii_case_insensitive_map);
309 else if (data.flags['s'])
311 bool show_real_server_name = ServerInstance->Config->HideServer.empty() || (source->HasPrivPermission("servers/auspex") && data.flags['x']);
312 const std::string server = show_real_server_name ? user->server->GetName() : ServerInstance->Config->HideServer;
313 match = InspIRCd::Match(server, data.matchtext, ascii_case_insensitive_map);
316 // The source wants to match against users' connection times.
317 else if (data.flags['t'])
319 time_t seconds = ServerInstance->Time() - InspIRCd::Duration(data.matchtext);
320 if (user->signon >= seconds)
324 // The source wants to match against users' idents.
325 else if (data.flags['u'])
326 match = InspIRCd::Match(user->ident, data.matchtext, ascii_case_insensitive_map);
328 // The <name> passed to WHO is matched against users' host, server,
329 // real name and nickname if the channel <name> cannot be found.
332 const std::string host = user->GetHost(source_can_see_target && data.flags['x']);
333 match = InspIRCd::Match(host, data.matchtext, ascii_case_insensitive_map);
337 bool show_real_server_name = ServerInstance->Config->HideServer.empty() || (source->HasPrivPermission("servers/auspex") && data.flags['x']);
338 const std::string server = show_real_server_name ? user->server->GetName() : ServerInstance->Config->HideServer;
339 match = InspIRCd::Match(server, data.matchtext, ascii_case_insensitive_map);
343 match = InspIRCd::Match(user->GetRealName(), data.matchtext, ascii_case_insensitive_map);
346 match = InspIRCd::Match(user->nick, data.matchtext);
352 void CommandWho::WhoChannel(LocalUser* source, const std::vector<std::string>& parameters, Channel* chan, WhoData& data)
354 if (!CanView(chan, source))
357 bool inside = chan->HasUser(source);
358 const Channel::MemberMap& users = chan->GetUsers();
359 for (Channel::MemberMap::const_iterator iter = users.begin(); iter != users.end(); ++iter)
361 User* user = iter->first;
362 Membership* memb = iter->second;
364 // Only show invisible users if the source is in the channel or has the users/auspex priv.
365 if (!inside && user->IsModeSet(invisiblemode) && !source->HasPrivPermission("users/auspex"))
368 // Skip the user if it doesn't match the query.
369 if (!MatchChannel(source, memb, data))
372 SendWhoLine(source, parameters, memb, user, data);
377 void CommandWho::WhoUsers(LocalUser* source, const std::vector<std::string>& parameters, const T& users, WhoData& data)
379 for (typename T::const_iterator iter = users.begin(); iter != users.end(); ++iter)
381 User* user = GetUser(iter);
383 // Only show users in response to a fuzzy WHO if we can see them normally.
384 bool can_see_normally = user == source || source->SharesChannelWith(user) || !user->IsModeSet(invisiblemode);
385 if (data.fuzzy_match && !can_see_normally && !source->HasPrivPermission("users/auspex"))
388 // Skip the user if it doesn't match the query.
389 if (!MatchUser(source, user, data))
392 SendWhoLine(source, parameters, NULL, user, data);
396 void CommandWho::SendWhoLine(LocalUser* source, const std::vector<std::string>& parameters, Membership* memb, User* user, WhoData& data)
399 memb = GetFirstVisibleChannel(source, user);
401 bool source_can_see_target = source == user || source->HasPrivPermission("users/auspex");
402 Numeric::Numeric wholine(data.whox ? RPL_WHOSPCRPL : RPL_WHOREPLY);
405 // The source used WHOX so we send a fancy customised response.
407 // Include the query type in the reply.
408 if (data.whox_fields['t'])
409 wholine.push(data.whox_querytype.empty() || data.whox_querytype.length() > 3 ? "0" : data.whox_querytype);
411 // Include the first channel name.
412 if (data.whox_fields['c'])
413 wholine.push(memb ? memb->chan->name : "*");
415 // Include the user's ident.
416 if (data.whox_fields['u'])
417 wholine.push(user->ident);
419 // Include the user's IP address.
420 if (data.whox_fields['i'])
421 wholine.push(source_can_see_target ? user->GetIPString() : "255.255.255.255");
423 // Include the user's hostname.
424 if (data.whox_fields['h'])
425 wholine.push(user->GetHost(source_can_see_target && data.flags['x']));
427 // Include the server name.
428 if (data.whox_fields['s'])
430 if (ServerInstance->Config->HideServer.empty() || (source->HasPrivPermission("servers/auspex") && data.flags['x']))
431 wholine.push(user->server->GetName());
433 wholine.push(ServerInstance->Config->HideServer);
436 // Include the user's nickname.
437 if (data.whox_fields['n'])
438 wholine.push(user->nick);
440 // Include the user's flags.
441 if (data.whox_fields['f'])
444 std::string flags(user->IsAway() ? "G" : "H");
448 flags.push_back('*');
450 // Membership prefix.
453 char prefix = memb->GetPrefixChar();
455 flags.push_back(prefix);
461 // Include the number of hops between the users.
462 if (data.whox_fields['d'])
465 // Include the user's idle time.
466 if (data.whox_fields['l'])
468 LocalUser* lu = IS_LOCAL(user);
469 unsigned long idle = lu ? ServerInstance->Time() - lu->idle_lastmsg : 0;
470 wholine.push(ConvToStr(idle));
473 // Include the user's account name.
474 if (data.whox_fields['a'])
476 const AccountExtItem* accountext = GetAccountExtItem();
477 const std::string* account = accountext ? accountext->get(user) : NULL;
478 wholine.push(account ? *account : "0");
481 // Include the user's operator rank level.
482 if (data.whox_fields['o'])
483 wholine.push(memb ? ConvToStr(memb->getRank()) : "0");
485 // Include the user's real name.
486 if (data.whox_fields['r'])
487 wholine.push(user->GetRealName());
491 // We are not using WHOX so we just send a plain RFC response.
493 // Include the channel name.
494 wholine.push(memb ? memb->chan->name : "*");
496 // Include the user's ident.
497 wholine.push(user->ident);
499 // Include the user's hostname.
500 wholine.push(user->GetHost(source_can_see_target && data.flags['x']));
502 // Include the server name.
503 if (ServerInstance->Config->HideServer.empty() || (source->HasPrivPermission("servers/auspex") && data.flags['x']))
504 wholine.push(user->server->GetName());
506 wholine.push(ServerInstance->Config->HideServer);
508 // Include the user's nick.
509 wholine.push(user->nick);
511 // Include the user's flags.
514 std::string flags(user->IsAway() ? "G" : "H");
518 flags.push_back('*');
520 // Membership prefix.
523 char prefix = memb->GetPrefixChar();
525 flags.push_back(prefix);
531 // Include the number of hops between the users and the user's real name.
533 wholine.GetParams().back().append(user->GetRealName());
537 FIRST_MOD_RESULT(OnSendWhoLine, res, (source, parameters, user, memb, wholine));
538 if (res != MOD_RES_DENY)
539 data.results.push_back(wholine);
542 CmdResult CommandWho::HandleLocal(LocalUser* user, const Params& parameters)
544 WhoData data(parameters);
546 // Is the source running a WHO on a channel?
547 Channel* chan = ServerInstance->FindChan(data.matchtext);
549 WhoChannel(user, parameters, chan, data);
551 // If we only want to match against opers we only have to iterate the oper list.
552 else if (data.flags['o'])
553 WhoUsers(user, parameters, ServerInstance->Users->all_opers, data);
555 // Otherwise we have to use the global user list.
557 WhoUsers(user, parameters, ServerInstance->Users->GetUsers(), data);
559 // Send the results to the source.
560 for (std::vector<Numeric::Numeric>::const_iterator n = data.results.begin(); n != data.results.end(); ++n)
561 user->WriteNumeric(*n);
562 user->WriteNumeric(RPL_ENDOFWHO, (data.matchtext.empty() ? "*" : data.matchtext.c_str()), "End of /WHO list.");
564 // Penalize the source a bit for large queries with one unit of penalty per 200 results.
565 user->CommandFloodPenalty += data.results.size() * 5;
569 class CoreModWho : public Module
580 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
585 Version GetVersion() CXX11_OVERRIDE
587 return Version("Provides the WHO command", VF_VENDOR|VF_CORE);
591 MODULE_INIT(CoreModWho)