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 /* get the last 'visible' chan of a user */
22 static char *getlastchanname(userrec *u)
24 UCListIter i = u->chans.begin();
25 if (i != u->chans.end())
27 if (!i->first->IsModeSet('s'))
28 return i->first->name;
34 bool cmd_who::whomatch(userrec* user, const char* matchtext)
36 bool realhost = false;
37 bool realname = false;
39 bool metadata = false;
45 if (user->registered != REG_ALL)
48 if (opt_local && !IS_LOCAL(user))
50 else if (opt_far && IS_LOCAL(user))
55 for (const char* n = matchtext; *n; n++)
67 if (user->IsModeSet(*n) != positive)
76 metadata = user->GetExt(matchtext, dummy);
80 realname = match(user->fullname, matchtext);
84 realhost = match(user->host, matchtext);
88 ident = match(user->ident, matchtext);
93 irc::portparser portrange(matchtext, false);
95 while ((portno = portrange.GetToken()))
96 if (portno == user->GetPort())
102 away = match(user->awaymsg, matchtext);
108 return ((port) || (away) || (ident) || (metadata) || (realname) || (realhost) || (match(user->dhost, matchtext)) || (match(user->nick, matchtext)) || (match(user->server, matchtext)));
114 extern "C" DllExport command_t* init_command(InspIRCd* Instance)
116 return new cmd_who(Instance);
119 bool cmd_who::CanView(chanrec* chan, userrec* user)
124 /* Execute items in fastest-to-execute first order */
129 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
131 else if (chan->HasUser(user))
137 void cmd_who::SendWhoLine(userrec* user, const std::string &initial, chanrec* ch, userrec* u, std::vector<std::string> &whoresults)
139 std::string lcn = getlastchanname(u);
140 chanrec* chlast = ServerInstance->FindChan(lcn);
142 /* Not visible to this user */
143 if (u->Visibility && !u->Visibility->VisibleTo(user))
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 cmd_who::Handle (const char** parameters, int pcnt, userrec *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 const char* matchtext = NULL;
198 /* Change '0' into '*' so the wildcard matcher can grok it */
199 matchtext = parameters[0];
200 if (!strcmp(matchtext,"0"))
206 const char *iter = parameters[1];
213 opt_viewopersonly = true;
217 opt_showrealhost = true;
254 /* who on a channel? */
255 ch = ServerInstance->FindChan(matchtext);
259 if (CanView(ch,user))
261 bool inside = ch->HasUser(user);
263 /* who on a channel. */
264 CUList *cu = ch->GetUsers();
266 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
268 /* opers only, please */
269 if (opt_viewopersonly && !IS_OPER(i->first))
272 /* If we're not inside the channel, hide +i users */
273 if (i->first->IsModeSet('i') && !inside)
276 SendWhoLine(user, initial, ch, i->first, whoresults);
282 /* Match against wildcard of nick, server or host */
284 if (opt_viewopersonly)
286 /* Showing only opers */
287 for (std::vector<userrec*>::iterator i = ServerInstance->all_opers.begin(); i != ServerInstance->all_opers.end(); i++)
291 if (whomatch(oper, matchtext))
293 if ((!oper->IsModeSet('i')) && (!IS_OPER(user)))
296 SendWhoLine(user, initial, NULL, oper, whoresults);
302 for (user_hash::iterator i = ServerInstance->clientlist->begin(); i != ServerInstance->clientlist->end(); i++)
304 if (whomatch(i->second, matchtext))
306 if ((i->second->IsModeSet('i')) && (!IS_OPER(user)))
309 SendWhoLine(user, initial, NULL, i->second, whoresults);
314 /* Send the results out */
315 if ((whoresults.size() <= (size_t)ServerInstance->Config->MaxWhoResults) || opt_unlimit)
317 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
319 user->WriteServ("315 %s %s :End of /WHO list.",user->nick, *parameters[0] ? parameters[0] : "*");
324 /* BZZT! Too many results. */
325 user->WriteServ("315 %s %s :Too many results",user->nick, parameters[0]);