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_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 sendto_one(sptr, rpl_str(RPL_ENDOFWHO),
444 me.name, parv[0], BadPtr(mask) ? "*" : mask);
446 /* Notify the user if we decided that his query was too long */
448 sendto_one(sptr, err_str(ERR_QUERYTOOLONG), me.name, parv[0], "WHO");
458 * parv[0] = sender prefix
459 * parv[1] = nickname mask list
460 * parv[2] = additional selection flag, only 'o' for now.
461 * and %flags to specify what fields to output
462 * plus a ,querytype if the t flag is specified
463 * so the final thing will be like o%tnchu,777
464 * parv[3] = _optional_ parameter that overrides parv[1]
465 * This can be used as "/quote who foo % :The Black Hacker
466 * to find me, parv[3] _can_ contain spaces !.
469 int m_who(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
471 char *mask; /* The mask we are looking for */
472 char ch; /* Scratch char register */
473 struct Channel *chptr; /* Channel to show */
474 struct Client *acptr; /* Client to show */
476 int bitsel; /* Mask of selectors to apply */
477 int matchsel; /* Wich fields the match should apply on */
478 int counter; /* Query size counter,
479 initially used to count fields */
480 int commas; /* Does our mask contain any comma ?
482 int fields; /* Mask of fields to show */
483 int isthere = 0; /* When this set the user is member of chptr */
484 char *nick; /* Single element extracted from
486 char *p; /* Scratch char pointer */
487 char *qrt; /* Pointer to the query type */
488 static char mymask[512]; /* To save the mask before corrupting it */
490 /* Let's find where is our mask, and if actually contains something */
491 mask = ((parc > 1) ? parv[1] : 0);
492 if (parc > 3 && parv[3])
494 if (mask && ((mask[0] == '\0') ||
495 (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
498 /* Evaluate the flags now, we consider the second parameter
499 as "matchFlags%fieldsToInclude,querytype" */
500 bitsel = fields = counter = matchsel = 0;
502 if (parc > 2 && parv[2] && *parv[2])
505 while (((ch = *(p++))) && (ch != '%') && (ch != ','))
510 bitsel |= WHOSELECT_OPER;
514 bitsel |= WHOSELECT_EXTRA;
517 write_log(WPATH, "# " TIME_T_FMT " %s!%s@%s WHO %s %s\n",
518 CurrentTime, sptr->name, sptr->user->username, sptr->user->host,
519 (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
524 matchsel |= WHO_FIELD_NIC;
528 matchsel |= WHO_FIELD_UID;
532 matchsel |= WHO_FIELD_HOS;
536 matchsel |= WHO_FIELD_NIP;
540 matchsel |= WHO_FIELD_SER;
544 matchsel |= WHO_FIELD_REN;
548 while ((ch = *p++) && (ch != ','))
555 fields |= WHO_FIELD_CHA;
559 fields |= WHO_FIELD_DIS;
563 fields |= WHO_FIELD_FLA;
567 fields |= WHO_FIELD_HOS;
571 fields |= WHO_FIELD_NIP;
575 fields |= WHO_FIELD_NIC;
579 fields |= WHO_FIELD_REN;
583 fields |= WHO_FIELD_SER;
587 fields |= WHO_FIELD_QTY;
591 fields |= WHO_FIELD_UID;
602 matchsel = WHO_FIELD_DEF;
606 if (qrt && (fields & WHO_FIELD_QTY))
609 if (!((*p > '9') || (*p < '0')))
611 if (!((*p > '9') || (*p < '0')))
613 if (!((*p > '9') || (*p < '0')))
620 /* I'd love to add also a check on the number of matches fields per time */
621 counter = (2048 / (counter + 4));
622 if (mask && (strlen(mask) > 510))
625 commas = (mask && strchr(mask, ','));
627 /* First treat mask as a list of plain nicks/channels */
630 strcpy(mymask, mask);
631 for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
632 nick = ircd_strtok(&p, 0, ","))
634 if (IsChannelName(nick) && (chptr = FindChannel(nick)))
636 isthere = (find_channel_member(sptr, chptr) != 0);
637 if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
639 struct Membership* member;
640 for (member = chptr->members; member; member = member->next_member)
642 acptr = member->user;
643 if ((bitsel & WHOSELECT_OPER) && !(IsAnOper(acptr)))
645 if ((acptr != sptr) && (member->status & CHFL_ZOMBIE))
647 if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
649 if (!Process(acptr)) /* This can't be moved before other checks */
651 if (!(isthere || (SHOW_MORE(sptr, counter))))
653 do_who(sptr, acptr, chptr, fields, qrt);
659 if ((acptr = FindUser(nick)) &&
660 ((!(bitsel & WHOSELECT_OPER)) || IsAnOper(acptr)) &&
661 Process(acptr) && SHOW_MORE(sptr, counter))
663 do_who(sptr, acptr, 0, fields, qrt);
669 /* If we didn't have any comma in the mask treat it as a
670 real mask and try to match all relevant fields */
671 if (!(commas || (counter < 1)))
674 static struct in_mask imask;
677 matchcomp(mymask, &minlen, &cset, mask);
678 if (matchcompIP(&imask, mask))
679 matchsel &= ~WHO_FIELD_NIP;
680 if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
681 matchsel &= ~WHO_FIELD_NIC;
682 if ((matchsel & WHO_FIELD_SER) &&
683 ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
684 || (!markMatchexServer(mymask, minlen))))
685 matchsel &= ~WHO_FIELD_SER;
686 if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
687 matchsel &= ~WHO_FIELD_UID;
688 if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
689 matchsel &= ~WHO_FIELD_HOS;
692 /* First of all loop through the clients in common channels */
693 if ((!(counter < 1)) && matchsel) {
694 struct Membership* member;
695 struct Membership* chan;
696 for (chan = sptr->user->channel; chan; chan = chan->next_channel) {
697 chptr = chan->channel;
698 for (member = chptr->members; member; member = member->next_member)
700 acptr = member->user;
701 if (!(IsUser(acptr) && Process(acptr)))
702 continue; /* Now Process() is at the beginning, if we fail
703 we'll never have to show this acptr in this query */
704 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
707 ((!(matchsel & WHO_FIELD_NIC))
708 || matchexec(acptr->name, mymask, minlen))
709 && ((!(matchsel & WHO_FIELD_UID))
710 || matchexec(acptr->user->username, mymask, minlen))
711 && ((!(matchsel & WHO_FIELD_SER))
712 || (!(acptr->user->server->flags & FLAGS_MAP)))
713 && ((!(matchsel & WHO_FIELD_HOS))
714 || matchexec(acptr->user->host, mymask, minlen))
715 && ((!(matchsel & WHO_FIELD_REN))
716 || matchexec(acptr->info, mymask, minlen))
717 && ((!(matchsel & WHO_FIELD_NIP))
718 || ((((acptr->ip.s_addr & imask.mask.s_addr) !=
719 imask.bits.s_addr)) || (imask.fall
720 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
722 if (!SHOW_MORE(sptr, counter))
724 do_who(sptr, acptr, chptr, fields, qrt);
728 /* Loop through all clients :-\, if we still have something to match to
729 and we can show more clients */
730 if ((!(counter < 1)) && matchsel)
731 for (acptr = me.prev; acptr; acptr = acptr->prev)
733 if (!(IsUser(acptr) && Process(acptr)))
735 if ((bitsel & WHOSELECT_OPER) && !IsAnOper(acptr))
737 if (!(SEE_USER(sptr, acptr, bitsel)))
740 ((!(matchsel & WHO_FIELD_NIC))
741 || matchexec(acptr->name, mymask, minlen))
742 && ((!(matchsel & WHO_FIELD_UID))
743 || matchexec(acptr->user->username, mymask, minlen))
744 && ((!(matchsel & WHO_FIELD_SER))
745 || (!(acptr->user->server->flags & FLAGS_MAP)))
746 && ((!(matchsel & WHO_FIELD_HOS))
747 || matchexec(acptr->user->host, mymask, minlen))
748 && ((!(matchsel & WHO_FIELD_REN))
749 || matchexec(acptr->info, mymask, minlen))
750 && ((!(matchsel & WHO_FIELD_NIP))
751 || ((((acptr->ip.s_addr & imask.mask.s_addr) != imask.bits.s_addr))
753 && matchexec(inet_ntoa(acptr->ip), mymask, minlen)))))
755 if (!SHOW_MORE(sptr, counter))
757 do_who(sptr, acptr, 0, fields, qrt);
761 /* Make a clean mask suitable to be sent in the "end of" */
762 if (mask && (p = strchr(mask, ' ')))
764 sendto_one(sptr, rpl_str(RPL_ENDOFWHO),
765 me.name, parv[0], BadPtr(mask) ? "*" : mask);
767 /* Notify the user if we decided that his query was too long */
769 sendto_one(sptr, err_str(ERR_QUERYTOOLONG), me.name, parv[0], "WHO");