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"
106 * Sends a suitably formatted 'names' reply to 'sptr' consisting of nicks within
107 * 'chptr', depending on 'filter'.
109 * NAMES_ALL - Lists all users on channel.
110 * NAMES_VIS - Only list visible (-i) users. --Gte (04/06/2000).
114 void do_names(struct Client* sptr, struct Channel* chptr, int filter)
122 struct Client *c2ptr;
123 struct Membership* member;
125 /* Tag Pub/Secret channels accordingly. */
128 if (PubChannel(chptr))
130 else if (SecretChannel(chptr))
133 len = strlen(chptr->chname);
134 strcpy(buf + 2, chptr->chname);
135 strcpy(buf + 2 + len, " :");
141 if (!ShowChannel(sptr, chptr)) /* Don't list private channels unless we are on them. */
144 /* Iterate over all channel members, and build up the list. */
146 mlen = strlen(me.name) + 10 + strlen(sptr->name);
148 for (member = chptr->members; member; member = member->next_member)
150 c2ptr = member->user;
152 if ((filter == NAMES_VIS) && IsInvisible(c2ptr))
155 if (IsZombie(member) && member->user != sptr)
163 if (IsZombie(member))
168 else if (IsChanOp(member))
173 else if (HasVoice(member))
178 strcat(buf, c2ptr->name);
179 idx += strlen(c2ptr->name) + 1;
181 if (mlen + idx + NICKLEN + 5 > BUFSIZE)
182 /* space, modifier, nick, \r \n \0 */
184 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, sptr->name, buf);
186 ircd_strncpy(buf + 2, chptr->chname, len + 1);
189 if (PubChannel(chptr))
191 else if (SecretChannel(chptr))
199 send_reply(sptr, RPL_NAMREPLY, buf);
203 * m_names - generic message handler
205 * parv[0] = sender prefix
209 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
211 struct Channel *chptr;
212 struct Channel *ch2ptr;
213 struct Client *c2ptr;
214 struct Membership* member;
216 char* para = parc > 1 ? parv[1] : 0;
218 if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
221 if (EmptyString(para)) {
222 send_reply(sptr, RPL_ENDOFNAMES, "*");
225 else if (*para == '0')
228 s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. Eww. */
231 m_names(cptr, sptr, parc, parv);
235 * Special Case 1: "/names 0".
236 * Full list as per RFC.
243 struct Channel *ch3ptr;
246 mlen = strlen(me.name) + 10 + strlen(sptr->name);
248 /* List all visible channels/visible members */
250 for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
252 if (!ShowChannel(sptr, ch2ptr))
253 continue; /* Don't show secret chans. */
255 if (find_channel_member(sptr, ch2ptr))
257 do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
259 do_names(sptr, ch2ptr, NAMES_VIS);
263 /* List all remaining users on channel '*' */
265 strcpy(buf, "* * :");
269 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
273 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
276 member = c2ptr->user->channel;
280 ch3ptr = member->channel;
282 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
285 member = member->next_channel;
288 if (showflag) /* Have we already shown them? */
291 strcat(buf, c2ptr->name);
293 idx += strlen(c2ptr->name) + 1;
296 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
298 send_reply(sptr, RPL_NAMREPLY, buf);
299 strcpy(buf, "* * :");
305 send_reply(sptr, RPL_NAMREPLY, buf);
306 send_reply(sptr, RPL_ENDOFNAMES, "*");
311 * Special Case 2: User is on this channel, requesting full names list.
312 * (As performed with each /join) - ** High frequency usage **
315 clean_channelname(para);
316 chptr = FindChannel(para);
319 member = find_member_link(chptr, sptr);
322 do_names(sptr, chptr, NAMES_ALL);
323 if (!EmptyString(para))
325 send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
332 * Special Case 3: User isn't on this channel, show all visible users, in
333 * non secret channels.
335 do_names(sptr, chptr, NAMES_VIS);
337 } else { /* Channel doesn't exist. */
338 send_reply(sptr, RPL_ENDOFNAMES, para);
345 * ms_names - server message handler
347 * parv[0] = sender prefix
350 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
352 struct Channel *chptr;
353 struct Channel *ch2ptr;
354 struct Client *c2ptr;
355 struct Membership* member;
357 char* para = parc > 1 ? parv[1] : 0;
359 if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
362 if (EmptyString(para)) {
363 send_reply(sptr, RPL_ENDOFNAMES, "*");
366 else if (*para == '0')
369 s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
372 m_names(cptr, sptr, parc, parv);
376 * Special Case 1: "/names 0".
377 * Full list as per RFC.
384 struct Channel *ch3ptr;
387 mlen = strlen(me.name) + 10 + strlen(sptr->name);
389 /* List all visible channels/visible members */
391 for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
393 if (!ShowChannel(sptr, ch2ptr))
394 continue; /* Don't show secret chans. */
396 if (find_channel_member(sptr, ch2ptr))
398 do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
400 do_names(sptr, ch2ptr, NAMES_VIS);
404 /* List all remaining users on channel '*' */
406 strcpy(buf, "* * :");
410 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
414 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
417 member = c2ptr->user->channel;
421 ch3ptr = member->channel;
423 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
426 member = member->next_channel;
429 if (showflag) /* Have we already shown them? */
432 strcat(buf, c2ptr->name);
434 idx += strlen(c2ptr->name) + 1;
437 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
439 send_reply(sptr, RPL_NAMREPLY, buf);
440 strcpy(buf, "* * :");
446 send_reply(sptr, RPL_NAMREPLY, buf);
447 send_reply(sptr, RPL_ENDOFNAMES, "*");
452 * Special Case 2: User is on this channel, requesting full names list.
453 * (As performed with each /join) - ** High frequency usage **
456 clean_channelname(para);
457 chptr = FindChannel(para);
460 member = find_member_link(chptr, sptr);
463 do_names(sptr, chptr, NAMES_ALL);
464 if (!EmptyString(para))
466 send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
473 * Special Case 3: User isn't on this channel, show all visible users, in
474 * non secret channels.
476 do_names(sptr, chptr, NAMES_VIS);
478 } else { /* Channel doesn't exist. */
479 send_reply(sptr, RPL_ENDOFNAMES, para);
487 * m_names - Added by Jto 27 Apr 1989
489 * parv[0] = sender prefix
492 int m_names(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
494 struct Channel *chptr;
495 struct Client *c2ptr;
496 struct Membership* member;
497 struct Channel *ch2ptr = 0;
498 int idx, flag, len, mlen;
499 char *s, *para = parc > 1 ? parv[1] : 0;
502 if (parc > 2 && hunt_server(1, cptr, sptr, "%s%s " TOK_NAMES " %s %s", 2, parc, parv)) /* XXX DEAD */
505 mlen = strlen(me.name) + 10 + strlen(sptr->name);
507 if (!EmptyString(para))
509 s = strchr(para, ',');
513 m_names(cptr, sptr, parc, parv);
515 clean_channelname(para);
516 ch2ptr = FindChannel(para);
520 * First, do all visible channels (public and the one user self is)
523 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
525 if ((chptr != ch2ptr) && !EmptyString(para))
526 continue; /* -- wanted a specific channel */
527 if (!MyConnect(sptr) && EmptyString(para))
530 if (!ShowChannel(sptr, chptr))
531 continue; /* -- users on this are not listed */
534 /* Find users on same channel (defined by chptr) */
537 len = strlen(chptr->chname);
538 strcpy(buf + 2, chptr->chname);
539 strcpy(buf + 2 + len, " :");
541 if (PubChannel(chptr))
543 else if (SecretChannel(chptr))
547 for (member = chptr->members; member; member = member->next_member)
549 c2ptr = member->user;
551 if (sptr != c2ptr && IsInvisible(c2ptr) && !find_channel_member(sptr, chptr))
554 if (IsZombie(member))
556 if (member->user != sptr)
564 else if (IsChanOp(member))
569 else if (HasVoice(member))
574 strcat(buf, c2ptr->name);
576 idx += strlen(c2ptr->name) + 1;
581 sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
582 assert(c2ptr == findNUser(yxx));
583 sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
586 if (mlen + idx + NICKLEN + 11 > BUFSIZE)
588 if (mlen + idx + NICKLEN + 5 > BUFSIZE)
590 /* space, modifier, nick, \r \n \0 */
592 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
594 ircd_strncpy(buf + 2, chptr->chname, len + 1);
597 if (PubChannel(chptr))
599 else if (SecretChannel(chptr))
606 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
608 if (!EmptyString(para))
610 sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], /* XXX DEAD */
611 ch2ptr ? ch2ptr->chname : para);
615 /* Second, do all non-public, non-secret channels in one big sweep */
617 strcpy(buf, "* * :");
620 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
622 struct Channel *ch3ptr;
623 int showflag = 0, secret = 0;
626 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
631 member = c2ptr->user->channel;
633 * Don't show a client if they are on a secret channel or when
634 * they are on a channel sptr is on since they have already
635 * been show earlier. -avalon
639 ch3ptr = member->channel;
641 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
644 if (SecretChannel(ch3ptr))
646 member = member->next_channel;
648 if (showflag) /* Have we already shown them ? */
651 if (secret) /* On any secret channels ? */
654 strcat(buf, c2ptr->name);
656 idx += strlen(c2ptr->name) + 1;
661 sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
662 assert(c2ptr == findNUser(yxx));
663 sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
668 if (mlen + idx + NICKLEN + 9 > BUFSIZE)
670 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
673 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
674 strcpy(buf, "* * :");
680 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
681 sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], "*"); /* XXX DEAD */