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