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_reply.h"
99 #include "ircd_string.h"
102 #include "numnicks.h"
113 * A little spin-marking utility to tell us wich clients we have already
114 * processed and wich not
116 static int who_marker = 0;
117 static void move_marker(void)
121 struct Client *cptr = GlobalClientList;
124 cli_marker(cptr) = 0;
125 cptr = cli_next(cptr);
131 #define CheckMark(x, y) ((x == y) ? 0 : (x = y))
132 #define Process(cptr) CheckMark(cli_marker(cptr), who_marker)
135 * m_who - generic message handler
137 * parv[0] = sender prefix
138 * parv[1] = nickname mask list
139 * parv[2] = additional selection flag, only 'o' for now.
140 * and %flags to specify what fields to output
141 * plus a ,querytype if the t flag is specified
142 * so the final thing will be like o%tnchu,777
143 * parv[3] = _optional_ parameter that overrides parv[1]
144 * This can be used as "/quote who foo % :The Black Hacker
145 * to find me, parv[3] _can_ contain spaces !.
147 int m_who(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
149 char *mask; /* The mask we are looking for */
150 char ch; /* Scratch char register */
151 struct Channel *chptr; /* Channel to show */
152 struct Client *acptr; /* Client to show */
154 int bitsel; /* Mask of selectors to apply */
155 int matchsel; /* Wich fields the match should apply on */
156 int counter; /* Query size counter,
157 initially used to count fields */
158 int commas; /* Does our mask contain any comma ?
160 int fields; /* Mask of fields to show */
161 int isthere = 0; /* When this set the user is member of chptr */
162 char *nick; /* Single element extracted from
164 char *p; /* Scratch char pointer */
165 char *qrt; /* Pointer to the query type */
166 static char mymask[512]; /* To save the mask before corrupting it */
168 /* Let's find where is our mask, and if actually contains something */
169 mask = ((parc > 1) ? parv[1] : 0);
170 if (parc > 3 && parv[3])
172 if (mask && ((mask[0] == '\0') ||
173 (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
176 /* Evaluate the flags now, we consider the second parameter
177 as "matchFlags%fieldsToInclude,querytype" */
178 bitsel = fields = counter = matchsel = 0;
180 if (parc > 2 && parv[2] && *parv[2])
183 while (((ch = *(p++))) && (ch != '%') && (ch != ','))
188 bitsel |= WHOSELECT_OPER;
192 bitsel |= WHOSELECT_EXTRA;
193 if (HasPriv(sptr, PRIV_WHOX))
194 log_write(LS_WHO, L_INFO, LOG_NOSNOTICE, "%#C WHO %s %s", sptr,
195 (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
199 matchsel |= WHO_FIELD_NIC;
203 matchsel |= WHO_FIELD_UID;
207 matchsel |= WHO_FIELD_HOS;
211 matchsel |= WHO_FIELD_NIP;
215 #ifdef HEAD_IN_SAND_WHO_SERVERNAME
218 matchsel |= WHO_FIELD_SER;
222 matchsel |= WHO_FIELD_REN;
226 while ((ch = *p++) && (ch != ','))
233 fields |= WHO_FIELD_CHA;
237 fields |= WHO_FIELD_DIS;
241 fields |= WHO_FIELD_FLA;
245 fields |= WHO_FIELD_HOS;
249 fields |= WHO_FIELD_NIP;
253 fields |= WHO_FIELD_IDL;
256 fields |= WHO_FIELD_NIC;
260 fields |= WHO_FIELD_REN;
264 #ifdef HEAD_IN_SAND_WHO_SERVERNAME
267 fields |= WHO_FIELD_SER;
271 fields |= WHO_FIELD_QTY;
275 fields |= WHO_FIELD_UID;
286 matchsel = WHO_FIELD_DEF;
290 if (qrt && (fields & WHO_FIELD_QTY))
293 if (!((*p > '9') || (*p < '0')))
295 if (!((*p > '9') || (*p < '0')))
297 if (!((*p > '9') || (*p < '0')))
304 /* I'd love to add also a check on the number of matches fields per time */
305 counter = (2048 / (counter + 4));
306 if (mask && (strlen(mask) > 510))
309 commas = (mask && strchr(mask, ','));
311 /* First treat mask as a list of plain nicks/channels */
314 strcpy(mymask, mask);
315 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
316 nick = ircd_strtok(&p, 0, ","))
318 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
320 isthere = (find_channel_member(sptr, chptr) != 0);
321 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
323 struct Membership* member;
324 for (member = chptr->members; member; member = member->next_member)
326 acptr = member->user;
327 if ((bitsel & WHOSELECT_OPER) &&
328 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
329 HasPriv(sptr, PRIV_SEE_OPERS))))
331 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
333 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
335 if (!Process(acptr)) /* This can't be moved before other checks */
337 if (!(isthere || (SHOW_MORE(sptr, counter))))
339 do_who(sptr, acptr, chptr, fields, qrt);
345 if ((acptr = FindUser(nick)) &&
346 ((!(bitsel & WHOSELECT_OPER)) ||
347 (IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
348 HasPriv(sptr, PRIV_SEE_OPERS)))) &&
349 Process(acptr) && SHOW_MORE(sptr, counter))
351 do_who(sptr, acptr, 0, fields, qrt);
357 /* If we didn't have any comma in the mask treat it as a
358 real mask and try to match all relevant fields */
359 if (!(commas || (counter < 1)))
362 static struct in_mask imask;
365 matchcomp(mymask, &minlen, &cset, mask);
366 if (matchcompIP(&imask, mask))
367 matchsel &= ~WHO_FIELD_NIP;
368 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
369 matchsel &= ~WHO_FIELD_NIC;
370 if ((matchsel & WHO_FIELD_SER) &&
371 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
372 || (!markMatchexServer(mymask, minlen))))
373 matchsel &= ~WHO_FIELD_SER;
374 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
375 matchsel &= ~WHO_FIELD_UID;
376 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
377 matchsel &= ~WHO_FIELD_HOS;
380 /* First of all loop through the clients in common channels */
381 if ((!(counter < 1)) && matchsel) {
382 struct Membership* member;
383 struct Membership* chan;
384 for (chan = cli_user(sptr)->channel; chan; chan = chan->next_channel) {
385 chptr = chan->channel;
386 for (member = chptr->members; member; member = member->next_member)
388 acptr = member->user;
389 if (!(IsUser(acptr) && Process(acptr)))
390 continue; /* Now Process() is at the beginning, if we fail
391 we'll never have to show this acptr in this query */
392 if ((bitsel & WHOSELECT_OPER) &&
393 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
394 HasPriv(sptr, PRIV_SEE_OPERS))))
397 ((!(matchsel & WHO_FIELD_NIC))
398 || matchexec(cli_name(acptr), mymask, minlen))
399 && ((!(matchsel & WHO_FIELD_UID))
400 || matchexec(cli_user(acptr)->username, mymask, minlen))
401 && ((!(matchsel & WHO_FIELD_SER))
402 || (!(cli_flags(cli_user(acptr)->server) & FLAGS_MAP)))
403 && ((!(matchsel & WHO_FIELD_HOS))
404 || matchexec(cli_user(acptr)->host, mymask, minlen))
405 && ((!(matchsel & WHO_FIELD_REN))
406 || matchexec(cli_info(acptr), mymask, minlen))
407 && ((!(matchsel & WHO_FIELD_NIP))
408 || ((((cli_ip(acptr).s_addr & imask.mask.s_addr) !=
409 imask.bits.s_addr)) || (imask.fall
410 && matchexec(ircd_ntoa((const char*) &(cli_ip(acptr))), mymask, minlen)))))
412 if (!SHOW_MORE(sptr, counter))
414 do_who(sptr, acptr, chptr, fields, qrt);
418 /* Loop through all clients :-\, if we still have something to match to
419 and we can show more clients */
420 if ((!(counter < 1)) && matchsel)
421 for (acptr = cli_prev(&me); acptr; acptr = cli_prev(acptr))
423 if (!(IsUser(acptr) && Process(acptr)))
425 if ((bitsel & WHOSELECT_OPER) &&
426 !(IsAnOper(acptr) && (HasPriv(acptr, PRIV_DISPLAY) ||
427 HasPriv(sptr, PRIV_SEE_OPERS))))
429 if (!(SEE_USER(sptr, acptr, bitsel)))
432 ((!(matchsel & WHO_FIELD_NIC))
433 || matchexec(cli_name(acptr), mymask, minlen))
434 && ((!(matchsel & WHO_FIELD_UID))
435 || matchexec(cli_user(acptr)->username, mymask, minlen))
436 && ((!(matchsel & WHO_FIELD_SER))
437 || (!(cli_flags(cli_user(acptr)->server) & FLAGS_MAP)))
438 && ((!(matchsel & WHO_FIELD_HOS))
439 || matchexec(cli_user(acptr)->host, mymask, minlen))
440 && ((!(matchsel & WHO_FIELD_REN))
441 || matchexec(cli_info(acptr), mymask, minlen))
442 && ((!(matchsel & WHO_FIELD_NIP))
443 || ((((cli_ip(acptr).s_addr & imask.mask.s_addr) != imask.bits.s_addr))
445 && matchexec(ircd_ntoa((const char*) &(cli_ip(acptr))), mymask, minlen)))))
447 if (!SHOW_MORE(sptr, counter))
449 do_who(sptr, acptr, 0, fields, qrt);
453 /* Make a clean mask suitable to be sent in the "end of" */
454 if (mask && (p = strchr(mask, ' ')))
456 send_reply(sptr, RPL_ENDOFWHO, BadPtr(mask) ? "*" : mask);
458 /* Notify the user if we decided that his query was too long */
460 send_reply(sptr, ERR_QUERYTOOLONG, "WHO");
470 * parv[0] = sender prefix
471 * parv[1] = nickname mask list
472 * parv[2] = additional selection flag, only 'o' for now.
473 * and %flags to specify what fields to output
474 * plus a ,querytype if the t flag is specified
475 * so the final thing will be like o%tnchu,777
476 * parv[3] = _optional_ parameter that overrides parv[1]
477 * This can be used as "/quote who foo % :The Black Hacker
478 * to find me, parv[3] _can_ contain spaces !.
481 int m_who(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
483 char *mask; /* The mask we are looking for */
484 char ch; /* Scratch char register */
485 struct Channel *chptr; /* Channel to show */
486 struct Client *acptr; /* Client to show */
488 int bitsel; /* Mask of selectors to apply */
489 int matchsel; /* Wich fields the match should apply on */
490 int counter; /* Query size counter,
491 initially used to count fields */
492 int commas; /* Does our mask contain any comma ?
494 int fields; /* Mask of fields to show */
495 int isthere = 0; /* When this set the user is member of chptr */
496 char *nick; /* Single element extracted from
498 char *p; /* Scratch char pointer */
499 char *qrt; /* Pointer to the query type */
500 static char mymask[512]; /* To save the mask before corrupting it */
502 /* Let's find where is our mask, and if actually contains something */
503 mask = ((parc > 1) ? parv[1] : 0);
504 if (parc > 3 && parv[3])
506 if (mask && ((mask[0] == '\0') ||
507 (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
510 /* Evaluate the flags now, we consider the second parameter
511 as "matchFlags%fieldsToInclude,querytype" */
512 bitsel = fields = counter = matchsel = 0;
514 if (parc > 2 && parv[2] && *parv[2])
517 while (((ch = *(p++))) && (ch != '%') && (ch != ','))
522 bitsel |= WHOSELECT_OPER;
526 bitsel |= WHOSELECT_EXTRA;
529 write_log(WPATH, "# " TIME_T_FMT " %s!%s@%s WHO %s %s\n", /* XXX DEAD */
530 CurrentTime, sptr->name, sptr->user->username, sptr->user->host,
531 (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
536 matchsel |= WHO_FIELD_NIC;
540 matchsel |= WHO_FIELD_UID;
544 matchsel |= WHO_FIELD_HOS;
548 matchsel |= WHO_FIELD_NIP;
552 matchsel |= WHO_FIELD_SER;
556 matchsel |= WHO_FIELD_REN;
560 while ((ch = *p++) && (ch != ','))
567 fields |= WHO_FIELD_CHA;
571 fields |= WHO_FIELD_DIS;
575 fields |= WHO_FIELD_FLA;
579 fields |= WHO_FIELD_HOS;
583 fields |= WHO_FIELD_NIP;
587 fields |= WHO_FIELD_NIC;
591 fields |= WHO_FIELD_REN;
595 fields |= WHO_FIELD_SER;
599 fields |= WHO_FIELD_QTY;
603 fields |= WHO_FIELD_UID;
614 matchsel = WHO_FIELD_DEF;
618 if (qrt && (fields & WHO_FIELD_QTY))
621 if (!((*p > '9') || (*p < '0')))
623 if (!((*p > '9') || (*p < '0')))
625 if (!((*p > '9') || (*p < '0')))
632 /* I'd love to add also a check on the number of matches fields per time */
633 counter = (2048 / (counter + 4));
634 if (mask && (strlen(mask) > 510))
637 commas = (mask && strchr(mask, ','));
639 /* First treat mask as a list of plain nicks/channels */
642 strcpy(mymask, mask);
643 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
644 nick = ircd_strtok(&p, 0, ","))
646 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
648 isthere = (find_channel_member(sptr, chptr) != 0);
649 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
651 struct Membership* member;
652 for (member = chptr->members; member; member = member->next_member)
654 acptr = member->user;
655 if ((bitsel & WHOSELECT_OPER) && !(IsAnOper(acptr)))
657 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
659 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
661 if (!Process(acptr)) /* This can't be moved before other checks */
663 if (!(isthere || (SHOW_MORE(sptr, counter))))
665 do_who(sptr, acptr, chptr, fields, qrt);
671 if ((acptr = FindUser(nick)) &&
672 ((!(bitsel & WHOSELECT_OPER)) || IsAnOper(acptr)) &&
673 Process(acptr) && SHOW_MORE(sptr, counter))
675 do_who(sptr, acptr, 0, fields, qrt);
681 /* If we didn't have any comma in the mask treat it as a
682 real mask and try to match all relevant fields */
683 if (!(commas || (counter < 1)))
686 static struct in_mask imask;
689 matchcomp(mymask, &minlen, &cset, mask);
690 if (matchcompIP(&imask, mask))
691 matchsel &= ~WHO_FIELD_NIP;
692 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
693 matchsel &= ~WHO_FIELD_NIC;
694 if ((matchsel & WHO_FIELD_SER) &&
695 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
696 || (!markMatchexServer(mymask, minlen))))
697 matchsel &= ~WHO_FIELD_SER;
698 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
699 matchsel &= ~WHO_FIELD_UID;
700 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
701 matchsel &= ~WHO_FIELD_HOS;
704 /* First of all loop through the clients in common channels */
705 if ((!(counter < 1)) && matchsel) {
706 struct Membership* member;
707 struct Membership* chan;
708 for (chan = sptr->user->channel; chan; chan = chan->next_channel) {
709 chptr = chan->channel;
710 for (member = chptr->members; member; member = member->next_member)
712 acptr = member->user;
713 if (!(IsUser(acptr) && Process(acptr)))
714 continue; /* Now Process() is at the beginning, if we fail
715 we'll never have to show this acptr in this query */
716 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
719 ((!(matchsel & WHO_FIELD_NIC))
720 || matchexec(acptr->name, mymask, minlen))
721 && ((!(matchsel & WHO_FIELD_UID))
722 || matchexec(acptr->user->username, mymask, minlen))
723 && ((!(matchsel & WHO_FIELD_SER))
724 || (!(acptr->user->server->flags & FLAGS_MAP)))
725 && ((!(matchsel & WHO_FIELD_HOS))
726 || matchexec(acptr->user->host, mymask, minlen))
727 && ((!(matchsel & WHO_FIELD_REN))
728 || matchexec(acptr->info, mymask, minlen))
729 && ((!(matchsel & WHO_FIELD_NIP))
730 || ((((acptr->ip.s_addr & imask.mask.s_addr) !=
731 imask.bits.s_addr)) || (imask.fall
732 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
734 if (!SHOW_MORE(sptr, counter))
736 do_who(sptr, acptr, chptr, fields, qrt);
740 /* Loop through all clients :-\, if we still have something to match to
741 and we can show more clients */
742 if ((!(counter < 1)) && matchsel)
743 for (acptr = me.prev; acptr; acptr = acptr->prev)
745 if (!(IsUser(acptr) && Process(acptr)))
747 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
749 if (!(SEE_USER(sptr, acptr, bitsel)))
752 ((!(matchsel & WHO_FIELD_NIC))
753 || matchexec(acptr->name, mymask, minlen))
754 && ((!(matchsel & WHO_FIELD_UID))
755 || matchexec(acptr->user->username, mymask, minlen))
756 && ((!(matchsel & WHO_FIELD_SER))
757 || (!(acptr->user->server->flags & FLAGS_MAP)))
758 && ((!(matchsel & WHO_FIELD_HOS))
759 || matchexec(acptr->user->host, mymask, minlen))
760 && ((!(matchsel & WHO_FIELD_REN))
761 || matchexec(acptr->info, mymask, minlen))
762 && ((!(matchsel & WHO_FIELD_NIP))
763 || ((((acptr->ip.s_addr & imask.mask.s_addr) != imask.bits.s_addr))
765 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
767 if (!SHOW_MORE(sptr, counter))
769 do_who(sptr, acptr, 0, fields, qrt);
773 /* Make a clean mask suitable to be sent in the "end of" */
774 if (mask && (p = strchr(mask, ' ')))
776 sendto_one(sptr, rpl_str(RPL_ENDOFWHO), /* XXX DEAD */
777 me.name, parv[0], BadPtr(mask) ? "*" : mask);
779 /* Notify the user if we decided that his query was too long */
781 sendto_one(sptr, err_str(ERR_QUERYTOOLONG), me.name, parv[0], "WHO"); /* XXX DEAD */