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