1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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 * ---------------------------------------------------
16 /** Handle /WHO. These command handlers can be reloaded by the core,
17 * and handle basic RFC1459 commands. Commands within modules work
18 * the same way, however, they can be fully unloaded, where these
21 class CommandWho : public Command
23 bool CanView(Channel* chan, User* user);
24 bool opt_viewopersonly;
25 bool opt_showrealhost;
37 /** Constructor for who.
39 CommandWho ( Module* parent) : Command(parent,"WHO", 1) {
40 syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [ohurmMiaplf]";
42 void SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults);
44 * @param parameters The parameters to the comamnd
45 * @param pcnt The number of parameters passed to teh command
46 * @param user The user issuing the command
47 * @return A value from CmdResult to indicate command success or failure.
49 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
50 bool whomatch(User* cuser, User* user, const char* matchtext);
54 static const std::string star = "*";
56 static const std::string& get_first_visible_channel(User *u)
58 UCListIter i = u->chans.begin();
59 while (i != u->chans.end())
62 if (!c->IsModeSet('s'))
69 bool CommandWho::whomatch(User* cuser, User* user, const char* matchtext)
72 bool positive = false;
74 if (user->registered != REG_ALL)
77 if (opt_local && !IS_LOCAL(user))
79 else if (opt_far && IS_LOCAL(user))
84 for (const char* n = matchtext; *n; n++)
96 if (user->IsModeSet(*n) != positive)
104 * This was previously one awesome pile of ugly nested if, when really, it didn't need
105 * to be, since only one condition was ever checked, a chained if works just fine.
111 const Extensible::ExtensibleStore& list = user->GetExtList();
112 for(Extensible::ExtensibleStore::const_iterator i = list.begin(); i != list.end(); ++i)
113 if (InspIRCd::Match(i->first->name, matchtext))
116 else if (opt_realname)
117 match = InspIRCd::Match(user->fullname, matchtext);
118 else if (opt_showrealhost)
119 match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
121 match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
124 irc::portparser portrange(matchtext, false);
126 while ((portno = portrange.GetToken()))
127 if (IS_LOCAL(user) && portno == IS_LOCAL(user)->GetServerPort())
134 match = InspIRCd::Match(user->awaymsg, matchtext);
137 long seconds = ServerInstance->Duration(matchtext);
139 // Okay, so time matching, we want all users connected `seconds' ago
140 if (user->age >= ServerInstance->Time() - seconds)
145 * Once the conditionals have been checked, only check dhost/nick/server
146 * if they didn't match this user -- and only match if we don't find a match.
148 * This should make things minutely faster, and again, less ugly.
152 match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
155 match = InspIRCd::Match(user->nick, matchtext);
157 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
158 if (!match && (ServerInstance->Config->HideWhoisServer.empty() || cuser->HasPrivPermission("users/auspex")))
159 match = InspIRCd::Match(user->server, matchtext);
167 bool CommandWho::CanView(Channel* chan, User* user)
172 /* Bug #383 - moved higher up the list, because if we are in the channel
173 * we can see all its users
175 if (chan->HasUser(user))
178 if (user->HasPrivPermission("users/auspex"))
180 /* Cant see inside a +s or a +p channel unless we are a member (see above) */
181 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
187 void CommandWho::SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
189 const std::string& lcn = get_first_visible_channel(u);
190 Channel* chlast = ServerInstance->FindChan(lcn);
192 std::string wholine = initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
193 ((!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex")) ? ServerInstance->Config->HideWhoisServer : u->server) +
212 wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
214 FOREACH_MOD(I_OnSendWhoLine, OnSendWhoLine(user, u, ch, wholine));
216 if (!wholine.empty())
217 whoresults.push_back(wholine);
220 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
224 * The <name> passed to WHO is matched against users' host, server, real
226 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
230 opt_viewopersonly = false;
231 opt_showrealhost = false;
232 opt_realname = false;
235 opt_metadata = false;
243 std::vector<std::string> whoresults;
244 std::string initial = "352 " + std::string(user->nick) + " ";
246 char matchtext[MAXBUF];
247 bool usingwildcards = false;
249 /* Change '0' into '*' so the wildcard matcher can grok it */
250 if (parameters[0] == "0")
251 strlcpy(matchtext, "*", MAXBUF);
253 strlcpy(matchtext, parameters[0].c_str(), MAXBUF);
255 for (const char* check = matchtext; *check; check++)
257 if (*check == '*' || *check == '?')
259 usingwildcards = true;
264 if (parameters.size() > 1)
266 /* Fix for bug #444, WHO flags count as a wildcard */
267 usingwildcards = true;
269 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
274 opt_viewopersonly = true;
277 if (user->HasPrivPermission("users/auspex"))
278 opt_showrealhost = true;
284 if (user->HasPrivPermission("users/auspex"))
288 if (user->HasPrivPermission("users/auspex"))
295 if (user->HasPrivPermission("users/auspex"))
302 if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
306 if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
317 /* who on a channel? */
318 ch = ServerInstance->FindChan(matchtext);
322 if (CanView(ch,user))
324 bool inside = ch->HasUser(user);
326 /* who on a channel. */
327 const UserMembList *cu = ch->GetUsers();
329 for (UserMembCIter i = cu->begin(); i != cu->end(); i++)
331 /* None of this applies if we WHO ourselves */
332 if (user != i->first)
334 /* opers only, please */
335 if (opt_viewopersonly && !IS_OPER(i->first))
338 /* If we're not inside the channel, hide +i users */
339 if (i->first->IsModeSet('i') && !inside && !user->HasPrivPermission("users/auspex"))
343 SendWhoLine(user, initial, ch, i->first, whoresults);
349 /* Match against wildcard of nick, server or host */
350 if (opt_viewopersonly)
352 /* Showing only opers */
353 for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
357 if (whomatch(user, oper, matchtext))
359 if (!user->SharesChannelWith(oper))
361 if (usingwildcards && (!oper->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
365 SendWhoLine(user, initial, NULL, oper, whoresults);
371 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
373 if (whomatch(user, i->second, matchtext))
375 if (!user->SharesChannelWith(i->second))
377 if (usingwildcards && (i->second->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
381 SendWhoLine(user, initial, NULL, i->second, whoresults);
386 /* Send the results out */
387 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
389 user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
391 // Penalize the user a bit for large queries
392 // (add one unit of penalty per 200 results)
394 IS_LOCAL(user)->CommandFloodPenalty += whoresults.size() * 5;
398 COMMAND_INIT(CommandWho)