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