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 * ---------------------------------------------------
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, ascii_case_insensitive_map);
78 match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
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, ascii_case_insensitive_map);
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))
139 if (user->HasPrivPermission("users/auspex"))
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 && !user->HasPrivPermission("servers/auspex")) ? 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;
242 if (user->HasPrivPermission("users/auspex"))
243 opt_showrealhost = true;
246 if (user->HasPrivPermission("users/auspex"))
253 if (user->HasPrivPermission("users/auspex"))
257 if (user->HasPrivPermission("users/auspex"))
264 if (user->HasPrivPermission("users/auspex"))
271 if (user->HasPrivPermission("users/auspex") || !*ServerInstance->Config->HideWhoisServer)
275 if (user->HasPrivPermission("users/auspex") || !*ServerInstance->Config->HideWhoisServer)
286 /* who on a channel? */
287 ch = ServerInstance->FindChan(matchtext);
291 if (CanView(ch,user))
293 bool inside = ch->HasUser(user);
295 /* who on a channel. */
296 CUList *cu = ch->GetUsers();
298 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
300 /* None of this applies if we WHO ourselves */
301 if (user != i->first)
303 /* opers only, please */
304 if (opt_viewopersonly && !IS_OPER(i->first))
307 /* If we're not inside the channel, hide +i users */
308 if (i->first->IsModeSet('i') && !inside && !user->HasPrivPermission("users/auspex"))
312 SendWhoLine(user, initial, ch, i->first, whoresults);
318 /* Match against wildcard of nick, server or host */
319 if (opt_viewopersonly)
321 /* Showing only opers */
322 for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
326 if (whomatch(oper, matchtext))
328 if (!user->SharesChannelWith(oper))
330 if (usingwildcards && (!oper->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
334 SendWhoLine(user, initial, NULL, oper, whoresults);
340 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
342 if (whomatch(i->second, matchtext))
344 if (!user->SharesChannelWith(i->second))
346 if (usingwildcards && (i->second->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
350 SendWhoLine(user, initial, NULL, i->second, whoresults);
355 /* Send the results out */
356 if ((ServerInstance->Config->MaxWhoResults && (whoresults.size() <= (size_t)ServerInstance->Config->MaxWhoResults)) || opt_unlimit)
358 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
360 user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
365 /* BZZT! Too many results. */
366 user->WriteNumeric(315, "%s %s :Too many results",user->nick.c_str(), parameters[0].c_str());