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