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 * ---------------------------------------------------
14 #include "configreader.h"
18 #include "commands/cmd_who.h"
20 /* get the last 'visible' chan of a user */
21 static char *getlastchanname(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);
91 port = (user->GetPort() == ConvToInt(matchtext));
95 away = match(user->awaymsg, matchtext);
101 return ((port) || (away) || (ident) || (metadata) || (realname) || (realhost) || (match(user->dhost, matchtext)) || (match(user->nick, matchtext)) || (match(user->server, matchtext)));
107 extern "C" command_t* init_command(InspIRCd* Instance)
109 return new cmd_who(Instance);
112 bool cmd_who::CanView(chanrec* chan, userrec* user)
117 /* Execute items in fastest-to-execute first order */
122 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
124 else if (chan->HasUser(user))
130 void cmd_who::SendWhoLine(userrec* user, const std::string &initial, chanrec* ch, userrec* u, std::vector<std::string> &whoresults)
132 std::string lcn = getlastchanname(u);
133 chanrec* chlast = ServerInstance->FindChan(lcn);
135 /* Not visible to this user */
136 if (u->Visibility && !u->Visibility->VisibleTo(user))
139 std::string wholine = initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
140 ((*ServerInstance->Config->HideWhoisServer && !*user->oper) ? ServerInstance->Config->HideWhoisServer : u->server) +
159 wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
160 whoresults.push_back(wholine);
163 CmdResult cmd_who::Handle (const char** parameters, int pcnt, userrec *user)
167 * The <name> passed to WHO is matched against users' host, server, real
169 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
173 opt_viewopersonly = false;
174 opt_showrealhost = false;
176 opt_realname = false;
179 opt_metadata = false;
186 std::vector<std::string> whoresults;
187 std::string initial = "352 " + std::string(user->nick) + " ";
189 const char* matchtext = NULL;
191 /* Change '0' into '*' so the wildcard matcher can grok it */
192 matchtext = parameters[0];
193 if (!strcmp(matchtext,"0"))
199 const char *iter = parameters[1];
206 opt_viewopersonly = true;
210 opt_showrealhost = true;
247 /* who on a channel? */
248 ch = ServerInstance->FindChan(matchtext);
252 if (CanView(ch,user))
254 bool inside = ch->HasUser(user);
256 /* who on a channel. */
257 CUList *cu = ch->GetUsers();
259 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
261 /* opers only, please */
262 if (opt_viewopersonly && !IS_OPER(i->first))
265 /* If we're not inside the channel, hide +i users */
266 if (i->first->IsModeSet('i') && !inside)
269 SendWhoLine(user, initial, ch, i->first, whoresults);
275 /* Match against wildcard of nick, server or host */
277 if (opt_viewopersonly)
279 /* Showing only opers */
280 for (std::vector<userrec*>::iterator i = ServerInstance->all_opers.begin(); i != ServerInstance->all_opers.end(); i++)
284 if (whomatch(oper, matchtext))
286 if ((!oper->IsModeSet('i')) && (!IS_OPER(user)))
289 SendWhoLine(user, initial, NULL, oper, whoresults);
295 for (user_hash::iterator i = ServerInstance->clientlist->begin(); i != ServerInstance->clientlist->end(); i++)
297 if (whomatch(i->second, matchtext))
299 if ((i->second->IsModeSet('i')) && (!IS_OPER(user)))
302 SendWhoLine(user, initial, NULL, i->second, whoresults);
307 /* Send the results out */
308 if ((whoresults.size() <= (size_t)ServerInstance->Config->MaxWhoResults) || opt_unlimit)
310 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
312 user->WriteServ("315 %s %s :End of /WHO list.",user->nick, *parameters[0] ? parameters[0] : "*");
317 /* BZZT! Too many results. */
318 user->WriteServ("315 %s %s :Too many results",user->nick, parameters[0]);