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
88 #include "ircd_reply.h"
89 #include "ircd_string.h"
100 * Sends a suitably formatted 'names' reply to 'sptr' consisting of nicks within
101 * 'chptr', depending on 'filter'.
103 * NAMES_ALL - Lists all users on channel.
104 * NAMES_VIS - Only list visible (-i) users. --Gte (04/06/2000).
105 * NAMES_EON - When OR'd with the other two, adds an 'End of Names' numeric
110 void do_names(struct Client* sptr, struct Channel* chptr, int filter)
118 struct Client *c2ptr;
119 struct Membership* member;
123 assert((filter&NAMES_ALL) != (filter&NAMES_VIS));
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(cli_name(&me)) + 10 + strlen(cli_name(sptr));
148 for (member = chptr->members; member; member = member->next_member)
150 c2ptr = member->user;
152 if (((filter&NAMES_VIS)!=0) && 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, cli_name(c2ptr));
179 idx += strlen(cli_name(c2ptr));
181 if (mlen + idx + NICKLEN + 5 > BUFSIZE)
182 /* space, modifier, nick, \r \n \0 */
184 send_reply(sptr, RPL_NAMREPLY, 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);
200 if (filter&NAMES_EON)
201 send_reply(sptr, RPL_ENDOFNAMES, chptr->chname);
205 * m_names - generic message handler
207 * parv[0] = sender prefix
211 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
213 struct Channel *chptr;
214 struct Channel *ch2ptr;
215 struct Client *c2ptr;
216 struct Membership* member;
218 char* para = parc > 1 ? parv[1] : 0;
220 if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
223 if (EmptyString(para)) {
224 send_reply(sptr, RPL_ENDOFNAMES, "*");
227 else if (*para == '0')
230 s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. Eww. */
233 m_names(cptr, sptr, parc, parv);
237 * Special Case 1: "/names 0".
238 * Full list as per RFC.
245 struct Channel *ch3ptr;
248 mlen = strlen(cli_name(&me)) + 10 + strlen(cli_name(sptr));
250 /* List all visible channels/visible members */
252 for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
254 if (!ShowChannel(sptr, ch2ptr))
255 continue; /* Don't show secret chans. */
257 if (find_channel_member(sptr, ch2ptr))
259 do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
261 do_names(sptr, ch2ptr, NAMES_VIS);
265 /* List all remaining users on channel '*' */
267 strcpy(buf, "* * :");
271 for (c2ptr = GlobalClientList; c2ptr; c2ptr = cli_next(c2ptr))
275 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
278 member = cli_user(c2ptr)->channel;
282 ch3ptr = member->channel;
284 if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
287 member = member->next_channel;
290 if (showflag) /* Have we already shown them? */
293 strcat(buf, cli_name(c2ptr));
295 idx += strlen(cli_name(c2ptr)) + 1;
298 if (mlen + idx + NICKLEN + 3 > BUFSIZE) /* space, \r\n\0 */
300 send_reply(sptr, RPL_NAMREPLY, buf);
301 strcpy(buf, "* * :");
307 send_reply(sptr, RPL_NAMREPLY, buf);
308 send_reply(sptr, RPL_ENDOFNAMES, "*");
313 * Special Case 2: User is on this channel, requesting full names list.
314 * (As performed with each /join) - ** High frequency usage **
317 clean_channelname(para);
318 chptr = FindChannel(para);
321 member = find_member_link(chptr, sptr);
324 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.
337 do_names(sptr, chptr, NAMES_VIS);
338 send_reply(sptr, RPL_ENDOFNAMES, para);
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(cli_name(&me)) + 10 + strlen(cli_name(sptr));
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 = cli_next(c2ptr))
417 if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
420 member = cli_user(c2ptr)->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, cli_name(c2ptr));
437 idx += strlen(cli_name(c2ptr)) + 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);