01ee78e4371b862ec73c73294b09799590a7de0e
[ircu2.10.12-pk.git] / ircd / m_whois.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_whois.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 1, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id: m_whois.c 1667 2006-06-09 02:16:17Z entrope $
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #include "channel.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_features.h"
89 #include "ircd_log.h"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
92 #include "match.h"
93 #include "msg.h"
94 #include "numeric.h"
95 #include "numnicks.h"
96 #include "s_user.h"
97 #include "send.h"
98 #include "whocmds.h"
99
100 /* #include <assert.h> -- Now using assert in ircd_log.h */
101 #include <string.h>
102
103 /*
104  * 2000-07-01: Isomer
105  *  * Rewritten to make this understandable
106  *  * You can no longer /whois unregistered clients.
107  *  
108  *
109  * General rules:
110  *  /whois nick always shows the nick.
111  *  /whois wild* shows the nick if:
112  *   * they aren't +i and aren't on any channels.
113  *   * they are on a common channel.
114  *   * they aren't +i and are on a public channel. (not +p and not +s)
115  *   * they aren't +i and are on a private channel. (+p but not +s)
116  *  Or to look at it another way (I think):
117  *   * +i users are only shown if your on a common channel.
118  *   * users on +s channels only aren't shown.
119  *
120  *  whois doesn't show what channels a +k client is on, for the reason that
121  *  /whois X or /whois W floods a user off the server. :)
122  *
123  * nb: if the code and this comment disagree, the codes right and I screwed
124  *     up.
125  */
126
127 /*
128  * Send whois information for acptr to sptr
129  */
130 static void do_whois(struct Client* sptr, struct Client *acptr, int parc)
131 {
132   struct Client *a2cptr=0;
133   struct Channel *chptr=0;
134   int mlen;
135   int len;
136   static char buf[512];
137   
138   const struct User* user = cli_user(acptr);
139   const char* name = (!*(cli_name(acptr))) ? "?" : cli_name(acptr);  
140   a2cptr = feature_bool(FEAT_HIS_WHOIS_SERVERNAME) && !IsAnOper(sptr)
141       && sptr != acptr ? &his : user->server;
142   assert(user);
143   send_reply(sptr, RPL_WHOISUSER, name, user->username, user->host,
144                    cli_info(acptr));
145
146   /* Display the channels this user is on. */
147   if (!IsChannelService(acptr) && (!IsNoChan(acptr) || IsAnOper(sptr) || acptr == sptr))
148   {
149     struct Membership* chan;
150     mlen = strlen(cli_name(&me)) + strlen(cli_name(sptr)) + 12 + strlen(name);
151     len = 0;
152     *buf = '\0';
153     for (chan = user->channel; chan; chan = chan->next_channel)
154     {
155        chptr = chan->channel;
156        
157        if (!ShowChannel(sptr, chptr) && !IsAnOper(sptr))
158           continue;
159
160        if (acptr != sptr && IsZombie(chan))
161           continue;
162
163        /* Don't show local channels when HIS is defined, unless it's a
164         * remote WHOIS --ULtimaTe_
165         */
166        if (IsLocalChannel(chptr->chname) && (acptr != sptr) && (parc == 2)
167            && feature_bool(FEAT_HIS_WHOIS_LOCALCHAN) && !IsAnOper(sptr))
168           continue;
169
170        if (len+strlen(chptr->chname) + mlen > BUFSIZE - 5)
171        {
172           send_reply(sptr, SND_EXPLICIT | RPL_WHOISCHANNELS, "%s :%s", name, buf);
173           *buf = '\0';
174           len = 0;
175        }
176        if (IsDeaf(acptr))
177          *(buf + len++) = '-';
178        if (!ShowChannel(sptr, chptr))
179          *(buf + len++) = '*';
180        if (IsDelayedJoin(chan) && (sptr != acptr))
181          *(buf + len++) = '<';
182        else if (IsChanOp(chan))
183          *(buf + len++) = '@';
184        else if (HasVoice(chan))
185          *(buf + len++) = '+';
186        else if (IsZombie(chan))
187          *(buf + len++) = '!';
188        if (len)
189           *(buf + len) = '\0';
190        strcpy(buf + len, chptr->chname);
191        len += strlen(chptr->chname);
192        strcat(buf + len, " ");
193        len++;
194      }
195      if (buf[0] != '\0')
196         send_reply(sptr, RPL_WHOISCHANNELS, name, buf);
197   }
198   if (MyUser(acptr) && IsAnOper(sptr) && *cli_connclass(acptr)) {
199      send_reply(sptr, RPL_WHOISSERVER, name, cli_name(a2cptr),cli_info(a2cptr),"class: ",cli_connclass(acptr));
200   } else {
201      send_reply(sptr, RPL_WHOISSERVER, name, cli_name(a2cptr),cli_info(a2cptr),"","*");
202   }
203
204   if (user)
205   {
206     if (user->away)
207        send_reply(sptr, RPL_AWAY, name, user->away);
208
209     if (SeeOper(sptr,acptr)) {
210            if (IsNetServ(acptr)) {
211            if (HasFlag(acptr, FLAG_SECURITY_SERV)) {
212            send_reply(sptr, RPL_WHOISOPERATOR, name, "is a Network Service");
213            } else {
214            send_reply(sptr, RPL_WHOISOPERATOR, name, "is an illegal Network Service - HACKER!");
215            }
216            } else if (IsHiddenOper(acptr)) {
217        send_reply(sptr, RPL_WHOISOPERATOR, name, "is a hidden IRC Operator");
218            } else {
219            send_reply(sptr, RPL_WHOISOPERATOR, name, "is an IRC Operator");
220            }
221            }
222
223     if (IsAccount(acptr))
224       send_reply(sptr, RPL_WHOISACCOUNT, name, user->account);
225
226     if (HasHiddenHost(acptr) && (IsAnOper(sptr) || acptr == sptr))
227       send_reply(sptr, RPL_WHOISACTUALLY, name, user->username,
228                  user->realhost, ircd_ntoa(&cli_ip(acptr)));
229
230     /* Hint: if your looking to add more flags to a user, eg +h, here's
231      *       probably a good place to add them :)
232      */
233
234     if (IsSSL(acptr))
235       send_reply(sptr, RPL_WHOISSSL, name);
236
237     if (IsWebIRC(acptr)) {
238         /* Unless the real host is not broadcasted we need to check here, whether we know the real host. */
239         if(*cli_webirc(acptr) && (IsOper(sptr) || acptr == sptr))
240             send_reply(sptr, RPL_WHOISWEBIRCLONG, name, cli_webirc(acptr), cli_real_sockhost(acptr), cli_real_sock_ip(acptr));
241         else
242             send_reply(sptr, RPL_WHOISWEBIRC, name);
243     }
244
245     if (MyConnect(acptr) && CanSeeIdletime(sptr, acptr))
246        send_reply(sptr, RPL_WHOISIDLE, name, CurrentTime - user->last,
247                   cli_firsttime(acptr));
248   }
249 }
250
251 /*
252  * Search and return as many people as matched by the wild 'nick'.
253  * returns the number of people found (or, obviously, 0, if none where
254  * found).
255  */
256 static int do_wilds(struct Client* sptr, char *nick, int count, int parc)
257 {
258   struct Client *acptr; /* Current client we're considering */
259   struct User *user;    /* the user portion of the client */
260   const char *name;     /* the name of this client */
261   struct Membership* chan; 
262   int invis;            /* does +i apply? */
263   int member;           /* Is this user on any channels? */
264   int showperson;       /* Should we show this person? */
265   int found = 0 ;       /* How many were found? */
266   
267   /* Ech! This is hideous! */
268   for (acptr = GlobalClientList; (acptr = next_client(acptr, nick));
269       acptr = cli_next(acptr))
270   {
271     if (!IsRegistered(acptr)) 
272       continue;
273       
274     if (IsServer(acptr))
275       continue;
276     /*
277      * I'm always last :-) and acptr->next == 0!!
278      *
279      * Isomer: Does this strike anyone else as being a horrible hideous
280      *         hack?
281      */
282     if (IsMe(acptr)) {
283       assert(!cli_next(acptr));
284       break;
285     }
286     
287     /*
288      * 'Rules' established for sending a WHOIS reply:
289      *
290      * - if wildcards are being used don't send a reply if
291      *   the querier isn't any common channels and the
292      *   client in question is invisible.
293      *
294      * - only send replies about common or public channels
295      *   the target user(s) are on;
296      */
297     user = cli_user(acptr);
298     name = (!*(cli_name(acptr))) ? "?" : cli_name(acptr);
299     assert(user);
300
301     invis = (acptr != sptr) && IsInvisible(acptr);
302     member = (user && user->channel) ? 1 : 0;
303     showperson = !invis && !member;
304     
305     /* Should we show this person now? */
306     if (showperson) {
307         found++;
308         do_whois(sptr, acptr, parc);
309         if (count+found>MAX_WHOIS_LINES)
310           return found;
311         continue;
312     }
313     
314     /* Step through the channels this user is on */
315     for (chan = user->channel; chan; chan = chan->next_channel)
316     {
317       struct Channel *chptr = chan->channel;
318
319       /* If this is a public channel, show the person */
320       if (!invis && PubChannel(chptr)) {
321         showperson = 1;
322         break;
323       }
324       
325       /* if this channel is +p and not +s, show them */
326       if (!invis && HiddenChannel(chptr) && !SecretChannel(chptr)) {
327           showperson = 1;
328           break;
329       }
330       
331       member = find_channel_member(sptr, chptr) ? 1 : 0;
332       if (invis && !member)
333         continue;
334
335       /* If sptr isn't really on this channel, skip it */
336       if (IsZombie(chan))
337         continue;
338        
339       /* Is this a common channel? */ 
340       if (member) {
341         showperson = 1;
342         break;
343       }
344     } /* of for (chan in channels) */
345     
346     /* Don't show this person */
347     if (!showperson)
348       continue;
349       
350     do_whois(sptr, acptr, parc);
351     found++;
352     if (count+found>MAX_WHOIS_LINES)
353        return found;  
354   } /* of global client list */
355   
356   return found;
357 }
358
359 /*
360  * m_whois - generic message handler
361  *
362  * parv[0] = sender prefix
363  * parv[1] = nickname masklist
364  *
365  * or
366  *
367  * parv[1] = target server, or a nickname representing a server to target.
368  * parv[2] = nickname masklist
369  */
370 int m_whois(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
371 {
372   char*           nick;
373   char*           tmp;
374   char*           p = 0;
375   int             found = 0;
376   int             total = 0;
377   int             wildscount = 0;
378
379   if (parc < 2)
380   {
381     send_reply(sptr, ERR_NONICKNAMEGIVEN);
382     return 0;
383   }
384
385   if (parc > 2)
386   {
387     /* For convenience: Accept a nickname as first parameter, by replacing
388      * it with the correct servername - as is needed by hunt_server().
389      * This is the secret behind the /whois nick nick trick.
390      */
391     if (feature_int(FEAT_HIS_REMOTE))
392     {
393       /* If remote queries are disabled, then use the *second* parameter of
394        * of whois, so /whois nick nick still works.
395        */
396       if (!IsAnOper(sptr))
397       {
398         if (!FindUser(parv[2]))
399         {
400           send_reply(sptr, ERR_NOSUCHNICK, parv[2]);
401           send_reply(sptr, RPL_ENDOFWHOIS, parv[2]);
402           return 0;
403         }
404         parv[1] = parv[2];
405       }
406     }
407
408     if (hunt_server_cmd(sptr, CMD_WHOIS, cptr, 0, "%C :%s", 1, parc, parv) !=
409        HUNTED_ISME)
410     return 0;
411     
412     parv[1] = parv[2];
413   }
414
415   for (tmp = parv[1]; (nick = ircd_strtok(&p, tmp, ",")); tmp = 0)
416   {
417     int wilds;
418
419     found = 0;
420     
421     collapse(nick);
422     
423     wilds = (strchr(nick, '?') || strchr(nick, '*'));
424     if (!wilds) {
425       struct Client *acptr = 0;
426       /* No wildcards */
427       acptr = FindUser(nick);
428       if (acptr && !IsServer(acptr)) {
429         do_whois(sptr, acptr, parc);
430         found = 1;
431       }
432     }
433     else /* wilds */
434     {
435       if (++wildscount > 3) {
436         send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
437         break;
438       }
439       found=do_wilds(sptr, nick, total, parc);
440     }
441
442     if (!found)
443       send_reply(sptr, ERR_NOSUCHNICK, nick);
444     total+=found;
445     if (total >= MAX_WHOIS_LINES) {
446       send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
447       break;
448     }
449     if (p)
450       p[-1] = ',';
451   } /* of tokenised parm[1] */
452   send_reply(sptr, RPL_ENDOFWHOIS, parv[1]);
453
454   return 0;
455 }
456
457 /*
458  * ms_whois - server message handler
459  *
460  * parv[0] = sender prefix
461  * parv[1] = nickname masklist
462  *
463  * or
464  *
465  * parv[1] = target server, or a nickname representing a server to target.
466  * parv[2] = nickname masklist
467  */
468 int ms_whois(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
469 {
470   char*           nick;
471   char*           tmp;
472   char*           p = 0;
473   int             found = 0;
474   int             total = 0;
475
476   if (parc < 2)
477   {
478     send_reply(sptr, ERR_NONICKNAMEGIVEN);
479     return 0;
480   }
481
482   if (parc > 2)
483   {
484     if (hunt_server_cmd(sptr, CMD_WHOIS, cptr, 0, "%C :%s", 1, parc, parv) !=
485         HUNTED_ISME)
486       return 0;
487     parv[1] = parv[2];
488   }
489
490   total = 0;
491   
492   for (tmp = parv[1]; (nick = ircd_strtok(&p, tmp, ",")); tmp = 0)
493   {
494     struct Client *acptr = 0;
495
496     found = 0;
497     
498     collapse(nick);
499     
500
501     acptr = FindUser(nick);
502     if (acptr && !IsServer(acptr)) {
503       found++;
504       do_whois(sptr, acptr, parc);
505     }
506
507     if (!found)
508       send_reply(sptr, ERR_NOSUCHNICK, nick);
509       
510     total+=found;
511       
512     if (total >= MAX_WHOIS_LINES) {
513       send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
514       break;
515     }
516       
517     if (p)
518       p[-1] = ',';
519   } /* of tokenised parm[1] */
520   send_reply(sptr, RPL_ENDOFWHOIS, parv[1]);
521
522   return 0;
523 }
524