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