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;
44 /** Constructor for who.
46 CommandWho ( Module* parent) : Command(parent,"WHO", 1) {
47 syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [ohurmMiaplf]";
49 void SendWhoLine(User* user, const std::vector<std::string>& parms, 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);
61 static Channel* get_first_visible_channel(User *u)
63 UCListIter i = u->chans.begin();
64 while (i != u->chans.end())
67 if (!c->IsModeSet('s'))
73 bool CommandWho::whomatch(User* cuser, User* user, const char* matchtext)
76 bool positive = false;
78 if (user->registered != REG_ALL)
81 if (opt_local && !IS_LOCAL(user))
83 else if (opt_far && IS_LOCAL(user))
88 for (const char* n = matchtext; *n; n++)
100 if (user->IsModeSet(*n) != positive)
108 * This was previously one awesome pile of ugly nested if, when really, it didn't need
109 * to be, since only one condition was ever checked, a chained if works just fine.
115 const Extensible::ExtensibleStore& list = user->GetExtList();
116 for(Extensible::ExtensibleStore::const_iterator i = list.begin(); i != list.end(); ++i)
117 if (InspIRCd::Match(i->first->name, matchtext))
120 else if (opt_realname)
121 match = InspIRCd::Match(user->fullname, matchtext);
122 else if (opt_showrealhost)
123 match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
125 match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
128 irc::portparser portrange(matchtext, false);
130 while ((portno = portrange.GetToken()))
131 if (IS_LOCAL(user) && portno == IS_LOCAL(user)->GetServerPort())
138 match = InspIRCd::Match(user->awaymsg, matchtext);
141 long seconds = InspIRCd::Duration(matchtext);
143 // Okay, so time matching, we want all users connected `seconds' ago
144 if (user->age >= ServerInstance->Time() - seconds)
149 * Once the conditionals have been checked, only check dhost/nick/server
150 * if they didn't match this user -- and only match if we don't find a match.
152 * This should make things minutely faster, and again, less ugly.
156 match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
159 match = InspIRCd::Match(user->nick, matchtext);
161 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
162 if (!match && (ServerInstance->Config->HideWhoisServer.empty() || cuser->HasPrivPermission("users/auspex")))
163 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::vector<std::string>& parms, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
192 ch = get_first_visible_channel(u);
194 std::string wholine = initial + (ch ? ch->name : "*") + " " + u->ident + " " +
195 (opt_showrealhost ? u->host : u->dhost) + " ";
196 if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
197 wholine.append(ServerInstance->Config->HideWhoisServer);
199 wholine.append(u->server);
201 wholine.append(" " + u->nick + " ");
216 wholine.push_back('*');
220 wholine.append(ch->GetPrefixChar(u));
222 wholine.append(" :0 " + u->fullname);
224 FOREACH_MOD(I_OnSendWhoLine, OnSendWhoLine(user, parms, u, wholine));
226 if (!wholine.empty())
227 whoresults.push_back(wholine);
230 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
234 * The <name> passed to WHO is matched against users' host, server, real
236 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
240 opt_viewopersonly = false;
241 opt_showrealhost = false;
242 opt_realname = false;
245 opt_metadata = false;
252 std::vector<std::string> whoresults;
253 std::string initial = "352 " + user->nick + " ";
255 /* Change '0' into '*' so the wildcard matcher can grok it */
256 std::string matchtext = ((parameters[0] == "0") ? "*" : parameters[0]);
258 // WHO flags count as a wildcard
259 bool usingwildcards = ((parameters.size() > 1) || (matchtext.find_first_of("*?") != std::string::npos));
261 if (parameters.size() > 1)
263 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
268 opt_viewopersonly = true;
271 if (user->HasPrivPermission("users/auspex"))
272 opt_showrealhost = true;
278 if (user->HasPrivPermission("users/auspex"))
282 if (user->HasPrivPermission("users/auspex"))
289 if (user->HasPrivPermission("users/auspex"))
296 if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
300 if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
311 /* who on a channel? */
312 Channel* ch = ServerInstance->FindChan(matchtext);
316 if (CanView(ch,user))
318 bool inside = ch->HasUser(user);
320 /* who on a channel. */
321 const UserMembList *cu = ch->GetUsers();
323 for (UserMembCIter i = cu->begin(); i != cu->end(); i++)
325 /* None of this applies if we WHO ourselves */
326 if (user != i->first)
328 /* opers only, please */
329 if (opt_viewopersonly && !i->first->IsOper())
332 /* If we're not inside the channel, hide +i users */
333 if (i->first->IsModeSet('i') && !inside && !user->HasPrivPermission("users/auspex"))
337 SendWhoLine(user, parameters, initial, ch, i->first, whoresults);
343 /* Match against wildcard of nick, server or host */
344 if (opt_viewopersonly)
346 /* Showing only opers */
347 for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
351 if (whomatch(user, oper, matchtext.c_str()))
353 if (!user->SharesChannelWith(oper))
355 if (usingwildcards && (!oper->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
359 SendWhoLine(user, parameters, initial, NULL, oper, whoresults);
365 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
367 if (whomatch(user, i->second, matchtext.c_str()))
369 if (!user->SharesChannelWith(i->second))
371 if (usingwildcards && (i->second->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
375 SendWhoLine(user, parameters, initial, NULL, i->second, whoresults);
380 /* Send the results out */
381 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
383 user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
385 // Penalize the user a bit for large queries
386 // (add one unit of penalty per 200 results)
388 IS_LOCAL(user)->CommandFloodPenalty += whoresults.size() * 5;
392 COMMAND_INIT(CommandWho)