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