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