1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 "configreader.h"
19 #include "commands/cmd_who.h"
21 static char *get_first_visible_channel(userrec *u)
23 UCListIter i = u->chans.begin();
24 if (i != u->chans.end())
26 if (!i->first->IsModeSet('s'))
27 return i->first->name;
33 bool cmd_who::whomatch(userrec* user, const char* matchtext)
35 bool realhost = false;
36 bool realname = false;
38 bool metadata = false;
44 if (user->registered != REG_ALL)
47 if (opt_local && !IS_LOCAL(user))
49 else if (opt_far && IS_LOCAL(user))
54 for (const char* n = matchtext; *n; n++)
66 if (user->IsModeSet(*n) != positive)
75 metadata = user->GetExt(matchtext, dummy);
79 realname = match(user->fullname, matchtext);
83 realhost = match(user->host, matchtext);
87 ident = match(user->ident, matchtext);
92 irc::portparser portrange(matchtext, false);
94 while ((portno = portrange.GetToken()))
95 if (portno == user->GetPort())
101 away = match(user->awaymsg, matchtext);
107 return ((port) || (away) || (ident) || (metadata) || (realname) || (realhost) || (match(user->dhost, matchtext)) || (match(user->nick, matchtext)) || (match(user->server, matchtext)));
113 extern "C" DllExport command_t* init_command(InspIRCd* Instance)
115 return new cmd_who(Instance);
118 bool cmd_who::CanView(chanrec* chan, userrec* user)
123 /* Bug #383 - moved higher up the list, because if we are in the channel
124 * we can see all its users
126 if (chan->HasUser(user))
131 /* Cant see inside a +s or a +p channel unless we are a member (see above) */
132 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
138 void cmd_who::SendWhoLine(userrec* user, const std::string &initial, chanrec* ch, userrec* u, std::vector<std::string> &whoresults)
140 std::string lcn = get_first_visible_channel(u);
141 chanrec* chlast = ServerInstance->FindChan(lcn);
143 /* Not visible to this user */
144 if (u->Visibility && !u->Visibility->VisibleTo(user))
147 std::string wholine = initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
148 ((*ServerInstance->Config->HideWhoisServer && !IS_OPER(user)) ? ServerInstance->Config->HideWhoisServer : u->server) +
167 wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
168 whoresults.push_back(wholine);
171 CmdResult cmd_who::Handle (const char** parameters, int pcnt, userrec *user)
175 * The <name> passed to WHO is matched against users' host, server, real
177 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
181 opt_viewopersonly = false;
182 opt_showrealhost = false;
184 opt_realname = false;
187 opt_metadata = false;
194 std::vector<std::string> whoresults;
195 std::string initial = "352 " + std::string(user->nick) + " ";
197 const char* matchtext = NULL;
198 bool usingwildcards = false;
200 /* Change '0' into '*' so the wildcard matcher can grok it */
201 matchtext = parameters[0];
202 if (!strcmp(matchtext,"0"))
205 for (const char* check = matchtext; *check; check++)
207 if (*check == '*' || *check == '?')
209 usingwildcards = true;
217 const char *iter = parameters[1];
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)
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::vector<userrec*>::iterator i = ServerInstance->all_opers.begin(); i != ServerInstance->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->clientlist->begin(); i != ServerInstance->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->WriteServ("315 %s %s :End of /WHO list.",user->nick, *parameters[0] ? parameters[0] : "*");
344 /* BZZT! Too many results. */
345 user->WriteServ("315 %s %s :Too many results",user->nick, parameters[0]);