]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_who.cpp
739c675a9d43010b7c46530f452617ce06f0d2c6
[user/henk/code/inspircd.git] / src / coremods / core_who.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22
23 /** Handle /WHO.
24  */
25 class CommandWho : public Command
26 {
27         bool CanView(Channel* chan, User* user);
28         bool opt_viewopersonly;
29         bool opt_showrealhost;
30         bool opt_realname;
31         bool opt_mode;
32         bool opt_ident;
33         bool opt_metadata;
34         bool opt_port;
35         bool opt_away;
36         bool opt_local;
37         bool opt_far;
38         bool opt_time;
39         ChanModeReference secretmode;
40         ChanModeReference privatemode;
41         UserModeReference invisiblemode;
42
43         Membership* get_first_visible_channel(User* u)
44         {
45                 for (User::ChanList::iterator i = u->chans.begin(); i != u->chans.end(); ++i)
46                 {
47                         Membership* memb = *i;
48                         if (!memb->chan->IsModeSet(secretmode))
49                                 return memb;
50                 }
51                 return NULL;
52         }
53
54  public:
55         /** Constructor for who.
56          */
57         CommandWho(Module* parent)
58                 : Command(parent, "WHO", 1)
59                 , secretmode(parent, "secret")
60                 , privatemode(parent, "private")
61                 , invisiblemode(parent, "invisible")
62         {
63                 syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [ohurmMiaplf]";
64         }
65
66         void SendWhoLine(User* user, const std::vector<std::string>& parms, Membership* memb, User* u, std::vector<Numeric::Numeric>& whoresults);
67         /** Handle command.
68          * @param parameters The parameters to the command
69          * @param user The user issuing the command
70          * @return A value from CmdResult to indicate command success or failure.
71          */
72         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
73         bool whomatch(User* cuser, User* user, const char* matchtext);
74 };
75
76 bool CommandWho::whomatch(User* cuser, User* user, const char* matchtext)
77 {
78         bool match = false;
79         bool positive = false;
80
81         if (user->registered != REG_ALL)
82                 return false;
83
84         if (opt_local && !IS_LOCAL(user))
85                 return false;
86         else if (opt_far && IS_LOCAL(user))
87                 return false;
88
89         if (opt_mode)
90         {
91                 for (const char* n = matchtext; *n; n++)
92                 {
93                         if (*n == '+')
94                         {
95                                 positive = true;
96                                 continue;
97                         }
98                         else if (*n == '-')
99                         {
100                                 positive = false;
101                                 continue;
102                         }
103                         if (user->IsModeSet(*n) != positive)
104                                 return false;
105                 }
106                 return true;
107         }
108         else
109         {
110                 /*
111                  * This was previously one awesome pile of ugly nested if, when really, it didn't need
112                  * to be, since only one condition was ever checked, a chained if works just fine.
113                  * -- w00t
114                  */
115                 if (opt_metadata)
116                 {
117                         match = false;
118                         const Extensible::ExtensibleStore& list = user->GetExtList();
119                         for(Extensible::ExtensibleStore::const_iterator i = list.begin(); i != list.end(); ++i)
120                                 if (InspIRCd::Match(i->first->name, matchtext))
121                                         match = true;
122                 }
123                 else if (opt_realname)
124                         match = InspIRCd::Match(user->fullname, matchtext);
125                 else if (opt_showrealhost)
126                         match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
127                 else if (opt_ident)
128                         match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
129                 else if (opt_port)
130                 {
131                         irc::portparser portrange(matchtext, false);
132                         long portno = -1;
133                         while ((portno = portrange.GetToken()))
134                                 if (IS_LOCAL(user) && portno == IS_LOCAL(user)->GetServerPort())
135                                 {
136                                         match = true;
137                                         break;
138                                 }
139                 }
140                 else if (opt_away)
141                         match = InspIRCd::Match(user->awaymsg, matchtext);
142                 else if (opt_time)
143                 {
144                         long seconds = InspIRCd::Duration(matchtext);
145
146                         // Okay, so time matching, we want all users connected `seconds' ago
147                         if (user->signon >= ServerInstance->Time() - seconds)
148                                 match = true;
149                 }
150
151                 /*
152                  * Once the conditionals have been checked, only check dhost/nick/server
153                  * if they didn't match this user -- and only match if we don't find a match.
154                  *
155                  * This should make things minutely faster, and again, less ugly.
156                  * -- w00t
157                  */
158                 if (!match)
159                         match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
160
161                 if (!match)
162                         match = InspIRCd::Match(user->nick, matchtext);
163
164                 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
165                 if (!match && (ServerInstance->Config->HideWhoisServer.empty() || cuser->HasPrivPermission("users/auspex")))
166                         match = InspIRCd::Match(user->server->GetName(), matchtext);
167
168                 return match;
169         }
170 }
171
172 bool CommandWho::CanView(Channel* chan, User* user)
173 {
174         /* Bug #383 - moved higher up the list, because if we are in the channel
175          * we can see all its users
176          */
177         if (chan->HasUser(user))
178                 return true;
179         /* Opers see all */
180         if (user->HasPrivPermission("users/auspex"))
181                 return true;
182         /* Cant see inside a +s or a +p channel unless we are a member (see above) */
183         else if (!chan->IsModeSet(secretmode) && !chan->IsModeSet(privatemode))
184                 return true;
185
186         return false;
187 }
188
189 void CommandWho::SendWhoLine(User* user, const std::vector<std::string>& parms, Membership* memb, User* u, std::vector<Numeric::Numeric>& whoresults)
190 {
191         if (!memb)
192                 memb = get_first_visible_channel(u);
193
194         Numeric::Numeric wholine(RPL_WHOREPLY);
195         wholine.push(memb ? memb->chan->name : "*").push(u->ident);
196         wholine.push(opt_showrealhost ? u->host : u->dhost);
197         if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
198                 wholine.push(ServerInstance->Config->HideWhoisServer);
199         else
200                 wholine.push(u->server->GetName());
201
202         wholine.push(u->nick);
203
204         std::string param;
205         /* away? */
206         if (u->IsAway())
207         {
208                 param.push_back('G');
209         }
210         else
211         {
212                 param.push_back('H');
213         }
214
215         /* oper? */
216         if (u->IsOper())
217         {
218                 param.push_back('*');
219         }
220
221         if (memb)
222         {
223                 char prefix = memb->GetPrefixChar();
224                 if (prefix)
225                         param.push_back(prefix);
226         }
227
228         wholine.push(param);
229         wholine.push("0 ");
230         wholine.GetParams().back().append(u->fullname);
231
232         ModResult res;
233         FIRST_MOD_RESULT(OnSendWhoLine, res, (user, parms, u, memb, wholine));
234         if (res != MOD_RES_DENY)
235                 whoresults.push_back(wholine);
236 }
237
238 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
239 {
240         /*
241          * XXX - RFC says:
242          *   The <name> passed to WHO is matched against users' host, server, real
243          *   name and nickname
244          * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
245          */
246
247         /* WHO options */
248         opt_viewopersonly = false;
249         opt_showrealhost = false;
250         opt_realname = false;
251         opt_mode = false;
252         opt_ident = false;
253         opt_metadata = false;
254         opt_port = false;
255         opt_away = false;
256         opt_local = false;
257         opt_far = false;
258         opt_time = false;
259
260         std::vector<Numeric::Numeric> whoresults;
261
262         /* Change '0' into '*' so the wildcard matcher can grok it */
263         std::string matchtext = ((parameters[0] == "0") ? "*" : parameters[0]);
264
265         // WHO flags count as a wildcard
266         bool usingwildcards = ((parameters.size() > 1) || (matchtext.find_first_of("*?.") != std::string::npos));
267
268         if (parameters.size() > 1)
269         {
270                 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
271                 {
272                         switch (*iter)
273                         {
274                                 case 'o':
275                                         opt_viewopersonly = true;
276                                         break;
277                                 case 'h':
278                                         if (user->HasPrivPermission("users/auspex"))
279                                                 opt_showrealhost = true;
280                                         break;
281                                 case 'r':
282                                         opt_realname = true;
283                                         break;
284                                 case 'm':
285                                         if (user->HasPrivPermission("users/auspex"))
286                                                 opt_mode = true;
287                                         break;
288                                 case 'M':
289                                         if (user->HasPrivPermission("users/auspex"))
290                                                 opt_metadata = true;
291                                         break;
292                                 case 'i':
293                                         opt_ident = true;
294                                         break;
295                                 case 'p':
296                                         if (user->HasPrivPermission("users/auspex"))
297                                                 opt_port = true;
298                                         break;
299                                 case 'a':
300                                         opt_away = true;
301                                         break;
302                                 case 'l':
303                                         if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
304                                                 opt_local = true;
305                                         break;
306                                 case 'f':
307                                         if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
308                                                 opt_far = true;
309                                         break;
310                                 case 't':
311                                         opt_time = true;
312                                         break;
313                         }
314                 }
315         }
316
317
318         /* who on a channel? */
319         Channel* ch = ServerInstance->FindChan(matchtext);
320
321         if (ch)
322         {
323                 if (CanView(ch,user))
324                 {
325                         bool inside = ch->HasUser(user);
326
327                         /* who on a channel. */
328                         const Channel::MemberMap& cu = ch->GetUsers();
329                         for (Channel::MemberMap::const_iterator i = cu.begin(); i != cu.end(); ++i)
330                         {
331                                 /* None of this applies if we WHO ourselves */
332                                 if (user != i->first)
333                                 {
334                                         /* opers only, please */
335                                         if (opt_viewopersonly && !i->first->IsOper())
336                                                 continue;
337
338                                         /* If we're not inside the channel, hide +i users */
339                                         if (i->first->IsModeSet(invisiblemode) && !inside && !user->HasPrivPermission("users/auspex"))
340                                                 continue;
341                                 }
342
343                                 SendWhoLine(user, parameters, i->second, i->first, whoresults);
344                         }
345                 }
346         }
347         else
348         {
349                 /* Match against wildcard of nick, server or host */
350                 if (opt_viewopersonly)
351                 {
352                         /* Showing only opers */
353                         const UserManager::OperList& opers = ServerInstance->Users->all_opers;
354                         for (UserManager::OperList::const_iterator i = opers.begin(); i != opers.end(); ++i)
355                         {
356                                 User* oper = *i;
357
358                                 if (whomatch(user, oper, matchtext.c_str()))
359                                 {
360                                         if (!user->SharesChannelWith(oper))
361                                         {
362                                                 if (usingwildcards && (oper->IsModeSet(invisiblemode)) && (!user->HasPrivPermission("users/auspex")))
363                                                         continue;
364                                         }
365
366                                         SendWhoLine(user, parameters, NULL, oper, whoresults);
367                                 }
368                         }
369                 }
370                 else
371                 {
372                         const user_hash& users = ServerInstance->Users->GetUsers();
373                         for (user_hash::const_iterator i = users.begin(); i != users.end(); ++i)
374                         {
375                                 if (whomatch(user, i->second, matchtext.c_str()))
376                                 {
377                                         if (!user->SharesChannelWith(i->second))
378                                         {
379                                                 if (usingwildcards && (i->second->IsModeSet(invisiblemode)) && (!user->HasPrivPermission("users/auspex")))
380                                                         continue;
381                                         }
382
383                                         SendWhoLine(user, parameters, NULL, i->second, whoresults);
384                                 }
385                         }
386                 }
387         }
388         /* Send the results out */
389         for (std::vector<Numeric::Numeric>::const_iterator n = whoresults.begin(); n != whoresults.end(); ++n)
390                 user->WriteNumeric(*n);
391         user->WriteNumeric(RPL_ENDOFWHO, (*parameters[0].c_str() ? parameters[0] : "*"), "End of /WHO list.");
392
393         // Penalize the user a bit for large queries
394         // (add one unit of penalty per 200 results)
395         if (IS_LOCAL(user))
396                 IS_LOCAL(user)->CommandFloodPenalty += whoresults.size() * 5;
397         return CMD_SUCCESS;
398 }
399
400 COMMAND_INIT(CommandWho)