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
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_reply.h"
95 #include "ircd_string.h"
105 #define NAMES_ALL 1 /* List all users in channel */
106 #define NAMES_VIS 2 /* List only visible users in non-secret channels */
109 * Sends a suitably formatted 'names' reply to 'sptr' consisting of nicks within
110 * 'chptr', depending on 'filter'.
112 * NAMES_ALL - Lists all users on channel.
113 * NAMES_VIS - Only list visible (-i) users. --Gte (04/06/2000).
117 void do_names(struct Client* sptr, struct Channel* chptr, int filter)
125 struct Client *c2ptr;
126 struct Membership* member;
128 /* Tag Pub/Secret channels accordingly. */
131 if (PubChannel(chptr))
133 else if (SecretChannel(chptr))
136 len = strlen(chptr->chname);
137 strcpy(buf + 2, chptr->chname);
138 strcpy(buf + 2 + len, " :");
144 if (!ShowChannel(sptr, chptr)) /* Don't list private channels unless we are on them. */
147 /* Iterate over all channel members, and build up the list. */
149 mlen = strlen(me.name) + 10 + strlen(sptr->name);
151 for (member = chptr->members; member; member = member->next_member)
153 c2ptr = member->user;
155 if ((filter == NAMES_VIS) && IsInvisible(c2ptr))
158 if (IsZombie(member) && member->user != sptr)
166 if (IsZombie(member))
171 else if (IsChanOp(member))
176 else if (HasVoice(member))
181 strcat(buf, c2ptr->name);
182 idx += strlen(c2ptr->name) + 1;
184 if (mlen + idx + NICKLEN + 5 > BUFSIZE)
185 /* space, modifier, nick, \r \n \0 */
187 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, sptr->name, buf);
189 ircd_strncpy(buf + 2, chptr->chname, len + 1);
192 if (PubChannel(chptr))
194 else if (SecretChannel(chptr))
202 send_reply(sptr, RPL_NAMREPLY, buf);
206 * m_names - generic message handler
208 * parv[0] = sender prefix
212 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
214 struct Channel *chptr;
215 struct Channel *ch2ptr;
216 struct Client *c2ptr;
217 struct Membership* member;
219 char* para = parc > 1 ? parv[1] : 0;
221 if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
224 if (EmptyString(para)) {
225 send_reply(sptr, RPL_ENDOFNAMES, "*");
228 else if (*para == '0')
231 s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. Eww. */
234 m_names(cptr, sptr, parc, parv);
238 * Special Case 1: "/names 0".
239 * Full list as per RFC.
246 struct Channel *ch3ptr;
249 mlen = strlen(me.name) + 10 + strlen(sptr->name);
251 /* List all visible channels/visible members */
253 for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
255 if (!ShowChannel(sptr, ch2ptr))
256 continue; /* Don't show secret chans. */
258 if (find_channel_member(sptr, ch2ptr))
260 do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
262 do_names(sptr, ch2ptr, NAMES_VIS);
266 /* List all remaining users on channel '*' */
268 strcpy(buf, "* * :");
272 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
276 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
279 member = c2ptr->user->channel;
283 ch3ptr = member->channel;
285 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
288 member = member->next_channel;
291 if (showflag) /* Have we already shown them? */
294 strcat(buf, c2ptr->name);
296 idx += strlen(c2ptr->name) + 1;
299 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
301 send_reply(sptr, RPL_NAMREPLY, buf);
302 strcpy(buf, "* * :");
308 send_reply(sptr, RPL_NAMREPLY, buf);
309 send_reply(sptr, RPL_ENDOFNAMES, "*");
314 * Special Case 2: User is on this channel, requesting full names list.
315 * (As performed with each /join) - ** High frequency usage **
318 clean_channelname(para);
319 chptr = FindChannel(para);
322 member = find_member_link(chptr, sptr);
325 do_names(sptr, chptr, NAMES_ALL);
326 if (!EmptyString(para))
328 send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
335 * Special Case 3: User isn't on this channel, show all visible users, in
336 * non secret channels.
338 do_names(sptr, chptr, NAMES_VIS);
340 } else { /* Channel doesn't exist. */
341 send_reply(sptr, RPL_ENDOFNAMES, para);
348 * ms_names - server message handler
350 * parv[0] = sender prefix
353 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
355 struct Channel *chptr;
356 struct Channel *ch2ptr;
357 struct Client *c2ptr;
358 struct Membership* member;
360 char* para = parc > 1 ? parv[1] : 0;
362 if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
365 if (EmptyString(para)) {
366 send_reply(sptr, RPL_ENDOFNAMES, "*");
369 else if (*para == '0')
372 s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
375 m_names(cptr, sptr, parc, parv);
379 * Special Case 1: "/names 0".
380 * Full list as per RFC.
387 struct Channel *ch3ptr;
390 mlen = strlen(me.name) + 10 + strlen(sptr->name);
392 /* List all visible channels/visible members */
394 for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
396 if (!ShowChannel(sptr, ch2ptr))
397 continue; /* Don't show secret chans. */
399 if (find_channel_member(sptr, ch2ptr))
401 do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
403 do_names(sptr, ch2ptr, NAMES_VIS);
407 /* List all remaining users on channel '*' */
409 strcpy(buf, "* * :");
413 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
417 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
420 member = c2ptr->user->channel;
424 ch3ptr = member->channel;
426 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
429 member = member->next_channel;
432 if (showflag) /* Have we already shown them? */
435 strcat(buf, c2ptr->name);
437 idx += strlen(c2ptr->name) + 1;
440 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
442 send_reply(sptr, RPL_NAMREPLY, buf);
443 strcpy(buf, "* * :");
449 send_reply(sptr, RPL_NAMREPLY, buf);
450 send_reply(sptr, RPL_ENDOFNAMES, "*");
455 * Special Case 2: User is on this channel, requesting full names list.
456 * (As performed with each /join) - ** High frequency usage **
459 clean_channelname(para);
460 chptr = FindChannel(para);
463 member = find_member_link(chptr, sptr);
466 do_names(sptr, chptr, NAMES_ALL);
467 if (!EmptyString(para))
469 send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
476 * Special Case 3: User isn't on this channel, show all visible users, in
477 * non secret channels.
479 do_names(sptr, chptr, NAMES_VIS);
481 } else { /* Channel doesn't exist. */
482 send_reply(sptr, RPL_ENDOFNAMES, para);
490 * m_names - Added by Jto 27 Apr 1989
492 * parv[0] = sender prefix
495 int m_names(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
497 struct Channel *chptr;
498 struct Client *c2ptr;
499 struct Membership* member;
500 struct Channel *ch2ptr = 0;
501 int idx, flag, len, mlen;
502 char *s, *para = parc > 1 ? parv[1] : 0;
505 if (parc > 2 && hunt_server(1, cptr, sptr, "%s%s " TOK_NAMES " %s %s", 2, parc, parv)) /* XXX DEAD */
508 mlen = strlen(me.name) + 10 + strlen(sptr->name);
510 if (!EmptyString(para))
512 s = strchr(para, ',');
516 m_names(cptr, sptr, parc, parv);
518 clean_channelname(para);
519 ch2ptr = FindChannel(para);
523 * First, do all visible channels (public and the one user self is)
526 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
528 if ((chptr != ch2ptr) && !EmptyString(para))
529 continue; /* -- wanted a specific channel */
530 if (!MyConnect(sptr) && EmptyString(para))
533 if (!ShowChannel(sptr, chptr))
534 continue; /* -- users on this are not listed */
537 /* Find users on same channel (defined by chptr) */
540 len = strlen(chptr->chname);
541 strcpy(buf + 2, chptr->chname);
542 strcpy(buf + 2 + len, " :");
544 if (PubChannel(chptr))
546 else if (SecretChannel(chptr))
550 for (member = chptr->members; member; member = member->next_member)
552 c2ptr = member->user;
554 if (sptr != c2ptr && IsInvisible(c2ptr) && !find_channel_member(sptr, chptr))
557 if (IsZombie(member))
559 if (member->user != sptr)
567 else if (IsChanOp(member))
572 else if (HasVoice(member))
577 strcat(buf, c2ptr->name);
579 idx += strlen(c2ptr->name) + 1;
584 sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
585 assert(c2ptr == findNUser(yxx));
586 sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
589 if (mlen + idx + NICKLEN + 11 > BUFSIZE)
591 if (mlen + idx + NICKLEN + 5 > BUFSIZE)
593 /* space, modifier, nick, \r \n \0 */
595 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
597 ircd_strncpy(buf + 2, chptr->chname, len + 1);
600 if (PubChannel(chptr))
602 else if (SecretChannel(chptr))
609 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
611 if (!EmptyString(para))
613 sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], /* XXX DEAD */
614 ch2ptr ? ch2ptr->chname : para);
618 /* Second, do all non-public, non-secret channels in one big sweep */
620 strcpy(buf, "* * :");
623 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
625 struct Channel *ch3ptr;
626 int showflag = 0, secret = 0;
629 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
634 member = c2ptr->user->channel;
636 * Don't show a client if they are on a secret channel or when
637 * they are on a channel sptr is on since they have already
638 * been show earlier. -avalon
642 ch3ptr = member->channel;
644 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
647 if (SecretChannel(ch3ptr))
649 member = member->next_channel;
651 if (showflag) /* Have we already shown them ? */
654 if (secret) /* On any secret channels ? */
657 strcat(buf, c2ptr->name);
659 idx += strlen(c2ptr->name) + 1;
664 sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
665 assert(c2ptr == findNUser(yxx));
666 sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
671 if (mlen + idx + NICKLEN + 9 > BUFSIZE)
673 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
676 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
677 strcpy(buf, "* * :");
683 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
684 sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], "*"); /* XXX DEAD */