]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_who.cpp
Change Extensible to use strongly typed entries
[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
82         if (user->registered != REG_ALL)
83                 return false;
84
85         if (opt_local && !IS_LOCAL(user))
86                 return false;
87         else if (opt_far && IS_LOCAL(user))
88                 return false;
89
90         if (opt_mode)
91         {
92                 for (const char* n = matchtext; *n; n++)
93                 {
94                         if (*n == '+')
95                         {
96                                 positive = true;
97                                 continue;
98                         }
99                         else if (*n == '-')
100                         {
101                                 positive = false;
102                                 continue;
103                         }
104                         if (user->IsModeSet(*n) != positive)
105                                 return false;
106                 }
107                 return true;
108         }
109         else
110         {
111                 /*
112                  * This was previously one awesome pile of ugly nested if, when really, it didn't need
113                  * to be, since only one condition was ever checked, a chained if works just fine.
114                  * -- w00t
115                  */
116                 if (opt_metadata)
117                         match = user->GetExtList().find(matchtext) != user->GetExtList().end();
118                 else if (opt_realname)
119                         match = InspIRCd::Match(user->fullname, matchtext);
120                 else if (opt_showrealhost)
121                         match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
122                 else if (opt_ident)
123                         match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
124                 else if (opt_port)
125                 {
126                         irc::portparser portrange(matchtext, false);
127                         long portno = -1;
128                         while ((portno = portrange.GetToken()))
129                                 if (portno == user->GetServerPort())
130                                 {
131                                         match = true;
132                                         break;
133                                 }
134                 }
135                 else if (opt_away)
136                         match = InspIRCd::Match(user->awaymsg, matchtext);
137                 else if (opt_time)
138                 {
139                         long seconds = ServerInstance->Duration(matchtext);
140
141                         // Okay, so time matching, we want all users connected `seconds' ago
142                         if (user->age >= ServerInstance->Time() - seconds)
143                                 match = true;
144                 }
145
146                 /*
147                  * Once the conditionals have been checked, only check dhost/nick/server
148                  * if they didn't match this user -- and only match if we don't find a match.
149                  *
150                  * This should make things minutely faster, and again, less ugly.
151                  * -- w00t
152                  */
153                 if (!match)
154                         match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
155
156                 if (!match)
157                         match = InspIRCd::Match(user->nick, matchtext);
158
159                 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
160                 if (!match && (!*ServerInstance->Config->HideWhoisServer || cuser->HasPrivPermission("users/auspex")))
161                         match = InspIRCd::Match(user->server, matchtext);
162
163                 return match;
164         }
165 }
166
167
168
169 bool CommandWho::CanView(Channel* chan, User* user)
170 {
171         if (!user || !chan)
172                 return false;
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('s') && !chan->IsModeSet('p'))
184                 return true;
185
186         return false;
187 }
188
189 void CommandWho::SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
190 {
191         const std::string& lcn = get_first_visible_channel(u);
192         Channel* chlast = ServerInstance->FindChan(lcn);
193
194         std::string wholine =   initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
195                                 ((*ServerInstance->Config->HideWhoisServer && !user->HasPrivPermission("servers/auspex")) ? ServerInstance->Config->HideWhoisServer : u->server) +
196                                 " " + u->nick + " ";
197
198         /* away? */
199         if (IS_AWAY(u))
200         {
201                 wholine.append("G");
202         }
203         else
204         {
205                 wholine.append("H");
206         }
207
208         /* oper? */
209         if (IS_OPER(u))
210         {
211                 wholine.append("*");
212         }
213
214         wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
215
216         FOREACH_MOD(I_OnSendWhoLine, OnSendWhoLine(user, u, ch, wholine));
217
218         if (!wholine.empty())
219                 whoresults.push_back(wholine);
220 }
221
222 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
223 {
224         /*
225          * XXX - RFC says:
226          *   The <name> passed to WHO is matched against users' host, server, real
227          *   name and nickname
228          * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
229          */
230
231         /* WHO options */
232         opt_viewopersonly = false;
233         opt_showrealhost = false;
234         opt_unlimit = false;
235         opt_realname = false;
236         opt_mode = false;
237         opt_ident = false;
238         opt_metadata = false;
239         opt_port = false;
240         opt_away = false;
241         opt_local = false;
242         opt_far = false;
243         opt_time = false;
244
245         Channel *ch = NULL;
246         std::vector<std::string> whoresults;
247         std::string initial = "352 " + std::string(user->nick) + " ";
248
249         char matchtext[MAXBUF];
250         bool usingwildcards = false;
251
252         /* Change '0' into '*' so the wildcard matcher can grok it */
253         if (parameters[0] == "0")
254                 strlcpy(matchtext, "*", MAXBUF);
255         else
256                 strlcpy(matchtext, parameters[0].c_str(), MAXBUF);
257
258         for (const char* check = matchtext; *check; check++)
259         {
260                 if (*check == '*' || *check == '?')
261                 {
262                         usingwildcards = true;
263                         break;
264                 }
265         }
266
267         if (ServerInstance->FindServerName(matchtext))
268                 usingwildcards = true;
269
270         if (parameters.size() > 1)
271         {
272                 /* Fix for bug #444, WHO flags count as a wildcard */
273                 usingwildcards = true;
274
275                 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
276                 {
277                         switch (*iter)
278                         {
279                                 case 'o':
280                                         opt_viewopersonly = true;
281                                         break;
282                                 case 'h':
283                                         if (user->HasPrivPermission("users/auspex"))
284                                                 opt_showrealhost = true;
285                                         break;
286                                 case 'u':
287                                         if (user->HasPrivPermission("users/auspex"))
288                                                 opt_unlimit = true;
289                                         break;
290                                 case 'r':
291                                         opt_realname = true;
292                                         break;
293                                 case 'm':
294                                         if (user->HasPrivPermission("users/auspex"))
295                                                 opt_mode = true;
296                                         break;
297                                 case 'M':
298                                         if (user->HasPrivPermission("users/auspex"))
299                                                 opt_metadata = true;
300                                         break;
301                                 case 'i':
302                                         opt_ident = true;
303                                         break;
304                                 case 'p':
305                                         if (user->HasPrivPermission("users/auspex"))
306                                                 opt_port = true;
307                                         break;
308                                 case 'a':
309                                         opt_away = true;
310                                         break;
311                                 case 'l':
312                                         if (user->HasPrivPermission("users/auspex") || !*ServerInstance->Config->HideWhoisServer)
313                                                 opt_local = true;
314                                         break;
315                                 case 'f':
316                                         if (user->HasPrivPermission("users/auspex") || !*ServerInstance->Config->HideWhoisServer)
317                                                 opt_far = true;
318                                         break;
319                                 case 't':
320                                         opt_time = true;
321                                         break;
322                         }
323                 }
324         }
325
326
327         /* who on a channel? */
328         ch = ServerInstance->FindChan(matchtext);
329
330         if (ch)
331         {
332                 if (CanView(ch,user))
333                 {
334                         bool inside = ch->HasUser(user);
335
336                         /* who on a channel. */
337                         CUList *cu = ch->GetUsers();
338
339                         for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
340                         {
341                                 /* None of this applies if we WHO ourselves */
342                                 if (user != i->first)
343                                 {
344                                         /* opers only, please */
345                                         if (opt_viewopersonly && !IS_OPER(i->first))
346                                                 continue;
347
348                                         /* If we're not inside the channel, hide +i users */
349                                         if (i->first->IsModeSet('i') && !inside && !user->HasPrivPermission("users/auspex"))
350                                                 continue;
351                                 }
352
353                                 SendWhoLine(user, initial, ch, i->first, whoresults);
354                         }
355                 }
356         }
357         else
358         {
359                 /* Match against wildcard of nick, server or host */
360                 if (opt_viewopersonly)
361                 {
362                         /* Showing only opers */
363                         for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
364                         {
365                                 User* oper = *i;
366
367                                 if (whomatch(user, oper, matchtext))
368                                 {
369                                         if (!user->SharesChannelWith(oper))
370                                         {
371                                                 if (usingwildcards && (!oper->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
372                                                         continue;
373                                         }
374
375                                         SendWhoLine(user, initial, NULL, oper, whoresults);
376                                 }
377                         }
378                 }
379                 else
380                 {
381                         for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
382                         {
383                                 if (whomatch(user, i->second, matchtext))
384                                 {
385                                         if (!user->SharesChannelWith(i->second))
386                                         {
387                                                 if (usingwildcards && (i->second->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
388                                                         continue;
389                                         }
390
391                                         SendWhoLine(user, initial, NULL, i->second, whoresults);
392                                 }
393                         }
394                 }
395         }
396         /* Send the results out */
397         if ((ServerInstance->Config->MaxWhoResults && (whoresults.size() <= (size_t)ServerInstance->Config->MaxWhoResults)) || opt_unlimit)
398         {
399                 for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
400                         user->WriteServ(*n);
401                 user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
402                 return CMD_SUCCESS;
403         }
404         else
405         {
406                 /* BZZT! Too many results. */
407                 user->WriteNumeric(315, "%s %s :Too many results",user->nick.c_str(), parameters[0].c_str());
408                 return CMD_FAILURE;
409         }
410 }
411
412 COMMAND_INIT(CommandWho)