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;
44 UserModeReference invisiblemode;
46 Channel* get_first_visible_channel(User *u)
48 UCListIter i = u->chans.begin();
49 while (i != u->chans.end())
52 if (!c->IsModeSet(secretmode))
59 /** Constructor for who.
61 CommandWho(Module* parent)
62 : Command(parent, "WHO", 1)
63 , secretmode(parent, "secret")
64 , privatemode(parent, "private")
65 , invisiblemode(parent, "invisible")
67 syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [ohurmMiaplf]";
70 void SendWhoLine(User* user, const std::vector<std::string>& parms, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults);
72 * @param parameters The parameters to the comamnd
73 * @param pcnt The number of parameters passed to teh command
74 * @param user The user issuing the command
75 * @return A value from CmdResult to indicate command success or failure.
77 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
78 bool whomatch(User* cuser, User* user, const char* matchtext);
81 bool CommandWho::whomatch(User* cuser, User* user, const char* matchtext)
84 bool positive = false;
86 if (user->registered != REG_ALL)
89 if (opt_local && !IS_LOCAL(user))
91 else if (opt_far && IS_LOCAL(user))
96 for (const char* n = matchtext; *n; n++)
108 if (user->IsModeSet(*n) != positive)
116 * This was previously one awesome pile of ugly nested if, when really, it didn't need
117 * to be, since only one condition was ever checked, a chained if works just fine.
123 const Extensible::ExtensibleStore& list = user->GetExtList();
124 for(Extensible::ExtensibleStore::const_iterator i = list.begin(); i != list.end(); ++i)
125 if (InspIRCd::Match(i->first->name, matchtext))
128 else if (opt_realname)
129 match = InspIRCd::Match(user->fullname, matchtext);
130 else if (opt_showrealhost)
131 match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
133 match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
136 irc::portparser portrange(matchtext, false);
138 while ((portno = portrange.GetToken()))
139 if (IS_LOCAL(user) && portno == IS_LOCAL(user)->GetServerPort())
146 match = InspIRCd::Match(user->awaymsg, matchtext);
149 long seconds = InspIRCd::Duration(matchtext);
151 // Okay, so time matching, we want all users connected `seconds' ago
152 if (user->age >= ServerInstance->Time() - seconds)
157 * Once the conditionals have been checked, only check dhost/nick/server
158 * if they didn't match this user -- and only match if we don't find a match.
160 * This should make things minutely faster, and again, less ugly.
164 match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
167 match = InspIRCd::Match(user->nick, matchtext);
169 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
170 if (!match && (ServerInstance->Config->HideWhoisServer.empty() || cuser->HasPrivPermission("users/auspex")))
171 match = InspIRCd::Match(user->server, matchtext);
177 bool CommandWho::CanView(Channel* chan, User* user)
182 /* Bug #383 - moved higher up the list, because if we are in the channel
183 * we can see all its users
185 if (chan->HasUser(user))
188 if (user->HasPrivPermission("users/auspex"))
190 /* Cant see inside a +s or a +p channel unless we are a member (see above) */
191 else if (!chan->IsModeSet(secretmode) && !chan->IsModeSet(privatemode))
197 void CommandWho::SendWhoLine(User* user, const std::vector<std::string>& parms, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
200 ch = get_first_visible_channel(u);
202 std::string wholine = initial + (ch ? ch->name : "*") + " " + u->ident + " " +
203 (opt_showrealhost ? u->host : u->dhost) + " ";
204 if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
205 wholine.append(ServerInstance->Config->HideWhoisServer);
207 wholine.append(u->server);
209 wholine.append(" " + u->nick + " ");
224 wholine.push_back('*');
228 wholine.append(ch->GetPrefixChar(u));
230 wholine.append(" :0 " + u->fullname);
232 FOREACH_MOD(OnSendWhoLine, (user, parms, u, wholine));
234 if (!wholine.empty())
235 whoresults.push_back(wholine);
238 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
242 * The <name> passed to WHO is matched against users' host, server, real
244 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
248 opt_viewopersonly = false;
249 opt_showrealhost = false;
250 opt_realname = false;
253 opt_metadata = false;
260 std::vector<std::string> whoresults;
261 std::string initial = "352 " + user->nick + " ";
263 /* Change '0' into '*' so the wildcard matcher can grok it */
264 std::string matchtext = ((parameters[0] == "0") ? "*" : parameters[0]);
266 // WHO flags count as a wildcard
267 bool usingwildcards = ((parameters.size() > 1) || (matchtext.find_first_of("*?.") != std::string::npos));
269 if (parameters.size() > 1)
271 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
276 opt_viewopersonly = true;
279 if (user->HasPrivPermission("users/auspex"))
280 opt_showrealhost = true;
286 if (user->HasPrivPermission("users/auspex"))
290 if (user->HasPrivPermission("users/auspex"))
297 if (user->HasPrivPermission("users/auspex"))
304 if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
308 if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
319 /* who on a channel? */
320 Channel* ch = ServerInstance->FindChan(matchtext);
324 if (CanView(ch,user))
326 bool inside = ch->HasUser(user);
328 /* who on a channel. */
329 const UserMembList *cu = ch->GetUsers();
331 for (UserMembCIter i = cu->begin(); i != cu->end(); i++)
333 /* None of this applies if we WHO ourselves */
334 if (user != i->first)
336 /* opers only, please */
337 if (opt_viewopersonly && !i->first->IsOper())
340 /* If we're not inside the channel, hide +i users */
341 if (i->first->IsModeSet(invisiblemode) && !inside && !user->HasPrivPermission("users/auspex"))
345 SendWhoLine(user, parameters, initial, ch, i->first, whoresults);
351 /* Match against wildcard of nick, server or host */
352 if (opt_viewopersonly)
354 /* Showing only opers */
355 for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
359 if (whomatch(user, oper, matchtext.c_str()))
361 if (!user->SharesChannelWith(oper))
363 if (usingwildcards && (!oper->IsModeSet(invisiblemode)) && (!user->HasPrivPermission("users/auspex")))
367 SendWhoLine(user, parameters, initial, NULL, oper, whoresults);
373 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
375 if (whomatch(user, i->second, matchtext.c_str()))
377 if (!user->SharesChannelWith(i->second))
379 if (usingwildcards && (i->second->IsModeSet(invisiblemode)) && (!user->HasPrivPermission("users/auspex")))
383 SendWhoLine(user, parameters, initial, NULL, i->second, whoresults);
388 /* Send the results out */
389 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
391 user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
393 // Penalize the user a bit for large queries
394 // (add one unit of penalty per 200 results)
396 IS_LOCAL(user)->CommandFloodPenalty += whoresults.size() * 5;
400 COMMAND_INIT(CommandWho)