]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_whois.cpp
bd05029980b2cb1ba106d6b9b10b70af04633a40
[user/henk/code/inspircd.git] / src / coremods / core_whois.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5  *   Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
6  *   Copyright (C) 2018 Dylan Frank <b00mx0r@aureus.pw>
7  *   Copyright (C) 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
8  *   Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
9  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
10  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
12  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
13  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
14  *   Copyright (C) 2006-2008, 2010 Craig Edwards <brain@inspircd.org>
15  *
16  * This file is part of InspIRCd.  InspIRCd is free software: you can
17  * redistribute it and/or modify it under the terms of the GNU General Public
18  * License as published by the Free Software Foundation, version 2.
19  *
20  * This program is distributed in the hope that it will be useful, but WITHOUT
21  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  */
28
29
30 #include "inspircd.h"
31 #include "modules/whois.h"
32
33 enum
34 {
35         // From RFC 1459.
36         RPL_WHOISUSER = 311,
37         RPL_WHOISOPERATOR = 313,
38         RPL_WHOISIDLE = 317,
39         RPL_WHOISCHANNELS = 319,
40
41         // From UnrealIRCd.
42         RPL_WHOISHOST = 378,
43         RPL_WHOISMODES = 379,
44
45         // InspIRCd-specific.
46         RPL_CHANNELSMSG = 651
47 };
48
49 enum SplitWhoisState
50 {
51         // Don't split private/secret channels into a separate RPL_WHOISCHANNELS numeric.
52         SPLITWHOIS_NONE,
53
54         // Split private/secret channels into a separate RPL_WHOISCHANNELS numeric.
55         SPLITWHOIS_SPLIT,
56
57         // Split private/secret channels into a separate RPL_WHOISCHANNELS numeric with RPL_CHANNELSMSG to explain the split.
58         SPLITWHOIS_SPLITMSG
59 };
60
61 class WhoisContextImpl : public Whois::Context
62 {
63         Events::ModuleEventProvider& lineevprov;
64
65  public:
66         WhoisContextImpl(LocalUser* src, User* targ, Events::ModuleEventProvider& evprov)
67                 : Whois::Context(src, targ)
68                 , lineevprov(evprov)
69         {
70         }
71
72         using Whois::Context::SendLine;
73         void SendLine(Numeric::Numeric& numeric) CXX11_OVERRIDE;
74 };
75
76 void WhoisContextImpl::SendLine(Numeric::Numeric& numeric)
77 {
78         ModResult MOD_RESULT;
79         FIRST_MOD_RESULT_CUSTOM(lineevprov, Whois::LineEventListener, OnWhoisLine, MOD_RESULT, (*this, numeric));
80
81         if (MOD_RESULT != MOD_RES_DENY)
82                 source->WriteNumeric(numeric);
83 }
84
85 /** Handle /WHOIS.
86  */
87 class CommandWhois : public SplitCommand
88 {
89         ChanModeReference secretmode;
90         ChanModeReference privatemode;
91         UserModeReference snomaskmode;
92         Events::ModuleEventProvider evprov;
93         Events::ModuleEventProvider lineevprov;
94
95         void DoWhois(LocalUser* user, User* dest, time_t signon, unsigned long idle);
96         void SendChanList(WhoisContextImpl& whois);
97
98  public:
99          /** If true then all opers are shown with a generic 'is a server operator' line rather than the oper type. */
100         bool genericoper;
101
102          /** How to handle private/secret channels in the WHOIS response. */
103         SplitWhoisState splitwhois;
104
105
106
107         /** Constructor for whois.
108          */
109         CommandWhois(Module* parent)
110                 : SplitCommand(parent, "WHOIS", 1)
111                 , secretmode(parent, "secret")
112                 , privatemode(parent, "private")
113                 , snomaskmode(parent, "snomask")
114                 , evprov(parent, "event/whois")
115                 , lineevprov(parent, "event/whoisline")
116         {
117                 Penalty = 2;
118                 syntax = "[<servername>] <nick>[,<nick>]+";
119         }
120
121         /** Handle command.
122          * @param parameters The parameters to the command
123          * @param user The user issuing the command
124          * @return A value from CmdResult to indicate command success or failure.
125          */
126         CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE;
127         CmdResult HandleRemote(RemoteUser* target, const Params& parameters) CXX11_OVERRIDE;
128 };
129
130 class WhoisNumericSink
131 {
132         WhoisContextImpl& whois;
133  public:
134         WhoisNumericSink(WhoisContextImpl& whoisref)
135                 : whois(whoisref)
136         {
137         }
138
139         void operator()(Numeric::Numeric& numeric) const
140         {
141                 whois.SendLine(numeric);
142         }
143 };
144
145 class WhoisChanListNumericBuilder : public Numeric::GenericBuilder<' ', false, WhoisNumericSink>
146 {
147  public:
148         WhoisChanListNumericBuilder(WhoisContextImpl& whois)
149                 : Numeric::GenericBuilder<' ', false, WhoisNumericSink>(WhoisNumericSink(whois), RPL_WHOISCHANNELS, false, whois.GetSource()->nick.size() + whois.GetTarget()->nick.size() + 1)
150         {
151                 GetNumeric().push(whois.GetTarget()->nick).push(std::string());
152         }
153 };
154
155 class WhoisChanList
156 {
157         const SplitWhoisState& splitwhois;
158         WhoisChanListNumericBuilder num;
159         WhoisChanListNumericBuilder secretnum;
160         std::string prefixstr;
161
162         void AddMember(Membership* memb, WhoisChanListNumericBuilder& out)
163         {
164                 prefixstr.clear();
165                 const char prefix = memb->GetPrefixChar();
166                 if (prefix)
167                         prefixstr.push_back(prefix);
168                 out.Add(prefixstr, memb->chan->name);
169         }
170
171  public:
172         WhoisChanList(WhoisContextImpl& whois, const SplitWhoisState& sws)
173                 : splitwhois(sws)
174                 , num(whois)
175                 , secretnum(whois)
176         {
177         }
178
179         void AddVisible(Membership* memb)
180         {
181                 AddMember(memb, num);
182         }
183
184         void AddHidden(Membership* memb)
185         {
186                 AddMember(memb, splitwhois == SPLITWHOIS_NONE ? num : secretnum);
187         }
188
189         void Flush(WhoisContextImpl& whois)
190         {
191                 num.Flush();
192                 if (!secretnum.IsEmpty() && splitwhois == SPLITWHOIS_SPLITMSG)
193                         whois.SendLine(RPL_CHANNELSMSG, "is on private/secret channels:");
194                 secretnum.Flush();
195         }
196 };
197
198 void CommandWhois::SendChanList(WhoisContextImpl& whois)
199 {
200         WhoisChanList chanlist(whois, splitwhois);
201
202         User* const target = whois.GetTarget();
203         bool hasoperpriv = whois.GetSource()->HasPrivPermission("users/channel-spy");
204         for (User::ChanList::iterator i = target->chans.begin(); i != target->chans.end(); ++i)
205         {
206                 Membership* memb = *i;
207                 Channel* c = memb->chan;
208
209                 // Anyone can view channels which are not private or secret.
210                 if (!c->IsModeSet(privatemode) && !c->IsModeSet(secretmode))
211                         chanlist.AddVisible(memb);
212
213                 // Hidden channels are visible when the following conditions are true:
214                 // (1) The source user and the target user are the same.
215                 // (2) The source user is a member of the hidden channel.
216                 // (3) The source user is an oper with the users/channel-spy privilege.
217                 else if (whois.IsSelfWhois() || c->HasUser(whois.GetSource()) || hasoperpriv)
218                         chanlist.AddHidden(memb);
219         }
220
221         chanlist.Flush(whois);
222 }
223
224 void CommandWhois::DoWhois(LocalUser* user, User* dest, time_t signon, unsigned long idle)
225 {
226         WhoisContextImpl whois(user, dest, lineevprov);
227
228         whois.SendLine(RPL_WHOISUSER, dest->ident, dest->GetDisplayedHost(), '*', dest->GetRealName());
229         if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
230         {
231                 whois.SendLine(RPL_WHOISHOST, InspIRCd::Format("is connecting from %s@%s %s", dest->ident.c_str(), dest->GetRealHost().c_str(), dest->GetIPString().c_str()));
232         }
233
234         SendChanList(whois);
235
236         if (!whois.IsSelfWhois() && !ServerInstance->Config->HideServer.empty() && !user->HasPrivPermission("servers/auspex"))
237         {
238                 whois.SendLine(RPL_WHOISSERVER, ServerInstance->Config->HideServer, ServerInstance->Config->Network);
239         }
240         else
241         {
242                 whois.SendLine(RPL_WHOISSERVER, dest->server->GetName(), dest->server->GetDesc());
243         }
244
245         if (dest->IsAway())
246         {
247                 whois.SendLine(RPL_AWAY, dest->awaymsg);
248         }
249
250         if (dest->IsOper())
251         {
252                 if (genericoper)
253                         whois.SendLine(RPL_WHOISOPERATOR, "is a server operator");
254                 else
255                         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()));
256         }
257
258         if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
259         {
260                 if (dest->IsModeSet(snomaskmode))
261                 {
262                         whois.SendLine(RPL_WHOISMODES, InspIRCd::Format("is using modes %s %s", dest->GetModeLetters().c_str(), snomaskmode->GetUserParameter(dest).c_str()));
263                 }
264                 else
265                 {
266                         whois.SendLine(RPL_WHOISMODES, InspIRCd::Format("is using modes %s", dest->GetModeLetters().c_str()));
267                 }
268         }
269
270         FOREACH_MOD_CUSTOM(evprov, Whois::EventListener, OnWhois, (whois));
271
272         /*
273          * We only send these if we've been provided them. That is, if hideserver is turned off, and user is local, or
274          * if remote whois is queried, too. This is to keep the user hidden, and also since you can't reliably tell remote time. -- w00t
275          */
276         if ((idle) || (signon))
277         {
278                 whois.SendLine(RPL_WHOISIDLE, idle, signon, "seconds idle, signon time");
279         }
280
281         whois.SendLine(RPL_ENDOFWHOIS, "End of /WHOIS list.");
282 }
283
284 CmdResult CommandWhois::HandleRemote(RemoteUser* target, const Params& parameters)
285 {
286         if (parameters.size() < 2)
287                 return CMD_FAILURE;
288
289         User* user = ServerInstance->FindUUID(parameters[0]);
290         if (!user)
291                 return CMD_FAILURE;
292
293         // User doing the whois must be on this server
294         LocalUser* localuser = IS_LOCAL(user);
295         if (!localuser)
296                 return CMD_FAILURE;
297
298         unsigned long idle = ConvToNum<unsigned long>(parameters.back());
299         DoWhois(localuser, target, target->signon, idle);
300
301         return CMD_SUCCESS;
302 }
303
304 CmdResult CommandWhois::HandleLocal(LocalUser* user, const Params& parameters)
305 {
306         User *dest;
307         unsigned int userindex = 0;
308         unsigned long idle = 0;
309         time_t signon = 0;
310
311         if (CommandParser::LoopCall(user, this, parameters, 0))
312                 return CMD_SUCCESS;
313
314         /*
315          * If 2 parameters are specified (/whois nick nick), ignore the first one like spanningtree
316          * does, and use the second one, otherwise, use the only parameter. -- djGrrr
317          */
318         if (parameters.size() > 1)
319                 userindex = 1;
320
321         dest = ServerInstance->FindNickOnly(parameters[userindex]);
322
323         if ((dest) && (dest->registered == REG_ALL))
324         {
325                 /*
326                  * Okay. Umpteenth attempt at doing this, so let's re-comment...
327                  * For local users (/w localuser), we show idletime if hideserver is disabled
328                  * For local users (/w localuser localuser), we always show idletime, hence parameters.size() > 1 check.
329                  * For remote users (/w remoteuser), we do NOT show idletime
330                  * For remote users (/w remoteuser remoteuser), spanningtree will handle calling do_whois, so we can ignore this case.
331                  * Thanks to djGrrr for not being impatient while I have a crap day coding. :p -- w00t
332                  */
333                 LocalUser* localuser = IS_LOCAL(dest);
334                 if (localuser && (ServerInstance->Config->HideServer.empty() || parameters.size() > 1))
335                 {
336                         idle = labs((long)((localuser->idle_lastmsg)-ServerInstance->Time()));
337                         signon = dest->signon;
338                 }
339
340                 DoWhois(user,dest,signon,idle);
341         }
342         else
343         {
344                 /* no such nick/channel */
345                 user->WriteNumeric(Numerics::NoSuchNick(!parameters[userindex].empty() ? parameters[userindex] : "*"));
346                 user->WriteNumeric(RPL_ENDOFWHOIS, (!parameters[userindex].empty() ? parameters[userindex] : "*"), "End of /WHOIS list.");
347                 return CMD_FAILURE;
348         }
349
350         return CMD_SUCCESS;
351 }
352
353 class CoreModWhois : public Module
354 {
355  private:
356         CommandWhois cmd;
357
358  public:
359         CoreModWhois()
360                 : cmd(this)
361         {
362         }
363
364         void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
365         {
366                 ConfigTag* tag = ServerInstance->Config->ConfValue("options");
367                 const std::string splitwhois = tag->getString("splitwhois", "no", 1);
368                 SplitWhoisState newsplitstate;
369                 if (stdalgo::string::equalsci(splitwhois, "no"))
370                         newsplitstate = SPLITWHOIS_NONE;
371                 else if (stdalgo::string::equalsci(splitwhois, "split"))
372                         newsplitstate = SPLITWHOIS_SPLIT;
373                 else if (stdalgo::string::equalsci(splitwhois, "splitmsg"))
374                         newsplitstate = SPLITWHOIS_SPLITMSG;
375                 else
376                         throw ModuleException(splitwhois + " is an invalid <options:splitwhois> value, at " + tag->getTagLocation());
377
378                 ConfigTag* security = ServerInstance->Config->ConfValue("security");
379                 cmd.genericoper = security->getBool("genericoper");
380                 cmd.splitwhois = newsplitstate;
381         }
382
383         Version GetVersion() CXX11_OVERRIDE
384         {
385                 return Version("Provides the WHOIS command", VF_VENDOR|VF_CORE);
386         }
387 };
388
389 MODULE_INIT(CoreModWhois)