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 "helperfuncs.h"
23 #include "commands/cmd_who.h"
25 extern InspIRCd* ServerInstance;
26 extern chan_hash chanlist;
27 extern std::vector<userrec*> all_opers;
29 /* get the last 'visible' chan of a user */
30 static char *getlastchanname(userrec *u)
32 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
34 ucrec* temp = (ucrec*)*v;
38 if (!temp->channel->IsModeSet('s'))
39 return temp->channel->name;
46 bool whomatch(userrec* user, const char* matchtext, bool opt_realname, bool opt_showrealhost)
48 bool realhost = false;
49 bool realname = false;
52 realname = match(user->fullname, matchtext);
55 realhost = match(user->host, matchtext);
57 return ((realname) || (realhost) || (match(user->dhost, matchtext)) || (match(user->nick, matchtext)) || (match(user->server, matchtext)));
60 void cmd_who::Handle (const char** parameters, int pcnt, userrec *user)
64 * The <name> passed to WHO is matched against users' host, server, real
66 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
70 bool opt_viewopersonly = false;
71 bool opt_showrealhost = false;
72 bool opt_unlimit = false;
73 bool opt_realname = false;
76 std::vector<std::string> whoresults;
77 std::string initial = "352 " + std::string(user->nick) + " ";
79 const char* matchtext = NULL;
81 /* Change '0' into '*' so the wildcard matcher can grok it */
82 matchtext = parameters[0];
83 if (!strcmp(matchtext,"0"))
89 const char *iter = parameters[1];
96 opt_viewopersonly = true;
100 opt_showrealhost = true;
116 /* who on a channel? */
117 ch = ServerInstance->FindChan(matchtext);
121 /* who on a channel. */
122 CUList *cu = ch->GetUsers();
124 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
126 /* opers only, please */
127 if (opt_viewopersonly && !*(i->second)->oper)
130 /* XXX - code duplication; this could be more efficient -- w00t */
131 std::string wholine = initial;
133 wholine = wholine + ch->name + " " + i->second->ident + " " + (opt_showrealhost ? i->second->host : i->second->dhost) + " " +
134 i->second->server + " " + i->second->nick + " ";
137 if (*(i->second)->awaymsg)
147 if (*(i->second)->oper)
152 wholine = wholine + ch->GetStatusChar(i->second) + " :0 " + i->second->fullname;
153 whoresults.push_back(wholine);
158 /* Match against wildcard of nick, server or host */
160 if (opt_viewopersonly)
162 /* Showing only opers */
163 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
167 if (whomatch(oper, matchtext, opt_realname, opt_showrealhost))
169 std::string wholine = initial;
171 wholine = wholine + getlastchanname(oper) + " " + oper->ident + " " + (opt_showrealhost ? oper->host : oper->dhost) + " " +
172 oper->server + " " + oper->nick + " ";
190 wholine = wholine + ch->GetStatusChar(oper) + " :0 " + oper->fullname;
191 whoresults.push_back(wholine);
197 for (user_hash::iterator i = ServerInstance->clientlist.begin(); i != ServerInstance->clientlist.end(); i++)
199 if (whomatch(i->second, matchtext, opt_realname, opt_showrealhost))
201 std::string wholine = initial;
203 wholine = wholine + getlastchanname(i->second) + " " + i->second->ident + " " + (opt_showrealhost ? i->second->host : i->second->dhost) + " " +
204 i->second->server + " " + i->second->nick + " ";
207 if (*(i->second)->awaymsg)
217 if (*(i->second)->oper)
222 wholine = wholine + ch->GetStatusChar(i->second) + " :0 " + i->second->fullname;
223 whoresults.push_back(wholine);
228 /* Send the results out */
229 if ((whoresults.size() < (size_t)ServerInstance->Config->MaxWhoResults) && (!opt_unlimit))
231 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
233 user->WriteServ("315 %s %s :End of /WHO list.",user->nick, parameters[0]);
237 /* BZZT! Too many results. */
238 user->WriteServ("315 %s %s :Too many results",user->nick, parameters[0]);