2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class CommandWho : public Command
27 bool CanView(Channel* chan, User* user);
28 bool opt_viewopersonly;
29 bool opt_showrealhost;
39 ChanModeReference secretmode;
40 ChanModeReference privatemode;
41 UserModeReference invisiblemode;
43 Membership* get_first_visible_channel(User* u)
45 for (UCListIter i = u->chans.begin(); i != u->chans.end(); ++i)
47 Membership* memb = *i;
48 if (!memb->chan->IsModeSet(secretmode))
55 /** Constructor for who.
57 CommandWho(Module* parent)
58 : Command(parent, "WHO", 1)
59 , secretmode(parent, "secret")
60 , privatemode(parent, "private")
61 , invisiblemode(parent, "invisible")
63 syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [ohurmMiaplf]";
66 void SendWhoLine(User* user, const std::vector<std::string>& parms, const std::string& initial, Membership* memb, User* u, std::vector<std::string>& whoresults);
68 * @param parameters The parameters to the command
69 * @param user The user issuing the command
70 * @return A value from CmdResult to indicate command success or failure.
72 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
73 bool whomatch(User* cuser, User* user, const char* matchtext);
76 bool CommandWho::whomatch(User* cuser, User* user, const char* matchtext)
79 bool positive = false;
81 if (user->registered != REG_ALL)
84 if (opt_local && !IS_LOCAL(user))
86 else if (opt_far && IS_LOCAL(user))
91 for (const char* n = matchtext; *n; n++)
103 if (user->IsModeSet(*n) != positive)
111 * This was previously one awesome pile of ugly nested if, when really, it didn't need
112 * to be, since only one condition was ever checked, a chained if works just fine.
118 const Extensible::ExtensibleStore& list = user->GetExtList();
119 for(Extensible::ExtensibleStore::const_iterator i = list.begin(); i != list.end(); ++i)
120 if (InspIRCd::Match(i->first->name, matchtext))
123 else if (opt_realname)
124 match = InspIRCd::Match(user->fullname, matchtext);
125 else if (opt_showrealhost)
126 match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
128 match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
131 irc::portparser portrange(matchtext, false);
133 while ((portno = portrange.GetToken()))
134 if (IS_LOCAL(user) && portno == IS_LOCAL(user)->GetServerPort())
141 match = InspIRCd::Match(user->awaymsg, matchtext);
144 long seconds = InspIRCd::Duration(matchtext);
146 // Okay, so time matching, we want all users connected `seconds' ago
147 if (user->age >= ServerInstance->Time() - seconds)
152 * Once the conditionals have been checked, only check dhost/nick/server
153 * if they didn't match this user -- and only match if we don't find a match.
155 * This should make things minutely faster, and again, less ugly.
159 match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
162 match = InspIRCd::Match(user->nick, matchtext);
164 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
165 if (!match && (ServerInstance->Config->HideWhoisServer.empty() || cuser->HasPrivPermission("users/auspex")))
166 match = InspIRCd::Match(user->server->GetName(), matchtext);
172 bool CommandWho::CanView(Channel* chan, User* user)
177 /* Bug #383 - moved higher up the list, because if we are in the channel
178 * we can see all its users
180 if (chan->HasUser(user))
183 if (user->HasPrivPermission("users/auspex"))
185 /* Cant see inside a +s or a +p channel unless we are a member (see above) */
186 else if (!chan->IsModeSet(secretmode) && !chan->IsModeSet(privatemode))
192 void CommandWho::SendWhoLine(User* user, const std::vector<std::string>& parms, const std::string& initial, Membership* memb, User* u, std::vector<std::string>& whoresults)
195 memb = get_first_visible_channel(u);
197 std::string wholine = initial + (memb ? memb->chan->name : "*") + " " + u->ident + " " +
198 (opt_showrealhost ? u->host : u->dhost) + " ";
199 if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
200 wholine.append(ServerInstance->Config->HideWhoisServer);
202 wholine.append(u->server->GetName());
204 wholine.append(" " + u->nick + " ");
219 wholine.push_back('*');
224 char prefix = memb->GetPrefixChar();
226 wholine.push_back(prefix);
229 wholine.append(" :0 " + u->fullname);
231 FOREACH_MOD(OnSendWhoLine, (user, parms, u, memb, wholine));
233 if (!wholine.empty())
234 whoresults.push_back(wholine);
237 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
241 * The <name> passed to WHO is matched against users' host, server, real
243 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
247 opt_viewopersonly = false;
248 opt_showrealhost = false;
249 opt_realname = false;
252 opt_metadata = false;
259 std::vector<std::string> whoresults;
260 std::string initial = "352 " + user->nick + " ";
262 /* Change '0' into '*' so the wildcard matcher can grok it */
263 std::string matchtext = ((parameters[0] == "0") ? "*" : parameters[0]);
265 // WHO flags count as a wildcard
266 bool usingwildcards = ((parameters.size() > 1) || (matchtext.find_first_of("*?.") != std::string::npos));
268 if (parameters.size() > 1)
270 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
275 opt_viewopersonly = true;
278 if (user->HasPrivPermission("users/auspex"))
279 opt_showrealhost = true;
285 if (user->HasPrivPermission("users/auspex"))
289 if (user->HasPrivPermission("users/auspex"))
296 if (user->HasPrivPermission("users/auspex"))
303 if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
307 if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
318 /* who on a channel? */
319 Channel* ch = ServerInstance->FindChan(matchtext);
323 if (CanView(ch,user))
325 bool inside = ch->HasUser(user);
327 /* who on a channel. */
328 const UserMembList *cu = ch->GetUsers();
330 for (UserMembCIter i = cu->begin(); i != cu->end(); i++)
332 /* None of this applies if we WHO ourselves */
333 if (user != i->first)
335 /* opers only, please */
336 if (opt_viewopersonly && !i->first->IsOper())
339 /* If we're not inside the channel, hide +i users */
340 if (i->first->IsModeSet(invisiblemode) && !inside && !user->HasPrivPermission("users/auspex"))
344 SendWhoLine(user, parameters, initial, i->second, i->first, whoresults);
350 /* Match against wildcard of nick, server or host */
351 if (opt_viewopersonly)
353 /* Showing only opers */
354 const UserManager::OperList& opers = ServerInstance->Users->all_opers;
355 for (UserManager::OperList::const_iterator i = opers.begin(); i != opers.end(); ++i)
359 if (whomatch(user, oper, matchtext.c_str()))
361 if (!user->SharesChannelWith(oper))
363 if (usingwildcards && (!oper->IsModeSet(invisiblemode)) && (!user->HasPrivPermission("users/auspex")))
367 SendWhoLine(user, parameters, initial, NULL, oper, whoresults);
373 const user_hash& users = ServerInstance->Users->GetUsers();
374 for (user_hash::const_iterator i = users.begin(); i != users.end(); ++i)
376 if (whomatch(user, i->second, matchtext.c_str()))
378 if (!user->SharesChannelWith(i->second))
380 if (usingwildcards && (i->second->IsModeSet(invisiblemode)) && (!user->HasPrivPermission("users/auspex")))
384 SendWhoLine(user, parameters, initial, NULL, i->second, whoresults);
389 /* Send the results out */
390 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
392 user->WriteNumeric(RPL_ENDOFWHO, "%s :End of /WHO list.", *parameters[0].c_str() ? parameters[0].c_str() : "*");
394 // Penalize the user a bit for large queries
395 // (add one unit of penalty per 200 results)
397 IS_LOCAL(user)->CommandFloodPenalty += whoresults.size() * 5;
401 COMMAND_INIT(CommandWho)