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