]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_who.cpp
Delink servers using an SSL module that is being reloaded
[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 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::vector<std::string>& parms, 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
191         std::string wholine = initial + (ch ? ch->name : lcn) + " " + u->ident + " " + 
192                 (opt_showrealhost ? u->host : u->dhost) + " ";
193         if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
194                 wholine.append(ServerInstance->Config->HideWhoisServer);
195         else
196                 wholine.append(u->server);
197         
198         wholine.append(" " + u->nick + " ");
199
200         /* away? */
201         if (IS_AWAY(u))
202         {
203                 wholine.append("G");
204         }
205         else
206         {
207                 wholine.append("H");
208         }
209
210         /* oper? */
211         if (IS_OPER(u))
212         {
213                 wholine.push_back('*');
214         }
215
216         if (ch)
217                 wholine.append(ch->GetPrefixChar(u));
218         else
219         {
220                 Channel* lch = ServerInstance->FindChan(lcn);
221                 if (lch)
222                         wholine.append(lch->GetPrefixChar(u));
223         }
224
225         wholine.append(" :0 " + u->fullname);
226
227         FOREACH_MOD(I_OnSendWhoLine, OnSendWhoLine(user, parms, u, ch, wholine));
228
229         if (!wholine.empty())
230                 whoresults.push_back(wholine);
231 }
232
233 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
234 {
235         /*
236          * XXX - RFC says:
237          *   The <name> passed to WHO is matched against users' host, server, real
238          *   name and nickname
239          * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
240          */
241
242         /* WHO options */
243         opt_viewopersonly = false;
244         opt_showrealhost = false;
245         opt_realname = false;
246         opt_mode = false;
247         opt_ident = false;
248         opt_metadata = false;
249         opt_port = false;
250         opt_away = false;
251         opt_local = false;
252         opt_far = false;
253         opt_time = false;
254
255         Channel *ch = NULL;
256         std::vector<std::string> whoresults;
257         std::string initial = "352 " + std::string(user->nick) + " ";
258
259         char matchtext[MAXBUF];
260         bool usingwildcards = false;
261
262         /* Change '0' into '*' so the wildcard matcher can grok it */
263         if (parameters[0] == "0")
264                 strlcpy(matchtext, "*", MAXBUF);
265         else
266                 strlcpy(matchtext, parameters[0].c_str(), MAXBUF);
267
268         for (const char* check = matchtext; *check; check++)
269         {
270                 if (*check == '*' || *check == '?')
271                 {
272                         usingwildcards = true;
273                         break;
274                 }
275         }
276
277         if (parameters.size() > 1)
278         {
279                 /* Fix for bug #444, WHO flags count as a wildcard */
280                 usingwildcards = true;
281
282                 for (std::string::const_iterator iter = parameters[1].begin(); iter != parameters[1].end(); ++iter)
283                 {
284                         switch (*iter)
285                         {
286                                 case 'o':
287                                         opt_viewopersonly = true;
288                                         break;
289                                 case 'h':
290                                         if (user->HasPrivPermission("users/auspex"))
291                                                 opt_showrealhost = true;
292                                         break;
293                                 case 'r':
294                                         opt_realname = true;
295                                         break;
296                                 case 'm':
297                                         if (user->HasPrivPermission("users/auspex"))
298                                                 opt_mode = true;
299                                         break;
300                                 case 'M':
301                                         if (user->HasPrivPermission("users/auspex"))
302                                                 opt_metadata = true;
303                                         break;
304                                 case 'i':
305                                         opt_ident = true;
306                                         break;
307                                 case 'p':
308                                         if (user->HasPrivPermission("users/auspex"))
309                                                 opt_port = true;
310                                         break;
311                                 case 'a':
312                                         opt_away = true;
313                                         break;
314                                 case 'l':
315                                         if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
316                                                 opt_local = true;
317                                         break;
318                                 case 'f':
319                                         if (user->HasPrivPermission("users/auspex") || ServerInstance->Config->HideWhoisServer.empty())
320                                                 opt_far = true;
321                                         break;
322                                 case 't':
323                                         opt_time = true;
324                                         break;
325                         }
326                 }
327         }
328
329
330         /* who on a channel? */
331         ch = ServerInstance->FindChan(matchtext);
332
333         if (ch)
334         {
335                 if (CanView(ch,user))
336                 {
337                         bool inside = ch->HasUser(user);
338
339                         /* who on a channel. */
340                         const UserMembList *cu = ch->GetUsers();
341
342                         for (UserMembCIter i = cu->begin(); i != cu->end(); i++)
343                         {
344                                 /* None of this applies if we WHO ourselves */
345                                 if (user != i->first)
346                                 {
347                                         /* opers only, please */
348                                         if (opt_viewopersonly && !IS_OPER(i->first))
349                                                 continue;
350
351                                         /* If we're not inside the channel, hide +i users */
352                                         if (i->first->IsModeSet('i') && !inside && !user->HasPrivPermission("users/auspex"))
353                                                 continue;
354                                 }
355
356                                 SendWhoLine(user, parameters, initial, ch, i->first, whoresults);
357                         }
358                 }
359         }
360         else
361         {
362                 /* Match against wildcard of nick, server or host */
363                 if (opt_viewopersonly)
364                 {
365                         /* Showing only opers */
366                         for (std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); i++)
367                         {
368                                 User* oper = *i;
369
370                                 if (whomatch(user, oper, matchtext))
371                                 {
372                                         if (!user->SharesChannelWith(oper))
373                                         {
374                                                 if (usingwildcards && (!oper->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
375                                                         continue;
376                                         }
377
378                                         SendWhoLine(user, parameters, initial, NULL, oper, whoresults);
379                                 }
380                         }
381                 }
382                 else
383                 {
384                         for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
385                         {
386                                 if (whomatch(user, i->second, matchtext))
387                                 {
388                                         if (!user->SharesChannelWith(i->second))
389                                         {
390                                                 if (usingwildcards && (i->second->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
391                                                         continue;
392                                         }
393
394                                         SendWhoLine(user, parameters, initial, NULL, i->second, whoresults);
395                                 }
396                         }
397                 }
398         }
399         /* Send the results out */
400         for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
401                 user->WriteServ(*n);
402         user->WriteNumeric(315, "%s %s :End of /WHO list.",user->nick.c_str(), *parameters[0].c_str() ? parameters[0].c_str() : "*");
403
404         // Penalize the user a bit for large queries
405         // (add one unit of penalty per 200 results)
406         if (IS_LOCAL(user))
407                 IS_LOCAL(user)->CommandFloodPenalty += whoresults.size() * 5;
408         return CMD_SUCCESS;
409 }
410
411 COMMAND_INIT(CommandWho)