]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_who.cpp
2b3242211e70fea664cbdeb74db06262837b634c
[user/henk/code/inspircd.git] / src / commands / cmd_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. These command handlers can be reloaded by the core,
24  * and handle basic RFC1459 commands. Commands within modules work
25  * the same way, however, they can be fully unloaded, where these
26  * may not.
27  */
28 class CommandWho : public Command
29 {
30         bool CanView(Channel* chan, User* user);
31         bool opt_viewopersonly;
32         bool opt_showrealhost;
33         bool opt_realname;
34         bool opt_mode;
35         bool opt_ident;
36         bool opt_metadata;
37         bool opt_port;
38         bool opt_away;
39         bool opt_local;
40         bool opt_far;
41         bool opt_time;
42
43  public:
44         /** Constructor for who.
45          */
46         CommandWho ( Module* parent) : Command(parent,"WHO", 1) {
47                 syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [ohurmMiaplf]";
48         }
49         void SendWhoLine(User* user, const std::vector<std::string>& parms, 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
61 static Channel* get_first_visible_channel(User *u)
62 {
63         UCListIter i = u->chans.begin();
64         while (i != u->chans.end())
65         {
66                 Channel* c = *i++;
67                 if (!c->IsModeSet('s'))
68                         return c;
69         }
70         return NULL;
71 }
72
73 bool CommandWho::whomatch(User* cuser, User* user, const char* matchtext)
74 {
75         bool match = false;
76         bool positive = false;
77
78         if (user->registered != REG_ALL)
79                 return false;
80
81         if (opt_local && !IS_LOCAL(user))
82                 return false;
83         else if (opt_far && IS_LOCAL(user))
84                 return false;
85
86         if (opt_mode)
87         {
88                 for (const char* n = matchtext; *n; n++)
89                 {
90                         if (*n == '+')
91                         {
92                                 positive = true;
93                                 continue;
94                         }
95                         else if (*n == '-')
96                         {
97                                 positive = false;
98                                 continue;
99                         }
100                         if (user->IsModeSet(*n) != positive)
101                                 return false;
102                 }
103                 return true;
104         }
105         else
106         {
107                 /*
108                  * This was previously one awesome pile of ugly nested if, when really, it didn't need
109                  * to be, since only one condition was ever checked, a chained if works just fine.
110                  * -- w00t
111                  */
112                 if (opt_metadata)
113                 {
114                         match = false;
115                         const Extensible::ExtensibleStore& list = user->GetExtList();
116                         for(Extensible::ExtensibleStore::const_iterator i = list.begin(); i != list.end(); ++i)
117                                 if (InspIRCd::Match(i->first->name, matchtext))
118                                         match = true;
119                 }
120                 else if (opt_realname)
121                         match = InspIRCd::Match(user->fullname, matchtext);
122                 else if (opt_showrealhost)
123                         match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
124                 else if (opt_ident)
125                         match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
126                 else if (opt_port)
127                 {
128                         irc::portparser portrange(matchtext, false);
129                         long portno = -1;
130                         while ((portno = portrange.GetToken()))
131                                 if (IS_LOCAL(user) && portno == IS_LOCAL(user)->GetServerPort())
132                                 {
133                                         match = true;
134                                         break;
135                                 }
136                 }
137                 else if (opt_away)
138                         match = InspIRCd::Match(user->awaymsg, matchtext);
139                 else if (opt_time)
140                 {
141                         long seconds = ServerInstance->Duration(matchtext);
142
143                         // Okay, so time matching, we want all users connected `seconds' ago
144                         if (user->age >= ServerInstance->Time() - seconds)
145                                 match = true;
146                 }
147
148                 /*
149                  * Once the conditionals have been checked, only check dhost/nick/server
150                  * if they didn't match this user -- and only match if we don't find a match.
151                  *
152                  * This should make things minutely faster, and again, less ugly.
153                  * -- w00t
154                  */
155                 if (!match)
156                         match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
157
158                 if (!match)
159                         match = InspIRCd::Match(user->nick, matchtext);
160
161                 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
162                 if (!match && (ServerInstance->Config->HideWhoisServer.empty() || cuser->HasPrivPermission("users/auspex")))
163                         match = InspIRCd::Match(user->server, matchtext);
164
165                 return match;
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::vector<std::string>& parms, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
190 {
191         if (!ch)
192                 ch = get_first_visible_channel(u);
193
194         std::string wholine = initial + (ch ? ch->name : "*") + " " + u->ident + " " +
195                 (opt_showrealhost ? u->host : u->dhost) + " ";
196         if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
197                 wholine.append(ServerInstance->Config->HideWhoisServer);
198         else
199                 wholine.append(u->server);
200         
201         wholine.append(" " + u->nick + " ");
202
203         /* away? */
204         if (IS_AWAY(u))
205         {
206                 wholine.append("G");
207         }
208         else
209         {
210                 wholine.append("H");
211         }
212
213         /* oper? */
214         if (IS_OPER(u))
215         {
216                 wholine.push_back('*');
217         }
218
219         if (ch)
220                 wholine.append(ch->GetPrefixChar(u));
221
222         wholine.append(" :0 " + u->fullname);
223
224         FOREACH_MOD(I_OnSendWhoLine, OnSendWhoLine(user, parms, u, wholine));
225
226         if (!wholine.empty())
227                 whoresults.push_back(wholine);
228 }
229
230 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
231 {
232         /*
233          * XXX - RFC says:
234          *   The <name> passed to WHO is matched against users' host, server, real
235          *   name and nickname
236          * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
237          */
238
239         /* WHO options */
240         opt_viewopersonly = false;
241         opt_showrealhost = false;
242         opt_realname = false;
243         opt_mode = false;
244         opt_ident = false;
245         opt_metadata = false;
246         opt_port = false;
247         opt_away = false;
248         opt_local = false;
249         opt_far = false;
250         opt_time = false;
251
252         Channel *ch = NULL;
253         std::vector<std::string> whoresults;
254         std::string initial = "352 " + user->nick + " ";
255
256         char matchtext[MAXBUF];
257         bool usingwildcards = false;
258
259         /* Change '0' into '*' so the wildcard matcher can grok it */
260         if (parameters[0] == "0")
261                 strlcpy(matchtext, "*", MAXBUF);
262         else
263                 strlcpy(matchtext, parameters[0].c_str(), MAXBUF);
264
265         for (const char* check = matchtext; *check; check++)
266         {
267                 if (*check == '*' || *check == '?' || *check == '.')
268                 {
269                         usingwildcards = true;
270                         break;
271                 }
272         }
273
274         if (parameters.size() > 1)
275         {
276                 /* Fix for bug #444, WHO flags count as a wildcard */
277                 usingwildcards = true;
278
279                 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
280                 {
281                         switch (*iter)
282                         {
283                                 case 'o':
284                                         opt_viewopersonly = true;
285                                         break;
286                                 case 'h':
287                                         if (user->HasPrivPermission("users/auspex"))
288                                                 opt_showrealhost = 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.empty())
313                                                 opt_local = true;
314                                         break;
315                                 case 'f':
316                                         if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
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                         const UserMembList *cu = ch->GetUsers();
338
339                         for (UserMembCIter 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, parameters, 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, parameters, 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, parameters, initial, NULL, i->second, whoresults);
392                                 }
393                         }
394                 }
395         }
396         /* Send the results out */
397         for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
398                 user->WriteServ(*n);
399         user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
400
401         // Penalize the user a bit for large queries
402         // (add one unit of penalty per 200 results)
403         if (IS_LOCAL(user))
404                 IS_LOCAL(user)->CommandFloodPenalty += whoresults.size() * 5;
405         return CMD_SUCCESS;
406 }
407
408 COMMAND_INIT(CommandWho)