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))
163 if (IsZombie(member))
165 if (member->user != sptr)
173 else if (IsChanOp(member))
178 else if (HasVoice(member))
183 strcat(buf, c2ptr->name);
184 idx += strlen(c2ptr->name) + 1;
186 if (mlen + idx + NICKLEN + 5 > BUFSIZE)
187 /* space, modifier, nick, \r \n \0 */
189 send_reply(sptr, RPL_NAMREPLY, buf);
191 ircd_strncpy(buf + 2, chptr->chname, len + 1);
194 if (PubChannel(chptr))
196 else if (SecretChannel(chptr))
204 send_reply(sptr, RPL_NAMREPLY, buf);
208 * m_names - generic message handler
210 * parv[0] = sender prefix
214 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
216 struct Channel *chptr;
217 struct Channel *ch2ptr;
218 struct Client *c2ptr;
219 struct Membership* member;
221 char* para = parc > 1 ? parv[1] : 0;
223 if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc,
227 if (EmptyString(para))
229 else if (*para == '0')
232 s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
235 m_names(cptr, sptr, parc, parv);
239 * Special Case 1: "/names 0".
240 * Full list as per RFC.
247 struct Channel *ch3ptr;
250 mlen = strlen(me.name) + 10 + strlen(sptr->name);
252 /* List all visible channels/visible members */
254 for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
256 if (!ShowChannel(sptr, ch2ptr))
257 continue; /* Don't show secret chans. */
259 if (find_channel_member(sptr, ch2ptr))
261 do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
263 do_names(sptr, ch2ptr, NAMES_VIS);
267 /* List all remaining users on channel '*' */
269 strcpy(buf, "* * :");
273 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
277 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
280 member = c2ptr->user->channel;
284 ch3ptr = member->channel;
286 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
289 member = member->next_channel;
292 if (showflag) /* Have we already shown them? */
295 strcat(buf, c2ptr->name);
297 idx += strlen(c2ptr->name) + 1;
300 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
302 send_reply(sptr, RPL_NAMREPLY, buf);
303 strcpy(buf, "* * :");
309 send_reply(sptr, RPL_NAMREPLY, buf);
310 send_reply(sptr, RPL_ENDOFNAMES, "*");
315 * Special Case 2: User is on this channel, requesting full names list.
316 * (As performed with each /join) - ** High frequency usage **
319 clean_channelname(para);
320 chptr = FindChannel(para);
322 member = find_member_link(chptr, sptr);
325 if (chptr) do_names(sptr, chptr, NAMES_ALL);
326 if (!EmptyString(para))
328 send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
336 * Special Case 3: User isn't on this channel, show all visible users, in
337 * non secret channels.
340 if (chptr) do_names(sptr, chptr, NAMES_VIS);
341 if (!EmptyString(para))
343 send_reply(sptr, RPL_ENDOFNAMES, para);
351 * ms_names - server message handler
353 * parv[0] = sender prefix
356 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
358 struct Channel *chptr;
359 struct Channel *ch2ptr;
360 struct Client *c2ptr;
361 struct Membership* member;
363 char* para = parc > 1 ? parv[1] : 0;
365 if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc,
369 if (EmptyString(para))
371 else if (*para == '0')
374 s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
377 m_names(cptr, sptr, parc, parv);
381 * Special Case 1: "/names 0".
382 * Full list as per RFC.
389 struct Channel *ch3ptr;
392 mlen = strlen(me.name) + 10 + strlen(sptr->name);
394 /* List all visible channels/visible members */
396 for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
398 if (!ShowChannel(sptr, ch2ptr))
399 continue; /* Don't show secret chans. */
401 if (find_channel_member(sptr, ch2ptr))
403 do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
405 do_names(sptr, ch2ptr, NAMES_VIS);
409 /* List all remaining users on channel '*' */
411 strcpy(buf, "* * :");
415 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
419 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
422 member = c2ptr->user->channel;
426 ch3ptr = member->channel;
428 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
431 member = member->next_channel;
434 if (showflag) /* Have we already shown them? */
437 strcat(buf, c2ptr->name);
439 idx += strlen(c2ptr->name) + 1;
442 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
444 send_reply(sptr, RPL_NAMREPLY, buf);
445 strcpy(buf, "* * :");
451 send_reply(sptr, RPL_NAMREPLY, buf);
452 send_reply(sptr, RPL_ENDOFNAMES, "*");
457 * Special Case 2: User is on this channel, requesting full names list.
458 * (As performed with each /join) - ** High frequency usage **
461 clean_channelname(para);
462 chptr = FindChannel(para);
464 member = find_member_link(chptr, sptr);
467 if (chptr) do_names(sptr, chptr, NAMES_ALL);
468 if (!EmptyString(para))
470 send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
478 * Special Case 3: User isn't on this channel, show all visible users, in
479 * non secret channels.
482 if (chptr) do_names(sptr, chptr, NAMES_VIS);
483 if (!EmptyString(para))
485 send_reply(sptr, RPL_ENDOFNAMES, para);
494 * m_names - Added by Jto 27 Apr 1989
496 * parv[0] = sender prefix
499 int m_names(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
501 struct Channel *chptr;
502 struct Client *c2ptr;
503 struct Membership* member;
504 struct Channel *ch2ptr = 0;
505 int idx, flag, len, mlen;
506 char *s, *para = parc > 1 ? parv[1] : 0;
509 if (parc > 2 && hunt_server(1, cptr, sptr, "%s%s " TOK_NAMES " %s %s", 2, parc, parv)) /* XXX DEAD */
512 mlen = strlen(me.name) + 10 + strlen(sptr->name);
514 if (!EmptyString(para))
516 s = strchr(para, ',');
520 m_names(cptr, sptr, parc, parv);
522 clean_channelname(para);
523 ch2ptr = FindChannel(para);
527 * First, do all visible channels (public and the one user self is)
530 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
532 if ((chptr != ch2ptr) && !EmptyString(para))
533 continue; /* -- wanted a specific channel */
534 if (!MyConnect(sptr) && EmptyString(para))
537 if (!ShowChannel(sptr, chptr))
538 continue; /* -- users on this are not listed */
541 /* Find users on same channel (defined by chptr) */
544 len = strlen(chptr->chname);
545 strcpy(buf + 2, chptr->chname);
546 strcpy(buf + 2 + len, " :");
548 if (PubChannel(chptr))
550 else if (SecretChannel(chptr))
554 for (member = chptr->members; member; member = member->next_member)
556 c2ptr = member->user;
558 if (sptr != c2ptr && IsInvisible(c2ptr) && !find_channel_member(sptr, chptr))
561 if (IsZombie(member))
563 if (member->user != sptr)
571 else if (IsChanOp(member))
576 else if (HasVoice(member))
581 strcat(buf, c2ptr->name);
583 idx += strlen(c2ptr->name) + 1;
588 sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
589 assert(c2ptr == findNUser(yxx));
590 sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
593 if (mlen + idx + NICKLEN + 11 > BUFSIZE)
595 if (mlen + idx + NICKLEN + 5 > BUFSIZE)
597 /* space, modifier, nick, \r \n \0 */
599 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
601 ircd_strncpy(buf + 2, chptr->chname, len + 1);
604 if (PubChannel(chptr))
606 else if (SecretChannel(chptr))
613 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
615 if (!EmptyString(para))
617 sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], /* XXX DEAD */
618 ch2ptr ? ch2ptr->chname : para);
622 /* Second, do all non-public, non-secret channels in one big sweep */
624 strcpy(buf, "* * :");
627 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
629 struct Channel *ch3ptr;
630 int showflag = 0, secret = 0;
633 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
638 member = c2ptr->user->channel;
640 * Don't show a client if they are on a secret channel or when
641 * they are on a channel sptr is on since they have already
642 * been show earlier. -avalon
646 ch3ptr = member->channel;
648 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
651 if (SecretChannel(ch3ptr))
653 member = member->next_channel;
655 if (showflag) /* Have we already shown them ? */
658 if (secret) /* On any secret channels ? */
661 strcat(buf, c2ptr->name);
663 idx += strlen(c2ptr->name) + 1;
668 sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
669 assert(c2ptr == findNUser(yxx));
670 sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
675 if (mlen + idx + NICKLEN + 9 > BUFSIZE)
677 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
680 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
681 strcpy(buf, "* * :");
687 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
688 sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], "*"); /* XXX DEAD */