1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 // include the common header files
24 /** Handle /WHO. These command handlers can be reloaded by the core,
25 * and handle basic RFC1459 commands. Commands within modules work
26 * the same way, however, they can be fully unloaded, where these
29 class CommandWho : public Command
31 bool CanView(Channel* chan, User* user);
32 bool opt_viewopersonly;
33 bool opt_showrealhost;
46 /** Constructor for who.
48 CommandWho (InspIRCd* Instance, Module* parent) : Command(Instance,parent,"WHO", 0, 1, false, 2) { syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [ohurmMiaplf]"; }
49 void SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults);
51 * @param parameters The parameters to the comamnd
52 * @param pcnt The number of parameters passed to teh command
53 * @param user The user issuing the command
54 * @return A value from CmdResult to indicate command success or failure.
56 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
57 bool whomatch(User* user, const char* matchtext);
63 static const std::string star = "*";
65 static const std::string& get_first_visible_channel(User *u)
67 UCListIter i = u->chans.begin();
68 if (i != u->chans.end())
70 if (!i->first->IsModeSet('s'))
71 return i->first->name;
77 bool CommandWho::whomatch(User* user, const char* matchtext)
80 bool positive = false;
83 if (user->registered != REG_ALL)
86 if (opt_local && !IS_LOCAL(user))
88 else if (opt_far && IS_LOCAL(user))
93 for (const char* n = matchtext; *n; n++)
105 if (user->IsModeSet(*n) != positive)
113 * This was previously one awesome pile of ugly nested if, when really, it didn't need
114 * to be, since only one condition was ever checked, a chained if works just fine.
118 match = user->GetExt(matchtext, dummy);
119 else if (opt_realname)
120 match = InspIRCd::Match(user->fullname, matchtext);
121 else if (opt_showrealhost)
122 match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
124 match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
127 irc::portparser portrange(matchtext, false);
129 while ((portno = portrange.GetToken()))
130 if (portno == user->GetServerPort())
137 match = InspIRCd::Match(user->awaymsg, matchtext);
140 long seconds = ServerInstance->Duration(matchtext);
142 // Okay, so time matching, we want all users connected `seconds' ago
143 if (user->age >= ServerInstance->Time() - seconds)
148 * Once the conditionals have been checked, only check dhost/nick/server
149 * if they didn't match this user -- and only match if we don't find a match.
151 * This should make things minutely faster, and again, less ugly.
155 match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
158 match = InspIRCd::Match(user->nick, matchtext);
161 match = InspIRCd::Match(user->server, matchtext);
169 bool CommandWho::CanView(Channel* chan, User* user)
174 /* Bug #383 - moved higher up the list, because if we are in the channel
175 * we can see all its users
177 if (chan->HasUser(user))
180 if (user->HasPrivPermission("users/auspex"))
182 /* Cant see inside a +s or a +p channel unless we are a member (see above) */
183 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
189 void CommandWho::SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
191 const std::string& lcn = get_first_visible_channel(u);
192 Channel* chlast = ServerInstance->FindChan(lcn);
194 std::string wholine = initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
195 ((*ServerInstance->Config->HideWhoisServer && !user->HasPrivPermission("servers/auspex")) ? ServerInstance->Config->HideWhoisServer : u->server) +
214 wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
216 FOREACH_MOD(I_OnSendWhoLine, OnSendWhoLine(user, u, ch, wholine));
218 if (!wholine.empty())
219 whoresults.push_back(wholine);
222 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
226 * The <name> passed to WHO is matched against users' host, server, real
228 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
232 opt_viewopersonly = false;
233 opt_showrealhost = false;
235 opt_realname = false;
238 opt_metadata = false;
246 std::vector<std::string> whoresults;
247 std::string initial = "352 " + std::string(user->nick) + " ";
249 char matchtext[MAXBUF];
250 bool usingwildcards = false;
252 /* Change '0' into '*' so the wildcard matcher can grok it */
253 if (parameters[0] == "0")
254 strlcpy(matchtext, "*", MAXBUF);
256 strlcpy(matchtext, parameters[0].c_str(), MAXBUF);
258 for (const char* check = matchtext; *check; check++)
260 if (*check == '*' || *check == '?')
262 usingwildcards = true;
267 if (ServerInstance->FindServerName(matchtext))
268 usingwildcards = true;
270 if (parameters.size() > 1)
272 /* Fix for bug #444, WHO flags count as a wildcard */
273 usingwildcards = true;
275 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
280 opt_viewopersonly = true;
283 if (user->HasPrivPermission("users/auspex"))
284 opt_showrealhost = true;
287 if (user->HasPrivPermission("users/auspex"))
294 if (user->HasPrivPermission("users/auspex"))
298 if (user->HasPrivPermission("users/auspex"))
305 if (user->HasPrivPermission("users/auspex"))
312 if (user->HasPrivPermission("users/auspex") || !*ServerInstance->Config->HideWhoisServer)
316 if (user->HasPrivPermission("users/auspex") || !*ServerInstance->Config->HideWhoisServer)
327 /* who on a channel? */
328 ch = ServerInstance->FindChan(matchtext);
332 if (CanView(ch,user))
334 bool inside = ch->HasUser(user);
336 /* who on a channel. */
337 CUList *cu = ch->GetUsers();
339 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
341 /* None of this applies if we WHO ourselves */
342 if (user != i->first)
344 /* opers only, please */
345 if (opt_viewopersonly && !IS_OPER(i->first))
348 /* If we're not inside the channel, hide +i users */
349 if (i->first->IsModeSet('i') && !inside && !user->HasPrivPermission("users/auspex"))
353 SendWhoLine(user, initial, ch, i->first, whoresults);
359 /* Match against wildcard of nick, server or host */
360 if (opt_viewopersonly)
362 /* Showing only opers */
363 for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
367 if (whomatch(oper, matchtext))
369 if (!user->SharesChannelWith(oper))
371 if (usingwildcards && (!oper->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
375 SendWhoLine(user, initial, NULL, oper, whoresults);
381 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
383 if (whomatch(i->second, matchtext))
385 if (!user->SharesChannelWith(i->second))
387 if (usingwildcards && (i->second->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
391 SendWhoLine(user, initial, NULL, i->second, whoresults);
396 /* Send the results out */
397 if ((ServerInstance->Config->MaxWhoResults && (whoresults.size() <= (size_t)ServerInstance->Config->MaxWhoResults)) || opt_unlimit)
399 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
401 user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
406 /* BZZT! Too many results. */
407 user->WriteNumeric(315, "%s %s :Too many results",user->nick.c_str(), parameters[0].c_str());
412 COMMAND_INIT(CommandWho)