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