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).
111 * NAMES_EON - When OR'd with the other two, adds an 'End of Names' numeric
116 void do_names(struct Client* sptr, struct Channel* chptr, int filter)
124 struct Client *c2ptr;
125 struct Membership* member;
129 assert((filter&NAMES_ALL) != (filter&NAMES_VIS));
131 /* Tag Pub/Secret channels accordingly. */
134 if (PubChannel(chptr))
136 else if (SecretChannel(chptr))
139 len = strlen(chptr->chname);
140 strcpy(buf + 2, chptr->chname);
141 strcpy(buf + 2 + len, " :");
147 if (!ShowChannel(sptr, chptr)) /* Don't list private channels unless we are on them. */
150 /* Iterate over all channel members, and build up the list. */
152 mlen = strlen(me.name) + 10 + strlen(sptr->name);
154 for (member = chptr->members; member; member = member->next_member)
156 c2ptr = member->user;
158 if (((filter&NAMES_VIS)!=0) && IsInvisible(c2ptr))
161 if (IsZombie(member) && member->user != sptr)
169 if (IsZombie(member))
174 else if (IsChanOp(member))
179 else if (HasVoice(member))
184 strcat(buf, c2ptr->name);
185 idx += strlen(c2ptr->name) + 1;
187 if (mlen + idx + NICKLEN + 5 > BUFSIZE)
188 /* space, modifier, nick, \r \n \0 */
190 sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, sptr->name, buf);
192 ircd_strncpy(buf + 2, chptr->chname, len + 1);
195 if (PubChannel(chptr))
197 else if (SecretChannel(chptr))
205 send_reply(sptr, RPL_NAMREPLY, buf);
206 if (filter&NAMES_EON)
207 send_reply(sptr, RPL_ENDOFNAMES, chptr->chname);
211 * m_names - generic message handler
213 * parv[0] = sender prefix
217 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
219 struct Channel *chptr;
220 struct Channel *ch2ptr;
221 struct Client *c2ptr;
222 struct Membership* member;
224 char* para = parc > 1 ? parv[1] : 0;
226 if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
229 if (EmptyString(para)) {
230 send_reply(sptr, RPL_ENDOFNAMES, "*");
233 else if (*para == '0')
236 s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. Eww. */
239 m_names(cptr, sptr, parc, parv);
243 * Special Case 1: "/names 0".
244 * Full list as per RFC.
251 struct Channel *ch3ptr;
254 mlen = strlen(me.name) + 10 + strlen(sptr->name);
256 /* List all visible channels/visible members */
258 for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
260 if (!ShowChannel(sptr, ch2ptr))
261 continue; /* Don't show secret chans. */
263 if (find_channel_member(sptr, ch2ptr))
265 do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
267 do_names(sptr, ch2ptr, NAMES_VIS);
271 /* List all remaining users on channel '*' */
273 strcpy(buf, "* * :");
277 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
281 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
284 member = c2ptr->user->channel;
288 ch3ptr = member->channel;
290 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
293 member = member->next_channel;
296 if (showflag) /* Have we already shown them? */
299 strcat(buf, c2ptr->name);
301 idx += strlen(c2ptr->name) + 1;
304 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
306 send_reply(sptr, RPL_NAMREPLY, buf);
307 strcpy(buf, "* * :");
313 send_reply(sptr, RPL_NAMREPLY, buf);
314 send_reply(sptr, RPL_ENDOFNAMES, "*");
319 * Special Case 2: User is on this channel, requesting full names list.
320 * (As performed with each /join) - ** High frequency usage **
323 clean_channelname(para);
324 chptr = FindChannel(para);
327 member = find_member_link(chptr, sptr);
330 do_names(sptr, chptr, NAMES_ALL);
331 if (!EmptyString(para))
333 send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
340 * Special Case 3: User isn't on this channel, show all visible users, in
341 * non secret channels.
343 do_names(sptr, chptr, NAMES_VIS);
345 } else { /* Channel doesn't exist. */
346 send_reply(sptr, RPL_ENDOFNAMES, para);
353 * ms_names - server message handler
355 * parv[0] = sender prefix
358 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
360 struct Channel *chptr;
361 struct Channel *ch2ptr;
362 struct Client *c2ptr;
363 struct Membership* member;
365 char* para = parc > 1 ? parv[1] : 0;
367 if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
370 if (EmptyString(para)) {
371 send_reply(sptr, RPL_ENDOFNAMES, "*");
374 else if (*para == '0')
377 s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
380 m_names(cptr, sptr, parc, parv);
384 * Special Case 1: "/names 0".
385 * Full list as per RFC.
392 struct Channel *ch3ptr;
395 mlen = strlen(me.name) + 10 + strlen(sptr->name);
397 /* List all visible channels/visible members */
399 for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
401 if (!ShowChannel(sptr, ch2ptr))
402 continue; /* Don't show secret chans. */
404 if (find_channel_member(sptr, ch2ptr))
406 do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
408 do_names(sptr, ch2ptr, NAMES_VIS);
412 /* List all remaining users on channel '*' */
414 strcpy(buf, "* * :");
418 for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
422 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
425 member = c2ptr->user->channel;
429 ch3ptr = member->channel;
431 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
434 member = member->next_channel;
437 if (showflag) /* Have we already shown them? */
440 strcat(buf, c2ptr->name);
442 idx += strlen(c2ptr->name) + 1;
445 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
447 send_reply(sptr, RPL_NAMREPLY, buf);
448 strcpy(buf, "* * :");
454 send_reply(sptr, RPL_NAMREPLY, buf);
455 send_reply(sptr, RPL_ENDOFNAMES, "*");
460 * Special Case 2: User is on this channel, requesting full names list.
461 * (As performed with each /join) - ** High frequency usage **
464 clean_channelname(para);
465 chptr = FindChannel(para);
468 member = find_member_link(chptr, sptr);
471 do_names(sptr, chptr, NAMES_ALL);
472 if (!EmptyString(para))
474 send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
481 * Special Case 3: User isn't on this channel, show all visible users, in
482 * non secret channels.
484 do_names(sptr, chptr, NAMES_VIS);
486 } else { /* Channel doesn't exist. */
487 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 */