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