Add new feature FEAT_HIS_WHOIS_LOCALCHAN.
[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$
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_reply.h"
90 #include "ircd_string.h"
91 #include "match.h"
92 #include "msg.h"
93 #include "numeric.h"
94 #include "numnicks.h"
95 #include "s_user.h"
96 #include "send.h"
97 #include "whocmds.h"
98
99 #include <assert.h>
100 #include <string.h>
101
102 /*
103  * 2000-07-01: Isomer
104  *  * Rewritten to make this understandable
105  *  * You can nolonger /whois unregistered clients.
106  *  
107  *
108  * General rules:
109  *  /whois nick always shows the nick.
110  *  /whois wild* shows the nick if:
111  *   * they aren't +i and aren't on any channels.
112  *   * they are on a common channel.
113  *   * they aren't +i and are on a public channel. (not +p and not +s)
114  *   * they aren't +i and are on a private channel. (+p but not +s)
115  *  Or to look at it another way (I think):
116  *   * +i users are only shown if your on a common channel.
117  *   * users on +s channels only aren't shown.
118  *
119  *  whois doesn't show what channels a +k client is on, for the reason that
120  *  /whois X or /whois W floods a user off the server. :)
121  *
122  * nb: if the code and this comment disagree, the codes right and I screwed
123  *     up.
124  */
125
126 /*
127  * Send whois information for acptr to sptr
128  */
129 static void do_whois(struct Client* sptr, struct Client *acptr, int parc)
130 {
131   struct Client *a2cptr=0;
132   struct Channel *chptr=0;
133   int mlen;
134   int len;
135   static char buf[512];
136   
137   const struct User* user = cli_user(acptr);
138   const char* name = (!*(cli_name(acptr))) ? "?" : cli_name(acptr);  
139   a2cptr = user->server;
140   assert(user);
141   send_reply(sptr, RPL_WHOISUSER, name, user->username, user->host,
142                    cli_info(acptr));
143
144   /* Display the channels this user is on. */
145   if (!IsChannelService(acptr))
146   {
147     struct Membership* chan;
148     mlen = strlen(cli_name(&me)) + strlen(cli_name(sptr)) + 12 + strlen(name);
149     len = 0;
150     *buf = '\0';
151     for (chan = user->channel; chan; chan = chan->next_channel)
152     {
153        chptr = chan->channel;
154        
155        if (!ShowChannel(sptr, chptr)
156            && !(IsOper(sptr) && IsLocalChannel(chptr->chname)))
157           continue;
158
159        if (acptr != sptr && IsZombie(chan))
160           continue;
161
162        /* Don't show local channels when HIS is defined, unless it's a
163         * remote WHOIS --ULtimaTe_
164         */
165        if (IsLocalChannel(chptr->chname) && (acptr != sptr) && (parc == 2)
166            && feature_bool(FEAT_HIS_WHOIS_LOCALCHAN) && !IsAnOper(sptr))
167           continue;
168
169        if (len+strlen(chptr->chname) + mlen > BUFSIZE - 5)
170        {
171           send_reply(sptr, SND_EXPLICIT | RPL_WHOISCHANNELS, "%s :%s", name, buf);
172           *buf = '\0';
173           len = 0;
174        }
175        if (IsDeaf(acptr))
176          *(buf + len++) = '-';
177        if (IsOper(sptr) && !ShowChannel(sptr, chptr))
178          *(buf + len++) = '*';
179        if (IsDelayedJoin(chan) && (sptr != acptr))
180          *(buf + len++) = '<';
181        else if (IsChanOp(chan))
182          *(buf + len++) = '@';
183        else if (HasVoice(chan))
184          *(buf + len++) = '+';
185        else if (IsZombie(chan))
186          *(buf + len++) = '!';
187        if (len)
188           *(buf + len) = '\0';
189        strcpy(buf + len, chptr->chname);
190        len += strlen(chptr->chname);
191        strcat(buf + len, " ");
192        len++;
193      }
194      if (buf[0] != '\0')
195         send_reply(sptr, RPL_WHOISCHANNELS, name, buf);
196   }
197
198   if (feature_bool(FEAT_HIS_WHOIS_SERVERNAME) && !IsAnOper(sptr) &&
199       sptr != acptr)
200     send_reply(sptr, RPL_WHOISSERVER, name, feature_str(FEAT_HIS_SERVERNAME),
201                feature_str(FEAT_HIS_SERVERINFO));
202   else
203     send_reply(sptr, RPL_WHOISSERVER, name, cli_name(a2cptr),
204                cli_info(a2cptr));
205
206   if (user)
207   {
208     if (user->away)
209        send_reply(sptr, RPL_AWAY, name, user->away);
210
211     if (SeeOper(sptr,acptr))
212        send_reply(sptr, RPL_WHOISOPERATOR, name);
213
214     if (IsAccount(acptr))
215       send_reply(sptr, RPL_WHOISACCOUNT, name, user->account);
216
217     if (HasHiddenHost(acptr) && (IsAnOper(sptr) || acptr == sptr))
218       send_reply(sptr, RPL_WHOISACTUALLY, name, user->username,
219                  user->realhost, ircd_ntoa(&cli_ip(acptr)));
220
221     /* Hint: if your looking to add more flags to a user, eg +h, here's
222      *       probably a good place to add them :)
223      */
224
225     if (MyConnect(acptr) && (!feature_bool(FEAT_HIS_WHOIS_IDLETIME) ||
226                              (sptr == acptr || IsAnOper(sptr) || parc >= 3)))
227        send_reply(sptr, RPL_WHOISIDLE, name, CurrentTime - user->last,
228                   cli_firsttime(acptr));
229   }
230 }
231
232 /*
233  * Search and return as many people as matched by the wild 'nick'.
234  * returns the number of people found (or, obviously, 0, if none where
235  * found).
236  */
237 static int do_wilds(struct Client* sptr, char *nick, int count, int parc)
238 {
239   struct Client *acptr; /* Current client we're concidering */
240   struct User *user;    /* the user portion of the client */
241   const char *name;     /* the name of this client */
242   struct Membership* chan; 
243   int invis;            /* does +i apply? */
244   int member;           /* Is this user on any channels? */
245   int showperson;       /* Should we show this person? */
246   int found = 0 ;       /* How many were found? */
247   
248   /* Ech! This is hidious! */
249   for (acptr = GlobalClientList; (acptr = next_client(acptr, nick));
250       acptr = cli_next(acptr))
251   {
252     if (!IsRegistered(acptr)) 
253       continue;
254       
255     if (IsServer(acptr))
256       continue;
257     /*
258      * I'm always last :-) and acptr->next == 0!!
259      *
260      * Isomer: Does this strike anyone else as being a horrible hidious
261      *         hack?
262      */
263     if (IsMe(acptr)) {
264       assert(!cli_next(acptr));
265       break;
266     }
267     
268     /*
269      * 'Rules' established for sending a WHOIS reply:
270      *
271      * - if wildcards are being used dont send a reply if
272      *   the querier isnt any common channels and the
273      *   client in question is invisible.
274      *
275      * - only send replies about common or public channels
276      *   the target user(s) are on;
277      */
278     user = cli_user(acptr);
279     name = (!*(cli_name(acptr))) ? "?" : cli_name(acptr);
280     assert(user);
281
282     invis = (acptr != sptr) && IsInvisible(acptr);
283     member = (user && user->channel) ? 1 : 0;
284     showperson = !invis && !member;
285     
286     /* Should we show this person now? */
287     if (showperson) {
288         found++;
289         do_whois(sptr, acptr, parc);
290         if (count+found>MAX_WHOIS_LINES)
291           return found;
292         continue;
293     }
294     
295     /* Step through the channels this user is on */
296     for (chan = user->channel; chan; chan = chan->next_channel)
297     {
298       struct Channel *chptr = chan->channel;
299
300       /* If this is a public channel, show the person */
301       if (!invis && PubChannel(chptr)) {
302         showperson = 1;
303         break;
304       }
305       
306       /* if this channel is +p and not +s, show them */
307       if (!invis && HiddenChannel(chptr) && !SecretChannel(chptr)) {
308           showperson = 1;
309           break;
310       }
311       
312       member = find_channel_member(sptr, chptr) ? 1 : 0;
313       if (invis && !member)
314         continue;
315
316       /* If sptr isn't really on this channel, skip it */
317       if (IsZombie(chan))
318         continue;
319        
320       /* Is this a common channel? */ 
321       if (member) {
322         showperson = 1;
323         break;
324       }
325     } /* of for (chan in channels) */
326     
327     /* Don't show this person */
328     if (!showperson)
329       continue;
330       
331     do_whois(sptr, acptr, parc);
332     found++;
333     if (count+found>MAX_WHOIS_LINES)
334        return found;  
335   } /* of global client list */
336   
337   return found;
338 }
339
340 /*
341  * m_whois - generic message handler
342  *
343  * parv[0] = sender prefix
344  * parv[1] = nickname masklist
345  *
346  * or
347  *
348  * parv[1] = target server, or a nickname representing a server to target.
349  * parv[2] = nickname masklist
350  */
351 int m_whois(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
352 {
353   char*           nick;
354   char*           tmp;
355   char*           p = 0;
356   int             found = 0;
357   int             total = 0;
358   int             wildscount = 0;
359
360   if (parc < 2)
361   {
362     send_reply(sptr, ERR_NONICKNAMEGIVEN);
363     return 0;
364   }
365
366   if (parc > 2)
367   {
368     /* For convenience: Accept a nickname as first parameter, by replacing
369      * it with the correct servername - as is needed by hunt_server().
370      * This is the secret behind the /whois nick nick trick.
371      */
372     if (feature_int(FEAT_HIS_REMOTE))
373     {
374       /* If remote queries are disabled, then use the *second* parameter of
375        * of whois, so /whois nick nick still works.
376        */
377       if (!IsAnOper(sptr))
378       {
379         if (!FindUser(parv[2]))
380         {
381           send_reply(sptr, ERR_NOSUCHNICK, parv[2]);
382           send_reply(sptr, RPL_ENDOFWHOIS, parv[2]);
383           return 0;
384         }
385         parv[1] = parv[2];
386       }
387     }
388
389     if (hunt_server_cmd(sptr, CMD_WHOIS, cptr, 0, "%C :%s", 1, parc, parv) !=
390        HUNTED_ISME)
391     return 0;
392     
393     parv[1] = parv[2];
394   }
395
396   for (tmp = parv[1]; (nick = ircd_strtok(&p, tmp, ",")); tmp = 0)
397   {
398     int wilds;
399
400     found = 0;
401     
402     collapse(nick);
403     
404     wilds = (strchr(nick, '?') || strchr(nick, '*'));
405     if (!wilds) {
406       struct Client *acptr = 0;
407       /* No wildcards */
408       acptr = FindUser(nick);
409       if (acptr && !IsServer(acptr)) {
410         do_whois(sptr, acptr, parc);
411         found = 1;
412       }
413     }
414     else /* wilds */
415     {
416       if (++wildscount > 3) {
417         send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
418         break;
419       }
420       found=do_wilds(sptr, nick, total, parc);
421     }
422
423     if (!found)
424       send_reply(sptr, ERR_NOSUCHNICK, nick);
425     total+=found;
426     if (total >= MAX_WHOIS_LINES) {
427       send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
428       break;
429     }
430     if (p)
431       p[-1] = ',';
432   } /* of tokenised parm[1] */
433   send_reply(sptr, RPL_ENDOFWHOIS, parv[1]);
434
435   return 0;
436 }
437
438 /*
439  * ms_whois - server message handler
440  *
441  * parv[0] = sender prefix
442  * parv[1] = nickname masklist
443  *
444  * or
445  *
446  * parv[1] = target server, or a nickname representing a server to target.
447  * parv[2] = nickname masklist
448  */
449 int ms_whois(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
450 {
451   char*           nick;
452   char*           tmp;
453   char*           p = 0;
454   int             found = 0;
455   int             total = 0;
456
457   if (parc < 2)
458   {
459     send_reply(sptr, ERR_NONICKNAMEGIVEN);
460     return 0;
461   }
462
463   if (parc > 2)
464   {
465     if (hunt_server_cmd(sptr, CMD_WHOIS, cptr, 0, "%C :%s", 1, parc, parv) !=
466         HUNTED_ISME)
467       return 0;
468     parv[1] = parv[2];
469   }
470
471   total = 0;
472   
473   for (tmp = parv[1]; (nick = ircd_strtok(&p, tmp, ",")); tmp = 0)
474   {
475     struct Client *acptr = 0;
476
477     found = 0;
478     
479     collapse(nick);
480     
481
482     acptr = FindUser(nick);
483     if (acptr && !IsServer(acptr)) {
484       found++;
485       do_whois(sptr, acptr, parc);
486     }
487
488     if (!found)
489       send_reply(sptr, ERR_NOSUCHNICK, nick);
490       
491     total+=found;
492       
493     if (total >= MAX_WHOIS_LINES) {
494       send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
495       break;
496     }
497       
498     if (p)
499       p[-1] = ',';
500   } /* of tokenised parm[1] */
501   send_reply(sptr, RPL_ENDOFWHOIS, parv[1]);
502
503   return 0;
504 }
505