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 whomatch(userrec* user, const char* matchtext, bool opt_realname, bool opt_showrealhost, bool opt_mode)
35 bool realhost = false;
36 bool realname = false;
39 if (user->registered != REG_ALL)
44 for (const char* n = matchtext; *n; n++)
56 if (user->IsModeSet(*n) != positive)
63 realname = match(user->fullname, matchtext);
66 realhost = match(user->host, matchtext);
68 return ((realname) || (realhost) || (match(user->dhost, matchtext)) || (match(user->nick, matchtext)) || (match(user->server, matchtext)));
73 extern "C" command_t* init_command(InspIRCd* Instance)
75 return new cmd_who(Instance);
78 bool cmd_who::CanView(chanrec* chan, userrec* user)
83 /* Execute items in fastest-to-execute first order */
88 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
90 else if (chan->HasUser(user))
96 void cmd_who::SendWhoLine(userrec* user, const std::string &initial, chanrec* ch, userrec* u, std::vector<std::string> &whoresults)
98 std::string lcn = getlastchanname(u);
99 chanrec* chlast = ServerInstance->FindChan(lcn);
101 /* Not visible to this user */
102 if (u->Visibility && !u->Visibility->VisibleTo(user))
105 std::string wholine = initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
106 ((*ServerInstance->Config->HideWhoisServer && !*user->oper) ? ServerInstance->Config->HideWhoisServer : u->server) +
125 wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
126 whoresults.push_back(wholine);
129 CmdResult cmd_who::Handle (const char** parameters, int pcnt, userrec *user)
133 * The <name> passed to WHO is matched against users' host, server, real
135 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
139 opt_viewopersonly = false;
140 opt_showrealhost = false;
142 opt_realname = false;
146 std::vector<std::string> whoresults;
147 std::string initial = "352 " + std::string(user->nick) + " ";
149 const char* matchtext = NULL;
151 /* Change '0' into '*' so the wildcard matcher can grok it */
152 matchtext = parameters[0];
153 if (!strcmp(matchtext,"0"))
159 const char *iter = parameters[1];
166 opt_viewopersonly = true;
170 opt_showrealhost = true;
189 /* who on a channel? */
190 ch = ServerInstance->FindChan(matchtext);
194 if (CanView(ch,user))
196 bool inside = ch->HasUser(user);
198 /* who on a channel. */
199 CUList *cu = ch->GetUsers();
201 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
203 /* opers only, please */
204 if (opt_viewopersonly && !*(i->second)->oper)
207 /* If we're not inside the channel, hide +i users */
208 if (i->second->IsModeSet('i') && !inside)
211 SendWhoLine(user, initial, ch, i->second, whoresults);
217 /* Match against wildcard of nick, server or host */
219 if (opt_viewopersonly)
221 /* Showing only opers */
222 for (std::vector<userrec*>::iterator i = ServerInstance->all_opers.begin(); i != ServerInstance->all_opers.end(); i++)
226 if (whomatch(oper, matchtext, opt_realname, opt_showrealhost, opt_mode))
228 if (!oper->IsModeSet('i'))
229 SendWhoLine(user, initial, NULL, oper, whoresults);
235 for (user_hash::iterator i = ServerInstance->clientlist->begin(); i != ServerInstance->clientlist->end(); i++)
237 if (whomatch(i->second, matchtext, opt_realname, opt_showrealhost, opt_mode))
239 if (!i->second->IsModeSet('i'))
240 SendWhoLine(user, initial, NULL, i->second, whoresults);
245 /* Send the results out */
246 if ((whoresults.size() < (size_t)ServerInstance->Config->MaxWhoResults) && (!opt_unlimit))
248 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
250 user->WriteServ("315 %s %s :End of /WHO list.",user->nick, *parameters[0] ? parameters[0] : "*");
255 /* BZZT! Too many results. */
256 user->WriteServ("315 %s %s :Too many results",user->nick, parameters[0]);