Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / m_who.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_who.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_chattr.h"
89 #include "ircd_log.h"
90 #include "ircd_policy.h"
91 #include "ircd_reply.h"
92 #include "ircd_string.h"
93 #include "match.h"
94 #include "numeric.h"
95 #include "numnicks.h"
96 #include "send.h"
97 #include "support.h"
98 #include "whocmds.h"
99
100
101 #include <assert.h>
102 #include <string.h>
103
104
105 /*
106  * A little spin-marking utility to tell us wich clients we have already
107  * processed and wich not
108  */
109 static int who_marker = 0;
110 static void move_marker(void)
111 {
112   if (!++who_marker)
113   {
114     struct Client *cptr = GlobalClientList;
115     while (cptr)
116     {
117       cli_marker(cptr) = 0;
118       cptr = cli_next(cptr);
119     }
120     who_marker++;
121   }
122 }
123
124 #define CheckMark(x, y) ((x == y) ? 0 : (x = y))
125 #define Process(cptr) CheckMark(cli_marker(cptr), who_marker)
126
127 /*
128  * m_who - generic message handler
129  *
130  *  parv[0] = sender prefix
131  *  parv[1] = nickname mask list
132  *  parv[2] = additional selection flag, only 'o' for now.
133  *            and %flags to specify what fields to output
134  *            plus a ,querytype if the t flag is specified
135  *            so the final thing will be like o%tnchu,777
136  *  parv[3] = _optional_ parameter that overrides parv[1]
137  *            This can be used as "/quote who foo % :The Black Hacker
138  *            to find me, parv[3] _can_ contain spaces !.
139  */
140 int m_who(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
141 {
142   char *mask;           /* The mask we are looking for              */
143   char ch;                      /* Scratch char register                    */
144   struct Channel *chptr;                /* Channel to show                          */
145   struct Client *acptr;         /* Client to show                           */
146
147   int bitsel;                   /* Mask of selectors to apply               */
148   int matchsel;                 /* Wich fields the match should apply on    */
149   int counter;                  /* Query size counter,
150                                    initially used to count fields           */
151   int commas;                   /* Does our mask contain any comma ?
152                                    If so is a list..                        */
153   int fields;                   /* Mask of fields to show                   */
154   int isthere = 0;              /* When this set the user is member of chptr */
155   char *nick;                   /* Single element extracted from
156                                    the mask list                            */
157   char *p;                      /* Scratch char pointer                     */
158   char *qrt;                    /* Pointer to the query type                */
159   static char mymask[512];      /* To save the mask before corrupting it    */
160
161   /* Let's find where is our mask, and if actually contains something */
162   mask = ((parc > 1) ? parv[1] : 0);
163   if (parc > 3 && parv[3])
164     mask = parv[3];
165   if (mask && ((mask[0] == '\0') ||
166       (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
167     mask = 0;
168
169   /* Evaluate the flags now, we consider the second parameter 
170      as "matchFlags%fieldsToInclude,querytype"           */
171   bitsel = fields = counter = matchsel = 0;
172   qrt = 0;
173   if (parc > 2 && parv[2] && *parv[2])
174   {
175     p = parv[2];
176     while (((ch = *(p++))) && (ch != '%') && (ch != ','))
177       switch (ch)
178       {
179         case 'o':
180         case 'O':
181           bitsel |= WHOSELECT_OPER;
182           continue;
183         case 'x':
184         case 'X':
185           bitsel |= WHOSELECT_EXTRA;
186           if (HasPriv(sptr, PRIV_WHOX))
187             log_write(LS_WHO, L_INFO, LOG_NOSNOTICE, "%#C WHO %s %s", sptr,
188                       (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
189           continue;
190         case 'n':
191         case 'N':
192           matchsel |= WHO_FIELD_NIC;
193           continue;
194         case 'u':
195         case 'U':
196           matchsel |= WHO_FIELD_UID;
197           continue;
198         case 'h':
199         case 'H':
200           matchsel |= WHO_FIELD_HOS;
201           continue;
202         case 'i':
203         case 'I':
204           matchsel |= WHO_FIELD_NIP;
205           continue;
206         case 's':
207         case 'S':
208 #ifdef HEAD_IN_SAND_WHO_SERVERNAME
209           if (IsAnOper(sptr))
210 #endif
211             matchsel |= WHO_FIELD_SER;
212           continue;
213         case 'r':
214         case 'R':
215           matchsel |= WHO_FIELD_REN;
216           continue;
217       }
218     if (ch == '%')
219       while ((ch = *p++) && (ch != ','))
220       {
221         counter++;
222         switch (ch)
223         {
224           case 'c':
225           case 'C':
226             fields |= WHO_FIELD_CHA;
227             break;
228           case 'd':
229           case 'D':
230             fields |= WHO_FIELD_DIS;
231             break;
232           case 'f':
233           case 'F':
234             fields |= WHO_FIELD_FLA;
235             break;
236           case 'h':
237           case 'H':
238             fields |= WHO_FIELD_HOS;
239             break;
240           case 'i':
241           case 'I':
242             fields |= WHO_FIELD_NIP;
243             break;
244           case 'l':
245           case 'L':
246             fields |= WHO_FIELD_IDL;
247           case 'n':
248           case 'N':
249             fields |= WHO_FIELD_NIC;
250             break;
251           case 'r':
252           case 'R':
253             fields |= WHO_FIELD_REN;
254             break;
255           case 's':
256           case 'S':
257 #ifdef HEAD_IN_SAND_WHO_SERVERNAME
258             if (IsAnOper(sptr))
259 #endif
260               fields |= WHO_FIELD_SER;
261             break;
262           case 't':
263           case 'T':
264             fields |= WHO_FIELD_QTY;
265             break;
266           case 'u':
267           case 'U':
268             fields |= WHO_FIELD_UID;
269             break;
270           default:
271             break;
272         }
273       };
274     if (ch)
275       qrt = p;
276   }
277
278   if (!matchsel)
279     matchsel = WHO_FIELD_DEF;
280   if (!fields)
281     counter = 7;
282
283   if (qrt && (fields & WHO_FIELD_QTY))
284   {
285     p = qrt;
286     if (!((*p > '9') || (*p < '0')))
287       p++;
288     if (!((*p > '9') || (*p < '0')))
289       p++;
290     if (!((*p > '9') || (*p < '0')))
291       p++;
292     *p = '\0';
293   }
294   else
295     qrt = 0;
296
297   /* I'd love to add also a check on the number of matches fields per time */
298   counter = (2048 / (counter + 4));
299   if (mask && (strlen(mask) > 510))
300     mask[510] = '\0';
301   move_marker();
302   commas = (mask && strchr(mask, ','));
303
304   /* First treat mask as a list of plain nicks/channels */
305   if (mask)
306   {
307     strcpy(mymask, mask);
308     for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
309         nick = ircd_strtok(&p, 0, ","))
310     {
311       if (IsChannelName(nick) && (chptr = FindChannel(nick)))
312       {
313         isthere = (find_channel_member(sptr, chptr) != 0);
314         if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
315         {
316           struct Membership* member;
317           for (member = chptr->members; member; member = member->next_member)
318           {
319             acptr = member->user;
320             if ((bitsel & WHOSELECT_OPER) &&
321                 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
322                                       HasPriv(sptr, PRIV_SEE_OPERS))))
323               continue;
324             if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
325               continue;
326             if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
327               continue;
328             if (!Process(acptr))        /* This can't be moved before other checks */
329               continue;
330             if (!(isthere || (SHOW_MORE(sptr, counter))))
331               break;
332             do_who(sptr, acptr, chptr, fields, qrt);
333           }
334         }
335       }
336       else
337       {
338         if ((acptr = FindUser(nick)) &&
339             ((!(bitsel & WHOSELECT_OPER)) ||
340              (IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
341                                   HasPriv(sptr, PRIV_SEE_OPERS)))) &&
342             Process(acptr) && SHOW_MORE(sptr, counter))
343         {
344           do_who(sptr, acptr, 0, fields, qrt);
345         }
346       }
347     }
348   }
349
350   /* If we didn't have any comma in the mask treat it as a
351      real mask and try to match all relevant fields */
352   if (!(commas || (counter < 1)))
353   {
354     int minlen, cset;
355     static struct in_mask imask;
356     if (mask)
357     {
358       matchcomp(mymask, &minlen, &cset, mask);
359       if (matchcompIP(&imask, mask))
360         matchsel &= ~WHO_FIELD_NIP;
361       if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
362         matchsel &= ~WHO_FIELD_NIC;
363       if ((matchsel & WHO_FIELD_SER) &&
364           ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
365           || (!markMatchexServer(mymask, minlen))))
366         matchsel &= ~WHO_FIELD_SER;
367       if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
368         matchsel &= ~WHO_FIELD_UID;
369       if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
370         matchsel &= ~WHO_FIELD_HOS;
371     }
372
373     /* First of all loop through the clients in common channels */
374     if ((!(counter < 1)) && matchsel) {
375       struct Membership* member;
376       struct Membership* chan;
377       for (chan = cli_user(sptr)->channel; chan; chan = chan->next_channel) {
378         chptr = chan->channel;
379         for (member = chptr->members; member; member = member->next_member)
380         {
381           acptr = member->user;
382           if (!(IsUser(acptr) && Process(acptr)))
383             continue;           /* Now Process() is at the beginning, if we fail
384                                    we'll never have to show this acptr in this query */
385           if ((bitsel & WHOSELECT_OPER) &&
386               !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
387                                     HasPriv(sptr, PRIV_SEE_OPERS))))
388             continue;
389           if ((mask) &&
390               ((!(matchsel & WHO_FIELD_NIC))
391               || matchexec(cli_name(acptr), mymask, minlen))
392               && ((!(matchsel & WHO_FIELD_UID))
393               || matchexec(cli_user(acptr)->username, mymask, minlen))
394               && ((!(matchsel & WHO_FIELD_SER))
395               || (!(cli_flags(cli_user(acptr)->server) & FLAGS_MAP)))
396               && ((!(matchsel & WHO_FIELD_HOS))
397               || matchexec(cli_user(acptr)->host, mymask, minlen))
398               && ((!(matchsel & WHO_FIELD_REN))
399               || matchexec(cli_info(acptr), mymask, minlen))
400               && ((!(matchsel & WHO_FIELD_NIP))
401               || ((((cli_ip(acptr).s_addr & imask.mask.s_addr) !=
402               imask.bits.s_addr)) || (imask.fall
403               && matchexec(ircd_ntoa((const char*) &(cli_ip(acptr))), mymask, minlen)))))
404             continue;
405           if (!SHOW_MORE(sptr, counter))
406             break;
407           do_who(sptr, acptr, chptr, fields, qrt);
408         }
409       }
410     }
411     /* Loop through all clients :-\, if we still have something to match to 
412        and we can show more clients */
413     if ((!(counter < 1)) && matchsel)
414       for (acptr = cli_prev(&me); acptr; acptr = cli_prev(acptr))
415       {
416         if (!(IsUser(acptr) && Process(acptr)))
417           continue;
418         if ((bitsel & WHOSELECT_OPER) &&
419             !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
420                                   HasPriv(sptr, PRIV_SEE_OPERS))))
421           continue;
422         if (!(SEE_USER(sptr, acptr, bitsel)))
423           continue;
424         if ((mask) &&
425             ((!(matchsel & WHO_FIELD_NIC))
426             || matchexec(cli_name(acptr), mymask, minlen))
427             && ((!(matchsel & WHO_FIELD_UID))
428             || matchexec(cli_user(acptr)->username, mymask, minlen))
429             && ((!(matchsel & WHO_FIELD_SER))
430             || (!(cli_flags(cli_user(acptr)->server) & FLAGS_MAP)))
431             && ((!(matchsel & WHO_FIELD_HOS))
432             || matchexec(cli_user(acptr)->host, mymask, minlen))
433             && ((!(matchsel & WHO_FIELD_REN))
434             || matchexec(cli_info(acptr), mymask, minlen))
435             && ((!(matchsel & WHO_FIELD_NIP))
436             || ((((cli_ip(acptr).s_addr & imask.mask.s_addr) != imask.bits.s_addr))
437             || (imask.fall
438             && matchexec(ircd_ntoa((const char*) &(cli_ip(acptr))), mymask, minlen)))))
439           continue;
440         if (!SHOW_MORE(sptr, counter))
441           break;
442         do_who(sptr, acptr, 0, fields, qrt);
443       }
444   }
445
446   /* Make a clean mask suitable to be sent in the "end of" */
447   if (mask && (p = strchr(mask, ' ')))
448     *p = '\0';
449   send_reply(sptr, RPL_ENDOFWHO, BadPtr(mask) ? "*" : mask);
450
451   /* Notify the user if we decided that his query was too long */
452   if (counter < 0)
453     send_reply(sptr, ERR_QUERYTOOLONG, "WHO");
454
455   return 0;
456 }