2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** Handle /WHOIS. These command handlers can be reloaded by the core,
25 * and handle basic RFC1459 commands. Commands within modules work
26 * the same way, however, they can be fully unloaded, where these
29 class CommandWhois : public SplitCommand
31 ChanModeReference secretmode;
32 ChanModeReference privatemode;
33 UserModeReference snomaskmode;
35 void SplitChanList(User* source, User* dest, const std::string& cl);
36 void DoWhois(User* user, User* dest, unsigned long signon, unsigned long idle);
37 std::string ChannelList(User* source, User* dest, bool spy);
40 /** Constructor for whois.
42 CommandWhois(Module* parent)
43 : SplitCommand(parent, "WHOIS", 1)
44 , secretmode(parent, "secret")
45 , privatemode(parent, "private")
46 , snomaskmode(parent, "snomask")
49 syntax = "<nick>{,<nick>}";
53 * @param parameters The parameters to the comamnd
54 * @param pcnt The number of parameters passed to teh command
55 * @param user The user issuing the command
56 * @return A value from CmdResult to indicate command success or failure.
58 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user);
59 CmdResult HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target);
62 std::string CommandWhois::ChannelList(User* source, User* dest, bool spy)
66 for (UCListIter i = dest->chans.begin(); i != dest->chans.end(); i++)
68 Channel* c = (*i)->chan;
69 /* If the target is the sender, neither +p nor +s is set, or
70 * the channel contains the user, it is not a spy channel
72 if (spy != (source == dest || !(c->IsModeSet(privatemode) || c->IsModeSet(secretmode)) || c->HasUser(source)))
73 list.append(c->GetPrefixChar(dest)).append(c->name).append(" ");
79 void CommandWhois::SplitChanList(User* source, User* dest, const std::string& cl)
82 std::ostringstream prefix;
83 std::string::size_type start, pos;
85 prefix << dest->nick << " :";
87 int namelen = ServerInstance->Config->ServerName.length() + 6;
89 for (start = 0; (pos = cl.find(' ', start)) != std::string::npos; start = pos+1)
91 if (line.length() + namelen + pos - start > 510)
93 ServerInstance->SendWhoisLine(source, dest, 319, line);
97 line.append(cl.substr(start, pos - start + 1));
100 if (line.length() != prefix.str().length())
102 ServerInstance->SendWhoisLine(source, dest, 319, line);
106 void CommandWhois::DoWhois(User* user, User* dest, unsigned long signon, unsigned long idle)
108 ServerInstance->SendWhoisLine(user, dest, 311, "%s %s %s * :%s", dest->nick.c_str(), dest->ident.c_str(), dest->dhost.c_str(), dest->fullname.c_str());
109 if (user == dest || user->HasPrivPermission("users/auspex"))
111 ServerInstance->SendWhoisLine(user, dest, 378, "%s :is connecting from %s@%s %s", dest->nick.c_str(), dest->ident.c_str(), dest->host.c_str(), dest->GetIPString().c_str());
114 std::string cl = ChannelList(user, dest, false);
115 const ServerConfig::OperSpyWhoisState state = user->HasPrivPermission("users/auspex") ? ServerInstance->Config->OperSpyWhois : ServerConfig::SPYWHOIS_NONE;
117 if (state == ServerConfig::SPYWHOIS_SINGLEMSG)
118 cl.append(ChannelList(user, dest, true));
120 SplitChanList(user, dest, cl);
122 if (state == ServerConfig::SPYWHOIS_SPLITMSG)
124 std::string scl = ChannelList(user, dest, true);
127 ServerInstance->SendWhoisLine(user, dest, 336, "%s :is on private/secret channels:", dest->nick.c_str());
128 SplitChanList(user, dest, scl);
131 if (user != dest && !ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
133 ServerInstance->SendWhoisLine(user, dest, 312, "%s %s :%s", dest->nick.c_str(), ServerInstance->Config->HideWhoisServer.c_str(), ServerInstance->Config->Network.c_str());
137 ServerInstance->SendWhoisLine(user, dest, 312, "%s %s :%s", dest->nick.c_str(), dest->server->GetName().c_str(), dest->server->GetDesc().c_str());
142 ServerInstance->SendWhoisLine(user, dest, 301, "%s :%s", dest->nick.c_str(), dest->awaymsg.c_str());
147 if (ServerInstance->Config->GenericOper)
148 ServerInstance->SendWhoisLine(user, dest, 313, "%s :is an IRC operator", dest->nick.c_str());
150 ServerInstance->SendWhoisLine(user, dest, 313, "%s :is %s %s on %s", dest->nick.c_str(), (strchr("AEIOUaeiou",dest->oper->name[0]) ? "an" : "a"),dest->oper->name.c_str(), ServerInstance->Config->Network.c_str());
153 if (user == dest || user->HasPrivPermission("users/auspex"))
155 if (dest->IsModeSet(snomaskmode))
157 ServerInstance->SendWhoisLine(user, dest, 379, "%s :is using modes +%s %s", dest->nick.c_str(), dest->FormatModes(), snomaskmode->GetUserParameter(dest).c_str());
161 ServerInstance->SendWhoisLine(user, dest, 379, "%s :is using modes +%s", dest->nick.c_str(), dest->FormatModes());
165 FOREACH_MOD(OnWhois, (user,dest));
168 * We only send these if we've been provided them. That is, if hidewhois is turned off, and user is local, or
169 * if remote whois is queried, too. This is to keep the user hidden, and also since you can't reliably tell remote time. -- w00t
171 if ((idle) || (signon))
173 ServerInstance->SendWhoisLine(user, dest, 317, "%s %lu %lu :seconds idle, signon time", dest->nick.c_str(), idle, signon);
176 ServerInstance->SendWhoisLine(user, dest, 318, "%s :End of /WHOIS list.", dest->nick.c_str());
179 CmdResult CommandWhois::HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target)
181 if (parameters.size() < 2)
184 User* user = ServerInstance->FindUUID(parameters[0]);
188 unsigned long idle = ConvToInt(parameters.back());
189 DoWhois(user, target, target->signon, idle);
194 CmdResult CommandWhois::HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
198 unsigned long idle = 0, signon = 0;
200 if (CommandParser::LoopCall(user, this, parameters, 0))
204 * If 2 paramters are specified (/whois nick nick), ignore the first one like spanningtree
205 * does, and use the second one, otherwise, use the only paramter. -- djGrrr
207 if (parameters.size() > 1)
210 dest = ServerInstance->FindNickOnly(parameters[userindex]);
212 if ((dest) && (dest->registered == REG_ALL))
215 * Okay. Umpteenth attempt at doing this, so let's re-comment...
216 * For local users (/w localuser), we show idletime if hidewhois is disabled
217 * For local users (/w localuser localuser), we always show idletime, hence parameters.size() > 1 check.
218 * For remote users (/w remoteuser), we do NOT show idletime
219 * For remote users (/w remoteuser remoteuser), spanningtree will handle calling do_whois, so we can ignore this case.
220 * Thanks to djGrrr for not being impatient while I have a crap day coding. :p -- w00t
222 LocalUser* localuser = IS_LOCAL(dest);
223 if (localuser && (ServerInstance->Config->HideWhoisServer.empty() || parameters.size() > 1))
225 idle = abs((long)((localuser->idle_lastmsg)-ServerInstance->Time()));
226 signon = dest->signon;
229 DoWhois(user,dest,signon,idle);
233 /* no such nick/channel */
234 user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", !parameters[userindex].empty() ? parameters[userindex].c_str() : "*");
235 user->WriteNumeric(RPL_ENDOFWHOIS, "%s :End of /WHOIS list.", !parameters[userindex].empty() ? parameters[userindex].c_str() : "*");
242 COMMAND_INIT(CommandWhois)