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 UCListIter i = u->chans.begin();
27 if (i != u->chans.end())
29 if (!i->first->IsModeSet('s'))
30 return i->first->name;
36 bool whomatch(userrec* user, const char* matchtext, bool opt_realname, bool opt_showrealhost, bool opt_mode)
38 bool realhost = false;
39 bool realname = false;
42 if (user->registered != REG_ALL)
47 for (const char* n = matchtext; *n; n++)
59 if (user->IsModeSet(*n) != positive)
66 realname = match(user->fullname, matchtext);
69 realhost = match(user->host, matchtext);
71 return ((realname) || (realhost) || (match(user->dhost, matchtext)) || (match(user->nick, matchtext)) || (match(user->server, matchtext)));
76 extern "C" command_t* init_command(InspIRCd* Instance)
78 return new cmd_who(Instance);
81 bool cmd_who::CanView(chanrec* chan, userrec* user)
86 /* Execute items in fastest-to-execute first order */
89 else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
91 else if (chan->HasUser(user))
97 CmdResult cmd_who::Handle (const char** parameters, int pcnt, userrec *user)
101 * The <name> passed to WHO is matched against users' host, server, real
103 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
107 bool opt_viewopersonly = false;
108 bool opt_showrealhost = false;
109 bool opt_unlimit = false;
110 bool opt_realname = false;
111 bool opt_mode = false;
114 std::vector<std::string> whoresults;
115 std::string initial = "352 " + std::string(user->nick) + " ";
117 const char* matchtext = NULL;
119 /* Change '0' into '*' so the wildcard matcher can grok it */
120 matchtext = parameters[0];
121 if (!strcmp(matchtext,"0"))
127 const char *iter = parameters[1];
134 opt_viewopersonly = true;
138 opt_showrealhost = true;
157 /* who on a channel? */
158 ch = ServerInstance->FindChan(matchtext);
160 if ((ch) && (CanView(ch,user)))
162 /* who on a channel. */
163 CUList *cu = ch->GetUsers();
165 for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
167 /* opers only, please */
168 if (opt_viewopersonly && !*(i->second)->oper)
171 /* XXX - code duplication; this could be more efficient -- w00t */
172 std::string wholine = initial;
174 wholine = wholine + ch->name + " " + i->second->ident + " " + (opt_showrealhost ? i->second->host : i->second->dhost) + " " +
175 i->second->server + " " + i->second->nick + " ";
178 if (*(i->second)->awaymsg)
188 if (*(i->second)->oper)
193 wholine = wholine + ch->GetPrefixChar(i->second) + " :0 " + i->second->fullname;
194 whoresults.push_back(wholine);
199 /* Match against wildcard of nick, server or host */
201 if (opt_viewopersonly)
203 /* Showing only opers */
204 for (std::vector<userrec*>::iterator i = ServerInstance->all_opers.begin(); i != ServerInstance->all_opers.end(); i++)
208 if (whomatch(oper, matchtext, opt_realname, opt_showrealhost, opt_mode))
210 std::string wholine = initial;
212 wholine = wholine + getlastchanname(oper) + " " + oper->ident + " " + (opt_showrealhost ? oper->host : oper->dhost) + " " +
213 oper->server + " " + oper->nick + " ";
215 ch = ServerInstance->FindChan(getlastchanname(oper));
233 wholine = wholine + (ch ? ch->GetPrefixChar(oper) : "") + " :0 " + oper->fullname;
234 whoresults.push_back(wholine);
240 for (user_hash::iterator i = ServerInstance->clientlist.begin(); i != ServerInstance->clientlist.end(); i++)
242 if (whomatch(i->second, matchtext, opt_realname, opt_showrealhost, opt_mode))
244 std::string wholine = initial;
246 wholine = wholine + getlastchanname(i->second) + " " + i->second->ident + " " + (opt_showrealhost ? i->second->host : i->second->dhost) + " " +
247 i->second->server + " " + i->second->nick + " ";
249 ch = ServerInstance->FindChan(getlastchanname(i->second));
252 if (*(i->second)->awaymsg)
262 if (*(i->second)->oper)
267 wholine = wholine + (ch ? ch->GetPrefixChar(i->second) : "") + " :0 " + i->second->fullname;
268 whoresults.push_back(wholine);
273 /* Send the results out */
274 if ((whoresults.size() < (size_t)ServerInstance->Config->MaxWhoResults) && (!opt_unlimit))
276 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
278 user->WriteServ("315 %s %s :End of /WHO list.",user->nick, *parameters[0] ? parameters[0] : "*");
283 /* BZZT! Too many results. */
284 user->WriteServ("315 %s %s :Too many results",user->nick, parameters[0]);