]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_who.cpp
Don't allow /who to match servers when HideWhoisServer is enabled (unless the user...
[user/henk/code/inspircd.git] / src / commands / cmd_who.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 #ifndef __CMD_WHO_H__
17 #define __CMD_WHO_H__
18
19 // include the common header files
20
21 #include "users.h"
22 #include "channels.h"
23
24 /** Handle /WHO. These command handlers can be reloaded by the core,
25  * and handle basic RFC1459 commands. Commands within modules work
26  * the same way, however, they can be fully unloaded, where these
27  * may not.
28  */
29 class CommandWho : public Command
30 {
31         bool CanView(Channel* chan, User* user);
32         bool opt_viewopersonly;
33         bool opt_showrealhost;
34         bool opt_unlimit;
35         bool opt_realname;
36         bool opt_mode;
37         bool opt_ident;
38         bool opt_metadata;
39         bool opt_port;
40         bool opt_away;
41         bool opt_local;
42         bool opt_far;
43         bool opt_time;
44
45  public:
46         /** Constructor for who.
47          */
48         CommandWho (InspIRCd* Instance, Module* parent) : Command(Instance,parent,"WHO", 0, 1, false, 2) { syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [ohurmMiaplf]"; }
49         void SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults);
50         /** Handle command.
51          * @param parameters The parameters to the comamnd
52          * @param pcnt The number of parameters passed to teh command
53          * @param user The user issuing the command
54          * @return A value from CmdResult to indicate command success or failure.
55          */
56         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
57         bool whomatch(User* cuser, User* user, const char* matchtext);
58 };
59
60 #endif
61
62
63 static const std::string star = "*";
64
65 static const std::string& get_first_visible_channel(User *u)
66 {
67         UCListIter i = u->chans.begin();
68         if (i != u->chans.end())
69         {
70                 if (!i->first->IsModeSet('s'))
71                         return i->first->name;
72         }
73
74         return star;
75 }
76
77 bool CommandWho::whomatch(User* cuser, User* user, const char* matchtext)
78 {
79         bool match = false;
80         bool positive = false;
81         char* dummy = NULL;
82
83         if (user->registered != REG_ALL)
84                 return false;
85
86         if (opt_local && !IS_LOCAL(user))
87                 return false;
88         else if (opt_far && IS_LOCAL(user))
89                 return false;
90
91         if (opt_mode)
92         {
93                 for (const char* n = matchtext; *n; n++)
94                 {
95                         if (*n == '+')
96                         {
97                                 positive = true;
98                                 continue;
99                         }
100                         else if (*n == '-')
101                         {
102                                 positive = false;
103                                 continue;
104                         }
105                         if (user->IsModeSet(*n) != positive)
106                                 return false;
107                 }
108                 return true;
109         }
110         else
111         {
112                 /*
113                  * This was previously one awesome pile of ugly nested if, when really, it didn't need
114                  * to be, since only one condition was ever checked, a chained if works just fine.
115                  * -- w00t
116                  */
117                 if (opt_metadata)
118                         match = user->GetExt(matchtext, dummy);
119                 else if (opt_realname)
120                         match = InspIRCd::Match(user->fullname, matchtext);
121                 else if (opt_showrealhost)
122                         match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
123                 else if (opt_ident)
124                         match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
125                 else if (opt_port)
126                 {
127                         irc::portparser portrange(matchtext, false);
128                         long portno = -1;
129                         while ((portno = portrange.GetToken()))
130                                 if (portno == user->GetServerPort())
131                                 {
132                                         match = true;
133                                         break;
134                                 }
135                 }
136                 else if (opt_away)
137                         match = InspIRCd::Match(user->awaymsg, matchtext);
138                 else if (opt_time)
139                 {
140                         long seconds = ServerInstance->Duration(matchtext);
141
142                         // Okay, so time matching, we want all users connected `seconds' ago
143                         if (user->age >= ServerInstance->Time() - seconds)
144                                 match = true;
145                 }
146
147                 /*
148                  * Once the conditionals have been checked, only check dhost/nick/server
149                  * if they didn't match this user -- and only match if we don't find a match.
150                  *
151                  * This should make things minutely faster, and again, less ugly.
152                  * -- w00t
153                  */
154                 if (!match)
155                         match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
156
157                 if (!match)
158                         match = InspIRCd::Match(user->nick, matchtext);
159
160                 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
161                 if (!match && (!*ServerInstance->Config->HideWhoisServer || cuser->HasPrivPermission("users/auspex")))
162                         match = InspIRCd::Match(user->server, matchtext);
163
164                 return match;
165         }
166 }
167
168
169
170 bool CommandWho::CanView(Channel* chan, User* user)
171 {
172         if (!user || !chan)
173                 return false;
174
175         /* Bug #383 - moved higher up the list, because if we are in the channel
176          * we can see all its users
177          */
178         if (chan->HasUser(user))
179                 return true;
180         /* Opers see all */
181         if (user->HasPrivPermission("users/auspex"))
182                 return true;
183         /* Cant see inside a +s or a +p channel unless we are a member (see above) */
184         else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
185                 return true;
186
187         return false;
188 }
189
190 void CommandWho::SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
191 {
192         const std::string& lcn = get_first_visible_channel(u);
193         Channel* chlast = ServerInstance->FindChan(lcn);
194
195         std::string wholine =   initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
196                                 ((*ServerInstance->Config->HideWhoisServer && !user->HasPrivPermission("servers/auspex")) ? ServerInstance->Config->HideWhoisServer : u->server) +
197                                 " " + u->nick + " ";
198
199         /* away? */
200         if (IS_AWAY(u))
201         {
202                 wholine.append("G");
203         }
204         else
205         {
206                 wholine.append("H");
207         }
208
209         /* oper? */
210         if (IS_OPER(u))
211         {
212                 wholine.append("*");
213         }
214
215         wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
216
217         FOREACH_MOD(I_OnSendWhoLine, OnSendWhoLine(user, u, ch, wholine));
218
219         if (!wholine.empty())
220                 whoresults.push_back(wholine);
221 }
222
223 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
224 {
225         /*
226          * XXX - RFC says:
227          *   The <name> passed to WHO is matched against users' host, server, real
228          *   name and nickname
229          * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
230          */
231
232         /* WHO options */
233         opt_viewopersonly = false;
234         opt_showrealhost = false;
235         opt_unlimit = false;
236         opt_realname = false;
237         opt_mode = false;
238         opt_ident = false;
239         opt_metadata = false;
240         opt_port = false;
241         opt_away = false;
242         opt_local = false;
243         opt_far = false;
244         opt_time = false;
245
246         Channel *ch = NULL;
247         std::vector<std::string> whoresults;
248         std::string initial = "352 " + std::string(user->nick) + " ";
249
250         char matchtext[MAXBUF];
251         bool usingwildcards = false;
252
253         /* Change '0' into '*' so the wildcard matcher can grok it */
254         if (parameters[0] == "0")
255                 strlcpy(matchtext, "*", MAXBUF);
256         else
257                 strlcpy(matchtext, parameters[0].c_str(), MAXBUF);
258
259         for (const char* check = matchtext; *check; check++)
260         {
261                 if (*check == '*' || *check == '?')
262                 {
263                         usingwildcards = true;
264                         break;
265                 }
266         }
267
268         if (ServerInstance->FindServerName(matchtext))
269                 usingwildcards = true;
270
271         if (parameters.size() > 1)
272         {
273                 /* Fix for bug #444, WHO flags count as a wildcard */
274                 usingwildcards = true;
275
276                 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
277                 {
278                         switch (*iter)
279                         {
280                                 case 'o':
281                                         opt_viewopersonly = true;
282                                         break;
283                                 case 'h':
284                                         if (user->HasPrivPermission("users/auspex"))
285                                                 opt_showrealhost = true;
286                                         break;
287                                 case 'u':
288                                         if (user->HasPrivPermission("users/auspex"))
289                                                 opt_unlimit = true;
290                                         break;
291                                 case 'r':
292                                         opt_realname = true;
293                                         break;
294                                 case 'm':
295                                         if (user->HasPrivPermission("users/auspex"))
296                                                 opt_mode = true;
297                                         break;
298                                 case 'M':
299                                         if (user->HasPrivPermission("users/auspex"))
300                                                 opt_metadata = true;
301                                         break;
302                                 case 'i':
303                                         opt_ident = true;
304                                         break;
305                                 case 'p':
306                                         if (user->HasPrivPermission("users/auspex"))
307                                                 opt_port = true;
308                                         break;
309                                 case 'a':
310                                         opt_away = true;
311                                         break;
312                                 case 'l':
313                                         if (user->HasPrivPermission("users/auspex") || !*ServerInstance->Config->HideWhoisServer)
314                                                 opt_local = true;
315                                         break;
316                                 case 'f':
317                                         if (user->HasPrivPermission("users/auspex") || !*ServerInstance->Config->HideWhoisServer)
318                                                 opt_far = true;
319                                         break;
320                                 case 't':
321                                         opt_time = true;
322                                         break;
323                         }
324                 }
325         }
326
327
328         /* who on a channel? */
329         ch = ServerInstance->FindChan(matchtext);
330
331         if (ch)
332         {
333                 if (CanView(ch,user))
334                 {
335                         bool inside = ch->HasUser(user);
336
337                         /* who on a channel. */
338                         CUList *cu = ch->GetUsers();
339
340                         for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
341                         {
342                                 /* None of this applies if we WHO ourselves */
343                                 if (user != i->first)
344                                 {
345                                         /* opers only, please */
346                                         if (opt_viewopersonly && !IS_OPER(i->first))
347                                                 continue;
348
349                                         /* If we're not inside the channel, hide +i users */
350                                         if (i->first->IsModeSet('i') && !inside && !user->HasPrivPermission("users/auspex"))
351                                                 continue;
352                                 }
353
354                                 SendWhoLine(user, initial, ch, i->first, whoresults);
355                         }
356                 }
357         }
358         else
359         {
360                 /* Match against wildcard of nick, server or host */
361                 if (opt_viewopersonly)
362                 {
363                         /* Showing only opers */
364                         for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
365                         {
366                                 User* oper = *i;
367
368                                 if (whomatch(user, oper, matchtext))
369                                 {
370                                         if (!user->SharesChannelWith(oper))
371                                         {
372                                                 if (usingwildcards && (!oper->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
373                                                         continue;
374                                         }
375
376                                         SendWhoLine(user, initial, NULL, oper, whoresults);
377                                 }
378                         }
379                 }
380                 else
381                 {
382                         for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
383                         {
384                                 if (whomatch(user, i->second, matchtext))
385                                 {
386                                         if (!user->SharesChannelWith(i->second))
387                                         {
388                                                 if (usingwildcards && (i->second->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
389                                                         continue;
390                                         }
391
392                                         SendWhoLine(user, initial, NULL, i->second, whoresults);
393                                 }
394                         }
395                 }
396         }
397         /* Send the results out */
398         if ((ServerInstance->Config->MaxWhoResults && (whoresults.size() <= (size_t)ServerInstance->Config->MaxWhoResults)) || opt_unlimit)
399         {
400                 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
401                         user->WriteServ(*n);
402                 user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
403                 return CMD_SUCCESS;
404         }
405         else
406         {
407                 /* BZZT! Too many results. */
408                 user->WriteNumeric(315, "%s %s :Too many results",user->nick.c_str(), parameters[0].c_str());
409                 return CMD_FAILURE;
410         }
411 }
412
413 COMMAND_INIT(CommandWho)