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