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