]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_who.cpp
Allow connect classes to be specified without an allow or deny mask
[user/henk/code/inspircd.git] / src / commands / cmd_who.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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_realname;
27         bool opt_mode;
28         bool opt_ident;
29         bool opt_metadata;
30         bool opt_port;
31         bool opt_away;
32         bool opt_local;
33         bool opt_far;
34         bool opt_time;
35
36  public:
37         /** Constructor for who.
38          */
39         CommandWho ( Module* parent) : Command(parent,"WHO", 1) {
40                 syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [ohurmMiaplf]";
41         }
42         void SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults);
43         /** Handle command.
44          * @param parameters The parameters to the comamnd
45          * @param pcnt The number of parameters passed to teh command
46          * @param user The user issuing the command
47          * @return A value from CmdResult to indicate command success or failure.
48          */
49         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
50         bool whomatch(User* cuser, User* user, const char* matchtext);
51 };
52
53
54 static const std::string star = "*";
55
56 static const std::string& get_first_visible_channel(User *u)
57 {
58         UCListIter i = u->chans.begin();
59         while (i != u->chans.end())
60         {
61                 Channel* c = *i++;
62                 if (!c->IsModeSet('s'))
63                         return c->name;
64         }
65
66         return star;
67 }
68
69 bool CommandWho::whomatch(User* cuser, User* user, const char* matchtext)
70 {
71         bool match = false;
72         bool positive = false;
73
74         if (user->registered != REG_ALL)
75                 return false;
76
77         if (opt_local && !IS_LOCAL(user))
78                 return false;
79         else if (opt_far && IS_LOCAL(user))
80                 return false;
81
82         if (opt_mode)
83         {
84                 for (const char* n = matchtext; *n; n++)
85                 {
86                         if (*n == '+')
87                         {
88                                 positive = true;
89                                 continue;
90                         }
91                         else if (*n == '-')
92                         {
93                                 positive = false;
94                                 continue;
95                         }
96                         if (user->IsModeSet(*n) != positive)
97                                 return false;
98                 }
99                 return true;
100         }
101         else
102         {
103                 /*
104                  * This was previously one awesome pile of ugly nested if, when really, it didn't need
105                  * to be, since only one condition was ever checked, a chained if works just fine.
106                  * -- w00t
107                  */
108                 if (opt_metadata)
109                 {
110                         match = false;
111                         const Extensible::ExtensibleStore& list = user->GetExtList();
112                         for(Extensible::ExtensibleStore::const_iterator i = list.begin(); i != list.end(); ++i)
113                                 if (InspIRCd::Match(i->first->name, matchtext))
114                                         match = true;
115                 }
116                 else if (opt_realname)
117                         match = InspIRCd::Match(user->fullname, matchtext);
118                 else if (opt_showrealhost)
119                         match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
120                 else if (opt_ident)
121                         match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
122                 else if (opt_port)
123                 {
124                         irc::portparser portrange(matchtext, false);
125                         long portno = -1;
126                         while ((portno = portrange.GetToken()))
127                                 if (IS_LOCAL(user) && portno == IS_LOCAL(user)->GetServerPort())
128                                 {
129                                         match = true;
130                                         break;
131                                 }
132                 }
133                 else if (opt_away)
134                         match = InspIRCd::Match(user->awaymsg, matchtext);
135                 else if (opt_time)
136                 {
137                         long seconds = ServerInstance->Duration(matchtext);
138
139                         // Okay, so time matching, we want all users connected `seconds' ago
140                         if (user->age >= ServerInstance->Time() - seconds)
141                                 match = true;
142                 }
143
144                 /*
145                  * Once the conditionals have been checked, only check dhost/nick/server
146                  * if they didn't match this user -- and only match if we don't find a match.
147                  *
148                  * This should make things minutely faster, and again, less ugly.
149                  * -- w00t
150                  */
151                 if (!match)
152                         match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
153
154                 if (!match)
155                         match = InspIRCd::Match(user->nick, matchtext);
156
157                 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
158                 if (!match && (ServerInstance->Config->HideWhoisServer.empty() || cuser->HasPrivPermission("users/auspex")))
159                         match = InspIRCd::Match(user->server, matchtext);
160
161                 return match;
162         }
163 }
164
165
166
167 bool CommandWho::CanView(Channel* chan, User* user)
168 {
169         if (!user || !chan)
170                 return false;
171
172         /* Bug #383 - moved higher up the list, because if we are in the channel
173          * we can see all its users
174          */
175         if (chan->HasUser(user))
176                 return true;
177         /* Opers see all */
178         if (user->HasPrivPermission("users/auspex"))
179                 return true;
180         /* Cant see inside a +s or a +p channel unless we are a member (see above) */
181         else if (!chan->IsModeSet('s') && !chan->IsModeSet('p'))
182                 return true;
183
184         return false;
185 }
186
187 void CommandWho::SendWhoLine(User* user, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
188 {
189         const std::string& lcn = get_first_visible_channel(u);
190         Channel* chlast = ServerInstance->FindChan(lcn);
191
192         std::string wholine =   initial + (ch ? ch->name : lcn) + " " + u->ident + " " + (opt_showrealhost ? u->host : u->dhost) + " " +
193                                 ((!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex")) ? ServerInstance->Config->HideWhoisServer : u->server) +
194                                 " " + u->nick + " ";
195
196         /* away? */
197         if (IS_AWAY(u))
198         {
199                 wholine.append("G");
200         }
201         else
202         {
203                 wholine.append("H");
204         }
205
206         /* oper? */
207         if (IS_OPER(u))
208         {
209                 wholine.append("*");
210         }
211
212         wholine = wholine + (ch ? ch->GetPrefixChar(u) : (chlast ? chlast->GetPrefixChar(u) : "")) + " :0 " + u->fullname;
213
214         FOREACH_MOD(I_OnSendWhoLine, OnSendWhoLine(user, u, ch, wholine));
215
216         if (!wholine.empty())
217                 whoresults.push_back(wholine);
218 }
219
220 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
221 {
222         /*
223          * XXX - RFC says:
224          *   The <name> passed to WHO is matched against users' host, server, real
225          *   name and nickname
226          * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
227          */
228
229         /* WHO options */
230         opt_viewopersonly = false;
231         opt_showrealhost = 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 'r':
281                                         opt_realname = true;
282                                         break;
283                                 case 'm':
284                                         if (user->HasPrivPermission("users/auspex"))
285                                                 opt_mode = true;
286                                         break;
287                                 case 'M':
288                                         if (user->HasPrivPermission("users/auspex"))
289                                                 opt_metadata = true;
290                                         break;
291                                 case 'i':
292                                         opt_ident = true;
293                                         break;
294                                 case 'p':
295                                         if (user->HasPrivPermission("users/auspex"))
296                                                 opt_port = true;
297                                         break;
298                                 case 'a':
299                                         opt_away = true;
300                                         break;
301                                 case 'l':
302                                         if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
303                                                 opt_local = true;
304                                         break;
305                                 case 'f':
306                                         if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
307                                                 opt_far = true;
308                                         break;
309                                 case 't':
310                                         opt_time = true;
311                                         break;
312                         }
313                 }
314         }
315
316
317         /* who on a channel? */
318         ch = ServerInstance->FindChan(matchtext);
319
320         if (ch)
321         {
322                 if (CanView(ch,user))
323                 {
324                         bool inside = ch->HasUser(user);
325
326                         /* who on a channel. */
327                         const UserMembList *cu = ch->GetUsers();
328
329                         for (UserMembCIter i = cu->begin(); i != cu->end(); i++)
330                         {
331                                 /* None of this applies if we WHO ourselves */
332                                 if (user != i->first)
333                                 {
334                                         /* opers only, please */
335                                         if (opt_viewopersonly && !IS_OPER(i->first))
336                                                 continue;
337
338                                         /* If we're not inside the channel, hide +i users */
339                                         if (i->first->IsModeSet('i') && !inside && !user->HasPrivPermission("users/auspex"))
340                                                 continue;
341                                 }
342
343                                 SendWhoLine(user, initial, ch, i->first, whoresults);
344                         }
345                 }
346         }
347         else
348         {
349                 /* Match against wildcard of nick, server or host */
350                 if (opt_viewopersonly)
351                 {
352                         /* Showing only opers */
353                         for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
354                         {
355                                 User* oper = *i;
356
357                                 if (whomatch(user, oper, matchtext))
358                                 {
359                                         if (!user->SharesChannelWith(oper))
360                                         {
361                                                 if (usingwildcards && (!oper->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
362                                                         continue;
363                                         }
364
365                                         SendWhoLine(user, initial, NULL, oper, whoresults);
366                                 }
367                         }
368                 }
369                 else
370                 {
371                         for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
372                         {
373                                 if (whomatch(user, i->second, matchtext))
374                                 {
375                                         if (!user->SharesChannelWith(i->second))
376                                         {
377                                                 if (usingwildcards && (i->second->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
378                                                         continue;
379                                         }
380
381                                         SendWhoLine(user, initial, NULL, i->second, whoresults);
382                                 }
383                         }
384                 }
385         }
386         /* Send the results out */
387         for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
388                 user->WriteServ(*n);
389         user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
390
391         // Penalize the user a bit for large queries
392         // (add one unit of penalty per 200 results)
393         if (IS_LOCAL(user))
394                 IS_LOCAL(user)->CommandFloodPenalty += whoresults.size() * 5;
395         return CMD_SUCCESS;
396 }
397
398 COMMAND_INIT(CommandWho)