]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_whois.cpp
Merge pull request #1103 from rburchell/master-fix-info
[user/henk/code/inspircd.git] / src / coremods / core_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 class WhoisContextImpl : public Whois::Context
25 {
26         Events::ModuleEventProvider& lineevprov;
27
28  public:
29         WhoisContextImpl(LocalUser* src, User* targ, Events::ModuleEventProvider& evprov)
30                 : Whois::Context(src, targ)
31                 , lineevprov(evprov)
32         {
33         }
34
35         using Whois::Context::SendLine;
36         void SendLine(unsigned int numeric, const std::string& text) CXX11_OVERRIDE;
37 };
38
39 void WhoisContextImpl::SendLine(unsigned int numeric, const std::string& text)
40 {
41         std::string copy_text = target->nick;
42         copy_text.push_back(' ');
43         copy_text.append(text);
44
45         ModResult MOD_RESULT;
46         FIRST_MOD_RESULT_CUSTOM(lineevprov, Whois::LineEventListener, OnWhoisLine, MOD_RESULT, (*this, numeric, copy_text));
47
48         if (MOD_RESULT != MOD_RES_DENY)
49                 source->WriteNumeric(numeric, copy_text);
50 }
51
52 /** Handle /WHOIS.
53  */
54 class CommandWhois : public SplitCommand
55 {
56         ChanModeReference secretmode;
57         ChanModeReference privatemode;
58         UserModeReference snomaskmode;
59         Events::ModuleEventProvider evprov;
60         Events::ModuleEventProvider lineevprov;
61
62         void SplitChanList(WhoisContextImpl& whois, const std::string& cl);
63         void DoWhois(LocalUser* user, User* dest, unsigned long signon, unsigned long idle);
64         std::string ChannelList(User* source, User* dest, bool spy);
65
66  public:
67         /** Constructor for whois.
68          */
69         CommandWhois(Module* parent)
70                 : SplitCommand(parent, "WHOIS", 1)
71                 , secretmode(parent, "secret")
72                 , privatemode(parent, "private")
73                 , snomaskmode(parent, "snomask")
74                 , evprov(parent, "event/whois")
75                 , lineevprov(parent, "event/whoisline")
76         {
77                 Penalty = 2;
78                 syntax = "<nick>{,<nick>}";
79         }
80
81         /** Handle command.
82          * @param parameters The parameters to the command
83          * @param user The user issuing the command
84          * @return A value from CmdResult to indicate command success or failure.
85          */
86         CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user);
87         CmdResult HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target);
88 };
89
90 std::string CommandWhois::ChannelList(User* source, User* dest, bool spy)
91 {
92         std::string list;
93
94         for (User::ChanList::iterator i = dest->chans.begin(); i != dest->chans.end(); i++)
95         {
96                 Membership* memb = *i;
97                 Channel* c = memb->chan;
98                 /* If the target is the sender, neither +p nor +s is set, or
99                  * the channel contains the user, it is not a spy channel
100                  */
101                 if (spy != (source == dest || !(c->IsModeSet(privatemode) || c->IsModeSet(secretmode)) || c->HasUser(source)))
102                 {
103                         char prefix = memb->GetPrefixChar();
104                         if (prefix)
105                                 list.push_back(prefix);
106                         list.append(c->name).push_back(' ');
107                 }
108         }
109
110         return list;
111 }
112
113 void CommandWhois::SplitChanList(WhoisContextImpl& whois, const std::string& cl)
114 {
115         std::string line(1, ':');
116         std::string::size_type start, pos;
117
118         // ":server.name 319 source target " ... "\r\n"
119         const std::string::size_type maxlen = ServerInstance->Config->Limits.MaxLine - 10 - ServerInstance->Config->ServerName.length() - whois.GetTarget()->nick.length() - whois.GetSource()->nick.length();
120
121         for (start = 0; (pos = cl.find(' ', start)) != std::string::npos; start = pos+1)
122         {
123                 if (line.length() + pos - start > maxlen)
124                 {
125                         // Erase last ' ' and send
126                         line.erase(line.length()-1);
127                         whois.SendLine(319, line);
128                         line.erase(1);
129                 }
130
131                 line.append(cl, start, pos - start + 1);
132         }
133
134         if (line.length() > 1)
135         {
136                 // Erase last ' ' and send
137                 line.erase(line.length()-1);
138                 whois.SendLine(319, line);
139         }
140 }
141
142 void CommandWhois::DoWhois(LocalUser* user, User* dest, unsigned long signon, unsigned long idle)
143 {
144         WhoisContextImpl whois(user, dest, lineevprov);
145
146         whois.SendLine(311, "%s %s * :%s", dest->ident.c_str(), dest->dhost.c_str(), dest->fullname.c_str());
147         if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
148         {
149                 whois.SendLine(378, ":is connecting from %s@%s %s", dest->ident.c_str(), dest->host.c_str(), dest->GetIPString().c_str());
150         }
151
152         std::string cl = ChannelList(user, dest, false);
153         const ServerConfig::OperSpyWhoisState state = user->HasPrivPermission("users/auspex") ? ServerInstance->Config->OperSpyWhois : ServerConfig::SPYWHOIS_NONE;
154
155         if (state == ServerConfig::SPYWHOIS_SINGLEMSG)
156                 cl.append(ChannelList(user, dest, true));
157
158         SplitChanList(whois, cl);
159
160         if (state == ServerConfig::SPYWHOIS_SPLITMSG)
161         {
162                 std::string scl = ChannelList(user, dest, true);
163                 if (scl.length())
164                 {
165                         whois.SendLine(336, ":is on private/secret channels:");
166                         SplitChanList(whois, scl);
167                 }
168         }
169         if (!whois.IsSelfWhois() && !ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
170         {
171                 whois.SendLine(312, "%s :%s", ServerInstance->Config->HideWhoisServer.c_str(), ServerInstance->Config->Network.c_str());
172         }
173         else
174         {
175                 whois.SendLine(312, "%s :%s", dest->server->GetName().c_str(), dest->server->GetDesc().c_str());
176         }
177
178         if (dest->IsAway())
179         {
180                 whois.SendLine(301, ":%s", dest->awaymsg.c_str());
181         }
182
183         if (dest->IsOper())
184         {
185                 if (ServerInstance->Config->GenericOper)
186                         whois.SendLine(313, ":is an IRC operator");
187                 else
188                         whois.SendLine(313, ":is %s %s on %s", (strchr("AEIOUaeiou",dest->oper->name[0]) ? "an" : "a"),dest->oper->name.c_str(), ServerInstance->Config->Network.c_str());
189         }
190
191         if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
192         {
193                 if (dest->IsModeSet(snomaskmode))
194                 {
195                         whois.SendLine(379, ":is using modes +%s %s", dest->FormatModes(), snomaskmode->GetUserParameter(dest).c_str());
196                 }
197                 else
198                 {
199                         whois.SendLine(379, ":is using modes +%s", dest->FormatModes());
200                 }
201         }
202
203         FOREACH_MOD_CUSTOM(evprov, Whois::EventListener, OnWhois, (whois));
204
205         /*
206          * We only send these if we've been provided them. That is, if hidewhois is turned off, and user is local, or
207          * if remote whois is queried, too. This is to keep the user hidden, and also since you can't reliably tell remote time. -- w00t
208          */
209         if ((idle) || (signon))
210         {
211                 whois.SendLine(317, "%lu %lu :seconds idle, signon time", idle, signon);
212         }
213
214         whois.SendLine(318, ":End of /WHOIS list.");
215 }
216
217 CmdResult CommandWhois::HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target)
218 {
219         if (parameters.size() < 2)
220                 return CMD_FAILURE;
221
222         User* user = ServerInstance->FindUUID(parameters[0]);
223         if (!user)
224                 return CMD_FAILURE;
225
226         // User doing the whois must be on this server
227         LocalUser* localuser = IS_LOCAL(user);
228         if (!localuser)
229                 return CMD_FAILURE;
230
231         unsigned long idle = ConvToInt(parameters.back());
232         DoWhois(localuser, target, target->signon, idle);
233
234         return CMD_SUCCESS;
235 }
236
237 CmdResult CommandWhois::HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
238 {
239         User *dest;
240         int userindex = 0;
241         unsigned long idle = 0, signon = 0;
242
243         if (CommandParser::LoopCall(user, this, parameters, 0))
244                 return CMD_SUCCESS;
245
246         /*
247          * If 2 paramters are specified (/whois nick nick), ignore the first one like spanningtree
248          * does, and use the second one, otherwise, use the only paramter. -- djGrrr
249          */
250         if (parameters.size() > 1)
251                 userindex = 1;
252
253         dest = ServerInstance->FindNickOnly(parameters[userindex]);
254
255         if ((dest) && (dest->registered == REG_ALL))
256         {
257                 /*
258                  * Okay. Umpteenth attempt at doing this, so let's re-comment...
259                  * For local users (/w localuser), we show idletime if hidewhois is disabled
260                  * For local users (/w localuser localuser), we always show idletime, hence parameters.size() > 1 check.
261                  * For remote users (/w remoteuser), we do NOT show idletime
262                  * For remote users (/w remoteuser remoteuser), spanningtree will handle calling do_whois, so we can ignore this case.
263                  * Thanks to djGrrr for not being impatient while I have a crap day coding. :p -- w00t
264                  */
265                 LocalUser* localuser = IS_LOCAL(dest);
266                 if (localuser && (ServerInstance->Config->HideWhoisServer.empty() || parameters.size() > 1))
267                 {
268                         idle = labs((long)((localuser->idle_lastmsg)-ServerInstance->Time()));
269                         signon = dest->signon;
270                 }
271
272                 DoWhois(user,dest,signon,idle);
273         }
274         else
275         {
276                 /* no such nick/channel */
277                 user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", !parameters[userindex].empty() ? parameters[userindex].c_str() : "*");
278                 user->WriteNumeric(RPL_ENDOFWHOIS, "%s :End of /WHOIS list.", !parameters[userindex].empty() ? parameters[userindex].c_str() : "*");
279                 return CMD_FAILURE;
280         }
281
282         return CMD_SUCCESS;
283 }
284
285 COMMAND_INIT(CommandWhois)