1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "commands/cmd_who.h"
17 static const std::string star = "*";
19 static const std::string& get_first_visible_channel(User *u)
21 UCListIter i = u->chans.begin();
22 if (i != u->chans.end())
24 if (!i->first->IsModeSet('s'))
25 return i->first->name;
31 bool CommandWho::whomatch(User* user, const char* matchtext)
34 bool positive = false;
37 if (user->registered != REG_ALL)
40 if (opt_local && !IS_LOCAL(user))
42 else if (opt_far && IS_LOCAL(user))
47 for (const char* n = matchtext; *n; n++)
59 if (user->IsModeSet(*n) != positive)
67 * This was previously one awesome pile of ugly nested if, when really, it didn't need
68 * to be, since only one condition was ever checked, a chained if works just fine.
72 match = user->GetExt(matchtext, dummy);
73 else if (opt_realname)
74 match = InspIRCd::Match(user->fullname, matchtext);
75 else if (opt_showrealhost)
76 match = InspIRCd::Match(user->host, matchtext);
78 match = InspIRCd::Match(user->ident, matchtext);
81 irc::portparser portrange(matchtext, false);
83 while ((portno = portrange.GetToken()))
84 if (portno == user->GetPort())
91 match = InspIRCd::Match(user->awaymsg, matchtext);
94 long seconds = ServerInstance->Duration(matchtext);
96 // Okay, so time matching, we want all users connected `seconds' ago
97 if (user->age >= ServerInstance->Time() - seconds)
102 * Once the conditionals have been checked, only check dhost/nick/server
103 * if they didn't match this user -- and only match if we don't find a match.
105 * This should make things minutely faster, and again, less ugly.
109 match = InspIRCd::Match(user->dhost, matchtext);
112 match = InspIRCd::Match(user->nick, matchtext);
115 match = InspIRCd::Match(user->server, matchtext);
123 extern "C" DllExport Command* init_command(InspIRCd* Instance)
125 return new CommandWho(Instance);
128 bool CommandWho::CanView(Channel* chan, User* user)
133 /* Bug #383 - moved higher up the list, because if we are in the channel
134 * we can see all its users
136 if (chan->HasUser(user))
141 /* Cant see inside a +s or a +p channel unless we are a member (see above) */
142 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
148 void CommandWho::SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
150 /* Not visible to this user */
151 if (u->Visibility && !u->Visibility->VisibleTo(user))
154 const std::string& lcn = get_first_visible_channel(u);
155 Channel* chlast = ServerInstance->FindChan(lcn);
157 std::string wholine = initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
158 ((*ServerInstance->Config->HideWhoisServer && !IS_OPER(user)) ? ServerInstance->Config->HideWhoisServer : u->server) +
177 wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
178 whoresults.push_back(wholine);
181 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
185 * The <name> passed to WHO is matched against users' host, server, real
187 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
191 opt_viewopersonly = false;
192 opt_showrealhost = false;
194 opt_realname = false;
197 opt_metadata = false;
205 std::vector<std::string> whoresults;
206 std::string initial = "352 " + std::string(user->nick) + " ";
208 char matchtext[MAXBUF];
209 bool usingwildcards = false;
211 /* Change '0' into '*' so the wildcard matcher can grok it */
212 if (parameters[0] == "0")
213 strlcpy(matchtext, "*", MAXBUF);
215 strlcpy(matchtext, parameters[0].c_str(), MAXBUF);
217 for (const char* check = matchtext; *check; check++)
219 if (*check == '*' || *check == '?')
221 usingwildcards = true;
226 if (ServerInstance->FindServerName(matchtext))
227 usingwildcards = true;
229 if (parameters.size() > 1)
231 /* Fix for bug #444, WHO flags count as a wildcard */
232 usingwildcards = true;
234 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
239 opt_viewopersonly = true;
243 opt_showrealhost = true;
281 /* who on a channel? */
282 ch = ServerInstance->FindChan(matchtext);
286 if (CanView(ch,user))
288 bool inside = ch->HasUser(user);
290 /* who on a channel. */
291 CUList *cu = ch->GetUsers();
293 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
295 /* None of this applies if we WHO ourselves */
296 if (user != i->first)
298 /* opers only, please */
299 if (opt_viewopersonly && !IS_OPER(i->first))
302 /* If we're not inside the channel, hide +i users */
303 if (i->first->IsModeSet('i') && !inside && !IS_OPER(user))
307 SendWhoLine(user, initial, ch, i->first, whoresults);
313 /* Match against wildcard of nick, server or host */
314 if (opt_viewopersonly)
316 /* Showing only opers */
317 for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
321 if (whomatch(oper, matchtext))
323 if (!user->SharesChannelWith(oper))
325 if (usingwildcards && (!oper->IsModeSet('i')) && (!IS_OPER(user)))
329 SendWhoLine(user, initial, NULL, oper, whoresults);
335 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
337 if (whomatch(i->second, matchtext))
339 if (!user->SharesChannelWith(i->second))
341 if (usingwildcards && (i->second->IsModeSet('i')) && (!IS_OPER(user)))
345 SendWhoLine(user, initial, NULL, i->second, whoresults);
350 /* Send the results out */
351 if ((ServerInstance->Config->MaxWhoResults && (whoresults.size() <= (size_t)ServerInstance->Config->MaxWhoResults)) || opt_unlimit)
353 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
355 user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
360 /* BZZT! Too many results. */
361 user->WriteNumeric(315, "%s %s :Too many results",user->nick.c_str(), parameters[0].c_str());