]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_who.cpp
Replace hardcoded mode letters, part 2
[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
45         Channel* get_first_visible_channel(User *u)
46         {
47                 UCListIter i = u->chans.begin();
48                 while (i != u->chans.end())
49                 {
50                         Channel* c = *i++;
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         {
65                 syntax = "<server>|<nickname>|<channel>|<realname>|<host>|0 [ohurmMiaplf]";
66         }
67
68         void SendWhoLine(User* user, const std::vector<std::string>& parms, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults);
69         /** Handle command.
70          * @param parameters The parameters to the comamnd
71          * @param pcnt The number of parameters passed to teh command
72          * @param user The user issuing the command
73          * @return A value from CmdResult to indicate command success or failure.
74          */
75         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
76         bool whomatch(User* cuser, User* user, const char* matchtext);
77 };
78
79 bool CommandWho::whomatch(User* cuser, User* user, const char* matchtext)
80 {
81         bool match = false;
82         bool positive = false;
83
84         if (user->registered != REG_ALL)
85                 return false;
86
87         if (opt_local && !IS_LOCAL(user))
88                 return false;
89         else if (opt_far && IS_LOCAL(user))
90                 return false;
91
92         if (opt_mode)
93         {
94                 for (const char* n = matchtext; *n; n++)
95                 {
96                         if (*n == '+')
97                         {
98                                 positive = true;
99                                 continue;
100                         }
101                         else if (*n == '-')
102                         {
103                                 positive = false;
104                                 continue;
105                         }
106                         if (user->IsModeSet(*n) != positive)
107                                 return false;
108                 }
109                 return true;
110         }
111         else
112         {
113                 /*
114                  * This was previously one awesome pile of ugly nested if, when really, it didn't need
115                  * to be, since only one condition was ever checked, a chained if works just fine.
116                  * -- w00t
117                  */
118                 if (opt_metadata)
119                 {
120                         match = false;
121                         const Extensible::ExtensibleStore& list = user->GetExtList();
122                         for(Extensible::ExtensibleStore::const_iterator i = list.begin(); i != list.end(); ++i)
123                                 if (InspIRCd::Match(i->first->name, matchtext))
124                                         match = true;
125                 }
126                 else if (opt_realname)
127                         match = InspIRCd::Match(user->fullname, matchtext);
128                 else if (opt_showrealhost)
129                         match = InspIRCd::Match(user->host, matchtext, ascii_case_insensitive_map);
130                 else if (opt_ident)
131                         match = InspIRCd::Match(user->ident, matchtext, ascii_case_insensitive_map);
132                 else if (opt_port)
133                 {
134                         irc::portparser portrange(matchtext, false);
135                         long portno = -1;
136                         while ((portno = portrange.GetToken()))
137                                 if (IS_LOCAL(user) && portno == IS_LOCAL(user)->GetServerPort())
138                                 {
139                                         match = true;
140                                         break;
141                                 }
142                 }
143                 else if (opt_away)
144                         match = InspIRCd::Match(user->awaymsg, matchtext);
145                 else if (opt_time)
146                 {
147                         long seconds = InspIRCd::Duration(matchtext);
148
149                         // Okay, so time matching, we want all users connected `seconds' ago
150                         if (user->age >= ServerInstance->Time() - seconds)
151                                 match = true;
152                 }
153
154                 /*
155                  * Once the conditionals have been checked, only check dhost/nick/server
156                  * if they didn't match this user -- and only match if we don't find a match.
157                  *
158                  * This should make things minutely faster, and again, less ugly.
159                  * -- w00t
160                  */
161                 if (!match)
162                         match = InspIRCd::Match(user->dhost, matchtext, ascii_case_insensitive_map);
163
164                 if (!match)
165                         match = InspIRCd::Match(user->nick, matchtext);
166
167                 /* Don't allow server name matches if HideWhoisServer is enabled, unless the command user has the priv */
168                 if (!match && (ServerInstance->Config->HideWhoisServer.empty() || cuser->HasPrivPermission("users/auspex")))
169                         match = InspIRCd::Match(user->server, matchtext);
170
171                 return match;
172         }
173 }
174
175 bool CommandWho::CanView(Channel* chan, User* user)
176 {
177         if (!user || !chan)
178                 return false;
179
180         /* Bug #383 - moved higher up the list, because if we are in the channel
181          * we can see all its users
182          */
183         if (chan->HasUser(user))
184                 return true;
185         /* Opers see all */
186         if (user->HasPrivPermission("users/auspex"))
187                 return true;
188         /* Cant see inside a +s or a +p channel unless we are a member (see above) */
189         else if (!chan->IsModeSet(secretmode) && !chan->IsModeSet(privatemode))
190                 return true;
191
192         return false;
193 }
194
195 void CommandWho::SendWhoLine(User* user, const std::vector<std::string>& parms, const std::string &initial, Channel* ch, User* u, std::vector<std::string> &whoresults)
196 {
197         if (!ch)
198                 ch = get_first_visible_channel(u);
199
200         std::string wholine = initial + (ch ? ch->name : "*") + " " + u->ident + " " +
201                 (opt_showrealhost ? u->host : u->dhost) + " ";
202         if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
203                 wholine.append(ServerInstance->Config->HideWhoisServer);
204         else
205                 wholine.append(u->server);
206
207         wholine.append(" " + u->nick + " ");
208
209         /* away? */
210         if (u->IsAway())
211         {
212                 wholine.append("G");
213         }
214         else
215         {
216                 wholine.append("H");
217         }
218
219         /* oper? */
220         if (u->IsOper())
221         {
222                 wholine.push_back('*');
223         }
224
225         if (ch)
226                 wholine.append(ch->GetPrefixChar(u));
227
228         wholine.append(" :0 " + u->fullname);
229
230         FOREACH_MOD(I_OnSendWhoLine, OnSendWhoLine(user, parms, u, wholine));
231
232         if (!wholine.empty())
233                 whoresults.push_back(wholine);
234 }
235
236 CmdResult CommandWho::Handle (const std::vector<std::string>& parameters, User *user)
237 {
238         /*
239          * XXX - RFC says:
240          *   The <name> passed to WHO is matched against users' host, server, real
241          *   name and nickname
242          * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
243          */
244
245         /* WHO options */
246         opt_viewopersonly = false;
247         opt_showrealhost = false;
248         opt_realname = false;
249         opt_mode = false;
250         opt_ident = false;
251         opt_metadata = false;
252         opt_port = false;
253         opt_away = false;
254         opt_local = false;
255         opt_far = false;
256         opt_time = false;
257
258         std::vector<std::string> whoresults;
259         std::string initial = "352 " + user->nick + " ";
260
261         /* Change '0' into '*' so the wildcard matcher can grok it */
262         std::string matchtext = ((parameters[0] == "0") ? "*" : parameters[0]);
263
264         // WHO flags count as a wildcard
265         bool usingwildcards = ((parameters.size() > 1) || (matchtext.find_first_of("*?.") != std::string::npos));
266
267         if (parameters.size() > 1)
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         Channel* 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 && !i->first->IsOper())
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, parameters, 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.c_str()))
358                                 {
359                                         if (!user->SharesChannelWith(oper))
360                                         {
361                                                 if (usingwildcards && (!oper->IsModeSet('i')) && (!user->HasPrivPermission("users/auspex")))
362                                                         continue;
363                                         }
364
365                                         SendWhoLine(user, parameters, 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.c_str()))
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, parameters, 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)