2 * IRC - Internet Relay Chat, ircd/m_who.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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)
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.
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.
27 * m_functions execute protocol messages on this server:
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...).
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.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
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 ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
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*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
88 #include "ircd_chattr.h"
89 #include "ircd_features.h"
91 #include "ircd_reply.h"
92 #include "ircd_string.h"
105 * A little spin-marking utility to tell us wich clients we have already
106 * processed and wich not
108 static int who_marker = 0;
109 static void move_marker(void)
113 struct Client *cptr = GlobalClientList;
116 cli_marker(cptr) = 0;
117 cptr = cli_next(cptr);
123 #define CheckMark(x, y) ((x == y) ? 0 : (x = y))
124 #define Process(cptr) CheckMark(cli_marker(cptr), who_marker)
127 * m_who - generic message handler
129 * parv[0] = sender prefix
130 * parv[1] = nickname mask list
131 * parv[2] = additional selection flag, only 'o' for now.
132 * and %flags to specify what fields to output
133 * plus a ,querytype if the t flag is specified
134 * so the final thing will be like o%tnchu,777
135 * parv[3] = _optional_ parameter that overrides parv[1]
136 * This can be used as "/quote who foo % :The Black Hacker
137 * to find me, parv[3] _can_ contain spaces !.
139 int m_who(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
141 char *mask; /* The mask we are looking for */
142 char ch; /* Scratch char register */
143 struct Channel *chptr; /* Channel to show */
144 struct Client *acptr; /* Client to show */
146 int bitsel; /* Mask of selectors to apply */
147 int matchsel; /* Wich fields the match should apply on */
148 int counter; /* Query size counter,
149 initially used to count fields */
150 int commas; /* Does our mask contain any comma ?
152 int fields; /* Mask of fields to show */
153 int isthere = 0; /* When this set the user is member of chptr */
154 char *nick; /* Single element extracted from
156 char *p; /* Scratch char pointer */
157 char *qrt; /* Pointer to the query type */
158 static char mymask[512]; /* To save the mask before corrupting it */
160 /* Let's find where is our mask, and if actually contains something */
161 mask = ((parc > 1) ? parv[1] : 0);
162 if (parc > 3 && parv[3])
164 if (mask && ((mask[0] == '\0') ||
165 (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
168 /* Evaluate the flags now, we consider the second parameter
169 as "matchFlags%fieldsToInclude,querytype" */
170 bitsel = fields = counter = matchsel = 0;
172 if (parc > 2 && parv[2] && *parv[2])
175 while (((ch = *(p++))) && (ch != '%') && (ch != ','))
180 bitsel |= WHOSELECT_OPER;
184 bitsel |= WHOSELECT_EXTRA;
185 if (HasPriv(sptr, PRIV_WHOX))
186 log_write(LS_WHO, L_INFO, LOG_NOSNOTICE, "%#C WHO %s %s", sptr,
187 (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
191 matchsel |= WHO_FIELD_NIC;
195 matchsel |= WHO_FIELD_UID;
199 matchsel |= WHO_FIELD_HOS;
203 matchsel |= WHO_FIELD_NIP;
207 matchsel |= WHO_FIELD_SER;
211 matchsel |= WHO_FIELD_REN;
215 matchsel |= WHO_FIELD_ACC;
219 while ((ch = *p++) && (ch != ','))
226 fields |= WHO_FIELD_CHA;
230 fields |= WHO_FIELD_DIS;
234 fields |= WHO_FIELD_FLA;
238 fields |= WHO_FIELD_HOS;
242 fields |= WHO_FIELD_NIP;
246 fields |= WHO_FIELD_IDL;
249 fields |= WHO_FIELD_NIC;
253 fields |= WHO_FIELD_REN;
257 fields |= WHO_FIELD_SER;
261 fields |= WHO_FIELD_QTY;
265 fields |= WHO_FIELD_UID;
269 fields |= WHO_FIELD_ACC;
280 matchsel = WHO_FIELD_DEF;
284 if (feature_bool(FEAT_HIS_WHO_SERVERNAME) && !IsAnOper(sptr))
285 matchsel &= ~WHO_FIELD_SER;
287 if (qrt && (fields & WHO_FIELD_QTY))
290 if (!((*p > '9') || (*p < '0')))
292 if (!((*p > '9') || (*p < '0')))
294 if (!((*p > '9') || (*p < '0')))
301 /* I'd love to add also a check on the number of matches fields per time */
302 counter = (2048 / (counter + 4));
303 if (mask && (strlen(mask) > 510))
306 commas = (mask && strchr(mask, ','));
308 /* First treat mask as a list of plain nicks/channels */
311 strcpy(mymask, mask);
312 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
313 nick = ircd_strtok(&p, 0, ","))
315 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
317 isthere = (find_channel_member(sptr, chptr) != 0);
318 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
320 struct Membership* member;
321 for (member = chptr->members; member; member = member->next_member)
323 acptr = member->user;
324 if ((bitsel & WHOSELECT_OPER) &&
325 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
326 HasPriv(sptr, PRIV_SEE_OPERS))))
328 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
330 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
332 if (!Process(acptr)) /* This can't be moved before other checks */
334 if (!(isthere || (SHOW_MORE(sptr, counter))))
336 do_who(sptr, acptr, chptr, fields, qrt);
342 if ((acptr = FindUser(nick)) &&
343 ((!(bitsel & WHOSELECT_OPER)) ||
344 (IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
345 HasPriv(sptr, PRIV_SEE_OPERS)))) &&
346 Process(acptr) && SHOW_MORE(sptr, counter))
348 do_who(sptr, acptr, 0, fields, qrt);
354 /* If we didn't have any comma in the mask treat it as a
355 real mask and try to match all relevant fields */
356 if (!(commas || (counter < 1)))
359 static struct in_mask imask;
362 matchcomp(mymask, &minlen, &cset, mask);
363 if (matchcompIP(&imask, mask))
364 matchsel &= ~WHO_FIELD_NIP;
365 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
366 matchsel &= ~WHO_FIELD_NIC;
367 if ((matchsel & WHO_FIELD_SER) &&
368 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
369 || (!markMatchexServer(mymask, minlen))))
370 matchsel &= ~WHO_FIELD_SER;
371 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
372 matchsel &= ~WHO_FIELD_UID;
373 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
374 matchsel &= ~WHO_FIELD_HOS;
377 /* First of all loop through the clients in common channels */
378 if ((!(counter < 1)) && matchsel) {
379 struct Membership* member;
380 struct Membership* chan;
381 for (chan = cli_user(sptr)->channel; chan; chan = chan->next_channel) {
382 chptr = chan->channel;
383 for (member = chptr->members; member; member = member->next_member)
385 acptr = member->user;
386 if (!(IsUser(acptr) && Process(acptr)))
387 continue; /* Now Process() is at the beginning, if we fail
388 we'll never have to show this acptr in this query */
389 if ((bitsel & WHOSELECT_OPER) &&
390 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
391 HasPriv(sptr, PRIV_SEE_OPERS))))
394 ((!(matchsel & WHO_FIELD_NIC))
395 || matchexec(cli_name(acptr), mymask, minlen))
396 && ((!(matchsel & WHO_FIELD_UID))
397 || matchexec(cli_user(acptr)->username, mymask, minlen))
398 && ((!(matchsel & WHO_FIELD_SER))
399 || (!(HasFlag(cli_user(acptr)->server, FLAG_MAP))))
400 && ((!(matchsel & WHO_FIELD_HOS))
401 || matchexec(cli_user(acptr)->host, mymask, minlen))
402 && ((!(matchsel & WHO_FIELD_HOS))
403 || !HasHiddenHost(acptr)
405 || matchexec(cli_user(acptr)->realhost, mymask, minlen))
406 && ((!(matchsel & WHO_FIELD_REN))
407 || matchexec(cli_info(acptr), mymask, minlen))
408 && ((!(matchsel & WHO_FIELD_NIP))
409 || (HasHiddenHost(acptr) && !IsAnOper(sptr))
410 || ((((cli_ip(acptr).s_addr & imask.mask.s_addr) !=
411 imask.bits.s_addr)) || (imask.fall
412 && matchexec(ircd_ntoa((const char*) &(cli_ip(acptr))), mymask, minlen)))))
414 if (!SHOW_MORE(sptr, counter))
416 do_who(sptr, acptr, chptr, fields, qrt);
420 /* Loop through all clients :-\, if we still have something to match to
421 and we can show more clients */
422 if ((!(counter < 1)) && matchsel)
423 for (acptr = cli_prev(&me); acptr; acptr = cli_prev(acptr))
425 if (!(IsUser(acptr) && Process(acptr)))
427 if ((bitsel & WHOSELECT_OPER) &&
428 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
429 HasPriv(sptr, PRIV_SEE_OPERS))))
431 if (!(SEE_USER(sptr, acptr, bitsel)))
434 ((!(matchsel & WHO_FIELD_NIC))
435 || matchexec(cli_name(acptr), mymask, minlen))
436 && ((!(matchsel & WHO_FIELD_UID))
437 || matchexec(cli_user(acptr)->username, mymask, minlen))
438 && ((!(matchsel & WHO_FIELD_SER))
439 || (!(HasFlag(cli_user(acptr)->server, FLAG_MAP))))
440 && ((!(matchsel & WHO_FIELD_HOS))
441 || matchexec(cli_user(acptr)->host, mymask, minlen))
442 && ((!(matchsel & WHO_FIELD_HOS))
443 || !HasHiddenHost(acptr)
445 || matchexec(cli_user(acptr)->realhost, mymask, minlen))
446 && ((!(matchsel & WHO_FIELD_REN))
447 || matchexec(cli_info(acptr), mymask, minlen))
448 && ((!(matchsel & WHO_FIELD_NIP))
449 || (HasHiddenHost(acptr) && !IsAnOper(sptr))
450 || ((((cli_ip(acptr).s_addr & imask.mask.s_addr) != imask.bits.s_addr))
452 && matchexec(ircd_ntoa((const char*) &(cli_ip(acptr))), mymask, minlen)))))
454 if (!SHOW_MORE(sptr, counter))
456 do_who(sptr, acptr, 0, fields, qrt);
460 /* Make a clean mask suitable to be sent in the "end of" */
461 if (mask && (p = strchr(mask, ' ')))
463 send_reply(sptr, RPL_ENDOFWHO, BadPtr(mask) ? "*" : mask);
465 /* Notify the user if we decided that his query was too long */
467 send_reply(sptr, ERR_QUERYTOOLONG, "WHO");