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;
129 #define CheckMark(x, y) ((x == y) ? 0 : (x = y))
130 #define Process(cptr) CheckMark(cptr->marker, 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;
193 write_log(WPATH, "# " TIME_T_FMT " %s!%s@%s WHO %s %s\n",
194 CurrentTime, sptr->name, sptr->user->username, sptr->user->host,
195 (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 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_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 = sptr->user->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(acptr->name, mymask, minlen))
385 && ((!(matchsel & WHO_FIELD_UID))
386 || matchexec(acptr->user->username, mymask, minlen))
387 && ((!(matchsel & WHO_FIELD_SER))
388 || (!(acptr->user->server->flags & FLAGS_MAP)))
389 && ((!(matchsel & WHO_FIELD_HOS))
390 || matchexec(acptr->user->host, mymask, minlen))
391 && ((!(matchsel & WHO_FIELD_REN))
392 || matchexec(acptr->info, mymask, minlen))
393 && ((!(matchsel & WHO_FIELD_NIP))
394 || ((((acptr->ip.s_addr & imask.mask.s_addr) !=
395 imask.bits.s_addr)) || (imask.fall
396 && matchexec(ircd_ntoa((const char*) &acptr->ip), 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 = me.prev; acptr; acptr = acptr->prev)
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(acptr->name, mymask, minlen))
418 && ((!(matchsel & WHO_FIELD_UID))
419 || matchexec(acptr->user->username, mymask, minlen))
420 && ((!(matchsel & WHO_FIELD_SER))
421 || (!(acptr->user->server->flags & FLAGS_MAP)))
422 && ((!(matchsel & WHO_FIELD_HOS))
423 || matchexec(acptr->user->host, mymask, minlen))
424 && ((!(matchsel & WHO_FIELD_REN))
425 || matchexec(acptr->info, mymask, minlen))
426 && ((!(matchsel & WHO_FIELD_NIP))
427 || ((((acptr->ip.s_addr & imask.mask.s_addr) != imask.bits.s_addr))
429 && matchexec(ircd_ntoa((const char*) &acptr->ip), 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 sendto_one(sptr, rpl_str(RPL_ENDOFWHO),
441 me.name, parv[0], BadPtr(mask) ? "*" : mask);
443 /* Notify the user if we decided that his query was too long */
445 sendto_one(sptr, err_str(ERR_QUERYTOOLONG), me.name, parv[0], "WHO");
455 * parv[0] = sender prefix
456 * parv[1] = nickname mask list
457 * parv[2] = additional selection flag, only 'o' for now.
458 * and %flags to specify what fields to output
459 * plus a ,querytype if the t flag is specified
460 * so the final thing will be like o%tnchu,777
461 * parv[3] = _optional_ parameter that overrides parv[1]
462 * This can be used as "/quote who foo % :The Black Hacker
463 * to find me, parv[3] _can_ contain spaces !.
466 int m_who(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
468 char *mask; /* The mask we are looking for */
469 char ch; /* Scratch char register */
470 struct Channel *chptr; /* Channel to show */
471 struct Client *acptr; /* Client to show */
473 int bitsel; /* Mask of selectors to apply */
474 int matchsel; /* Wich fields the match should apply on */
475 int counter; /* Query size counter,
476 initially used to count fields */
477 int commas; /* Does our mask contain any comma ?
479 int fields; /* Mask of fields to show */
480 int isthere = 0; /* When this set the user is member of chptr */
481 char *nick; /* Single element extracted from
483 char *p; /* Scratch char pointer */
484 char *qrt; /* Pointer to the query type */
485 static char mymask[512]; /* To save the mask before corrupting it */
487 /* Let's find where is our mask, and if actually contains something */
488 mask = ((parc > 1) ? parv[1] : 0);
489 if (parc > 3 && parv[3])
491 if (mask && ((mask[0] == '\0') ||
492 (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
495 /* Evaluate the flags now, we consider the second parameter
496 as "matchFlags%fieldsToInclude,querytype" */
497 bitsel = fields = counter = matchsel = 0;
499 if (parc > 2 && parv[2] && *parv[2])
502 while (((ch = *(p++))) && (ch != '%') && (ch != ','))
507 bitsel |= WHOSELECT_OPER;
511 bitsel |= WHOSELECT_EXTRA;
514 write_log(WPATH, "# " TIME_T_FMT " %s!%s@%s WHO %s %s\n",
515 CurrentTime, sptr->name, sptr->user->username, sptr->user->host,
516 (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
521 matchsel |= WHO_FIELD_NIC;
525 matchsel |= WHO_FIELD_UID;
529 matchsel |= WHO_FIELD_HOS;
533 matchsel |= WHO_FIELD_NIP;
537 matchsel |= WHO_FIELD_SER;
541 matchsel |= WHO_FIELD_REN;
545 while ((ch = *p++) && (ch != ','))
552 fields |= WHO_FIELD_CHA;
556 fields |= WHO_FIELD_DIS;
560 fields |= WHO_FIELD_FLA;
564 fields |= WHO_FIELD_HOS;
568 fields |= WHO_FIELD_NIP;
572 fields |= WHO_FIELD_NIC;
576 fields |= WHO_FIELD_REN;
580 fields |= WHO_FIELD_SER;
584 fields |= WHO_FIELD_QTY;
588 fields |= WHO_FIELD_UID;
599 matchsel = WHO_FIELD_DEF;
603 if (qrt && (fields & WHO_FIELD_QTY))
606 if (!((*p > '9') || (*p < '0')))
608 if (!((*p > '9') || (*p < '0')))
610 if (!((*p > '9') || (*p < '0')))
617 /* I'd love to add also a check on the number of matches fields per time */
618 counter = (2048 / (counter + 4));
619 if (mask && (strlen(mask) > 510))
622 commas = (mask && strchr(mask, ','));
624 /* First treat mask as a list of plain nicks/channels */
627 strcpy(mymask, mask);
628 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
629 nick = ircd_strtok(&p, 0, ","))
631 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
633 isthere = (find_channel_member(sptr, chptr) != 0);
634 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
636 struct Membership* member;
637 for (member = chptr->members; member; member = member->next_member)
639 acptr = member->user;
640 if ((bitsel & WHOSELECT_OPER) && !(IsAnOper(acptr)))
642 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
644 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
646 if (!Process(acptr)) /* This can't be moved before other checks */
648 if (!(isthere || (SHOW_MORE(sptr, counter))))
650 do_who(sptr, acptr, chptr, fields, qrt);
656 if ((acptr = FindUser(nick)) &&
657 ((!(bitsel & WHOSELECT_OPER)) || IsAnOper(acptr)) &&
658 Process(acptr) && SHOW_MORE(sptr, counter))
660 do_who(sptr, acptr, 0, fields, qrt);
666 /* If we didn't have any comma in the mask treat it as a
667 real mask and try to match all relevant fields */
668 if (!(commas || (counter < 1)))
671 static struct in_mask imask;
674 matchcomp(mymask, &minlen, &cset, mask);
675 if (matchcompIP(&imask, mask))
676 matchsel &= ~WHO_FIELD_NIP;
677 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
678 matchsel &= ~WHO_FIELD_NIC;
679 if ((matchsel & WHO_FIELD_SER) &&
680 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
681 || (!markMatchexServer(mymask, minlen))))
682 matchsel &= ~WHO_FIELD_SER;
683 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
684 matchsel &= ~WHO_FIELD_UID;
685 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
686 matchsel &= ~WHO_FIELD_HOS;
689 /* First of all loop through the clients in common channels */
690 if ((!(counter < 1)) && matchsel) {
691 struct Membership* member;
692 struct Membership* chan;
693 for (chan = sptr->user->channel; chan; chan = chan->next_channel) {
694 chptr = chan->channel;
695 for (member = chptr->members; member; member = member->next_member)
697 acptr = member->user;
698 if (!(IsUser(acptr) && Process(acptr)))
699 continue; /* Now Process() is at the beginning, if we fail
700 we'll never have to show this acptr in this query */
701 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
704 ((!(matchsel & WHO_FIELD_NIC))
705 || matchexec(acptr->name, mymask, minlen))
706 && ((!(matchsel & WHO_FIELD_UID))
707 || matchexec(acptr->user->username, mymask, minlen))
708 && ((!(matchsel & WHO_FIELD_SER))
709 || (!(acptr->user->server->flags & FLAGS_MAP)))
710 && ((!(matchsel & WHO_FIELD_HOS))
711 || matchexec(acptr->user->host, mymask, minlen))
712 && ((!(matchsel & WHO_FIELD_REN))
713 || matchexec(acptr->info, mymask, minlen))
714 && ((!(matchsel & WHO_FIELD_NIP))
715 || ((((acptr->ip.s_addr & imask.mask.s_addr) !=
716 imask.bits.s_addr)) || (imask.fall
717 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
719 if (!SHOW_MORE(sptr, counter))
721 do_who(sptr, acptr, chptr, fields, qrt);
725 /* Loop through all clients :-\, if we still have something to match to
726 and we can show more clients */
727 if ((!(counter < 1)) && matchsel)
728 for (acptr = me.prev; acptr; acptr = acptr->prev)
730 if (!(IsUser(acptr) && Process(acptr)))
732 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
734 if (!(SEE_USER(sptr, acptr, bitsel)))
737 ((!(matchsel & WHO_FIELD_NIC))
738 || matchexec(acptr->name, mymask, minlen))
739 && ((!(matchsel & WHO_FIELD_UID))
740 || matchexec(acptr->user->username, mymask, minlen))
741 && ((!(matchsel & WHO_FIELD_SER))
742 || (!(acptr->user->server->flags & FLAGS_MAP)))
743 && ((!(matchsel & WHO_FIELD_HOS))
744 || matchexec(acptr->user->host, mymask, minlen))
745 && ((!(matchsel & WHO_FIELD_REN))
746 || matchexec(acptr->info, mymask, minlen))
747 && ((!(matchsel & WHO_FIELD_NIP))
748 || ((((acptr->ip.s_addr & imask.mask.s_addr) != imask.bits.s_addr))
750 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
752 if (!SHOW_MORE(sptr, counter))
754 do_who(sptr, acptr, 0, fields, qrt);
758 /* Make a clean mask suitable to be sent in the "end of" */
759 if (mask && (p = strchr(mask, ' ')))
761 sendto_one(sptr, rpl_str(RPL_ENDOFWHO),
762 me.name, parv[0], BadPtr(mask) ? "*" : mask);
764 /* Notify the user if we decided that his query was too long */
766 sendto_one(sptr, err_str(ERR_QUERYTOOLONG), me.name, parv[0], "WHO");