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"
25 #include "modules/who.h"
37 struct WhoData : public Who::Request
39 WhoData(const CommandBase::Params& parameters)
41 // Find the matchtext and swap the 0 for a * so we can use InspIRCd::Match on it.
42 matchtext = parameters.size() > 2 ? parameters[2] : parameters[0];
46 // Fuzzy matches are when the source has not specified a specific user.
47 fuzzy_match = (parameters.size() > 1) || (matchtext.find_first_of("*?.") != std::string::npos);
49 // If flags have been specified by the source.
50 if (parameters.size() > 1)
52 std::bitset<UCHAR_MAX>* current_bitset = &flags;
53 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
55 unsigned char chr = static_cast<unsigned char>(*iter);
57 // If the source specifies a percentage the rest of the flags are WHOX fields.
61 current_bitset = &whox_fields;
65 // If we are in WHOX mode and the source specifies a comma
66 // the rest of the parameter is the query type.
67 if (whox && chr == ',')
69 whox_querytype.assign(++iter, parameters[1].end());
73 // The source specified a matching flag.
74 current_bitset->set(chr);
80 class CommandWho : public SplitCommand
83 ChanModeReference secretmode;
84 ChanModeReference privatemode;
85 UserModeReference hidechansmode;
86 UserModeReference invisiblemode;
87 Events::ModuleEventProvider whoevprov;
89 /** Determines whether a user can view the users of a channel. */
90 bool CanView(Channel* chan, User* user)
92 // If we are in a channel we can view all users in it.
93 if (chan->HasUser(user))
96 // Opers with the users/auspex priv can see everything.
97 if (user->HasPrivPermission("users/auspex"))
100 // You can see inside a channel from outside unless it is secret or private.
101 return !chan->IsModeSet(secretmode) && !chan->IsModeSet(privatemode);
104 /** Gets the first channel which is visible between the source and the target users. */
105 Membership* GetFirstVisibleChannel(LocalUser* source, User* user)
107 for (User::ChanList::iterator iter = user->chans.begin(); iter != user->chans.end(); ++iter)
109 Membership* memb = *iter;
111 // TODO: move the +I check into m_hidechans.
112 bool has_modes = memb->chan->IsModeSet(secretmode) || memb->chan->IsModeSet(privatemode) || user->IsModeSet(hidechansmode);
113 if (source == user || !has_modes || memb->chan->HasUser(source))
119 /** Determines whether WHO flags match a specific channel user. */
120 bool MatchChannel(LocalUser* source, Membership* memb, WhoData& data);
122 /** Determines whether WHO flags match a specific user. */
123 static bool MatchUser(LocalUser* source, User* target, WhoData& data);
125 /** Performs a WHO request on a channel. */
126 void WhoChannel(LocalUser* source, const std::vector<std::string>& parameters, Channel* c, WhoData& data);
128 /** Template for getting a user from various types of collection. */
130 static User* GetUser(T& t);
132 /** Performs a WHO request on a list of users. */
134 void WhoUsers(LocalUser* source, const std::vector<std::string>& parameters, const T& users, WhoData& data);
137 CommandWho(Module* parent)
138 : SplitCommand(parent, "WHO", 1, 3)
139 , secretmode(parent, "secret")
140 , privatemode(parent, "private")
141 , hidechansmode(parent, "hidechans")
142 , invisiblemode(parent, "invisible")
143 , whoevprov(parent, "event/who")
145 allow_empty_last_param = false;
146 syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [[Aafhilmnoprstux][%acdfhilnorstu] <server>|<nickname>|<channel>|<realname>|<host>|0]";
149 /** Sends a WHO reply to a user. */
150 void SendWhoLine(LocalUser* user, const std::vector<std::string>& parameters, Membership* memb, User* u, WhoData& data);
152 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE;
155 template<> User* CommandWho::GetUser(UserManager::OperList::const_iterator& t) { return *t; }
156 template<> User* CommandWho::GetUser(user_hash::const_iterator& t) { return t->second; }
158 bool CommandWho::MatchChannel(LocalUser* source, Membership* memb, WhoData& data)
160 bool source_has_users_auspex = source->HasPrivPermission("users/auspex");
161 bool source_can_see_server = ServerInstance->Config->HideServer.empty() || source_has_users_auspex;
163 // The source only wants remote users. This user is eligible if:
164 // (1) The source can't see server information.
165 // (2) The source is not local to the current server.
166 LocalUser* lu = IS_LOCAL(memb->user);
167 if (data.flags['f'] && source_can_see_server && lu)
170 // The source only wants local users. This user is eligible if:
171 // (1) The source can't see server information.
172 // (2) The source is local to the current server.
173 if (data.flags['l'] && source_can_see_server && !lu)
176 // Only show operators if the oper flag has been specified.
177 if (data.flags['o'] && !memb->user->IsOper())
180 // All other flags are ignored for channels.
184 bool CommandWho::MatchUser(LocalUser* source, User* user, WhoData& data)
186 // Users who are not fully registered can never match.
187 if (user->registered != REG_ALL)
190 bool source_has_users_auspex = source->HasPrivPermission("users/auspex");
191 bool source_can_see_target = source == user || source_has_users_auspex;
192 bool source_can_see_server = ServerInstance->Config->HideServer.empty() || source_has_users_auspex;
194 // The source only wants remote users. This user is eligible if:
195 // (1) The source can't see server information.
196 // (2) The source is not local to the current server.
197 LocalUser* lu = IS_LOCAL(user);
198 if (data.flags['f'] && source_can_see_server && lu)
201 // The source only wants local users. This user is eligible if:
202 // (1) The source can't see server information.
203 // (2) The source is local to the current server.
204 if (data.flags['l'] && source_can_see_server && !lu)
207 // The source wants to match against users' away messages.
210 match = user->IsAway() && InspIRCd::Match(user->awaymsg, data.matchtext, ascii_case_insensitive_map);
212 // The source wants to match against users' account names.
213 else if (data.flags['a'])
215 const AccountExtItem* accountext = GetAccountExtItem();
216 const std::string* account = accountext ? accountext->get(user) : NULL;
217 match = account && InspIRCd::Match(*account, data.matchtext);
220 // The source wants to match against users' hostnames.
221 else if (data.flags['h'])
223 const std::string host = user->GetHost(source_can_see_target && data.flags['x']);
224 match = InspIRCd::Match(host, data.matchtext, ascii_case_insensitive_map);
227 // The source wants to match against users' IP addresses.
228 else if (data.flags['i'])
229 match = source_can_see_target && InspIRCd::MatchCIDR(user->GetIPString(), data.matchtext, ascii_case_insensitive_map);
231 // The source wants to match against users' modes.
232 else if (data.flags['m'])
234 if (source_can_see_target)
237 for (std::string::const_iterator iter = data.matchtext.begin(); iter != data.matchtext.end(); ++iter)
239 unsigned char chr = static_cast<unsigned char>(*iter);
242 // The following user modes should be set.
247 // The following user modes should be unset.
253 if (user->IsModeSet(chr) != set)
259 // All of the modes matched.
264 // The source wants to match against users' nicks.
265 else if (data.flags['n'])
266 match = InspIRCd::Match(user->nick, data.matchtext);
268 // The source wants to match against users' connection ports.
269 else if (data.flags['p'])
271 if (source_can_see_target && lu)
273 irc::portparser portrange(data.matchtext, false);
275 while ((port = portrange.GetToken()))
277 if (port == lu->GetServerPort())
286 // The source wants to match against users' real names.
287 else if (data.flags['r'])
288 match = InspIRCd::Match(user->GetRealName(), data.matchtext, ascii_case_insensitive_map);
290 else if (data.flags['s'])
292 bool show_real_server_name = ServerInstance->Config->HideServer.empty() || (source->HasPrivPermission("servers/auspex") && data.flags['x']);
293 const std::string server = show_real_server_name ? user->server->GetName() : ServerInstance->Config->HideServer;
294 match = InspIRCd::Match(server, data.matchtext, ascii_case_insensitive_map);
297 // The source wants to match against users' connection times.
298 else if (data.flags['t'])
300 time_t seconds = ServerInstance->Time() - InspIRCd::Duration(data.matchtext);
301 if (user->signon >= seconds)
305 // The source wants to match against users' idents.
306 else if (data.flags['u'])
307 match = InspIRCd::Match(user->ident, data.matchtext, ascii_case_insensitive_map);
309 // The <name> passed to WHO is matched against users' host, server,
310 // real name and nickname if the channel <name> cannot be found.
313 const std::string host = user->GetHost(source_can_see_target && data.flags['x']);
314 match = InspIRCd::Match(host, data.matchtext, ascii_case_insensitive_map);
318 bool show_real_server_name = ServerInstance->Config->HideServer.empty() || (source->HasPrivPermission("servers/auspex") && data.flags['x']);
319 const std::string server = show_real_server_name ? user->server->GetName() : ServerInstance->Config->HideServer;
320 match = InspIRCd::Match(server, data.matchtext, ascii_case_insensitive_map);
324 match = InspIRCd::Match(user->GetRealName(), data.matchtext, ascii_case_insensitive_map);
327 match = InspIRCd::Match(user->nick, data.matchtext);
333 void CommandWho::WhoChannel(LocalUser* source, const std::vector<std::string>& parameters, Channel* chan, WhoData& data)
335 if (!CanView(chan, source))
338 bool inside = chan->HasUser(source);
339 const Channel::MemberMap& users = chan->GetUsers();
340 for (Channel::MemberMap::const_iterator iter = users.begin(); iter != users.end(); ++iter)
342 User* user = iter->first;
343 Membership* memb = iter->second;
345 // Only show invisible users if the source is in the channel or has the users/auspex priv.
346 if (!inside && user->IsModeSet(invisiblemode) && !source->HasPrivPermission("users/auspex"))
349 // Skip the user if it doesn't match the query.
350 if (!MatchChannel(source, memb, data))
353 SendWhoLine(source, parameters, memb, user, data);
358 void CommandWho::WhoUsers(LocalUser* source, const std::vector<std::string>& parameters, const T& users, WhoData& data)
360 for (typename T::const_iterator iter = users.begin(); iter != users.end(); ++iter)
362 User* user = GetUser(iter);
364 // Only show users in response to a fuzzy WHO if we can see them normally.
365 bool can_see_normally = user == source || source->SharesChannelWith(user) || !user->IsModeSet(invisiblemode);
366 if (data.fuzzy_match && !can_see_normally && !source->HasPrivPermission("users/auspex"))
369 // Skip the user if it doesn't match the query.
370 if (!MatchUser(source, user, data))
373 SendWhoLine(source, parameters, NULL, user, data);
377 void CommandWho::SendWhoLine(LocalUser* source, const std::vector<std::string>& parameters, Membership* memb, User* user, WhoData& data)
380 memb = GetFirstVisibleChannel(source, user);
382 bool source_can_see_target = source == user || source->HasPrivPermission("users/auspex");
383 Numeric::Numeric wholine(data.whox ? RPL_WHOSPCRPL : RPL_WHOREPLY);
386 // The source used WHOX so we send a fancy customised response.
388 // Include the query type in the reply.
389 if (data.whox_fields['t'])
390 wholine.push(data.whox_querytype.empty() || data.whox_querytype.length() > 3 ? "0" : data.whox_querytype);
392 // Include the first channel name.
393 if (data.whox_fields['c'])
394 wholine.push(memb ? memb->chan->name : "*");
396 // Include the user's ident.
397 if (data.whox_fields['u'])
398 wholine.push(user->ident);
400 // Include the user's IP address.
401 if (data.whox_fields['i'])
402 wholine.push(source_can_see_target ? user->GetIPString() : "255.255.255.255");
404 // Include the user's hostname.
405 if (data.whox_fields['h'])
406 wholine.push(user->GetHost(source_can_see_target && data.flags['x']));
408 // Include the server name.
409 if (data.whox_fields['s'])
411 if (ServerInstance->Config->HideServer.empty() || (source->HasPrivPermission("servers/auspex") && data.flags['x']))
412 wholine.push(user->server->GetName());
414 wholine.push(ServerInstance->Config->HideServer);
417 // Include the user's nickname.
418 if (data.whox_fields['n'])
419 wholine.push(user->nick);
421 // Include the user's flags.
422 if (data.whox_fields['f'])
425 std::string flags(user->IsAway() ? "G" : "H");
429 flags.push_back('*');
431 // Membership prefix.
434 char prefix = memb->GetPrefixChar();
436 flags.push_back(prefix);
442 // Include the number of hops between the users.
443 if (data.whox_fields['d'])
446 // Include the user's idle time.
447 if (data.whox_fields['l'])
449 LocalUser* lu = IS_LOCAL(user);
450 unsigned long idle = lu ? ServerInstance->Time() - lu->idle_lastmsg : 0;
451 wholine.push(ConvToStr(idle));
454 // Include the user's account name.
455 if (data.whox_fields['a'])
457 const AccountExtItem* accountext = GetAccountExtItem();
458 const std::string* account = accountext ? accountext->get(user) : NULL;
459 wholine.push(account ? *account : "0");
462 // Include the user's operator rank level.
463 if (data.whox_fields['o'])
464 wholine.push(memb ? ConvToStr(memb->getRank()) : "0");
466 // Include the user's real name.
467 if (data.whox_fields['r'])
468 wholine.push(user->GetRealName());
472 // We are not using WHOX so we just send a plain RFC response.
474 // Include the channel name.
475 wholine.push(memb ? memb->chan->name : "*");
477 // Include the user's ident.
478 wholine.push(user->ident);
480 // Include the user's hostname.
481 wholine.push(user->GetHost(source_can_see_target && data.flags['x']));
483 // Include the server name.
484 if (ServerInstance->Config->HideServer.empty() || (source->HasPrivPermission("servers/auspex") && data.flags['x']))
485 wholine.push(user->server->GetName());
487 wholine.push(ServerInstance->Config->HideServer);
489 // Include the user's nick.
490 wholine.push(user->nick);
492 // Include the user's flags.
495 std::string flags(user->IsAway() ? "G" : "H");
499 flags.push_back('*');
501 // Membership prefix.
504 char prefix = memb->GetPrefixChar();
506 flags.push_back(prefix);
512 // Include the number of hops between the users and the user's real name.
514 wholine.GetParams().back().append(user->GetRealName());
518 FIRST_MOD_RESULT_CUSTOM(whoevprov, Who::EventListener, OnWhoLine, res, (data, source, user, memb, wholine));
519 if (res != MOD_RES_DENY)
520 data.results.push_back(wholine);
523 CmdResult CommandWho::HandleLocal(LocalUser* user, const Params& parameters)
525 WhoData data(parameters);
527 // Is the source running a WHO on a channel?
528 Channel* chan = ServerInstance->FindChan(data.matchtext);
530 WhoChannel(user, parameters, chan, data);
532 // If we only want to match against opers we only have to iterate the oper list.
533 else if (data.flags['o'])
534 WhoUsers(user, parameters, ServerInstance->Users->all_opers, data);
536 // Otherwise we have to use the global user list.
538 WhoUsers(user, parameters, ServerInstance->Users->GetUsers(), data);
540 // Send the results to the source.
541 for (std::vector<Numeric::Numeric>::const_iterator n = data.results.begin(); n != data.results.end(); ++n)
542 user->WriteNumeric(*n);
543 user->WriteNumeric(RPL_ENDOFWHO, (data.matchtext.empty() ? "*" : data.matchtext.c_str()), "End of /WHO list.");
545 // Penalize the source a bit for large queries with one unit of penalty per 200 results.
546 user->CommandFloodPenalty += data.results.size() * 5;
550 class CoreModWho : public Module
561 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
566 Version GetVersion() CXX11_OVERRIDE
568 return Version("Provides the WHO command", VF_VENDOR|VF_CORE);
572 MODULE_INIT(CoreModWho)