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 " %#C WHO %s %s\n",
194 CurrentTime, sptr, (BadPtr(parv[3]) ? parv[1] : parv[3]),
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_IDL;
254 fields |= WHO_FIELD_NIC;
258 fields |= WHO_FIELD_REN;
262 fields |= WHO_FIELD_SER;
266 fields |= WHO_FIELD_QTY;
270 fields |= WHO_FIELD_UID;
281 matchsel = WHO_FIELD_DEF;
285 if (qrt && (fields & WHO_FIELD_QTY))
288 if (!((*p > '9') || (*p < '0')))
290 if (!((*p > '9') || (*p < '0')))
292 if (!((*p > '9') || (*p < '0')))
299 /* I'd love to add also a check on the number of matches fields per time */
300 counter = (2048 / (counter + 4));
301 if (mask && (strlen(mask) > 510))
304 commas = (mask && strchr(mask, ','));
306 /* First treat mask as a list of plain nicks/channels */
309 strcpy(mymask, mask);
310 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
311 nick = ircd_strtok(&p, 0, ","))
313 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
315 isthere = (find_channel_member(sptr, chptr) != 0);
316 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
318 struct Membership* member;
319 for (member = chptr->members; member; member = member->next_member)
321 acptr = member->user;
322 if ((bitsel & WHOSELECT_OPER) && !(IsAnOper(acptr)))
324 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
326 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
328 if (!Process(acptr)) /* This can't be moved before other checks */
330 if (!(isthere || (SHOW_MORE(sptr, counter))))
332 do_who(sptr, acptr, chptr, fields, qrt);
338 if ((acptr = FindUser(nick)) &&
339 ((!(bitsel & WHOSELECT_OPER)) || IsAnOper(acptr)) &&
340 Process(acptr) && SHOW_MORE(sptr, counter))
342 do_who(sptr, acptr, 0, fields, qrt);
348 /* If we didn't have any comma in the mask treat it as a
349 real mask and try to match all relevant fields */
350 if (!(commas || (counter < 1)))
353 static struct in_mask imask;
356 matchcomp(mymask, &minlen, &cset, mask);
357 if (matchcompIP(&imask, mask))
358 matchsel &= ~WHO_FIELD_NIP;
359 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
360 matchsel &= ~WHO_FIELD_NIC;
361 if ((matchsel & WHO_FIELD_SER) &&
362 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
363 || (!markMatchexServer(mymask, minlen))))
364 matchsel &= ~WHO_FIELD_SER;
365 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
366 matchsel &= ~WHO_FIELD_UID;
367 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
368 matchsel &= ~WHO_FIELD_HOS;
371 /* First of all loop through the clients in common channels */
372 if ((!(counter < 1)) && matchsel) {
373 struct Membership* member;
374 struct Membership* chan;
375 for (chan = sptr->user->channel; chan; chan = chan->next_channel) {
376 chptr = chan->channel;
377 for (member = chptr->members; member; member = member->next_member)
379 acptr = member->user;
380 if (!(IsUser(acptr) && Process(acptr)))
381 continue; /* Now Process() is at the beginning, if we fail
382 we'll never have to show this acptr in this query */
383 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
386 ((!(matchsel & WHO_FIELD_NIC))
387 || matchexec(acptr->name, mymask, minlen))
388 && ((!(matchsel & WHO_FIELD_UID))
389 || matchexec(acptr->user->username, mymask, minlen))
390 && ((!(matchsel & WHO_FIELD_SER))
391 || (!(acptr->user->server->flags & FLAGS_MAP)))
392 && ((!(matchsel & WHO_FIELD_HOS))
393 || matchexec(acptr->user->host, mymask, minlen))
394 && ((!(matchsel & WHO_FIELD_REN))
395 || matchexec(acptr->info, mymask, minlen))
396 && ((!(matchsel & WHO_FIELD_NIP))
397 || ((((acptr->ip.s_addr & imask.mask.s_addr) !=
398 imask.bits.s_addr)) || (imask.fall
399 && matchexec(ircd_ntoa((const char*) &acptr->ip), mymask, minlen)))))
401 if (!SHOW_MORE(sptr, counter))
403 do_who(sptr, acptr, chptr, fields, qrt);
407 /* Loop through all clients :-\, if we still have something to match to
408 and we can show more clients */
409 if ((!(counter < 1)) && matchsel)
410 for (acptr = me.prev; acptr; acptr = acptr->prev)
412 if (!(IsUser(acptr) && Process(acptr)))
414 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
416 if (!(SEE_USER(sptr, acptr, bitsel)))
419 ((!(matchsel & WHO_FIELD_NIC))
420 || matchexec(acptr->name, mymask, minlen))
421 && ((!(matchsel & WHO_FIELD_UID))
422 || matchexec(acptr->user->username, mymask, minlen))
423 && ((!(matchsel & WHO_FIELD_SER))
424 || (!(acptr->user->server->flags & FLAGS_MAP)))
425 && ((!(matchsel & WHO_FIELD_HOS))
426 || matchexec(acptr->user->host, mymask, minlen))
427 && ((!(matchsel & WHO_FIELD_REN))
428 || matchexec(acptr->info, mymask, minlen))
429 && ((!(matchsel & WHO_FIELD_NIP))
430 || ((((acptr->ip.s_addr & imask.mask.s_addr) != imask.bits.s_addr))
432 && matchexec(ircd_ntoa((const char*) &acptr->ip), mymask, minlen)))))
434 if (!SHOW_MORE(sptr, counter))
436 do_who(sptr, acptr, 0, fields, qrt);
440 /* Make a clean mask suitable to be sent in the "end of" */
441 if (mask && (p = strchr(mask, ' ')))
443 send_reply(sptr, RPL_ENDOFWHO, BadPtr(mask) ? "*" : mask);
445 /* Notify the user if we decided that his query was too long */
447 send_reply(sptr, ERR_QUERYTOOLONG, "WHO");
457 * parv[0] = sender prefix
458 * parv[1] = nickname mask list
459 * parv[2] = additional selection flag, only 'o' for now.
460 * and %flags to specify what fields to output
461 * plus a ,querytype if the t flag is specified
462 * so the final thing will be like o%tnchu,777
463 * parv[3] = _optional_ parameter that overrides parv[1]
464 * This can be used as "/quote who foo % :The Black Hacker
465 * to find me, parv[3] _can_ contain spaces !.
468 int m_who(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
470 char *mask; /* The mask we are looking for */
471 char ch; /* Scratch char register */
472 struct Channel *chptr; /* Channel to show */
473 struct Client *acptr; /* Client to show */
475 int bitsel; /* Mask of selectors to apply */
476 int matchsel; /* Wich fields the match should apply on */
477 int counter; /* Query size counter,
478 initially used to count fields */
479 int commas; /* Does our mask contain any comma ?
481 int fields; /* Mask of fields to show */
482 int isthere = 0; /* When this set the user is member of chptr */
483 char *nick; /* Single element extracted from
485 char *p; /* Scratch char pointer */
486 char *qrt; /* Pointer to the query type */
487 static char mymask[512]; /* To save the mask before corrupting it */
489 /* Let's find where is our mask, and if actually contains something */
490 mask = ((parc > 1) ? parv[1] : 0);
491 if (parc > 3 && parv[3])
493 if (mask && ((mask[0] == '\0') ||
494 (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
497 /* Evaluate the flags now, we consider the second parameter
498 as "matchFlags%fieldsToInclude,querytype" */
499 bitsel = fields = counter = matchsel = 0;
501 if (parc > 2 && parv[2] && *parv[2])
504 while (((ch = *(p++))) && (ch != '%') && (ch != ','))
509 bitsel |= WHOSELECT_OPER;
513 bitsel |= WHOSELECT_EXTRA;
516 write_log(WPATH, "# " TIME_T_FMT " %s!%s@%s WHO %s %s\n",
517 CurrentTime, sptr->name, sptr->user->username, sptr->user->host,
518 (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
523 matchsel |= WHO_FIELD_NIC;
527 matchsel |= WHO_FIELD_UID;
531 matchsel |= WHO_FIELD_HOS;
535 matchsel |= WHO_FIELD_NIP;
539 matchsel |= WHO_FIELD_SER;
543 matchsel |= WHO_FIELD_REN;
547 while ((ch = *p++) && (ch != ','))
554 fields |= WHO_FIELD_CHA;
558 fields |= WHO_FIELD_DIS;
562 fields |= WHO_FIELD_FLA;
566 fields |= WHO_FIELD_HOS;
570 fields |= WHO_FIELD_NIP;
574 fields |= WHO_FIELD_NIC;
578 fields |= WHO_FIELD_REN;
582 fields |= WHO_FIELD_SER;
586 fields |= WHO_FIELD_QTY;
590 fields |= WHO_FIELD_UID;
601 matchsel = WHO_FIELD_DEF;
605 if (qrt && (fields & WHO_FIELD_QTY))
608 if (!((*p > '9') || (*p < '0')))
610 if (!((*p > '9') || (*p < '0')))
612 if (!((*p > '9') || (*p < '0')))
619 /* I'd love to add also a check on the number of matches fields per time */
620 counter = (2048 / (counter + 4));
621 if (mask && (strlen(mask) > 510))
624 commas = (mask && strchr(mask, ','));
626 /* First treat mask as a list of plain nicks/channels */
629 strcpy(mymask, mask);
630 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
631 nick = ircd_strtok(&p, 0, ","))
633 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
635 isthere = (find_channel_member(sptr, chptr) != 0);
636 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
638 struct Membership* member;
639 for (member = chptr->members; member; member = member->next_member)
641 acptr = member->user;
642 if ((bitsel & WHOSELECT_OPER) && !(IsAnOper(acptr)))
644 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
646 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
648 if (!Process(acptr)) /* This can't be moved before other checks */
650 if (!(isthere || (SHOW_MORE(sptr, counter))))
652 do_who(sptr, acptr, chptr, fields, qrt);
658 if ((acptr = FindUser(nick)) &&
659 ((!(bitsel & WHOSELECT_OPER)) || IsAnOper(acptr)) &&
660 Process(acptr) && SHOW_MORE(sptr, counter))
662 do_who(sptr, acptr, 0, fields, qrt);
668 /* If we didn't have any comma in the mask treat it as a
669 real mask and try to match all relevant fields */
670 if (!(commas || (counter < 1)))
673 static struct in_mask imask;
676 matchcomp(mymask, &minlen, &cset, mask);
677 if (matchcompIP(&imask, mask))
678 matchsel &= ~WHO_FIELD_NIP;
679 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
680 matchsel &= ~WHO_FIELD_NIC;
681 if ((matchsel & WHO_FIELD_SER) &&
682 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
683 || (!markMatchexServer(mymask, minlen))))
684 matchsel &= ~WHO_FIELD_SER;
685 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
686 matchsel &= ~WHO_FIELD_UID;
687 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
688 matchsel &= ~WHO_FIELD_HOS;
691 /* First of all loop through the clients in common channels */
692 if ((!(counter < 1)) && matchsel) {
693 struct Membership* member;
694 struct Membership* chan;
695 for (chan = sptr->user->channel; chan; chan = chan->next_channel) {
696 chptr = chan->channel;
697 for (member = chptr->members; member; member = member->next_member)
699 acptr = member->user;
700 if (!(IsUser(acptr) && Process(acptr)))
701 continue; /* Now Process() is at the beginning, if we fail
702 we'll never have to show this acptr in this query */
703 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
706 ((!(matchsel & WHO_FIELD_NIC))
707 || matchexec(acptr->name, mymask, minlen))
708 && ((!(matchsel & WHO_FIELD_UID))
709 || matchexec(acptr->user->username, mymask, minlen))
710 && ((!(matchsel & WHO_FIELD_SER))
711 || (!(acptr->user->server->flags & FLAGS_MAP)))
712 && ((!(matchsel & WHO_FIELD_HOS))
713 || matchexec(acptr->user->host, mymask, minlen))
714 && ((!(matchsel & WHO_FIELD_REN))
715 || matchexec(acptr->info, mymask, minlen))
716 && ((!(matchsel & WHO_FIELD_NIP))
717 || ((((acptr->ip.s_addr & imask.mask.s_addr) !=
718 imask.bits.s_addr)) || (imask.fall
719 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
721 if (!SHOW_MORE(sptr, counter))
723 do_who(sptr, acptr, chptr, fields, qrt);
727 /* Loop through all clients :-\, if we still have something to match to
728 and we can show more clients */
729 if ((!(counter < 1)) && matchsel)
730 for (acptr = me.prev; acptr; acptr = acptr->prev)
732 if (!(IsUser(acptr) && Process(acptr)))
734 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
736 if (!(SEE_USER(sptr, acptr, bitsel)))
739 ((!(matchsel & WHO_FIELD_NIC))
740 || matchexec(acptr->name, mymask, minlen))
741 && ((!(matchsel & WHO_FIELD_UID))
742 || matchexec(acptr->user->username, mymask, minlen))
743 && ((!(matchsel & WHO_FIELD_SER))
744 || (!(acptr->user->server->flags & FLAGS_MAP)))
745 && ((!(matchsel & WHO_FIELD_HOS))
746 || matchexec(acptr->user->host, mymask, minlen))
747 && ((!(matchsel & WHO_FIELD_REN))
748 || matchexec(acptr->info, mymask, minlen))
749 && ((!(matchsel & WHO_FIELD_NIP))
750 || ((((acptr->ip.s_addr & imask.mask.s_addr) != imask.bits.s_addr))
752 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
754 if (!SHOW_MORE(sptr, counter))
756 do_who(sptr, acptr, 0, fields, qrt);
760 /* Make a clean mask suitable to be sent in the "end of" */
761 if (mask && (p = strchr(mask, ' ')))
763 sendto_one(sptr, rpl_str(RPL_ENDOFWHO), /* XXX DEAD */
764 me.name, parv[0], BadPtr(mask) ? "*" : mask);
766 /* Notify the user if we decided that his query was too long */
768 sendto_one(sptr, err_str(ERR_QUERYTOOLONG), me.name, parv[0], "WHO"); /* XXX DEAD */