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