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);
321 member = find_member_link(chptr, sptr);
324 if (chptr) do_names(sptr, chptr, NAMES_ALL);
325 if (!EmptyString(para))
327 send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
334 * Special Case 3: User isn't on this channel, show all visible users, in
335 * non secret channels.
338 if (chptr) do_names(sptr, chptr, NAMES_VIS);
339 if (!EmptyString(para))
341 send_reply(sptr, RPL_ENDOFNAMES, para);
350 * ms_names - server message handler
352 * parv[0] = sender prefix
355 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
357 struct Channel *chptr;
358 struct Channel *ch2ptr;
359 struct Client *c2ptr;
360 struct Membership* member;
362 char* para = parc > 1 ? parv[1] : 0;
364 if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
367 if (EmptyString(para)) {
368 send_reply(sptr, RPL_ENDOFNAMES, "*");
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);
495 * m_names - Added by Jto 27 Apr 1989
497 * parv[0] = sender prefix
500 int m_names(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
502 struct Channel *chptr;
503 struct Client *c2ptr;
504 struct Membership* member;
505 struct Channel *ch2ptr = 0;
506 int idx, flag, len, mlen;
507 char *s, *para = parc > 1 ? parv[1] : 0;
510 if (parc > 2 && hunt_server(1, cptr, sptr, "%s%s " TOK_NAMES " %s %s", 2, parc, parv)) /* XXX DEAD */
513 mlen = strlen(me.name) + 10 + strlen(sptr->name);
515 if (!EmptyString(para))
517 s = strchr(para, ',');
521 m_names(cptr, sptr, parc, parv);
523 clean_channelname(para);
524 ch2ptr = FindChannel(para);
528 * First, do all visible channels (public and the one user self is)
531 for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
533 if ((chptr != ch2ptr) && !EmptyString(para))
534 continue; /* -- wanted a specific channel */
535 if (!MyConnect(sptr) && EmptyString(para))
538 if (!ShowChannel(sptr, chptr))
539 continue; /* -- users on this are not listed */
542 /* Find users on same channel (defined by chptr) */
545 len = strlen(chptr->chname);
546 strcpy(buf + 2, chptr->chname);
547 strcpy(buf + 2 + len, " :");
549 if (PubChannel(chptr))
551 else if (SecretChannel(chptr))
555 for (member = chptr->members; member; member = member->next_member)
557 c2ptr = member->user;
559 if (sptr != c2ptr && IsInvisible(c2ptr) && !find_channel_member(sptr, chptr))
562 if (IsZombie(member))
564 if (member->user != sptr)
572 else if (IsChanOp(member))
577 else if (HasVoice(member))
582 strcat(buf, c2ptr->name);
584 idx += strlen(c2ptr->name) + 1;
589 sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
590 assert(c2ptr == findNUser(yxx));
591 sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
594 if (mlen + idx + NICKLEN + 11 > BUFSIZE)
596 if (mlen + idx + NICKLEN + 5 > BUFSIZE)
598 /* space, modifier, nick, \r \n \0 */
600 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
602 ircd_strncpy(buf + 2, chptr->chname, len + 1);
605 if (PubChannel(chptr))
607 else if (SecretChannel(chptr))
614 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
616 if (!EmptyString(para))
618 sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], /* XXX DEAD */
619 ch2ptr ? ch2ptr->chname : para);
623 /* Second, do all non-public, non-secret channels in one big sweep */
625 strcpy(buf, "* * :");
628 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
630 struct Channel *ch3ptr;
631 int showflag = 0, secret = 0;
634 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
639 member = c2ptr->user->channel;
641 * Don't show a client if they are on a secret channel or when
642 * they are on a channel sptr is on since they have already
643 * been show earlier. -avalon
647 ch3ptr = member->channel;
649 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
652 if (SecretChannel(ch3ptr))
654 member = member->next_channel;
656 if (showflag) /* Have we already shown them ? */
659 if (secret) /* On any secret channels ? */
662 strcat(buf, c2ptr->name);
664 idx += strlen(c2ptr->name) + 1;
669 sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
670 assert(c2ptr == findNUser(yxx));
671 sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
676 if (mlen + idx + NICKLEN + 9 > BUFSIZE)
678 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
681 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
682 strcpy(buf, "* * :");
688 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
689 sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], "*"); /* XXX DEAD */