2 * IRC - Internet Relay Chat, ircd/m_names.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_reply.h"
97 #include "ircd_string.h"
100 #include "numnicks.h"
108 * Sends a suitably formatted 'names' reply to 'sptr' consisting of nicks within
109 * 'chptr', depending on 'filter'.
111 * NAMES_ALL - Lists all users on channel.
112 * NAMES_VIS - Only list visible (-i) users. --Gte (04/06/2000).
113 * NAMES_EON - When OR'd with the other two, adds an 'End of Names' numeric
118 void do_names(struct Client* sptr, struct Channel* chptr, int filter)
126 struct Client *c2ptr;
127 struct Membership* member;
131 assert((filter&NAMES_ALL) != (filter&NAMES_VIS));
133 /* Tag Pub/Secret channels accordingly. */
136 if (PubChannel(chptr))
138 else if (SecretChannel(chptr))
141 len = strlen(chptr->chname);
142 strcpy(buf + 2, chptr->chname);
143 strcpy(buf + 2 + len, " :");
149 if (!ShowChannel(sptr, chptr)) /* Don't list private channels unless we are on them. */
152 /* Iterate over all channel members, and build up the list. */
154 mlen = strlen(cli_name(&me)) + 10 + strlen(cli_name(sptr));
156 for (member = chptr->members; member; member = member->next_member)
158 c2ptr = member->user;
160 if (((filter&NAMES_VIS)!=0) && IsInvisible(c2ptr))
163 if (IsZombie(member) && member->user != sptr)
171 if (IsZombie(member))
176 else if (IsChanOp(member))
181 else if (HasVoice(member))
186 strcat(buf, cli_name(c2ptr));
187 idx += strlen(cli_name(c2ptr)) + 1;
189 if (mlen + idx + NICKLEN + 5 > BUFSIZE)
190 /* space, modifier, nick, \r \n \0 */
192 send_reply(sptr, RPL_NAMREPLY, buf);
194 ircd_strncpy(buf + 2, chptr->chname, len + 1);
197 if (PubChannel(chptr))
199 else if (SecretChannel(chptr))
207 send_reply(sptr, RPL_NAMREPLY, buf);
208 if (filter&NAMES_EON)
209 send_reply(sptr, RPL_ENDOFNAMES, chptr->chname);
213 * m_names - generic message handler
215 * parv[0] = sender prefix
219 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
221 struct Channel *chptr;
222 struct Channel *ch2ptr;
223 struct Client *c2ptr;
224 struct Membership* member;
226 char* para = parc > 1 ? parv[1] : 0;
228 if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
231 if (EmptyString(para)) {
232 send_reply(sptr, RPL_ENDOFNAMES, "*");
235 else if (*para == '0')
238 s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. Eww. */
241 m_names(cptr, sptr, parc, parv);
245 * Special Case 1: "/names 0".
246 * Full list as per RFC.
253 struct Channel *ch3ptr;
256 mlen = strlen(cli_name(&me)) + 10 + strlen(cli_name(sptr));
258 /* List all visible channels/visible members */
260 for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
262 if (!ShowChannel(sptr, ch2ptr))
263 continue; /* Don't show secret chans. */
265 if (find_channel_member(sptr, ch2ptr))
267 do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
269 do_names(sptr, ch2ptr, NAMES_VIS);
273 /* List all remaining users on channel '*' */
275 strcpy(buf, "* * :");
279 for (c2ptr = GlobalClientList; c2ptr; c2ptr = cli_next(c2ptr))
283 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
286 member = cli_user(c2ptr)->channel;
290 ch3ptr = member->channel;
292 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
295 member = member->next_channel;
298 if (showflag) /* Have we already shown them? */
301 strcat(buf, cli_name(c2ptr));
303 idx += strlen(cli_name(c2ptr)) + 1;
306 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
308 send_reply(sptr, RPL_NAMREPLY, buf);
309 strcpy(buf, "* * :");
315 send_reply(sptr, RPL_NAMREPLY, buf);
316 send_reply(sptr, RPL_ENDOFNAMES, "*");
321 * Special Case 2: User is on this channel, requesting full names list.
322 * (As performed with each /join) - ** High frequency usage **
325 clean_channelname(para);
326 chptr = FindChannel(para);
329 member = find_member_link(chptr, sptr);
332 do_names(sptr, chptr, NAMES_ALL);
333 if (!EmptyString(para))
335 send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
342 * Special Case 3: User isn't on this channel, show all visible users, in
343 * non secret channels.
345 do_names(sptr, chptr, NAMES_VIS);
347 } else { /* Channel doesn't exist. */
348 send_reply(sptr, RPL_ENDOFNAMES, para);
355 * ms_names - server message handler
357 * parv[0] = sender prefix
360 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
362 struct Channel *chptr;
363 struct Channel *ch2ptr;
364 struct Client *c2ptr;
365 struct Membership* member;
367 char* para = parc > 1 ? parv[1] : 0;
369 if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
372 if (EmptyString(para)) {
373 send_reply(sptr, RPL_ENDOFNAMES, "*");
376 else if (*para == '0')
379 s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
382 m_names(cptr, sptr, parc, parv);
386 * Special Case 1: "/names 0".
387 * Full list as per RFC.
394 struct Channel *ch3ptr;
397 mlen = strlen(cli_name(&me)) + 10 + strlen(cli_name(sptr));
399 /* List all visible channels/visible members */
401 for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
403 if (!ShowChannel(sptr, ch2ptr))
404 continue; /* Don't show secret chans. */
406 if (find_channel_member(sptr, ch2ptr))
408 do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
410 do_names(sptr, ch2ptr, NAMES_VIS);
414 /* List all remaining users on channel '*' */
416 strcpy(buf, "* * :");
420 for (c2ptr = GlobalClientList; c2ptr; c2ptr = cli_next(c2ptr))
424 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
427 member = cli_user(c2ptr)->channel;
431 ch3ptr = member->channel;
433 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
436 member = member->next_channel;
439 if (showflag) /* Have we already shown them? */
442 strcat(buf, cli_name(c2ptr));
444 idx += strlen(cli_name(c2ptr)) + 1;
447 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
449 send_reply(sptr, RPL_NAMREPLY, buf);
450 strcpy(buf, "* * :");
456 send_reply(sptr, RPL_NAMREPLY, buf);
457 send_reply(sptr, RPL_ENDOFNAMES, "*");
462 * Special Case 2: User is on this channel, requesting full names list.
463 * (As performed with each /join) - ** High frequency usage **
466 clean_channelname(para);
467 chptr = FindChannel(para);
470 member = find_member_link(chptr, sptr);
473 do_names(sptr, chptr, NAMES_ALL);
474 if (!EmptyString(para))
476 send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
483 * Special Case 3: User isn't on this channel, show all visible users, in
484 * non secret channels.
486 do_names(sptr, chptr, NAMES_VIS);
488 } else { /* Channel doesn't exist. */
489 send_reply(sptr, RPL_ENDOFNAMES, para);
497 * m_names - Added by Jto 27 Apr 1989
499 * parv[0] = sender prefix
502 int m_names(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
504 struct Channel *chptr;
505 struct Client *c2ptr;
506 struct Membership* member;
507 struct Channel *ch2ptr = 0;
508 int idx, flag, len, mlen;
509 char *s, *para = parc > 1 ? parv[1] : 0;
512 if (parc > 2 && hunt_server(1, cptr, sptr, "%s%s " TOK_NAMES " %s %s", 2, parc, parv)) /* XXX DEAD */
515 mlen = strlen(me.name) + 10 + strlen(sptr->name);
517 if (!EmptyString(para))
519 s = strchr(para, ',');
523 m_names(cptr, sptr, parc, parv);
525 clean_channelname(para);
526 ch2ptr = FindChannel(para);
530 * First, do all visible channels (public and the one user self is)
533 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
535 if ((chptr != ch2ptr) && !EmptyString(para))
536 continue; /* -- wanted a specific channel */
537 if (!MyConnect(sptr) && EmptyString(para))
540 if (!ShowChannel(sptr, chptr))
541 continue; /* -- users on this are not listed */
544 /* Find users on same channel (defined by chptr) */
547 len = strlen(chptr->chname);
548 strcpy(buf + 2, chptr->chname);
549 strcpy(buf + 2 + len, " :");
551 if (PubChannel(chptr))
553 else if (SecretChannel(chptr))
557 for (member = chptr->members; member; member = member->next_member)
559 c2ptr = member->user;
561 if (sptr != c2ptr && IsInvisible(c2ptr) && !find_channel_member(sptr, chptr))
564 if (IsZombie(member))
566 if (member->user != sptr)
574 else if (IsChanOp(member))
579 else if (HasVoice(member))
584 strcat(buf, c2ptr->name);
586 idx += strlen(c2ptr->name) + 1;
591 sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
592 assert(c2ptr == findNUser(yxx));
593 sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
596 if (mlen + idx + NICKLEN + 11 > BUFSIZE)
598 if (mlen + idx + NICKLEN + 5 > BUFSIZE)
600 /* space, modifier, nick, \r \n \0 */
602 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
604 ircd_strncpy(buf + 2, chptr->chname, len + 1);
607 if (PubChannel(chptr))
609 else if (SecretChannel(chptr))
616 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
618 if (!EmptyString(para))
620 sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], /* XXX DEAD */
621 ch2ptr ? ch2ptr->chname : para);
625 /* Second, do all non-public, non-secret channels in one big sweep */
627 strcpy(buf, "* * :");
630 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
632 struct Channel *ch3ptr;
633 int showflag = 0, secret = 0;
636 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
641 member = c2ptr->user->channel;
643 * Don't show a client if they are on a secret channel or when
644 * they are on a channel sptr is on since they have already
645 * been show earlier. -avalon
649 ch3ptr = member->channel;
651 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
654 if (SecretChannel(ch3ptr))
656 member = member->next_channel;
658 if (showflag) /* Have we already shown them ? */
661 if (secret) /* On any secret channels ? */
664 strcat(buf, c2ptr->name);
666 idx += strlen(c2ptr->name) + 1;
671 sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
672 assert(c2ptr == findNUser(yxx));
673 sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
678 if (mlen + idx + NICKLEN + 9 > BUFSIZE)
680 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
683 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
684 strcpy(buf, "* * :");
690 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
691 sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], "*"); /* XXX DEAD */