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* cuser, 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* cuser, 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);
160 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
161 if (!match && (!*ServerInstance->Config->HideWhoisServer || cuser->HasPrivPermission("users/auspex")))
162 match = InspIRCd::Match(user->server, matchtext);
170 bool CommandWho::CanView(Channel* chan, User* user)
175 /* Bug #383 - moved higher up the list, because if we are in the channel
176 * we can see all its users
178 if (chan->HasUser(user))
181 if (user->HasPrivPermission("users/auspex"))
183 /* Cant see inside a +s or a +p channel unless we are a member (see above) */
184 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
190 void CommandWho::SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
192 const std::string& lcn = get_first_visible_channel(u);
193 Channel* chlast = ServerInstance->FindChan(lcn);
195 std::string wholine = initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
196 ((*ServerInstance->Config->HideWhoisServer && !user->HasPrivPermission("servers/auspex")) ? ServerInstance->Config->HideWhoisServer : u->server) +
215 wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
217 FOREACH_MOD(I_OnSendWhoLine, OnSendWhoLine(user, u, ch, wholine));
219 if (!wholine.empty())
220 whoresults.push_back(wholine);
223 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
227 * The <name> passed to WHO is matched against users' host, server, real
229 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
233 opt_viewopersonly = false;
234 opt_showrealhost = false;
236 opt_realname = false;
239 opt_metadata = false;
247 std::vector<std::string> whoresults;
248 std::string initial = "352 " + std::string(user->nick) + " ";
250 char matchtext[MAXBUF];
251 bool usingwildcards = false;
253 /* Change '0' into '*' so the wildcard matcher can grok it */
254 if (parameters[0] == "0")
255 strlcpy(matchtext, "*", MAXBUF);
257 strlcpy(matchtext, parameters[0].c_str(), MAXBUF);
259 for (const char* check = matchtext; *check; check++)
261 if (*check == '*' || *check == '?')
263 usingwildcards = true;
268 if (ServerInstance->FindServerName(matchtext))
269 usingwildcards = true;
271 if (parameters.size() > 1)
273 /* Fix for bug #444, WHO flags count as a wildcard */
274 usingwildcards = true;
276 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
281 opt_viewopersonly = true;
284 if (user->HasPrivPermission("users/auspex"))
285 opt_showrealhost = true;
288 if (user->HasPrivPermission("users/auspex"))
295 if (user->HasPrivPermission("users/auspex"))
299 if (user->HasPrivPermission("users/auspex"))
306 if (user->HasPrivPermission("users/auspex"))
313 if (user->HasPrivPermission("users/auspex") || !*ServerInstance->Config->HideWhoisServer)
317 if (user->HasPrivPermission("users/auspex") || !*ServerInstance->Config->HideWhoisServer)
328 /* who on a channel? */
329 ch = ServerInstance->FindChan(matchtext);
333 if (CanView(ch,user))
335 bool inside = ch->HasUser(user);
337 /* who on a channel. */
338 CUList *cu = ch->GetUsers();
340 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
342 /* None of this applies if we WHO ourselves */
343 if (user != i->first)
345 /* opers only, please */
346 if (opt_viewopersonly && !IS_OPER(i->first))
349 /* If we're not inside the channel, hide +i users */
350 if (i->first->IsModeSet('i') && !inside && !user->HasPrivPermission("users/auspex"))
354 SendWhoLine(user, initial, ch, i->first, whoresults);
360 /* Match against wildcard of nick, server or host */
361 if (opt_viewopersonly)
363 /* Showing only opers */
364 for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
368 if (whomatch(user, oper, matchtext))
370 if (!user->SharesChannelWith(oper))
372 if (usingwildcards && (!oper->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
376 SendWhoLine(user, initial, NULL, oper, whoresults);
382 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
384 if (whomatch(user, i->second, matchtext))
386 if (!user->SharesChannelWith(i->second))
388 if (usingwildcards && (i->second->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
392 SendWhoLine(user, initial, NULL, i->second, whoresults);
397 /* Send the results out */
398 if ((ServerInstance->Config->MaxWhoResults && (whoresults.size() <= (size_t)ServerInstance->Config->MaxWhoResults)) || opt_unlimit)
400 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
402 user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
407 /* BZZT! Too many results. */
408 user->WriteNumeric(315, "%s %s :Too many results",user->nick.c_str(), parameters[0].c_str());
413 COMMAND_INIT(CommandWho)