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 #include "config.h"
83
84 #include "channel.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_policy.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           continue;
157           
158        if (acptr != sptr && IsZombie(chan))
159           continue;
160           
161        if (len+strlen(chptr->chname) + mlen > BUFSIZE - 5) 
162        {
163           send_reply(sptr, SND_EXPLICIT | RPL_WHOISCHANNELS, "%s :%s", name, buf);
164           *buf = '\0';
165           len = 0;
166        }
167        if (IsDeaf(acptr))
168          *(buf + len++) = '-';
169        if (is_chan_op(acptr, chptr))
170          *(buf + len++) = '@';
171        else if (has_voice(acptr, chptr))
172          *(buf + len++) = '+';
173        else if (IsZombie(chan))
174          *(buf + len++) = '!';
175        if (len)
176           *(buf + len) = '\0';
177        strcpy(buf + len, chptr->chname);
178        len += strlen(chptr->chname);
179        strcat(buf + len, " ");
180        len++;
181      }
182      if (buf[0] != '\0')
183         send_reply(sptr, RPL_WHOISCHANNELS, name, buf);
184   }
185
186 #ifdef HEAD_IN_SAND_WHOIS_SERVERNAME
187   if (!IsAnOper(sptr) && sptr != acptr)
188     send_reply(sptr, RPL_WHOISSERVER, name, "*.undernet.org",
189                "The Undernet Underworld");
190   else
191 #endif
192     send_reply(sptr, RPL_WHOISSERVER, name, cli_name(a2cptr),
193                cli_info(a2cptr));
194
195   if (user)
196   {
197     if (user->away)
198        send_reply(sptr, RPL_AWAY, name, user->away);
199
200     if (IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
201                             HasPriv(sptr, PRIV_SEE_OPERS)))
202        send_reply(sptr, RPL_WHOISOPERATOR, name);
203    
204     /* Hint: if your looking to add more flags to a user, eg +h, here's
205      *       probably a good place to add them :)
206      */
207      
208     if (MyConnect(acptr)
209 #ifdef HEAD_IN_SAND_WHOIS_IDLETIME
210         && (sptr == acptr || IsAnOper(sptr) || parc >= 3)
211 #endif
212         )
213        send_reply(sptr, RPL_WHOISIDLE, name, CurrentTime - user->last, 
214                   cli_firsttime(acptr));
215   }
216 }
217
218 /*
219  * Search and return as many people as matched by the wild 'nick'.
220  * returns the number of people found (or, obviously, 0, if none where
221  * found).
222  */
223 static int do_wilds(struct Client* sptr, char *nick, int count, int parc)
224 {
225   struct Client *acptr; /* Current client we're concidering */
226   struct User *user;    /* the user portion of the client */
227   char *name;           /* the name of this client */
228   struct Membership* chan; 
229   int invis;            /* does +i apply? */
230   int member;           /* Is this user on any channels? */
231   int showperson;       /* Should we show this person? */
232   int found = 0 ;       /* How many were found? */
233   
234   /* Ech! This is hidious! */
235   for (acptr = GlobalClientList; (acptr = next_client(acptr, nick));
236       acptr = cli_next(acptr))
237   {
238     if (!IsRegistered(acptr)) 
239       continue;
240       
241     if (IsServer(acptr))
242       continue;
243     /*
244      * I'm always last :-) and acptr->next == 0!!
245      *
246      * Isomer: Does this strike anyone else as being a horrible hidious
247      *         hack?
248      */
249     if (IsMe(acptr)) {
250       assert(!cli_next(acptr));
251       break;
252     }
253     
254     /*
255      * 'Rules' established for sending a WHOIS reply:
256      *
257      * - if wildcards are being used dont send a reply if
258      *   the querier isnt any common channels and the
259      *   client in question is invisible.
260      *
261      * - only send replies about common or public channels
262      *   the target user(s) are on;
263      */
264     user = cli_user(acptr);
265     name = (!*(cli_name(acptr))) ? "?" : cli_name(acptr);
266     assert(user);
267
268     invis = (acptr != sptr) && IsInvisible(acptr);
269     member = (user && user->channel) ? 1 : 0;
270     showperson = !invis && !member;
271     
272     /* Should we show this person now? */
273     if (showperson) {
274         found++;
275         do_whois(sptr, acptr, parc);
276         if (count+found>MAX_WHOIS_LINES)
277           return found;
278         continue;
279     }
280     
281     /* Step through the channels this user is on */
282     for (chan = user->channel; chan; chan = chan->next_channel)
283     {
284       struct Channel *chptr = chan->channel;
285
286       /* If this is a public channel, show the person */
287       if (!invis && PubChannel(chptr)) {
288         showperson = 1;
289         break;
290       }
291       
292       /* if this channel is +p and not +s, show them */
293       if (!invis && HiddenChannel(chptr) && !SecretChannel(chptr)) {
294           showperson = 1;
295           break;
296       }
297       
298       member = find_channel_member(sptr, chptr) ? 1 : 0;
299       if (invis && !member)
300         continue;
301
302       /* If sptr isn't really on this channel, skip it */
303       if (IsZombie(chan))
304         continue;
305        
306       /* Is this a common channel? */ 
307       if (member) {
308         showperson = 1;
309         break;
310       }
311     } /* of for (chan in channels) */
312     
313     /* Don't show this person */
314     if (!showperson)
315       continue;
316       
317     do_whois(sptr, acptr, parc);
318     found++;
319     if (count+found>MAX_WHOIS_LINES)
320        return found;  
321   } /* of global client list */
322   
323   return found;
324 }
325
326 /*
327  * m_whois - generic message handler
328  *
329  * parv[0] = sender prefix
330  * parv[1] = nickname masklist
331  *
332  * or
333  *
334  * parv[1] = target server, or a nickname representing a server to target.
335  * parv[2] = nickname masklist
336  */
337 int m_whois(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
338 {
339   char*           nick;
340   char*           tmp;
341   char*           p = 0;
342   int             found = 0;
343   int             total = 0;
344
345   if (parc < 2)
346   {
347     send_reply(sptr, ERR_NONICKNAMEGIVEN);
348     return 0;
349   }
350
351   if (parc > 2)
352   {
353     struct Client *acptr;
354     /* For convenience: Accept a nickname as first parameter, by replacing
355      * it with the correct servername - as is needed by hunt_server().
356      * This is the secret behind the /whois nick nick trick.
357      */
358 #if HEAD_IN_SAND_REMOTE
359     /* If remote queries are disabled, then use the *second* parameter of
360      * of whois, so /whois nick nick still works.
361      */
362     acptr = FindUser(parv[2]);
363 #else
364     acptr = FindUser(parv[1]);
365 #endif
366     if (acptr)
367       parv[1] = cli_name(cli_user(acptr)->server);
368     if (hunt_server_cmd(sptr, CMD_WHOIS, cptr, 0, "%C :%s", 1, parc, parv) !=
369         HUNTED_ISME)
370       return 0;
371     parv[1] = parv[2];
372   }
373
374   for (tmp = parv[1]; (nick = ircd_strtok(&p, tmp, ",")); tmp = 0)
375   {
376     int wilds;
377
378     found = 0;
379     
380     collapse(nick);
381     
382     wilds = (strchr(nick, '?') || strchr(nick, '*'));
383     if (!wilds) {
384       struct Client *acptr = 0;
385       /* No wildcards */
386       acptr = FindUser(nick);
387       if (acptr && !IsServer(acptr)) {
388         do_whois(sptr, acptr, parc);
389         found = 1;
390       }
391     }
392     else /* wilds */
393         found=do_wilds(sptr, nick, total, parc);
394
395     if (!found)
396       send_reply(sptr, ERR_NOSUCHNICK, nick);
397     total+=found;
398     if (total >= MAX_WHOIS_LINES) {
399       send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
400       break;
401     }
402     if (p)
403       p[-1] = ',';
404   } /* of tokenised parm[1] */
405   send_reply(sptr, RPL_ENDOFWHOIS, parv[1]);
406
407   return 0;
408 }
409
410 /*
411  * ms_whois - server message handler
412  *
413  * parv[0] = sender prefix
414  * parv[1] = nickname masklist
415  *
416  * or
417  *
418  * parv[1] = target server, or a nickname representing a server to target.
419  * parv[2] = nickname masklist
420  */
421 int ms_whois(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
422 {
423   char*           nick;
424   char*           tmp;
425   char*           p = 0;
426   int             found = 0;
427   int             total = 0;
428
429   if (parc < 2)
430   {
431     send_reply(sptr, ERR_NONICKNAMEGIVEN);
432     return 0;
433   }
434
435   if (parc > 2)
436   {
437     struct Client *acptr;
438     /* For convenience: Accept a nickname as first parameter, by replacing
439      * it with the correct servername - as is needed by hunt_server().
440      * This is the secret behind the /whois nick nick trick.
441      */
442     acptr = FindUser(parv[1]);
443     if (acptr)
444       parv[1] = cli_name(cli_user(acptr)->server);
445     if (hunt_server_cmd(sptr, CMD_WHOIS, cptr, 0, "%C :%s", 1, parc, parv) !=
446         HUNTED_ISME)
447       return 0;
448     parv[1] = parv[2];
449   }
450
451   total = 0;
452   
453   for (tmp = parv[1]; (nick = ircd_strtok(&p, tmp, ",")); tmp = 0)
454   {
455     struct Client *acptr = 0;
456
457     found = 0;
458     
459     collapse(nick);
460     
461
462     acptr = FindUser(nick);
463     if (acptr && !IsServer(acptr)) {
464       found++;
465       do_whois(sptr, acptr, parc);
466     }
467
468     if (!found)
469       send_reply(sptr, ERR_NOSUCHNICK, nick);
470       
471     total+=found;
472       
473     if (total >= MAX_WHOIS_LINES) {
474       send_reply(sptr, ERR_QUERYTOOLONG, parv[1]);
475       break;
476     }
477       
478     if (p)
479       p[-1] = ',';
480   } /* of tokenised parm[1] */
481   send_reply(sptr, RPL_ENDOFWHOIS, parv[1]);
482
483   return 0;
484 }