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 static const char whox_field_order[] = "tcuihsnfdlaor";
38 static const char who_field_order[] = "cuhsnf";
40 struct WhoData : public Who::Request
42 std::string query_flag_order;
44 bool GetFlagIndex(char flag, size_t& out) const CXX11_OVERRIDE
46 out = query_flag_order.find(flag);
47 return out != std::string::npos;
50 WhoData(const CommandBase::Params& parameters)
52 // Find the matchtext and swap the 0 for a * so we can use InspIRCd::Match on it.
53 matchtext = parameters.size() > 2 ? parameters[2] : parameters[0];
57 // Fuzzy matches are when the source has not specified a specific user.
58 fuzzy_match = (parameters.size() > 1) || (matchtext.find_first_of("*?.") != std::string::npos);
60 // If flags have been specified by the source.
61 if (parameters.size() > 1)
63 std::bitset<UCHAR_MAX>* current_bitset = &flags;
64 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
66 unsigned char chr = static_cast<unsigned char>(*iter);
68 // If the source specifies a percentage the rest of the flags are WHOX fields.
72 current_bitset = &whox_fields;
76 // If we are in WHOX mode and the source specifies a comma
77 // the rest of the parameter is the query type.
78 if (whox && chr == ',')
80 whox_querytype.assign(++iter, parameters[1].end());
84 // The source specified a matching flag.
85 current_bitset->set(chr);
91 for (const char *c = whox_field_order; c; c++)
94 query_flag_order.push_back(*c);
98 query_flag_order = who_field_order;
102 class CommandWho : public SplitCommand
105 ChanModeReference secretmode;
106 ChanModeReference privatemode;
107 UserModeReference hidechansmode;
108 UserModeReference invisiblemode;
109 Events::ModuleEventProvider whoevprov;
111 /** Determines whether a user can view the users of a channel. */
112 bool CanView(Channel* chan, User* user)
114 // If we are in a channel we can view all users in it.
115 if (chan->HasUser(user))
118 // Opers with the users/auspex priv can see everything.
119 if (user->HasPrivPermission("users/auspex"))
122 // You can see inside a channel from outside unless it is secret or private.
123 return !chan->IsModeSet(secretmode) && !chan->IsModeSet(privatemode);
126 /** Gets the first channel which is visible between the source and the target users. */
127 Membership* GetFirstVisibleChannel(LocalUser* source, User* user)
129 for (User::ChanList::iterator iter = user->chans.begin(); iter != user->chans.end(); ++iter)
131 Membership* memb = *iter;
133 // TODO: move the +I check into m_hidechans.
134 bool has_modes = memb->chan->IsModeSet(secretmode) || memb->chan->IsModeSet(privatemode) || user->IsModeSet(hidechansmode);
135 if (source == user || !has_modes || memb->chan->HasUser(source))
141 /** Determines whether WHO flags match a specific channel user. */
142 bool MatchChannel(LocalUser* source, Membership* memb, WhoData& data);
144 /** Determines whether WHO flags match a specific user. */
145 static bool MatchUser(LocalUser* source, User* target, WhoData& data);
147 /** Performs a WHO request on a channel. */
148 void WhoChannel(LocalUser* source, const std::vector<std::string>& parameters, Channel* c, WhoData& data);
150 /** Template for getting a user from various types of collection. */
152 static User* GetUser(T& t);
154 /** Performs a WHO request on a list of users. */
156 void WhoUsers(LocalUser* source, const std::vector<std::string>& parameters, const T& users, WhoData& data);
159 CommandWho(Module* parent)
160 : SplitCommand(parent, "WHO", 1, 3)
161 , secretmode(parent, "secret")
162 , privatemode(parent, "private")
163 , hidechansmode(parent, "hidechans")
164 , invisiblemode(parent, "invisible")
165 , whoevprov(parent, "event/who")
167 allow_empty_last_param = false;
168 syntax = "<server>|<nick>|<channel>|<realname>|<host>|0 [[Aafhilmnoprstux][%acdfhilnorstu] <server>|<nick>|<channel>|<realname>|<host>|0]";
171 /** Sends a WHO reply to a user. */
172 void SendWhoLine(LocalUser* user, const std::vector<std::string>& parameters, Membership* memb, User* u, WhoData& data);
174 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE;
177 template<> User* CommandWho::GetUser(UserManager::OperList::const_iterator& t) { return *t; }
178 template<> User* CommandWho::GetUser(user_hash::const_iterator& t) { return t->second; }
180 bool CommandWho::MatchChannel(LocalUser* source, Membership* memb, WhoData& data)
182 bool source_has_users_auspex = source->HasPrivPermission("users/auspex");
183 bool source_can_see_server = ServerInstance->Config->HideServer.empty() || source_has_users_auspex;
185 // The source only wants remote users. This user is eligible if:
186 // (1) The source can't see server information.
187 // (2) The source is not local to the current server.
188 LocalUser* lu = IS_LOCAL(memb->user);
189 if (data.flags['f'] && source_can_see_server && lu)
192 // The source only wants local users. This user is eligible if:
193 // (1) The source can't see server information.
194 // (2) The source is local to the current server.
195 if (data.flags['l'] && source_can_see_server && !lu)
198 // Only show operators if the oper flag has been specified.
199 if (data.flags['o'] && !memb->user->IsOper())
202 // All other flags are ignored for channels.
206 bool CommandWho::MatchUser(LocalUser* source, User* user, WhoData& data)
208 // Users who are not fully registered can never match.
209 if (user->registered != REG_ALL)
212 bool source_has_users_auspex = source->HasPrivPermission("users/auspex");
213 bool source_can_see_target = source == user || source_has_users_auspex;
214 bool source_can_see_server = ServerInstance->Config->HideServer.empty() || source_has_users_auspex;
216 // The source only wants remote users. This user is eligible if:
217 // (1) The source can't see server information.
218 // (2) The source is not local to the current server.
219 LocalUser* lu = IS_LOCAL(user);
220 if (data.flags['f'] && source_can_see_server && lu)
223 // The source only wants local users. This user is eligible if:
224 // (1) The source can't see server information.
225 // (2) The source is local to the current server.
226 if (data.flags['l'] && source_can_see_server && !lu)
229 // The source wants to match against users' away messages.
232 match = user->IsAway() && InspIRCd::Match(user->awaymsg, data.matchtext, ascii_case_insensitive_map);
234 // The source wants to match against users' account names.
235 else if (data.flags['a'])
237 const AccountExtItem* accountext = GetAccountExtItem();
238 const std::string* account = accountext ? accountext->get(user) : NULL;
239 match = account && InspIRCd::Match(*account, data.matchtext);
242 // The source wants to match against users' hostnames.
243 else if (data.flags['h'])
245 const std::string host = user->GetHost(source_can_see_target && data.flags['x']);
246 match = InspIRCd::Match(host, data.matchtext, ascii_case_insensitive_map);
249 // The source wants to match against users' IP addresses.
250 else if (data.flags['i'])
251 match = source_can_see_target && InspIRCd::MatchCIDR(user->GetIPString(), data.matchtext, ascii_case_insensitive_map);
253 // The source wants to match against users' modes.
254 else if (data.flags['m'])
256 if (source_can_see_target)
259 for (std::string::const_iterator iter = data.matchtext.begin(); iter != data.matchtext.end(); ++iter)
261 unsigned char chr = static_cast<unsigned char>(*iter);
264 // The following user modes should be set.
269 // The following user modes should be unset.
275 if (user->IsModeSet(chr) != set)
281 // All of the modes matched.
286 // The source wants to match against users' nicks.
287 else if (data.flags['n'])
288 match = InspIRCd::Match(user->nick, data.matchtext);
290 // The source wants to match against users' connection ports.
291 else if (data.flags['p'])
293 if (source_can_see_target && lu)
295 irc::portparser portrange(data.matchtext, false);
297 while ((port = portrange.GetToken()))
299 if (port == lu->server_sa.port())
308 // The source wants to match against users' real names.
309 else if (data.flags['r'])
310 match = InspIRCd::Match(user->GetRealName(), data.matchtext, ascii_case_insensitive_map);
312 else if (data.flags['s'])
314 bool show_real_server_name = ServerInstance->Config->HideServer.empty() || (source->HasPrivPermission("servers/auspex") && data.flags['x']);
315 const std::string server = show_real_server_name ? user->server->GetName() : ServerInstance->Config->HideServer;
316 match = InspIRCd::Match(server, data.matchtext, ascii_case_insensitive_map);
319 // The source wants to match against users' connection times.
320 else if (data.flags['t'])
322 time_t seconds = ServerInstance->Time() - InspIRCd::Duration(data.matchtext);
323 if (user->signon >= seconds)
327 // The source wants to match against users' idents.
328 else if (data.flags['u'])
329 match = InspIRCd::Match(user->ident, data.matchtext, ascii_case_insensitive_map);
331 // The <name> passed to WHO is matched against users' host, server,
332 // real name and nickname if the channel <name> cannot be found.
335 const std::string host = user->GetHost(source_can_see_target && data.flags['x']);
336 match = InspIRCd::Match(host, data.matchtext, ascii_case_insensitive_map);
340 bool show_real_server_name = ServerInstance->Config->HideServer.empty() || (source->HasPrivPermission("servers/auspex") && data.flags['x']);
341 const std::string server = show_real_server_name ? user->server->GetName() : ServerInstance->Config->HideServer;
342 match = InspIRCd::Match(server, data.matchtext, ascii_case_insensitive_map);
346 match = InspIRCd::Match(user->GetRealName(), data.matchtext, ascii_case_insensitive_map);
349 match = InspIRCd::Match(user->nick, data.matchtext);
355 void CommandWho::WhoChannel(LocalUser* source, const std::vector<std::string>& parameters, Channel* chan, WhoData& data)
357 if (!CanView(chan, source))
360 bool inside = chan->HasUser(source);
361 const Channel::MemberMap& users = chan->GetUsers();
362 for (Channel::MemberMap::const_iterator iter = users.begin(); iter != users.end(); ++iter)
364 User* user = iter->first;
365 Membership* memb = iter->second;
367 // Only show invisible users if the source is in the channel or has the users/auspex priv.
368 if (!inside && user->IsModeSet(invisiblemode) && !source->HasPrivPermission("users/auspex"))
371 // Skip the user if it doesn't match the query.
372 if (!MatchChannel(source, memb, data))
375 SendWhoLine(source, parameters, memb, user, data);
380 void CommandWho::WhoUsers(LocalUser* source, const std::vector<std::string>& parameters, const T& users, WhoData& data)
382 for (typename T::const_iterator iter = users.begin(); iter != users.end(); ++iter)
384 User* user = GetUser(iter);
386 // Only show users in response to a fuzzy WHO if we can see them normally.
387 bool can_see_normally = user == source || source->SharesChannelWith(user) || !user->IsModeSet(invisiblemode);
388 if (data.fuzzy_match && !can_see_normally && !source->HasPrivPermission("users/auspex"))
391 // Skip the user if it doesn't match the query.
392 if (!MatchUser(source, user, data))
395 SendWhoLine(source, parameters, NULL, user, data);
399 void CommandWho::SendWhoLine(LocalUser* source, const std::vector<std::string>& parameters, Membership* memb, User* user, WhoData& data)
402 memb = GetFirstVisibleChannel(source, user);
404 bool source_can_see_target = source == user || source->HasPrivPermission("users/auspex");
405 Numeric::Numeric wholine(data.whox ? RPL_WHOSPCRPL : RPL_WHOREPLY);
408 // The source used WHOX so we send a fancy customised response.
410 // Include the query type in the reply.
411 if (data.whox_fields['t'])
412 wholine.push(data.whox_querytype.empty() || data.whox_querytype.length() > 3 ? "0" : data.whox_querytype);
414 // Include the first channel name.
415 if (data.whox_fields['c'])
416 wholine.push(memb ? memb->chan->name : "*");
418 // Include the user's ident.
419 if (data.whox_fields['u'])
420 wholine.push(user->ident);
422 // Include the user's IP address.
423 if (data.whox_fields['i'])
424 wholine.push(source_can_see_target ? user->GetIPString() : "255.255.255.255");
426 // Include the user's hostname.
427 if (data.whox_fields['h'])
428 wholine.push(user->GetHost(source_can_see_target && data.flags['x']));
430 // Include the server name.
431 if (data.whox_fields['s'])
433 if (ServerInstance->Config->HideServer.empty() || (source->HasPrivPermission("servers/auspex") && data.flags['x']))
434 wholine.push(user->server->GetName());
436 wholine.push(ServerInstance->Config->HideServer);
439 // Include the user's nickname.
440 if (data.whox_fields['n'])
441 wholine.push(user->nick);
443 // Include the user's flags.
444 if (data.whox_fields['f'])
447 std::string flags(user->IsAway() ? "G" : "H");
451 flags.push_back('*');
453 // Membership prefix.
456 char prefix = memb->GetPrefixChar();
458 flags.push_back(prefix);
464 // Include the number of hops between the users.
465 if (data.whox_fields['d'])
468 // Include the user's idle time.
469 if (data.whox_fields['l'])
471 LocalUser* lu = IS_LOCAL(user);
472 unsigned long idle = lu ? ServerInstance->Time() - lu->idle_lastmsg : 0;
473 wholine.push(ConvToStr(idle));
476 // Include the user's account name.
477 if (data.whox_fields['a'])
479 const AccountExtItem* accountext = GetAccountExtItem();
480 const std::string* account = accountext ? accountext->get(user) : NULL;
481 wholine.push(account ? *account : "0");
484 // Include the user's operator rank level.
485 if (data.whox_fields['o'])
486 wholine.push(memb ? ConvToStr(memb->getRank()) : "0");
488 // Include the user's real name.
489 if (data.whox_fields['r'])
490 wholine.push(user->GetRealName());
494 // We are not using WHOX so we just send a plain RFC response.
496 // Include the channel name.
497 wholine.push(memb ? memb->chan->name : "*");
499 // Include the user's ident.
500 wholine.push(user->ident);
502 // Include the user's hostname.
503 wholine.push(user->GetHost(source_can_see_target && data.flags['x']));
505 // Include the server name.
506 if (ServerInstance->Config->HideServer.empty() || (source->HasPrivPermission("servers/auspex") && data.flags['x']))
507 wholine.push(user->server->GetName());
509 wholine.push(ServerInstance->Config->HideServer);
511 // Include the user's nick.
512 wholine.push(user->nick);
514 // Include the user's flags.
517 std::string flags(user->IsAway() ? "G" : "H");
521 flags.push_back('*');
523 // Membership prefix.
526 char prefix = memb->GetPrefixChar();
528 flags.push_back(prefix);
534 // Include the number of hops between the users and the user's real name.
536 wholine.GetParams().back().append(user->GetRealName());
540 FIRST_MOD_RESULT_CUSTOM(whoevprov, Who::EventListener, OnWhoLine, res, (data, source, user, memb, wholine));
541 if (res != MOD_RES_DENY)
542 data.results.push_back(wholine);
545 CmdResult CommandWho::HandleLocal(LocalUser* user, const Params& parameters)
547 WhoData data(parameters);
549 // Is the source running a WHO on a channel?
550 Channel* chan = ServerInstance->FindChan(data.matchtext);
552 WhoChannel(user, parameters, chan, data);
554 // If we only want to match against opers we only have to iterate the oper list.
555 else if (data.flags['o'])
556 WhoUsers(user, parameters, ServerInstance->Users->all_opers, data);
558 // Otherwise we have to use the global user list.
560 WhoUsers(user, parameters, ServerInstance->Users->GetUsers(), data);
562 // Send the results to the source.
563 for (std::vector<Numeric::Numeric>::const_iterator n = data.results.begin(); n != data.results.end(); ++n)
564 user->WriteNumeric(*n);
565 user->WriteNumeric(RPL_ENDOFWHO, (data.matchtext.empty() ? "*" : data.matchtext.c_str()), "End of /WHO list.");
567 // Penalize the source a bit for large queries with one unit of penalty per 200 results.
568 user->CommandFloodPenalty += data.results.size() * 5;
572 class CoreModWho : public Module
583 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
588 Version GetVersion() CXX11_OVERRIDE
590 return Version("Provides the WHO command", VF_VENDOR|VF_CORE);
594 MODULE_INIT(CoreModWho)