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