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 char *get_first_visible_channel(User *u)
20 UCListIter i = u->chans.begin();
21 if (i != u->chans.end())
23 if (!i->first->IsModeSet('s'))
24 return i->first->name;
30 bool CommandWho::whomatch(User* user, const char* matchtext)
32 bool realhost = false;
33 bool realname = false;
35 bool metadata = false;
41 if (user->registered != REG_ALL)
44 if (opt_local && !IS_LOCAL(user))
46 else if (opt_far && IS_LOCAL(user))
51 for (const char* n = matchtext; *n; n++)
63 if (user->IsModeSet(*n) != positive)
72 metadata = user->GetExt(matchtext, dummy);
76 realname = match(user->fullname, matchtext);
80 realhost = match(user->host, matchtext);
84 ident = match(user->ident, matchtext);
89 irc::portparser portrange(matchtext, false);
91 while ((portno = portrange.GetToken()))
92 if (portno == user->GetPort())
98 away = match(user->awaymsg, matchtext);
104 return ((port) || (away) || (ident) || (metadata) || (realname) || (realhost) || (match(user->dhost, matchtext)) || (match(user->nick, matchtext)) || (match(user->server, matchtext)));
110 extern "C" DllExport Command* init_command(InspIRCd* Instance)
112 return new CommandWho(Instance);
115 bool CommandWho::CanView(Channel* chan, User* user)
120 /* Bug #383 - moved higher up the list, because if we are in the channel
121 * we can see all its users
123 if (chan->HasUser(user))
128 /* Cant see inside a +s or a +p channel unless we are a member (see above) */
129 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
135 void CommandWho::SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
137 /* Not visible to this user */
138 if (u->Visibility && !u->Visibility->VisibleTo(user))
141 std::string lcn = get_first_visible_channel(u);
142 Channel* chlast = ServerInstance->FindChan(lcn);
144 std::string wholine = initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
145 ((*ServerInstance->Config->HideWhoisServer && !IS_OPER(user)) ? ServerInstance->Config->HideWhoisServer : u->server) +
164 wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
165 whoresults.push_back(wholine);
168 CmdResult CommandWho::Handle (const char* const* parameters, int pcnt, User *user)
172 * The <name> passed to WHO is matched against users' host, server, real
174 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
178 opt_viewopersonly = false;
179 opt_showrealhost = false;
181 opt_realname = false;
184 opt_metadata = false;
191 std::vector<std::string> whoresults;
192 std::string initial = "352 " + std::string(user->nick) + " ";
194 const char* matchtext = NULL;
195 bool usingwildcards = false;
197 /* Change '0' into '*' so the wildcard matcher can grok it */
198 matchtext = parameters[0];
199 if (!strcmp(matchtext,"0"))
202 for (const char* check = matchtext; *check; check++)
204 if (*check == '*' || *check == '?')
206 usingwildcards = true;
214 const char *iter = parameters[1];
216 /* Fix for bug #444, WHO flags count as a wildcard */
217 usingwildcards = true;
224 opt_viewopersonly = true;
228 opt_showrealhost = true;
265 /* who on a channel? */
266 ch = ServerInstance->FindChan(matchtext);
270 if (CanView(ch,user))
272 bool inside = ch->HasUser(user);
274 /* who on a channel. */
275 CUList *cu = ch->GetUsers();
277 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
279 /* None of this applies if we WHO ourselves */
280 if (user != i->first)
282 /* opers only, please */
283 if (opt_viewopersonly && !IS_OPER(i->first))
286 /* If we're not inside the channel, hide +i users */
287 if (i->first->IsModeSet('i') && !inside && !IS_OPER(user))
291 SendWhoLine(user, initial, ch, i->first, whoresults);
297 /* Match against wildcard of nick, server or host */
298 if (opt_viewopersonly)
300 /* Showing only opers */
301 for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
305 if (whomatch(oper, matchtext))
307 if (!user->SharesChannelWith(oper))
309 if (usingwildcards && (!oper->IsModeSet('i')) && (!IS_OPER(user)))
313 SendWhoLine(user, initial, NULL, oper, whoresults);
319 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
321 if (whomatch(i->second, matchtext))
323 if (!user->SharesChannelWith(i->second))
325 if (usingwildcards && (i->second->IsModeSet('i')) && (!IS_OPER(user)))
329 SendWhoLine(user, initial, NULL, i->second, whoresults);
334 /* Send the results out */
335 if ((ServerInstance->Config->MaxWhoResults && (whoresults.size() <= (size_t)ServerInstance->Config->MaxWhoResults)) || opt_unlimit)
337 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
339 user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick, *parameters[0] ? parameters[0] : "*");
344 /* BZZT! Too many results. */
345 user->WriteNumeric(315, "%s %s :Too many results",user->nick, parameters[0]);