1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 *the file COPYING for details.
14 * ---------------------------------------------------
17 #include "configreader.h"
21 #include "commands/cmd_who.h"
23 /* get the last 'visible' chan of a user */
24 static char *getlastchanname(userrec *u)
26 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
28 ucrec* temp = (ucrec*)*v;
32 if (!temp->channel->IsModeSet('s'))
33 return temp->channel->name;
40 bool whomatch(userrec* user, const char* matchtext, bool opt_realname, bool opt_showrealhost)
42 bool realhost = false;
43 bool realname = false;
45 if (user->registered != REG_ALL)
49 realname = match(user->fullname, matchtext);
52 realhost = match(user->host, matchtext);
54 return ((realname) || (realhost) || (match(user->dhost, matchtext)) || (match(user->nick, matchtext)) || (match(user->server, matchtext)));
57 void cmd_who::Handle (const char** parameters, int pcnt, userrec *user)
61 * The <name> passed to WHO is matched against users' host, server, real
63 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
67 bool opt_viewopersonly = false;
68 bool opt_showrealhost = false;
69 bool opt_unlimit = false;
70 bool opt_realname = false;
73 std::vector<std::string> whoresults;
74 std::string initial = "352 " + std::string(user->nick) + " ";
76 const char* matchtext = NULL;
78 /* Change '0' into '*' so the wildcard matcher can grok it */
79 matchtext = parameters[0];
80 if (!strcmp(matchtext,"0"))
86 const char *iter = parameters[1];
93 opt_viewopersonly = true;
97 opt_showrealhost = true;
113 /* who on a channel? */
114 ch = ServerInstance->FindChan(matchtext);
118 /* who on a channel. */
119 CUList *cu = ch->GetUsers();
121 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
123 /* opers only, please */
124 if (opt_viewopersonly && !*(i->second)->oper)
127 /* XXX - code duplication; this could be more efficient -- w00t */
128 std::string wholine = initial;
130 wholine = wholine + ch->name + " " + i->second->ident + " " + (opt_showrealhost ? i->second->host : i->second->dhost) + " " +
131 i->second->server + " " + i->second->nick + " ";
134 if (*(i->second)->awaymsg)
144 if (*(i->second)->oper)
149 wholine = wholine + ch->GetPrefixChar(i->second) + " :0 " + i->second->fullname;
150 whoresults.push_back(wholine);
155 /* Match against wildcard of nick, server or host */
157 if (opt_viewopersonly)
159 /* Showing only opers */
160 for (std::vector<userrec*>::iterator i = ServerInstance->all_opers.begin(); i != ServerInstance->all_opers.end(); i++)
164 if (whomatch(oper, matchtext, opt_realname, opt_showrealhost))
166 std::string wholine = initial;
168 wholine = wholine + getlastchanname(oper) + " " + oper->ident + " " + (opt_showrealhost ? oper->host : oper->dhost) + " " +
169 oper->server + " " + oper->nick + " ";
187 wholine = wholine + ch->GetPrefixChar(oper) + " :0 " + oper->fullname;
188 whoresults.push_back(wholine);
194 for (user_hash::iterator i = ServerInstance->clientlist.begin(); i != ServerInstance->clientlist.end(); i++)
196 if (whomatch(i->second, matchtext, opt_realname, opt_showrealhost))
198 std::string wholine = initial;
200 wholine = wholine + getlastchanname(i->second) + " " + i->second->ident + " " + (opt_showrealhost ? i->second->host : i->second->dhost) + " " +
201 i->second->server + " " + i->second->nick + " ";
204 if (*(i->second)->awaymsg)
214 if (*(i->second)->oper)
219 wholine = wholine + ch->GetPrefixChar(i->second) + " :0 " + i->second->fullname;
220 whoresults.push_back(wholine);
225 /* Send the results out */
226 if ((whoresults.size() < (size_t)ServerInstance->Config->MaxWhoResults) && (!opt_unlimit))
228 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
230 user->WriteServ("315 %s %s :End of /WHO list.",user->nick, *parameters[0] ? parameters[0] : "*");
234 /* BZZT! Too many results. */
235 user->WriteServ("315 %s %s :Too many results",user->nick, parameters[0]);