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
84 * No need to include handlers.h here the signatures must match
85 * and we don't need to force a rebuild of all the handlers everytime
86 * we add a new one to the list. --Bleep
94 #include "ircd_chattr.h"
96 #include "ircd_reply.h"
97 #include "ircd_string.h"
100 #include "numnicks.h"
111 * A little spin-marking utility to tell us wich clients we have already
112 * processed and wich not
114 static int who_marker = 0;
115 static void move_marker(void)
119 struct Client *cptr = GlobalClientList;
122 cli_marker(cptr) = 0;
123 cptr = cli_next(cptr);
129 #define CheckMark(x, y) ((x == y) ? 0 : (x = y))
130 #define Process(cptr) CheckMark(cli_marker(cptr), who_marker)
133 * m_who - generic message handler
135 * parv[0] = sender prefix
136 * parv[1] = nickname mask list
137 * parv[2] = additional selection flag, only 'o' for now.
138 * and %flags to specify what fields to output
139 * plus a ,querytype if the t flag is specified
140 * so the final thing will be like o%tnchu,777
141 * parv[3] = _optional_ parameter that overrides parv[1]
142 * This can be used as "/quote who foo % :The Black Hacker
143 * to find me, parv[3] _can_ contain spaces !.
145 int m_who(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
147 char *mask; /* The mask we are looking for */
148 char ch; /* Scratch char register */
149 struct Channel *chptr; /* Channel to show */
150 struct Client *acptr; /* Client to show */
152 int bitsel; /* Mask of selectors to apply */
153 int matchsel; /* Wich fields the match should apply on */
154 int counter; /* Query size counter,
155 initially used to count fields */
156 int commas; /* Does our mask contain any comma ?
158 int fields; /* Mask of fields to show */
159 int isthere = 0; /* When this set the user is member of chptr */
160 char *nick; /* Single element extracted from
162 char *p; /* Scratch char pointer */
163 char *qrt; /* Pointer to the query type */
164 static char mymask[512]; /* To save the mask before corrupting it */
166 /* Let's find where is our mask, and if actually contains something */
167 mask = ((parc > 1) ? parv[1] : 0);
168 if (parc > 3 && parv[3])
170 if (mask && ((mask[0] == '\0') ||
171 (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
174 /* Evaluate the flags now, we consider the second parameter
175 as "matchFlags%fieldsToInclude,querytype" */
176 bitsel = fields = counter = matchsel = 0;
178 if (parc > 2 && parv[2] && *parv[2])
181 while (((ch = *(p++))) && (ch != '%') && (ch != ','))
186 bitsel |= WHOSELECT_OPER;
190 bitsel |= WHOSELECT_EXTRA;
191 if (HasPriv(sptr, PRIV_WHOX))
192 log_write(LS_WHO, L_INFO, LOG_NOSNOTICE, "%#C WHO %s %s", sptr,
193 (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
197 matchsel |= WHO_FIELD_NIC;
201 matchsel |= WHO_FIELD_UID;
205 matchsel |= WHO_FIELD_HOS;
209 matchsel |= WHO_FIELD_NIP;
213 #ifdef HEAD_IN_SAND_WHO_SERVERNAME
216 matchsel |= WHO_FIELD_SER;
220 matchsel |= WHO_FIELD_REN;
224 while ((ch = *p++) && (ch != ','))
231 fields |= WHO_FIELD_CHA;
235 fields |= WHO_FIELD_DIS;
239 fields |= WHO_FIELD_FLA;
243 fields |= WHO_FIELD_HOS;
247 fields |= WHO_FIELD_NIP;
251 fields |= WHO_FIELD_IDL;
254 fields |= WHO_FIELD_NIC;
258 fields |= WHO_FIELD_REN;
262 #ifdef HEAD_IN_SAND_WHO_SERVERNAME
265 fields |= WHO_FIELD_SER;
269 fields |= WHO_FIELD_QTY;
273 fields |= WHO_FIELD_UID;
284 matchsel = WHO_FIELD_DEF;
288 if (qrt && (fields & WHO_FIELD_QTY))
291 if (!((*p > '9') || (*p < '0')))
293 if (!((*p > '9') || (*p < '0')))
295 if (!((*p > '9') || (*p < '0')))
302 /* I'd love to add also a check on the number of matches fields per time */
303 counter = (2048 / (counter + 4));
304 if (mask && (strlen(mask) > 510))
307 commas = (mask && strchr(mask, ','));
309 /* First treat mask as a list of plain nicks/channels */
312 strcpy(mymask, mask);
313 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
314 nick = ircd_strtok(&p, 0, ","))
316 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
318 isthere = (find_channel_member(sptr, chptr) != 0);
319 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
321 struct Membership* member;
322 for (member = chptr->members; member; member = member->next_member)
324 acptr = member->user;
325 if ((bitsel & WHOSELECT_OPER) &&
326 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
327 HasPriv(sptr, PRIV_SEE_OPERS))))
329 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
331 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
333 if (!Process(acptr)) /* This can't be moved before other checks */
335 if (!(isthere || (SHOW_MORE(sptr, counter))))
337 do_who(sptr, acptr, chptr, fields, qrt);
343 if ((acptr = FindUser(nick)) &&
344 ((!(bitsel & WHOSELECT_OPER)) ||
345 (IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
346 HasPriv(sptr, PRIV_SEE_OPERS)))) &&
347 Process(acptr) && SHOW_MORE(sptr, counter))
349 do_who(sptr, acptr, 0, fields, qrt);
355 /* If we didn't have any comma in the mask treat it as a
356 real mask and try to match all relevant fields */
357 if (!(commas || (counter < 1)))
360 static struct in_mask imask;
363 matchcomp(mymask, &minlen, &cset, mask);
364 if (matchcompIP(&imask, mask))
365 matchsel &= ~WHO_FIELD_NIP;
366 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
367 matchsel &= ~WHO_FIELD_NIC;
368 if ((matchsel & WHO_FIELD_SER) &&
369 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
370 || (!markMatchexServer(mymask, minlen))))
371 matchsel &= ~WHO_FIELD_SER;
372 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
373 matchsel &= ~WHO_FIELD_UID;
374 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
375 matchsel &= ~WHO_FIELD_HOS;
378 /* First of all loop through the clients in common channels */
379 if ((!(counter < 1)) && matchsel) {
380 struct Membership* member;
381 struct Membership* chan;
382 for (chan = cli_user(sptr)->channel; chan; chan = chan->next_channel) {
383 chptr = chan->channel;
384 for (member = chptr->members; member; member = member->next_member)
386 acptr = member->user;
387 if (!(IsUser(acptr) && Process(acptr)))
388 continue; /* Now Process() is at the beginning, if we fail
389 we'll never have to show this acptr in this query */
390 if ((bitsel & WHOSELECT_OPER) &&
391 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
392 HasPriv(sptr, PRIV_SEE_OPERS))))
395 ((!(matchsel & WHO_FIELD_NIC))
396 || matchexec(cli_name(acptr), mymask, minlen))
397 && ((!(matchsel & WHO_FIELD_UID))
398 || matchexec(cli_user(acptr)->username, mymask, minlen))
399 && ((!(matchsel & WHO_FIELD_SER))
400 || (!(cli_flags(cli_user(acptr)->server) & FLAGS_MAP)))
401 && ((!(matchsel & WHO_FIELD_HOS))
402 || matchexec(cli_user(acptr)->host, mymask, minlen))
403 && ((!(matchsel & WHO_FIELD_REN))
404 || matchexec(cli_info(acptr), mymask, minlen))
405 && ((!(matchsel & WHO_FIELD_NIP))
406 || ((((cli_ip(acptr).s_addr & imask.mask.s_addr) !=
407 imask.bits.s_addr)) || (imask.fall
408 && matchexec(ircd_ntoa((const char*) &(cli_ip(acptr))), mymask, minlen)))))
410 if (!SHOW_MORE(sptr, counter))
412 do_who(sptr, acptr, chptr, fields, qrt);
416 /* Loop through all clients :-\, if we still have something to match to
417 and we can show more clients */
418 if ((!(counter < 1)) && matchsel)
419 for (acptr = cli_prev(&me); acptr; acptr = cli_prev(acptr))
421 if (!(IsUser(acptr) && Process(acptr)))
423 if ((bitsel & WHOSELECT_OPER) &&
424 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
425 HasPriv(sptr, PRIV_SEE_OPERS))))
427 if (!(SEE_USER(sptr, acptr, bitsel)))
430 ((!(matchsel & WHO_FIELD_NIC))
431 || matchexec(cli_name(acptr), mymask, minlen))
432 && ((!(matchsel & WHO_FIELD_UID))
433 || matchexec(cli_user(acptr)->username, mymask, minlen))
434 && ((!(matchsel & WHO_FIELD_SER))
435 || (!(cli_flags(cli_user(acptr)->server) & FLAGS_MAP)))
436 && ((!(matchsel & WHO_FIELD_HOS))
437 || matchexec(cli_user(acptr)->host, mymask, minlen))
438 && ((!(matchsel & WHO_FIELD_REN))
439 || matchexec(cli_info(acptr), mymask, minlen))
440 && ((!(matchsel & WHO_FIELD_NIP))
441 || ((((cli_ip(acptr).s_addr & imask.mask.s_addr) != imask.bits.s_addr))
443 && matchexec(ircd_ntoa((const char*) &(cli_ip(acptr))), mymask, minlen)))))
445 if (!SHOW_MORE(sptr, counter))
447 do_who(sptr, acptr, 0, fields, qrt);
451 /* Make a clean mask suitable to be sent in the "end of" */
452 if (mask && (p = strchr(mask, ' ')))
454 send_reply(sptr, RPL_ENDOFWHO, BadPtr(mask) ? "*" : mask);
456 /* Notify the user if we decided that his query was too long */
458 send_reply(sptr, ERR_QUERYTOOLONG, "WHO");
468 * parv[0] = sender prefix
469 * parv[1] = nickname mask list
470 * parv[2] = additional selection flag, only 'o' for now.
471 * and %flags to specify what fields to output
472 * plus a ,querytype if the t flag is specified
473 * so the final thing will be like o%tnchu,777
474 * parv[3] = _optional_ parameter that overrides parv[1]
475 * This can be used as "/quote who foo % :The Black Hacker
476 * to find me, parv[3] _can_ contain spaces !.
479 int m_who(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
481 char *mask; /* The mask we are looking for */
482 char ch; /* Scratch char register */
483 struct Channel *chptr; /* Channel to show */
484 struct Client *acptr; /* Client to show */
486 int bitsel; /* Mask of selectors to apply */
487 int matchsel; /* Wich fields the match should apply on */
488 int counter; /* Query size counter,
489 initially used to count fields */
490 int commas; /* Does our mask contain any comma ?
492 int fields; /* Mask of fields to show */
493 int isthere = 0; /* When this set the user is member of chptr */
494 char *nick; /* Single element extracted from
496 char *p; /* Scratch char pointer */
497 char *qrt; /* Pointer to the query type */
498 static char mymask[512]; /* To save the mask before corrupting it */
500 /* Let's find where is our mask, and if actually contains something */
501 mask = ((parc > 1) ? parv[1] : 0);
502 if (parc > 3 && parv[3])
504 if (mask && ((mask[0] == '\0') ||
505 (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
508 /* Evaluate the flags now, we consider the second parameter
509 as "matchFlags%fieldsToInclude,querytype" */
510 bitsel = fields = counter = matchsel = 0;
512 if (parc > 2 && parv[2] && *parv[2])
515 while (((ch = *(p++))) && (ch != '%') && (ch != ','))
520 bitsel |= WHOSELECT_OPER;
524 bitsel |= WHOSELECT_EXTRA;
527 write_log(WPATH, "# " TIME_T_FMT " %s!%s@%s WHO %s %s\n", /* XXX DEAD */
528 CurrentTime, sptr->name, sptr->user->username, sptr->user->host,
529 (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
534 matchsel |= WHO_FIELD_NIC;
538 matchsel |= WHO_FIELD_UID;
542 matchsel |= WHO_FIELD_HOS;
546 matchsel |= WHO_FIELD_NIP;
550 matchsel |= WHO_FIELD_SER;
554 matchsel |= WHO_FIELD_REN;
558 while ((ch = *p++) && (ch != ','))
565 fields |= WHO_FIELD_CHA;
569 fields |= WHO_FIELD_DIS;
573 fields |= WHO_FIELD_FLA;
577 fields |= WHO_FIELD_HOS;
581 fields |= WHO_FIELD_NIP;
585 fields |= WHO_FIELD_NIC;
589 fields |= WHO_FIELD_REN;
593 fields |= WHO_FIELD_SER;
597 fields |= WHO_FIELD_QTY;
601 fields |= WHO_FIELD_UID;
612 matchsel = WHO_FIELD_DEF;
616 if (qrt && (fields & WHO_FIELD_QTY))
619 if (!((*p > '9') || (*p < '0')))
621 if (!((*p > '9') || (*p < '0')))
623 if (!((*p > '9') || (*p < '0')))
630 /* I'd love to add also a check on the number of matches fields per time */
631 counter = (2048 / (counter + 4));
632 if (mask && (strlen(mask) > 510))
635 commas = (mask && strchr(mask, ','));
637 /* First treat mask as a list of plain nicks/channels */
640 strcpy(mymask, mask);
641 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
642 nick = ircd_strtok(&p, 0, ","))
644 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
646 isthere = (find_channel_member(sptr, chptr) != 0);
647 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
649 struct Membership* member;
650 for (member = chptr->members; member; member = member->next_member)
652 acptr = member->user;
653 if ((bitsel & WHOSELECT_OPER) && !(IsAnOper(acptr)))
655 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
657 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
659 if (!Process(acptr)) /* This can't be moved before other checks */
661 if (!(isthere || (SHOW_MORE(sptr, counter))))
663 do_who(sptr, acptr, chptr, fields, qrt);
669 if ((acptr = FindUser(nick)) &&
670 ((!(bitsel & WHOSELECT_OPER)) || IsAnOper(acptr)) &&
671 Process(acptr) && SHOW_MORE(sptr, counter))
673 do_who(sptr, acptr, 0, fields, qrt);
679 /* If we didn't have any comma in the mask treat it as a
680 real mask and try to match all relevant fields */
681 if (!(commas || (counter < 1)))
684 static struct in_mask imask;
687 matchcomp(mymask, &minlen, &cset, mask);
688 if (matchcompIP(&imask, mask))
689 matchsel &= ~WHO_FIELD_NIP;
690 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
691 matchsel &= ~WHO_FIELD_NIC;
692 if ((matchsel & WHO_FIELD_SER) &&
693 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
694 || (!markMatchexServer(mymask, minlen))))
695 matchsel &= ~WHO_FIELD_SER;
696 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
697 matchsel &= ~WHO_FIELD_UID;
698 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
699 matchsel &= ~WHO_FIELD_HOS;
702 /* First of all loop through the clients in common channels */
703 if ((!(counter < 1)) && matchsel) {
704 struct Membership* member;
705 struct Membership* chan;
706 for (chan = sptr->user->channel; chan; chan = chan->next_channel) {
707 chptr = chan->channel;
708 for (member = chptr->members; member; member = member->next_member)
710 acptr = member->user;
711 if (!(IsUser(acptr) && Process(acptr)))
712 continue; /* Now Process() is at the beginning, if we fail
713 we'll never have to show this acptr in this query */
714 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
717 ((!(matchsel & WHO_FIELD_NIC))
718 || matchexec(acptr->name, mymask, minlen))
719 && ((!(matchsel & WHO_FIELD_UID))
720 || matchexec(acptr->user->username, mymask, minlen))
721 && ((!(matchsel & WHO_FIELD_SER))
722 || (!(acptr->user->server->flags & FLAGS_MAP)))
723 && ((!(matchsel & WHO_FIELD_HOS))
724 || matchexec(acptr->user->host, mymask, minlen))
725 && ((!(matchsel & WHO_FIELD_REN))
726 || matchexec(acptr->info, mymask, minlen))
727 && ((!(matchsel & WHO_FIELD_NIP))
728 || ((((acptr->ip.s_addr & imask.mask.s_addr) !=
729 imask.bits.s_addr)) || (imask.fall
730 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
732 if (!SHOW_MORE(sptr, counter))
734 do_who(sptr, acptr, chptr, fields, qrt);
738 /* Loop through all clients :-\, if we still have something to match to
739 and we can show more clients */
740 if ((!(counter < 1)) && matchsel)
741 for (acptr = me.prev; acptr; acptr = acptr->prev)
743 if (!(IsUser(acptr) && Process(acptr)))
745 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
747 if (!(SEE_USER(sptr, acptr, bitsel)))
750 ((!(matchsel & WHO_FIELD_NIC))
751 || matchexec(acptr->name, mymask, minlen))
752 && ((!(matchsel & WHO_FIELD_UID))
753 || matchexec(acptr->user->username, mymask, minlen))
754 && ((!(matchsel & WHO_FIELD_SER))
755 || (!(acptr->user->server->flags & FLAGS_MAP)))
756 && ((!(matchsel & WHO_FIELD_HOS))
757 || matchexec(acptr->user->host, mymask, minlen))
758 && ((!(matchsel & WHO_FIELD_REN))
759 || matchexec(acptr->info, mymask, minlen))
760 && ((!(matchsel & WHO_FIELD_NIP))
761 || ((((acptr->ip.s_addr & imask.mask.s_addr) != imask.bits.s_addr))
763 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
765 if (!SHOW_MORE(sptr, counter))
767 do_who(sptr, acptr, 0, fields, qrt);
771 /* Make a clean mask suitable to be sent in the "end of" */
772 if (mask && (p = strchr(mask, ' ')))
774 sendto_one(sptr, rpl_str(RPL_ENDOFWHO), /* XXX DEAD */
775 me.name, parv[0], BadPtr(mask) ? "*" : mask);
777 /* Notify the user if we decided that his query was too long */
779 sendto_one(sptr, err_str(ERR_QUERYTOOLONG), me.name, parv[0], "WHO"); /* XXX DEAD */