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/>.
23 /** Handle /WHO. These command handlers can be reloaded by the core,
24 * and handle basic RFC1459 commands. Commands within modules work
25 * the same way, however, they can be fully unloaded, where these
28 class CommandWho : public Command
30 bool CanView(Channel* chan, User* user);
31 bool opt_viewopersonly;
32 bool opt_showrealhost;
42 ChanModeReference secretmode;
43 ChanModeReference privatemode;
45 Channel* get_first_visible_channel(User *u)
47 UCListIter i = u->chans.begin();
48 while (i != u->chans.end())
51 if (!c->IsModeSet(secretmode))
58 /** Constructor for who.
60 CommandWho(Module* parent)
61 : Command(parent, "WHO", 1)
62 , secretmode(parent, "secret")
63 , privatemode(parent, "private")
65 syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [ohurmMiaplf]";
68 void SendWhoLine(User* user, const std::vector<std::string>& parms, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults);
70 * @param parameters The parameters to the comamnd
71 * @param pcnt The number of parameters passed to teh command
72 * @param user The user issuing the command
73 * @return A value from CmdResult to indicate command success or failure.
75 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
76 bool whomatch(User* cuser, User* user, const char* matchtext);
79 bool CommandWho::whomatch(User* cuser, User* user, const char* matchtext)
82 bool positive = false;
84 if (user->registered != REG_ALL)
87 if (opt_local && !IS_LOCAL(user))
89 else if (opt_far && IS_LOCAL(user))
94 for (const char* n = matchtext; *n; n++)
106 if (user->IsModeSet(*n) != positive)
114 * This was previously one awesome pile of ugly nested if, when really, it didn't need
115 * to be, since only one condition was ever checked, a chained if works just fine.
121 const Extensible::ExtensibleStore& list = user->GetExtList();
122 for(Extensible::ExtensibleStore::const_iterator i = list.begin(); i != list.end(); ++i)
123 if (InspIRCd::Match(i->first->name, matchtext))
126 else if (opt_realname)
127 match = InspIRCd::Match(user->fullname, matchtext);
128 else if (opt_showrealhost)
129 match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
131 match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
134 irc::portparser portrange(matchtext, false);
136 while ((portno = portrange.GetToken()))
137 if (IS_LOCAL(user) && portno == IS_LOCAL(user)->GetServerPort())
144 match = InspIRCd::Match(user->awaymsg, matchtext);
147 long seconds = InspIRCd::Duration(matchtext);
149 // Okay, so time matching, we want all users connected `seconds' ago
150 if (user->age >= ServerInstance->Time() - seconds)
155 * Once the conditionals have been checked, only check dhost/nick/server
156 * if they didn't match this user -- and only match if we don't find a match.
158 * This should make things minutely faster, and again, less ugly.
162 match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
165 match = InspIRCd::Match(user->nick, matchtext);
167 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
168 if (!match && (ServerInstance->Config->HideWhoisServer.empty() || cuser->HasPrivPermission("users/auspex")))
169 match = InspIRCd::Match(user->server, matchtext);
175 bool CommandWho::CanView(Channel* chan, User* user)
180 /* Bug #383 - moved higher up the list, because if we are in the channel
181 * we can see all its users
183 if (chan->HasUser(user))
186 if (user->HasPrivPermission("users/auspex"))
188 /* Cant see inside a +s or a +p channel unless we are a member (see above) */
189 else if (!chan->IsModeSet(secretmode) && !chan->IsModeSet(privatemode))
195 void CommandWho::SendWhoLine(User* user, const std::vector<std::string>& parms, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
198 ch = get_first_visible_channel(u);
200 std::string wholine = initial + (ch ? ch->name : "*") + " " + u->ident + " " +
201 (opt_showrealhost ? u->host : u->dhost) + " ";
202 if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
203 wholine.append(ServerInstance->Config->HideWhoisServer);
205 wholine.append(u->server);
207 wholine.append(" " + u->nick + " ");
222 wholine.push_back('*');
226 wholine.append(ch->GetPrefixChar(u));
228 wholine.append(" :0 " + u->fullname);
230 FOREACH_MOD(I_OnSendWhoLine, OnSendWhoLine(user, parms, u, wholine));
232 if (!wholine.empty())
233 whoresults.push_back(wholine);
236 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
240 * The <name> passed to WHO is matched against users' host, server, real
242 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
246 opt_viewopersonly = false;
247 opt_showrealhost = false;
248 opt_realname = false;
251 opt_metadata = false;
258 std::vector<std::string> whoresults;
259 std::string initial = "352 " + user->nick + " ";
261 /* Change '0' into '*' so the wildcard matcher can grok it */
262 std::string matchtext = ((parameters[0] == "0") ? "*" : parameters[0]);
264 // WHO flags count as a wildcard
265 bool usingwildcards = ((parameters.size() > 1) || (matchtext.find_first_of("*?.") != std::string::npos));
267 if (parameters.size() > 1)
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 Channel* 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 && !i->first->IsOper())
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, parameters, 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.c_str()))
359 if (!user->SharesChannelWith(oper))
361 if (usingwildcards && (!oper->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
365 SendWhoLine(user, parameters, 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.c_str()))
375 if (!user->SharesChannelWith(i->second))
377 if (usingwildcards && (i->second->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
381 SendWhoLine(user, parameters, 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)