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
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
96 #include "ircd_chattr.h"
98 #include "ircd_policy.h"
99 #include "ircd_reply.h"
100 #include "ircd_string.h"
103 #include "numnicks.h"
114 * A little spin-marking utility to tell us wich clients we have already
115 * processed and wich not
117 static int who_marker = 0;
118 static void move_marker(void)
122 struct Client *cptr = GlobalClientList;
125 cli_marker(cptr) = 0;
126 cptr = cli_next(cptr);
132 #define CheckMark(x, y) ((x == y) ? 0 : (x = y))
133 #define Process(cptr) CheckMark(cli_marker(cptr), who_marker)
136 * m_who - generic message handler
138 * parv[0] = sender prefix
139 * parv[1] = nickname mask list
140 * parv[2] = additional selection flag, only 'o' for now.
141 * and %flags to specify what fields to output
142 * plus a ,querytype if the t flag is specified
143 * so the final thing will be like o%tnchu,777
144 * parv[3] = _optional_ parameter that overrides parv[1]
145 * This can be used as "/quote who foo % :The Black Hacker
146 * to find me, parv[3] _can_ contain spaces !.
148 int m_who(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
150 char *mask; /* The mask we are looking for */
151 char ch; /* Scratch char register */
152 struct Channel *chptr; /* Channel to show */
153 struct Client *acptr; /* Client to show */
155 int bitsel; /* Mask of selectors to apply */
156 int matchsel; /* Wich fields the match should apply on */
157 int counter; /* Query size counter,
158 initially used to count fields */
159 int commas; /* Does our mask contain any comma ?
161 int fields; /* Mask of fields to show */
162 int isthere = 0; /* When this set the user is member of chptr */
163 char *nick; /* Single element extracted from
165 char *p; /* Scratch char pointer */
166 char *qrt; /* Pointer to the query type */
167 static char mymask[512]; /* To save the mask before corrupting it */
169 /* Let's find where is our mask, and if actually contains something */
170 mask = ((parc > 1) ? parv[1] : 0);
171 if (parc > 3 && parv[3])
173 if (mask && ((mask[0] == '\0') ||
174 (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
177 /* Evaluate the flags now, we consider the second parameter
178 as "matchFlags%fieldsToInclude,querytype" */
179 bitsel = fields = counter = matchsel = 0;
181 if (parc > 2 && parv[2] && *parv[2])
184 while (((ch = *(p++))) && (ch != '%') && (ch != ','))
189 bitsel |= WHOSELECT_OPER;
193 bitsel |= WHOSELECT_EXTRA;
194 if (HasPriv(sptr, PRIV_WHOX))
195 log_write(LS_WHO, L_INFO, LOG_NOSNOTICE, "%#C WHO %s %s", sptr,
196 (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
200 matchsel |= WHO_FIELD_NIC;
204 matchsel |= WHO_FIELD_UID;
208 matchsel |= WHO_FIELD_HOS;
212 matchsel |= WHO_FIELD_NIP;
216 #ifdef HEAD_IN_SAND_WHO_SERVERNAME
219 matchsel |= WHO_FIELD_SER;
223 matchsel |= WHO_FIELD_REN;
227 while ((ch = *p++) && (ch != ','))
234 fields |= WHO_FIELD_CHA;
238 fields |= WHO_FIELD_DIS;
242 fields |= WHO_FIELD_FLA;
246 fields |= WHO_FIELD_HOS;
250 fields |= WHO_FIELD_NIP;
254 fields |= WHO_FIELD_IDL;
257 fields |= WHO_FIELD_NIC;
261 fields |= WHO_FIELD_REN;
265 #ifdef HEAD_IN_SAND_WHO_SERVERNAME
268 fields |= WHO_FIELD_SER;
272 fields |= WHO_FIELD_QTY;
276 fields |= WHO_FIELD_UID;
287 matchsel = WHO_FIELD_DEF;
291 if (qrt && (fields & WHO_FIELD_QTY))
294 if (!((*p > '9') || (*p < '0')))
296 if (!((*p > '9') || (*p < '0')))
298 if (!((*p > '9') || (*p < '0')))
305 /* I'd love to add also a check on the number of matches fields per time */
306 counter = (2048 / (counter + 4));
307 if (mask && (strlen(mask) > 510))
310 commas = (mask && strchr(mask, ','));
312 /* First treat mask as a list of plain nicks/channels */
315 strcpy(mymask, mask);
316 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
317 nick = ircd_strtok(&p, 0, ","))
319 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
321 isthere = (find_channel_member(sptr, chptr) != 0);
322 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
324 struct Membership* member;
325 for (member = chptr->members; member; member = member->next_member)
327 acptr = member->user;
328 if ((bitsel & WHOSELECT_OPER) &&
329 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
330 HasPriv(sptr, PRIV_SEE_OPERS))))
332 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
334 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
336 if (!Process(acptr)) /* This can't be moved before other checks */
338 if (!(isthere || (SHOW_MORE(sptr, counter))))
340 do_who(sptr, acptr, chptr, fields, qrt);
346 if ((acptr = FindUser(nick)) &&
347 ((!(bitsel & WHOSELECT_OPER)) ||
348 (IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
349 HasPriv(sptr, PRIV_SEE_OPERS)))) &&
350 Process(acptr) && SHOW_MORE(sptr, counter))
352 do_who(sptr, acptr, 0, fields, qrt);
358 /* If we didn't have any comma in the mask treat it as a
359 real mask and try to match all relevant fields */
360 if (!(commas || (counter < 1)))
363 static struct in_mask imask;
366 matchcomp(mymask, &minlen, &cset, mask);
367 if (matchcompIP(&imask, mask))
368 matchsel &= ~WHO_FIELD_NIP;
369 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
370 matchsel &= ~WHO_FIELD_NIC;
371 if ((matchsel & WHO_FIELD_SER) &&
372 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
373 || (!markMatchexServer(mymask, minlen))))
374 matchsel &= ~WHO_FIELD_SER;
375 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
376 matchsel &= ~WHO_FIELD_UID;
377 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
378 matchsel &= ~WHO_FIELD_HOS;
381 /* First of all loop through the clients in common channels */
382 if ((!(counter < 1)) && matchsel) {
383 struct Membership* member;
384 struct Membership* chan;
385 for (chan = cli_user(sptr)->channel; chan; chan = chan->next_channel) {
386 chptr = chan->channel;
387 for (member = chptr->members; member; member = member->next_member)
389 acptr = member->user;
390 if (!(IsUser(acptr) && Process(acptr)))
391 continue; /* Now Process() is at the beginning, if we fail
392 we'll never have to show this acptr in this query */
393 if ((bitsel & WHOSELECT_OPER) &&
394 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
395 HasPriv(sptr, PRIV_SEE_OPERS))))
398 ((!(matchsel & WHO_FIELD_NIC))
399 || matchexec(cli_name(acptr), mymask, minlen))
400 && ((!(matchsel & WHO_FIELD_UID))
401 || matchexec(cli_user(acptr)->username, mymask, minlen))
402 && ((!(matchsel & WHO_FIELD_SER))
403 || (!(cli_flags(cli_user(acptr)->server) & FLAGS_MAP)))
404 && ((!(matchsel & WHO_FIELD_HOS))
405 || matchexec(cli_user(acptr)->host, mymask, minlen))
406 && ((!(matchsel & WHO_FIELD_REN))
407 || matchexec(cli_info(acptr), mymask, minlen))
408 && ((!(matchsel & WHO_FIELD_NIP))
409 || ((((cli_ip(acptr).s_addr & imask.mask.s_addr) !=
410 imask.bits.s_addr)) || (imask.fall
411 && matchexec(ircd_ntoa((const char*) &(cli_ip(acptr))), mymask, minlen)))))
413 if (!SHOW_MORE(sptr, counter))
415 do_who(sptr, acptr, chptr, fields, qrt);
419 /* Loop through all clients :-\, if we still have something to match to
420 and we can show more clients */
421 if ((!(counter < 1)) && matchsel)
422 for (acptr = cli_prev(&me); acptr; acptr = cli_prev(acptr))
424 if (!(IsUser(acptr) && Process(acptr)))
426 if ((bitsel & WHOSELECT_OPER) &&
427 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
428 HasPriv(sptr, PRIV_SEE_OPERS))))
430 if (!(SEE_USER(sptr, acptr, bitsel)))
433 ((!(matchsel & WHO_FIELD_NIC))
434 || matchexec(cli_name(acptr), mymask, minlen))
435 && ((!(matchsel & WHO_FIELD_UID))
436 || matchexec(cli_user(acptr)->username, mymask, minlen))
437 && ((!(matchsel & WHO_FIELD_SER))
438 || (!(cli_flags(cli_user(acptr)->server) & FLAGS_MAP)))
439 && ((!(matchsel & WHO_FIELD_HOS))
440 || matchexec(cli_user(acptr)->host, mymask, minlen))
441 && ((!(matchsel & WHO_FIELD_REN))
442 || matchexec(cli_info(acptr), mymask, minlen))
443 && ((!(matchsel & WHO_FIELD_NIP))
444 || ((((cli_ip(acptr).s_addr & imask.mask.s_addr) != imask.bits.s_addr))
446 && matchexec(ircd_ntoa((const char*) &(cli_ip(acptr))), mymask, minlen)))))
448 if (!SHOW_MORE(sptr, counter))
450 do_who(sptr, acptr, 0, fields, qrt);
454 /* Make a clean mask suitable to be sent in the "end of" */
455 if (mask && (p = strchr(mask, ' ')))
457 send_reply(sptr, RPL_ENDOFWHO, BadPtr(mask) ? "*" : mask);
459 /* Notify the user if we decided that his query was too long */
461 send_reply(sptr, ERR_QUERYTOOLONG, "WHO");
471 * parv[0] = sender prefix
472 * parv[1] = nickname mask list
473 * parv[2] = additional selection flag, only 'o' for now.
474 * and %flags to specify what fields to output
475 * plus a ,querytype if the t flag is specified
476 * so the final thing will be like o%tnchu,777
477 * parv[3] = _optional_ parameter that overrides parv[1]
478 * This can be used as "/quote who foo % :The Black Hacker
479 * to find me, parv[3] _can_ contain spaces !.
482 int m_who(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
484 char *mask; /* The mask we are looking for */
485 char ch; /* Scratch char register */
486 struct Channel *chptr; /* Channel to show */
487 struct Client *acptr; /* Client to show */
489 int bitsel; /* Mask of selectors to apply */
490 int matchsel; /* Wich fields the match should apply on */
491 int counter; /* Query size counter,
492 initially used to count fields */
493 int commas; /* Does our mask contain any comma ?
495 int fields; /* Mask of fields to show */
496 int isthere = 0; /* When this set the user is member of chptr */
497 char *nick; /* Single element extracted from
499 char *p; /* Scratch char pointer */
500 char *qrt; /* Pointer to the query type */
501 static char mymask[512]; /* To save the mask before corrupting it */
503 /* Let's find where is our mask, and if actually contains something */
504 mask = ((parc > 1) ? parv[1] : 0);
505 if (parc > 3 && parv[3])
507 if (mask && ((mask[0] == '\0') ||
508 (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
511 /* Evaluate the flags now, we consider the second parameter
512 as "matchFlags%fieldsToInclude,querytype" */
513 bitsel = fields = counter = matchsel = 0;
515 if (parc > 2 && parv[2] && *parv[2])
518 while (((ch = *(p++))) && (ch != '%') && (ch != ','))
523 bitsel |= WHOSELECT_OPER;
527 bitsel |= WHOSELECT_EXTRA;
530 write_log(WPATH, "# " TIME_T_FMT " %s!%s@%s WHO %s %s\n", /* XXX DEAD */
531 CurrentTime, sptr->name, sptr->user->username, sptr->user->host,
532 (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
537 matchsel |= WHO_FIELD_NIC;
541 matchsel |= WHO_FIELD_UID;
545 matchsel |= WHO_FIELD_HOS;
549 matchsel |= WHO_FIELD_NIP;
553 matchsel |= WHO_FIELD_SER;
557 matchsel |= WHO_FIELD_REN;
561 while ((ch = *p++) && (ch != ','))
568 fields |= WHO_FIELD_CHA;
572 fields |= WHO_FIELD_DIS;
576 fields |= WHO_FIELD_FLA;
580 fields |= WHO_FIELD_HOS;
584 fields |= WHO_FIELD_NIP;
588 fields |= WHO_FIELD_NIC;
592 fields |= WHO_FIELD_REN;
596 fields |= WHO_FIELD_SER;
600 fields |= WHO_FIELD_QTY;
604 fields |= WHO_FIELD_UID;
615 matchsel = WHO_FIELD_DEF;
619 if (qrt && (fields & WHO_FIELD_QTY))
622 if (!((*p > '9') || (*p < '0')))
624 if (!((*p > '9') || (*p < '0')))
626 if (!((*p > '9') || (*p < '0')))
633 /* I'd love to add also a check on the number of matches fields per time */
634 counter = (2048 / (counter + 4));
635 if (mask && (strlen(mask) > 510))
638 commas = (mask && strchr(mask, ','));
640 /* First treat mask as a list of plain nicks/channels */
643 strcpy(mymask, mask);
644 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
645 nick = ircd_strtok(&p, 0, ","))
647 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
649 isthere = (find_channel_member(sptr, chptr) != 0);
650 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
652 struct Membership* member;
653 for (member = chptr->members; member; member = member->next_member)
655 acptr = member->user;
656 if ((bitsel & WHOSELECT_OPER) && !(IsAnOper(acptr)))
658 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
660 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
662 if (!Process(acptr)) /* This can't be moved before other checks */
664 if (!(isthere || (SHOW_MORE(sptr, counter))))
666 do_who(sptr, acptr, chptr, fields, qrt);
672 if ((acptr = FindUser(nick)) &&
673 ((!(bitsel & WHOSELECT_OPER)) || IsAnOper(acptr)) &&
674 Process(acptr) && SHOW_MORE(sptr, counter))
676 do_who(sptr, acptr, 0, fields, qrt);
682 /* If we didn't have any comma in the mask treat it as a
683 real mask and try to match all relevant fields */
684 if (!(commas || (counter < 1)))
687 static struct in_mask imask;
690 matchcomp(mymask, &minlen, &cset, mask);
691 if (matchcompIP(&imask, mask))
692 matchsel &= ~WHO_FIELD_NIP;
693 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
694 matchsel &= ~WHO_FIELD_NIC;
695 if ((matchsel & WHO_FIELD_SER) &&
696 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
697 || (!markMatchexServer(mymask, minlen))))
698 matchsel &= ~WHO_FIELD_SER;
699 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
700 matchsel &= ~WHO_FIELD_UID;
701 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
702 matchsel &= ~WHO_FIELD_HOS;
705 /* First of all loop through the clients in common channels */
706 if ((!(counter < 1)) && matchsel) {
707 struct Membership* member;
708 struct Membership* chan;
709 for (chan = sptr->user->channel; chan; chan = chan->next_channel) {
710 chptr = chan->channel;
711 for (member = chptr->members; member; member = member->next_member)
713 acptr = member->user;
714 if (!(IsUser(acptr) && Process(acptr)))
715 continue; /* Now Process() is at the beginning, if we fail
716 we'll never have to show this acptr in this query */
717 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
720 ((!(matchsel & WHO_FIELD_NIC))
721 || matchexec(acptr->name, mymask, minlen))
722 && ((!(matchsel & WHO_FIELD_UID))
723 || matchexec(acptr->user->username, mymask, minlen))
724 && ((!(matchsel & WHO_FIELD_SER))
725 || (!(acptr->user->server->flags & FLAGS_MAP)))
726 && ((!(matchsel & WHO_FIELD_HOS))
727 || matchexec(acptr->user->host, mymask, minlen))
728 && ((!(matchsel & WHO_FIELD_REN))
729 || matchexec(acptr->info, mymask, minlen))
730 && ((!(matchsel & WHO_FIELD_NIP))
731 || ((((acptr->ip.s_addr & imask.mask.s_addr) !=
732 imask.bits.s_addr)) || (imask.fall
733 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
735 if (!SHOW_MORE(sptr, counter))
737 do_who(sptr, acptr, chptr, fields, qrt);
741 /* Loop through all clients :-\, if we still have something to match to
742 and we can show more clients */
743 if ((!(counter < 1)) && matchsel)
744 for (acptr = me.prev; acptr; acptr = acptr->prev)
746 if (!(IsUser(acptr) && Process(acptr)))
748 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
750 if (!(SEE_USER(sptr, acptr, bitsel)))
753 ((!(matchsel & WHO_FIELD_NIC))
754 || matchexec(acptr->name, mymask, minlen))
755 && ((!(matchsel & WHO_FIELD_UID))
756 || matchexec(acptr->user->username, mymask, minlen))
757 && ((!(matchsel & WHO_FIELD_SER))
758 || (!(acptr->user->server->flags & FLAGS_MAP)))
759 && ((!(matchsel & WHO_FIELD_HOS))
760 || matchexec(acptr->user->host, mymask, minlen))
761 && ((!(matchsel & WHO_FIELD_REN))
762 || matchexec(acptr->info, mymask, minlen))
763 && ((!(matchsel & WHO_FIELD_NIP))
764 || ((((acptr->ip.s_addr & imask.mask.s_addr) != imask.bits.s_addr))
766 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
768 if (!SHOW_MORE(sptr, counter))
770 do_who(sptr, acptr, 0, fields, qrt);
774 /* Make a clean mask suitable to be sent in the "end of" */
775 if (mask && (p = strchr(mask, ' ')))
777 sendto_one(sptr, rpl_str(RPL_ENDOFWHO), /* XXX DEAD */
778 me.name, parv[0], BadPtr(mask) ? "*" : mask);
780 /* Notify the user if we decided that his query was too long */
782 sendto_one(sptr, err_str(ERR_QUERYTOOLONG), me.name, parv[0], "WHO"); /* XXX DEAD */