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 * ---------------------------------------------------
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;
38 /** Constructor for who.
40 CommandWho ( Module* parent) : Command(parent,"WHO", 1) { Penalty = 2; syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [ohurmMiaplf]"; }
41 void SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults);
43 * @param parameters The parameters to the comamnd
44 * @param pcnt The number of parameters passed to teh command
45 * @param user The user issuing the command
46 * @return A value from CmdResult to indicate command success or failure.
48 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
49 bool whomatch(User* cuser, User* user, const char* matchtext);
53 static const std::string star = "*";
55 static const std::string& get_first_visible_channel(User *u)
57 UCListIter i = u->chans.begin();
58 while (i != u->chans.end())
61 if (!c->IsModeSet('s'))
68 bool CommandWho::whomatch(User* cuser, User* user, const char* matchtext)
71 bool positive = false;
73 if (user->registered != REG_ALL)
76 if (opt_local && !IS_LOCAL(user))
78 else if (opt_far && IS_LOCAL(user))
83 for (const char* n = matchtext; *n; n++)
95 if (user->IsModeSet(*n) != positive)
103 * This was previously one awesome pile of ugly nested if, when really, it didn't need
104 * to be, since only one condition was ever checked, a chained if works just fine.
108 match = user->GetExtList().find(matchtext) != user->GetExtList().end();
109 else if (opt_realname)
110 match = InspIRCd::Match(user->fullname, matchtext);
111 else if (opt_showrealhost)
112 match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
114 match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
117 irc::portparser portrange(matchtext, false);
119 while ((portno = portrange.GetToken()))
120 if (portno == user->GetServerPort())
127 match = InspIRCd::Match(user->awaymsg, matchtext);
130 long seconds = ServerInstance->Duration(matchtext);
132 // Okay, so time matching, we want all users connected `seconds' ago
133 if (user->age >= ServerInstance->Time() - seconds)
138 * Once the conditionals have been checked, only check dhost/nick/server
139 * if they didn't match this user -- and only match if we don't find a match.
141 * This should make things minutely faster, and again, less ugly.
145 match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
148 match = InspIRCd::Match(user->nick, matchtext);
150 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
151 if (!match && (!*ServerInstance->Config->HideWhoisServer || cuser->HasPrivPermission("users/auspex")))
152 match = InspIRCd::Match(user->server, matchtext);
160 bool CommandWho::CanView(Channel* chan, User* user)
165 /* Bug #383 - moved higher up the list, because if we are in the channel
166 * we can see all its users
168 if (chan->HasUser(user))
171 if (user->HasPrivPermission("users/auspex"))
173 /* Cant see inside a +s or a +p channel unless we are a member (see above) */
174 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
180 void CommandWho::SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
182 const std::string& lcn = get_first_visible_channel(u);
183 Channel* chlast = ServerInstance->FindChan(lcn);
185 std::string wholine = initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
186 ((*ServerInstance->Config->HideWhoisServer && !user->HasPrivPermission("servers/auspex")) ? ServerInstance->Config->HideWhoisServer : u->server) +
205 wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
207 FOREACH_MOD(I_OnSendWhoLine, OnSendWhoLine(user, u, ch, wholine));
209 if (!wholine.empty())
210 whoresults.push_back(wholine);
213 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
217 * The <name> passed to WHO is matched against users' host, server, real
219 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
223 opt_viewopersonly = false;
224 opt_showrealhost = false;
226 opt_realname = false;
229 opt_metadata = false;
237 std::vector<std::string> whoresults;
238 std::string initial = "352 " + std::string(user->nick) + " ";
240 char matchtext[MAXBUF];
241 bool usingwildcards = false;
243 /* Change '0' into '*' so the wildcard matcher can grok it */
244 if (parameters[0] == "0")
245 strlcpy(matchtext, "*", MAXBUF);
247 strlcpy(matchtext, parameters[0].c_str(), MAXBUF);
249 for (const char* check = matchtext; *check; check++)
251 if (*check == '*' || *check == '?')
253 usingwildcards = true;
258 if (ServerInstance->FindServerName(matchtext))
259 usingwildcards = true;
261 if (parameters.size() > 1)
263 /* Fix for bug #444, WHO flags count as a wildcard */
264 usingwildcards = true;
266 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
271 opt_viewopersonly = true;
274 if (user->HasPrivPermission("users/auspex"))
275 opt_showrealhost = true;
278 if (user->HasPrivPermission("users/auspex"))
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)
307 if (user->HasPrivPermission("users/auspex") || !*ServerInstance->Config->HideWhoisServer)
318 /* who on a channel? */
319 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 && !IS_OPER(i->first))
339 /* If we're not inside the channel, hide +i users */
340 if (i->first->IsModeSet('i') && !inside && !user->HasPrivPermission("users/auspex"))
344 SendWhoLine(user, initial, ch, i->first, whoresults);
350 /* Match against wildcard of nick, server or host */
351 if (opt_viewopersonly)
353 /* Showing only opers */
354 for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
358 if (whomatch(user, oper, matchtext))
360 if (!user->SharesChannelWith(oper))
362 if (usingwildcards && (!oper->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
366 SendWhoLine(user, initial, NULL, oper, whoresults);
372 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
374 if (whomatch(user, i->second, matchtext))
376 if (!user->SharesChannelWith(i->second))
378 if (usingwildcards && (i->second->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
382 SendWhoLine(user, initial, NULL, i->second, whoresults);
387 /* Send the results out */
388 if ((ServerInstance->Config->MaxWhoResults && (whoresults.size() <= (size_t)ServerInstance->Config->MaxWhoResults)) || opt_unlimit)
390 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
392 user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
397 /* BZZT! Too many results. */
398 user->WriteNumeric(315, "%s %s :Too many results",user->nick.c_str(), parameters[0].c_str());
403 COMMAND_INIT(CommandWho)