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 * ---------------------------------------------------
16 #include "commands/cmd_who.h"
18 static const std::string star = "*";
20 static const std::string& get_first_visible_channel(User *u)
22 UCListIter i = u->chans.begin();
23 if (i != u->chans.end())
25 if (!i->first->IsModeSet('s'))
26 return i->first->name;
32 bool CommandWho::whomatch(User* user, const char* matchtext)
34 bool realhost = false;
35 bool realname = false;
37 bool metadata = false;
43 if (user->registered != REG_ALL)
46 if (opt_local && !IS_LOCAL(user))
48 else if (opt_far && IS_LOCAL(user))
53 for (const char* n = matchtext; *n; n++)
65 if (user->IsModeSet(*n) != positive)
74 metadata = user->GetExt(matchtext, dummy);
78 realname = match(user->fullname, matchtext);
82 realhost = match(user->host, matchtext);
86 ident = match(user->ident, matchtext);
91 irc::portparser portrange(matchtext, false);
93 while ((portno = portrange.GetToken()))
94 if (portno == user->GetPort())
100 away = match(user->awaymsg, matchtext);
106 return ((port) || (away) || (ident) || (metadata) || (realname) || (realhost) || (match(user->dhost, matchtext)) || (match(user->nick, matchtext)) || (match(user->server, matchtext)));
112 extern "C" DllExport Command* init_command(InspIRCd* Instance)
114 return new CommandWho(Instance);
117 bool CommandWho::CanView(Channel* chan, User* user)
122 /* Bug #383 - moved higher up the list, because if we are in the channel
123 * we can see all its users
125 if (chan->HasUser(user))
130 /* Cant see inside a +s or a +p channel unless we are a member (see above) */
131 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
137 void CommandWho::SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
139 /* Not visible to this user */
140 if (u->Visibility && !u->Visibility->VisibleTo(user))
143 const std::string& lcn = get_first_visible_channel(u);
144 Channel* chlast = ServerInstance->FindChan(lcn);
146 std::string wholine = initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
147 ((*ServerInstance->Config->HideWhoisServer && !IS_OPER(user)) ? ServerInstance->Config->HideWhoisServer : u->server) +
166 wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
167 whoresults.push_back(wholine);
170 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
174 * The <name> passed to WHO is matched against users' host, server, real
176 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
180 opt_viewopersonly = false;
181 opt_showrealhost = false;
183 opt_realname = false;
186 opt_metadata = false;
193 std::vector<std::string> whoresults;
194 std::string initial = "352 " + std::string(user->nick) + " ";
196 char matchtext[MAXBUF];
197 bool usingwildcards = false;
199 /* Change '0' into '*' so the wildcard matcher can grok it */
200 if (parameters[0] == "0")
201 strlcpy(matchtext, "*", MAXBUF);
203 strlcpy(matchtext, parameters[0].c_str(), MAXBUF);
205 for (const char* check = matchtext; *check; check++)
207 if (*check == '*' || *check == '?')
209 usingwildcards = true;
214 if (ServerInstance->FindServerName(matchtext))
215 usingwildcards = true;
217 if (parameters.size() > 1)
219 /* Fix for bug #444, WHO flags count as a wildcard */
220 usingwildcards = true;
222 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
227 opt_viewopersonly = true;
231 opt_showrealhost = true;
266 /* who on a channel? */
267 ch = ServerInstance->FindChan(matchtext);
271 if (CanView(ch,user))
273 bool inside = ch->HasUser(user);
275 /* who on a channel. */
276 CUList *cu = ch->GetUsers();
278 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
280 /* None of this applies if we WHO ourselves */
281 if (user != i->first)
283 /* opers only, please */
284 if (opt_viewopersonly && !IS_OPER(i->first))
287 /* If we're not inside the channel, hide +i users */
288 if (i->first->IsModeSet('i') && !inside && !IS_OPER(user))
292 SendWhoLine(user, initial, ch, i->first, whoresults);
298 /* Match against wildcard of nick, server or host */
299 if (opt_viewopersonly)
301 /* Showing only opers */
302 for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
306 if (whomatch(oper, matchtext))
308 if (!user->SharesChannelWith(oper))
310 if (usingwildcards && (!oper->IsModeSet('i')) && (!IS_OPER(user)))
314 SendWhoLine(user, initial, NULL, oper, whoresults);
320 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
322 if (whomatch(i->second, matchtext))
324 if (!user->SharesChannelWith(i->second))
326 if (usingwildcards && (i->second->IsModeSet('i')) && (!IS_OPER(user)))
330 SendWhoLine(user, initial, NULL, i->second, whoresults);
335 /* Send the results out */
336 if ((ServerInstance->Config->MaxWhoResults && (whoresults.size() <= (size_t)ServerInstance->Config->MaxWhoResults)) || opt_unlimit)
338 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
340 user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
345 /* BZZT! Too many results. */
346 user->WriteNumeric(315, "%s %s :Too many results",user->nick.c_str(), parameters[0].c_str());