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