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;
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 matchsel |= WHO_FIELD_SER;
217 matchsel |= WHO_FIELD_REN;
221 while ((ch = *p++) && (ch != ','))
228 fields |= WHO_FIELD_CHA;
232 fields |= WHO_FIELD_DIS;
236 fields |= WHO_FIELD_FLA;
240 fields |= WHO_FIELD_HOS;
244 fields |= WHO_FIELD_NIP;
248 fields |= WHO_FIELD_IDL;
251 fields |= WHO_FIELD_NIC;
255 fields |= WHO_FIELD_REN;
259 fields |= WHO_FIELD_SER;
263 fields |= WHO_FIELD_QTY;
267 fields |= WHO_FIELD_UID;
278 matchsel = WHO_FIELD_DEF;
282 if (qrt && (fields & WHO_FIELD_QTY))
285 if (!((*p > '9') || (*p < '0')))
287 if (!((*p > '9') || (*p < '0')))
289 if (!((*p > '9') || (*p < '0')))
296 /* I'd love to add also a check on the number of matches fields per time */
297 counter = (2048 / (counter + 4));
298 if (mask && (strlen(mask) > 510))
301 commas = (mask && strchr(mask, ','));
303 /* First treat mask as a list of plain nicks/channels */
306 strcpy(mymask, mask);
307 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
308 nick = ircd_strtok(&p, 0, ","))
310 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
312 isthere = (find_channel_member(sptr, chptr) != 0);
313 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
315 struct Membership* member;
316 for (member = chptr->members; member; member = member->next_member)
318 acptr = member->user;
319 if ((bitsel & WHOSELECT_OPER) && !(IsAnOper(acptr)))
321 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
323 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
325 if (!Process(acptr)) /* This can't be moved before other checks */
327 if (!(isthere || (SHOW_MORE(sptr, counter))))
329 do_who(sptr, acptr, chptr, fields, qrt);
335 if ((acptr = FindUser(nick)) &&
336 ((!(bitsel & WHOSELECT_OPER)) || IsAnOper(acptr)) &&
337 Process(acptr) && SHOW_MORE(sptr, counter))
339 do_who(sptr, acptr, 0, fields, qrt);
345 /* If we didn't have any comma in the mask treat it as a
346 real mask and try to match all relevant fields */
347 if (!(commas || (counter < 1)))
350 static struct in_mask imask;
353 matchcomp(mymask, &minlen, &cset, mask);
354 if (matchcompIP(&imask, mask))
355 matchsel &= ~WHO_FIELD_NIP;
356 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
357 matchsel &= ~WHO_FIELD_NIC;
358 if ((matchsel & WHO_FIELD_SER) &&
359 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
360 || (!markMatchexServer(mymask, minlen))))
361 matchsel &= ~WHO_FIELD_SER;
362 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
363 matchsel &= ~WHO_FIELD_UID;
364 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
365 matchsel &= ~WHO_FIELD_HOS;
368 /* First of all loop through the clients in common channels */
369 if ((!(counter < 1)) && matchsel) {
370 struct Membership* member;
371 struct Membership* chan;
372 for (chan = cli_user(sptr)->channel; chan; chan = chan->next_channel) {
373 chptr = chan->channel;
374 for (member = chptr->members; member; member = member->next_member)
376 acptr = member->user;
377 if (!(IsUser(acptr) && Process(acptr)))
378 continue; /* Now Process() is at the beginning, if we fail
379 we'll never have to show this acptr in this query */
380 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
383 ((!(matchsel & WHO_FIELD_NIC))
384 || matchexec(cli_name(acptr), mymask, minlen))
385 && ((!(matchsel & WHO_FIELD_UID))
386 || matchexec(cli_user(acptr)->username, mymask, minlen))
387 && ((!(matchsel & WHO_FIELD_SER))
388 || (!(cli_flags(cli_user(acptr)->server) & FLAGS_MAP)))
389 && ((!(matchsel & WHO_FIELD_HOS))
390 || matchexec(cli_user(acptr)->host, mymask, minlen))
391 && ((!(matchsel & WHO_FIELD_REN))
392 || matchexec(cli_info(acptr), mymask, minlen))
393 && ((!(matchsel & WHO_FIELD_NIP))
394 || ((((cli_ip(acptr).s_addr & imask.mask.s_addr) !=
395 imask.bits.s_addr)) || (imask.fall
396 && matchexec(ircd_ntoa((const char*) &(cli_ip(acptr))), mymask, minlen)))))
398 if (!SHOW_MORE(sptr, counter))
400 do_who(sptr, acptr, chptr, fields, qrt);
404 /* Loop through all clients :-\, if we still have something to match to
405 and we can show more clients */
406 if ((!(counter < 1)) && matchsel)
407 for (acptr = cli_prev(&me); acptr; acptr = cli_prev(acptr))
409 if (!(IsUser(acptr) && Process(acptr)))
411 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
413 if (!(SEE_USER(sptr, acptr, bitsel)))
416 ((!(matchsel & WHO_FIELD_NIC))
417 || matchexec(cli_name(acptr), mymask, minlen))
418 && ((!(matchsel & WHO_FIELD_UID))
419 || matchexec(cli_user(acptr)->username, mymask, minlen))
420 && ((!(matchsel & WHO_FIELD_SER))
421 || (!(cli_flags(cli_user(acptr)->server) & FLAGS_MAP)))
422 && ((!(matchsel & WHO_FIELD_HOS))
423 || matchexec(cli_user(acptr)->host, mymask, minlen))
424 && ((!(matchsel & WHO_FIELD_REN))
425 || matchexec(cli_info(acptr), mymask, minlen))
426 && ((!(matchsel & WHO_FIELD_NIP))
427 || ((((cli_ip(acptr).s_addr & imask.mask.s_addr) != imask.bits.s_addr))
429 && matchexec(ircd_ntoa((const char*) &(cli_ip(acptr))), mymask, minlen)))))
431 if (!SHOW_MORE(sptr, counter))
433 do_who(sptr, acptr, 0, fields, qrt);
437 /* Make a clean mask suitable to be sent in the "end of" */
438 if (mask && (p = strchr(mask, ' ')))
440 send_reply(sptr, RPL_ENDOFWHO, BadPtr(mask) ? "*" : mask);
442 /* Notify the user if we decided that his query was too long */
444 send_reply(sptr, ERR_QUERYTOOLONG, "WHO");
454 * parv[0] = sender prefix
455 * parv[1] = nickname mask list
456 * parv[2] = additional selection flag, only 'o' for now.
457 * and %flags to specify what fields to output
458 * plus a ,querytype if the t flag is specified
459 * so the final thing will be like o%tnchu,777
460 * parv[3] = _optional_ parameter that overrides parv[1]
461 * This can be used as "/quote who foo % :The Black Hacker
462 * to find me, parv[3] _can_ contain spaces !.
465 int m_who(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
467 char *mask; /* The mask we are looking for */
468 char ch; /* Scratch char register */
469 struct Channel *chptr; /* Channel to show */
470 struct Client *acptr; /* Client to show */
472 int bitsel; /* Mask of selectors to apply */
473 int matchsel; /* Wich fields the match should apply on */
474 int counter; /* Query size counter,
475 initially used to count fields */
476 int commas; /* Does our mask contain any comma ?
478 int fields; /* Mask of fields to show */
479 int isthere = 0; /* When this set the user is member of chptr */
480 char *nick; /* Single element extracted from
482 char *p; /* Scratch char pointer */
483 char *qrt; /* Pointer to the query type */
484 static char mymask[512]; /* To save the mask before corrupting it */
486 /* Let's find where is our mask, and if actually contains something */
487 mask = ((parc > 1) ? parv[1] : 0);
488 if (parc > 3 && parv[3])
490 if (mask && ((mask[0] == '\0') ||
491 (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
494 /* Evaluate the flags now, we consider the second parameter
495 as "matchFlags%fieldsToInclude,querytype" */
496 bitsel = fields = counter = matchsel = 0;
498 if (parc > 2 && parv[2] && *parv[2])
501 while (((ch = *(p++))) && (ch != '%') && (ch != ','))
506 bitsel |= WHOSELECT_OPER;
510 bitsel |= WHOSELECT_EXTRA;
513 write_log(WPATH, "# " TIME_T_FMT " %s!%s@%s WHO %s %s\n", /* XXX DEAD */
514 CurrentTime, sptr->name, sptr->user->username, sptr->user->host,
515 (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
520 matchsel |= WHO_FIELD_NIC;
524 matchsel |= WHO_FIELD_UID;
528 matchsel |= WHO_FIELD_HOS;
532 matchsel |= WHO_FIELD_NIP;
536 matchsel |= WHO_FIELD_SER;
540 matchsel |= WHO_FIELD_REN;
544 while ((ch = *p++) && (ch != ','))
551 fields |= WHO_FIELD_CHA;
555 fields |= WHO_FIELD_DIS;
559 fields |= WHO_FIELD_FLA;
563 fields |= WHO_FIELD_HOS;
567 fields |= WHO_FIELD_NIP;
571 fields |= WHO_FIELD_NIC;
575 fields |= WHO_FIELD_REN;
579 fields |= WHO_FIELD_SER;
583 fields |= WHO_FIELD_QTY;
587 fields |= WHO_FIELD_UID;
598 matchsel = WHO_FIELD_DEF;
602 if (qrt && (fields & WHO_FIELD_QTY))
605 if (!((*p > '9') || (*p < '0')))
607 if (!((*p > '9') || (*p < '0')))
609 if (!((*p > '9') || (*p < '0')))
616 /* I'd love to add also a check on the number of matches fields per time */
617 counter = (2048 / (counter + 4));
618 if (mask && (strlen(mask) > 510))
621 commas = (mask && strchr(mask, ','));
623 /* First treat mask as a list of plain nicks/channels */
626 strcpy(mymask, mask);
627 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
628 nick = ircd_strtok(&p, 0, ","))
630 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
632 isthere = (find_channel_member(sptr, chptr) != 0);
633 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
635 struct Membership* member;
636 for (member = chptr->members; member; member = member->next_member)
638 acptr = member->user;
639 if ((bitsel & WHOSELECT_OPER) && !(IsAnOper(acptr)))
641 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
643 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
645 if (!Process(acptr)) /* This can't be moved before other checks */
647 if (!(isthere || (SHOW_MORE(sptr, counter))))
649 do_who(sptr, acptr, chptr, fields, qrt);
655 if ((acptr = FindUser(nick)) &&
656 ((!(bitsel & WHOSELECT_OPER)) || IsAnOper(acptr)) &&
657 Process(acptr) && SHOW_MORE(sptr, counter))
659 do_who(sptr, acptr, 0, fields, qrt);
665 /* If we didn't have any comma in the mask treat it as a
666 real mask and try to match all relevant fields */
667 if (!(commas || (counter < 1)))
670 static struct in_mask imask;
673 matchcomp(mymask, &minlen, &cset, mask);
674 if (matchcompIP(&imask, mask))
675 matchsel &= ~WHO_FIELD_NIP;
676 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
677 matchsel &= ~WHO_FIELD_NIC;
678 if ((matchsel & WHO_FIELD_SER) &&
679 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
680 || (!markMatchexServer(mymask, minlen))))
681 matchsel &= ~WHO_FIELD_SER;
682 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
683 matchsel &= ~WHO_FIELD_UID;
684 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
685 matchsel &= ~WHO_FIELD_HOS;
688 /* First of all loop through the clients in common channels */
689 if ((!(counter < 1)) && matchsel) {
690 struct Membership* member;
691 struct Membership* chan;
692 for (chan = sptr->user->channel; chan; chan = chan->next_channel) {
693 chptr = chan->channel;
694 for (member = chptr->members; member; member = member->next_member)
696 acptr = member->user;
697 if (!(IsUser(acptr) && Process(acptr)))
698 continue; /* Now Process() is at the beginning, if we fail
699 we'll never have to show this acptr in this query */
700 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
703 ((!(matchsel & WHO_FIELD_NIC))
704 || matchexec(acptr->name, mymask, minlen))
705 && ((!(matchsel & WHO_FIELD_UID))
706 || matchexec(acptr->user->username, mymask, minlen))
707 && ((!(matchsel & WHO_FIELD_SER))
708 || (!(acptr->user->server->flags & FLAGS_MAP)))
709 && ((!(matchsel & WHO_FIELD_HOS))
710 || matchexec(acptr->user->host, mymask, minlen))
711 && ((!(matchsel & WHO_FIELD_REN))
712 || matchexec(acptr->info, mymask, minlen))
713 && ((!(matchsel & WHO_FIELD_NIP))
714 || ((((acptr->ip.s_addr & imask.mask.s_addr) !=
715 imask.bits.s_addr)) || (imask.fall
716 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
718 if (!SHOW_MORE(sptr, counter))
720 do_who(sptr, acptr, chptr, fields, qrt);
724 /* Loop through all clients :-\, if we still have something to match to
725 and we can show more clients */
726 if ((!(counter < 1)) && matchsel)
727 for (acptr = me.prev; acptr; acptr = acptr->prev)
729 if (!(IsUser(acptr) && Process(acptr)))
731 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
733 if (!(SEE_USER(sptr, acptr, bitsel)))
736 ((!(matchsel & WHO_FIELD_NIC))
737 || matchexec(acptr->name, mymask, minlen))
738 && ((!(matchsel & WHO_FIELD_UID))
739 || matchexec(acptr->user->username, mymask, minlen))
740 && ((!(matchsel & WHO_FIELD_SER))
741 || (!(acptr->user->server->flags & FLAGS_MAP)))
742 && ((!(matchsel & WHO_FIELD_HOS))
743 || matchexec(acptr->user->host, mymask, minlen))
744 && ((!(matchsel & WHO_FIELD_REN))
745 || matchexec(acptr->info, mymask, minlen))
746 && ((!(matchsel & WHO_FIELD_NIP))
747 || ((((acptr->ip.s_addr & imask.mask.s_addr) != imask.bits.s_addr))
749 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
751 if (!SHOW_MORE(sptr, counter))
753 do_who(sptr, acptr, 0, fields, qrt);
757 /* Make a clean mask suitable to be sent in the "end of" */
758 if (mask && (p = strchr(mask, ' ')))
760 sendto_one(sptr, rpl_str(RPL_ENDOFWHO), /* XXX DEAD */
761 me.name, parv[0], BadPtr(mask) ? "*" : mask);
763 /* Notify the user if we decided that his query was too long */
765 sendto_one(sptr, err_str(ERR_QUERYTOOLONG), me.name, parv[0], "WHO"); /* XXX DEAD */