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