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, bool opt_mode)
42 bool realhost = false;
43 bool realname = false;
46 if (user->registered != REG_ALL)
51 for (const char* n = matchtext; *n; n++)
63 if (user->IsModeSet(*n) != positive)
70 realname = match(user->fullname, matchtext);
73 realhost = match(user->host, matchtext);
75 return ((realname) || (realhost) || (match(user->dhost, matchtext)) || (match(user->nick, matchtext)) || (match(user->server, matchtext)));
80 extern "C" command_t* init_command(InspIRCd* Instance)
82 return new cmd_who(Instance);
85 bool cmd_who::CanView(chanrec* chan, userrec* user)
90 /* Execute items in fastest-to-execute first order */
93 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
95 else if (chan->HasUser(user))
101 CmdResult cmd_who::Handle (const char** parameters, int pcnt, userrec *user)
105 * The <name> passed to WHO is matched against users' host, server, real
107 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
111 bool opt_viewopersonly = false;
112 bool opt_showrealhost = false;
113 bool opt_unlimit = false;
114 bool opt_realname = false;
115 bool opt_mode = false;
118 std::vector<std::string> whoresults;
119 std::string initial = "352 " + std::string(user->nick) + " ";
121 const char* matchtext = NULL;
123 /* Change '0' into '*' so the wildcard matcher can grok it */
124 matchtext = parameters[0];
125 if (!strcmp(matchtext,"0"))
131 const char *iter = parameters[1];
138 opt_viewopersonly = true;
142 opt_showrealhost = true;
161 /* who on a channel? */
162 ch = ServerInstance->FindChan(matchtext);
164 if ((ch) && (CanView(ch,user)))
166 /* who on a channel. */
167 CUList *cu = ch->GetUsers();
169 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
171 /* opers only, please */
172 if (opt_viewopersonly && !*(i->second)->oper)
175 /* XXX - code duplication; this could be more efficient -- w00t */
176 std::string wholine = initial;
178 wholine = wholine + ch->name + " " + i->second->ident + " " + (opt_showrealhost ? i->second->host : i->second->dhost) + " " +
179 i->second->server + " " + i->second->nick + " ";
182 if (*(i->second)->awaymsg)
192 if (*(i->second)->oper)
197 wholine = wholine + ch->GetPrefixChar(i->second) + " :0 " + i->second->fullname;
198 whoresults.push_back(wholine);
203 /* Match against wildcard of nick, server or host */
205 if (opt_viewopersonly)
207 /* Showing only opers */
208 for (std::vector<userrec*>::iterator i = ServerInstance->all_opers.begin(); i != ServerInstance->all_opers.end(); i++)
212 if (whomatch(oper, matchtext, opt_realname, opt_showrealhost, opt_mode))
214 std::string wholine = initial;
216 wholine = wholine + getlastchanname(oper) + " " + oper->ident + " " + (opt_showrealhost ? oper->host : oper->dhost) + " " +
217 oper->server + " " + oper->nick + " ";
219 ch = ServerInstance->FindChan(getlastchanname(oper));
237 wholine = wholine + (ch ? ch->GetPrefixChar(oper) : "") + " :0 " + oper->fullname;
238 whoresults.push_back(wholine);
244 for (user_hash::iterator i = ServerInstance->clientlist.begin(); i != ServerInstance->clientlist.end(); i++)
246 if (whomatch(i->second, matchtext, opt_realname, opt_showrealhost, opt_mode))
248 std::string wholine = initial;
250 wholine = wholine + getlastchanname(i->second) + " " + i->second->ident + " " + (opt_showrealhost ? i->second->host : i->second->dhost) + " " +
251 i->second->server + " " + i->second->nick + " ";
253 ch = ServerInstance->FindChan(getlastchanname(i->second));
256 if (*(i->second)->awaymsg)
266 if (*(i->second)->oper)
271 wholine = wholine + (ch ? ch->GetPrefixChar(i->second) : "") + " :0 " + i->second->fullname;
272 whoresults.push_back(wholine);
277 /* Send the results out */
278 if ((whoresults.size() < (size_t)ServerInstance->Config->MaxWhoResults) && (!opt_unlimit))
280 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
282 user->WriteServ("315 %s %s :End of /WHO list.",user->nick, *parameters[0] ? parameters[0] : "*");
287 /* BZZT! Too many results. */
288 user->WriteServ("315 %s %s :Too many results",user->nick, parameters[0]);