]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_whois.cpp
Merge insp20
[user/henk/code/inspircd.git] / src / commands / cmd_whois.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
7  *
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.
11  *
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
15  * details.
16  *
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/>.
19  */
20
21
22 #include "inspircd.h"
23
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
27  * may not.
28  */
29 class CommandWhois : public SplitCommand
30 {
31         void SplitChanList(User* source, User* dest, const std::string& cl);
32         void DoWhois(User* user, User* dest, unsigned long signon, unsigned long idle);
33         std::string ChannelList(User* source, User* dest, bool spy);
34
35  public:
36         /** Constructor for whois.
37          */
38         CommandWhois ( Module* parent) : SplitCommand(parent,"WHOIS", 1) { Penalty = 2; syntax = "<nick>{,<nick>}"; }
39         /** Handle command.
40          * @param parameters The parameters to the comamnd
41          * @param pcnt The number of parameters passed to teh command
42          * @param user The user issuing the command
43          * @return A value from CmdResult to indicate command success or failure.
44          */
45         CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user);
46         CmdResult HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target);
47 };
48
49 std::string CommandWhois::ChannelList(User* source, User* dest, bool spy)
50 {
51         std::string list;
52
53         for (UCListIter i = dest->chans.begin(); i != dest->chans.end(); i++)
54         {
55                 Channel* c = *i;
56                 /* If the target is the sender, neither +p nor +s is set, or
57                  * the channel contains the user, it is not a spy channel
58                  */
59                 if (spy != (source == dest || !(c->IsModeSet('p') || c->IsModeSet('s')) || c->HasUser(source)))
60                         list.append(c->GetPrefixChar(dest)).append(c->name).append(" ");
61         }
62
63         return list;
64 }
65
66 void CommandWhois::SplitChanList(User* source, User* dest, const std::string& cl)
67 {
68         std::string line;
69         std::ostringstream prefix;
70         std::string::size_type start, pos, length;
71
72         prefix << source->nick << " " << dest->nick << " :";
73         line = prefix.str();
74         int namelen = ServerInstance->Config->ServerName.length() + 6;
75
76         for (start = 0; (pos = cl.find(' ', start)) != std::string::npos; start = pos+1)
77         {
78                 length = (pos == std::string::npos) ? cl.length() : pos;
79
80                 if (line.length() + namelen + length - start > 510)
81                 {
82                         ServerInstance->SendWhoisLine(source, dest, 319, "%s", line.c_str());
83                         line = prefix.str();
84                 }
85
86                 if(pos == std::string::npos)
87                 {
88                         line.append(cl.substr(start, length - start));
89                         break;
90                 }
91                 else
92                 {
93                         line.append(cl.substr(start, length - start + 1));
94                 }
95         }
96
97         if (line.length() != prefix.str().length())
98         {
99                 ServerInstance->SendWhoisLine(source, dest, 319, "%s", line.c_str());
100         }
101 }
102
103 void CommandWhois::DoWhois(User* user, User* dest, unsigned long signon, unsigned long idle)
104 {
105         ServerInstance->SendWhoisLine(user, dest, 311, "%s %s %s %s * :%s",user->nick.c_str(), dest->nick.c_str(), dest->ident.c_str(), dest->dhost.c_str(), dest->fullname.c_str());
106         if (user == dest || user->HasPrivPermission("users/auspex"))
107         {
108                 ServerInstance->SendWhoisLine(user, dest, 378, "%s %s :is connecting from %s@%s %s", user->nick.c_str(), dest->nick.c_str(), dest->ident.c_str(), dest->host.c_str(), dest->GetIPString().c_str());
109         }
110
111         std::string cl = ChannelList(user, dest, false);
112         const ServerConfig::OperSpyWhoisState state = user->HasPrivPermission("users/auspex") ? ServerInstance->Config->OperSpyWhois : ServerConfig::SPYWHOIS_NONE;
113
114         if (state == ServerConfig::SPYWHOIS_SINGLEMSG)
115                 cl.append(ChannelList(user, dest, true));
116
117         SplitChanList(user, dest, cl);
118
119         if (state == ServerConfig::SPYWHOIS_SPLITMSG)
120         {
121                 std::string scl = ChannelList(user, dest, true);
122                 if (scl.length())
123                 {
124                         ServerInstance->SendWhoisLine(user, dest, 336, "%s %s :is on private/secret channels:",user->nick.c_str(), dest->nick.c_str());
125                         SplitChanList(user, dest, scl);
126                 }
127         }
128         if (user != dest && !ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
129         {
130                 ServerInstance->SendWhoisLine(user, dest, 312, "%s %s %s :%s",user->nick.c_str(), dest->nick.c_str(), ServerInstance->Config->HideWhoisServer.c_str(), ServerInstance->Config->Network.c_str());
131         }
132         else
133         {
134                 std::string serverdesc = ServerInstance->GetServerDescription(dest->server);
135                 ServerInstance->SendWhoisLine(user, dest, 312, "%s %s %s :%s",user->nick.c_str(), dest->nick.c_str(), dest->server.c_str(), serverdesc.c_str());
136         }
137
138         if (dest->IsAway())
139         {
140                 ServerInstance->SendWhoisLine(user, dest, 301, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), dest->awaymsg.c_str());
141         }
142
143         if (dest->IsOper())
144         {
145                 if (ServerInstance->Config->GenericOper)
146                         ServerInstance->SendWhoisLine(user, dest, 313, "%s %s :is an IRC operator",user->nick.c_str(), dest->nick.c_str());
147                 else
148                         ServerInstance->SendWhoisLine(user, dest, 313, "%s %s :is %s %s on %s",user->nick.c_str(), dest->nick.c_str(), (strchr("AEIOUaeiou",dest->oper->name[0]) ? "an" : "a"),dest->oper->NameStr(), ServerInstance->Config->Network.c_str());
149         }
150
151         if (user == dest || user->HasPrivPermission("users/auspex"))
152         {
153                 if (dest->IsModeSet('s') != 0)
154                 {
155                         ServerInstance->SendWhoisLine(user, dest, 379, "%s %s :is using modes +%s +%s", user->nick.c_str(), dest->nick.c_str(), dest->FormatModes(), dest->FormatNoticeMasks());
156                 }
157                 else
158                 {
159                         ServerInstance->SendWhoisLine(user, dest, 379, "%s %s :is using modes +%s", user->nick.c_str(), dest->nick.c_str(), dest->FormatModes());
160                 }
161         }
162
163         FOREACH_MOD(I_OnWhois,OnWhois(user,dest));
164
165         /*
166          * We only send these if we've been provided them. That is, if hidewhois is turned off, and user is local, or
167          * if remote whois is queried, too. This is to keep the user hidden, and also since you can't reliably tell remote time. -- w00t
168          */
169         if ((idle) || (signon))
170         {
171                 ServerInstance->SendWhoisLine(user, dest, 317, "%s %s %lu %lu :seconds idle, signon time",user->nick.c_str(), dest->nick.c_str(), idle, signon);
172         }
173
174         ServerInstance->SendWhoisLine(user, dest, 318, "%s %s :End of /WHOIS list.",user->nick.c_str(), dest->nick.c_str());
175 }
176
177 CmdResult CommandWhois::HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target)
178 {
179         if (parameters.size() < 2)
180                 return CMD_FAILURE;
181
182         User* user = ServerInstance->FindUUID(parameters[0]);
183         if (!user)
184                 return CMD_FAILURE;
185
186         unsigned long idle = ConvToInt(parameters[1]);
187         DoWhois(user, target, target->signon, idle);
188
189         return CMD_SUCCESS;
190 }
191
192 CmdResult CommandWhois::HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
193 {
194         User *dest;
195         int userindex = 0;
196         unsigned long idle = 0, signon = 0;
197
198         if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
199                 return CMD_SUCCESS;
200
201         /*
202          * If 2 paramters are specified (/whois nick nick), ignore the first one like spanningtree
203          * does, and use the second one, otherwise, use the only paramter. -- djGrrr
204          */
205         if (parameters.size() > 1)
206                 userindex = 1;
207
208         dest = ServerInstance->FindNickOnly(parameters[userindex]);
209
210         if ((dest) && (dest->registered == REG_ALL))
211         {
212                 /*
213                  * Okay. Umpteenth attempt at doing this, so let's re-comment...
214                  * For local users (/w localuser), we show idletime if hidewhois is disabled
215                  * For local users (/w localuser localuser), we always show idletime, hence parameters.size() > 1 check.
216                  * For remote users (/w remoteuser), we do NOT show idletime
217                  * For remote users (/w remoteuser remoteuser), spanningtree will handle calling do_whois, so we can ignore this case.
218                  * Thanks to djGrrr for not being impatient while I have a crap day coding. :p -- w00t
219                  */
220                 LocalUser* localuser = IS_LOCAL(dest);
221                 if (localuser && (ServerInstance->Config->HideWhoisServer.empty() || parameters.size() > 1))
222                 {
223                         idle = abs((long)((localuser->idle_lastmsg)-ServerInstance->Time()));
224                         signon = dest->signon;
225                 }
226
227                 DoWhois(user,dest,signon,idle);
228         }
229         else
230         {
231                 /* no such nick/channel */
232                 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), !parameters[userindex].empty() ? parameters[userindex].c_str() : "*");
233                 user->WriteNumeric(318, "%s %s :End of /WHOIS list.",user->nick.c_str(), !parameters[userindex].empty() ? parameters[userindex].c_str() : "*");
234                 return CMD_FAILURE;
235         }
236
237         return CMD_SUCCESS;
238 }
239
240 COMMAND_INIT(CommandWhois)