Forward port of delayed-join.patch from Quakenet's "Asuka" patch set
[ircu2.10.12-pk.git] / ircd / m_names.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_names.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
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)
12  * any later version.
13  *
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.
18  *
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.
22  *
23  * $Id$
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
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...).
34  *
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.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
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 ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
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*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #include "channel.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_reply.h"
89 #include "ircd_string.h"
90 #include "msg.h"
91 #include "numeric.h"
92 #include "numnicks.h"
93 #include "s_user.h"
94 #include "send.h"
95  
96 #include <assert.h>
97 #include <string.h>
98
99 /*
100  *  Sends a suitably formatted 'names' reply to 'sptr' consisting of nicks within
101  *  'chptr', depending on 'filter'.
102  *
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
106  *              used by m_join
107  *
108  */
109
110 void do_names(struct Client* sptr, struct Channel* chptr, int filter)
111
112   int mlen;
113   int idx;
114   int flag;
115   int needs_space; 
116   int len; 
117   char buf[BUFSIZE];
118   struct Client *c2ptr;
119   struct Membership* member;
120   
121   assert(chptr);
122   assert(sptr);
123   assert((filter&NAMES_ALL) != (filter&NAMES_VIS));
124
125   /* Tag Pub/Secret channels accordingly. */
126
127   strcpy(buf, "* ");
128   if (PubChannel(chptr))
129     *buf = '=';
130   else if (SecretChannel(chptr))
131     *buf = '@';
132  
133   len = strlen(chptr->chname);
134   strcpy(buf + 2, chptr->chname);
135   strcpy(buf + 2 + len, " :");
136
137   idx = len + 4;
138   flag = 1;
139   needs_space = 0;
140
141   if (!ShowChannel(sptr, chptr)) /* Don't list private channels unless we are on them. */
142     return;
143
144   /* Iterate over all channel members, and build up the list. */
145
146   mlen = strlen(cli_name(&me)) + 10 + strlen(cli_name(sptr));
147   
148   for (member = chptr->members; member; member = member->next_member)
149   {
150     c2ptr = member->user;
151
152     if (((filter&NAMES_VIS)!=0) && IsInvisible(c2ptr))
153       continue;
154
155     if (IsZombie(member) && member->user != sptr)
156       continue;
157
158     if (IsDelayedJoin(member) && (member->user != sptr) && !(filter & NAMES_DEL))
159         continue;
160
161     if ((!IsDelayedJoin(member) || (member->user == sptr)) && (filter & NAMES_DEL))
162         continue;
163
164     if (needs_space) {
165         strcat(buf, " ");
166       idx++;
167     }
168     needs_space=1;
169     if (IsZombie(member))
170     {
171       strcat(buf, "!");
172       idx++;
173     }
174     else if (IsChanOp(member))
175     {
176       strcat(buf, "@");
177       idx++;
178     }
179     else if (HasVoice(member))
180     {
181       strcat(buf, "+");
182       idx++;
183     }
184     strcat(buf, cli_name(c2ptr));
185     idx += strlen(cli_name(c2ptr));
186     flag = 1;
187     if (mlen + idx + NICKLEN + 5 > BUFSIZE)
188       /* space, modifier, nick, \r \n \0 */
189     { 
190       send_reply(sptr, (filter & NAMES_DEL) ? RPL_DELNAMREPLY : RPL_NAMREPLY, buf);
191       strcpy(buf, "* ");
192       ircd_strncpy(buf + 2, chptr->chname, len + 1);
193       buf[len + 2] = 0;
194       strcat(buf, " :");
195       if (PubChannel(chptr))
196         *buf = '=';
197       else if (SecretChannel(chptr))
198         *buf = '@';
199       idx = len + 4;
200       flag = 0;
201       needs_space=0;
202     }
203   }
204   if (flag)
205     send_reply(sptr, (filter & NAMES_DEL) ? RPL_DELNAMREPLY : RPL_NAMREPLY, buf); 
206   if (filter&NAMES_EON)
207     send_reply(sptr, RPL_ENDOFNAMES, chptr->chname);
208 }
209
210 /*
211  * m_names - generic message handler
212  *
213  * parv[0] = sender prefix
214  * parv[1] = channel
215  */
216
217 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
218 {
219   struct Channel *chptr; 
220   struct Channel *ch2ptr; 
221   struct Client *c2ptr;
222   struct Membership* member; 
223   char* s;
224   char* para = parc > 1 ? parv[1] : 0; 
225   int showingdelayed = 0;
226
227   if (parc > 1 && !ircd_strcmp(parv[1], "-D")) {
228       para = (parc > 2) ? parv[2] : 0;
229       showingdelayed = 1;
230   }
231
232   if ((parc - showingdelayed) > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2+showingdelayed, parc, parv))
233     return 0; 
234
235   if (EmptyString(para)) {
236     send_reply(sptr, RPL_ENDOFNAMES, "*");
237     return 0;
238   }
239   else if (*para == '0')
240     *para = '\0';
241   
242   s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. Eww. */
243   if (s) {
244     parv[1+showingdelayed] = ++s;
245     m_names(cptr, sptr, parc, parv);
246   }
247  
248   /*
249    * Special Case 1: "/names 0". 
250    * Full list as per RFC. 
251    */
252
253   if (!*para) { 
254     int idx; 
255     int mlen;
256     int flag;
257     struct Channel *ch3ptr;
258     char buf[BUFSIZE]; 
259
260     mlen = strlen(cli_name(&me)) + 10 + strlen(cli_name(sptr));
261
262     /* List all visible channels/visible members */ 
263
264     for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
265     { 
266       if (!ShowChannel(sptr, ch2ptr))
267         continue;                 /* Don't show secret chans. */ 
268
269       if (find_channel_member(sptr, ch2ptr))
270       {
271         do_names(sptr, ch2ptr, (showingdelayed?NAMES_DEL:0)|NAMES_ALL); /* Full list if we're in this chan. */
272       } else { 
273         do_names(sptr, ch2ptr, (showingdelayed?NAMES_DEL:0)|NAMES_VIS);
274       }
275     } 
276
277     /* List all remaining users on channel '*' */
278
279     strcpy(buf, "* * :");
280     idx = 5;
281     flag = 0;
282
283     for (c2ptr = GlobalClientList; c2ptr; c2ptr = cli_next(c2ptr))
284     {
285       int showflag = 0;
286
287       if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
288         continue;
289
290       member = cli_user(c2ptr)->channel;
291
292       while (member)
293       {
294         ch3ptr = member->channel;
295   
296         if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
297           showflag = 1;
298  
299         member = member->next_channel;
300       }
301
302       if (showflag)               /* Have we already shown them? */
303         continue;
304  
305       strcat(buf, cli_name(c2ptr));
306       strcat(buf, " ");
307       idx += strlen(cli_name(c2ptr)) + 1;
308       flag = 1;
309
310       if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
311       {
312         send_reply(sptr, RPL_NAMREPLY, buf);
313         strcpy(buf, "* * :");
314         idx = 5;
315         flag = 0;
316       }
317     }
318     if (flag)
319       send_reply(sptr, RPL_NAMREPLY, buf);
320     send_reply(sptr, RPL_ENDOFNAMES, "*");
321     return 1; 
322   } 
323
324   /*
325    *  Special Case 2: User is on this channel, requesting full names list.
326    *  (As performed with each /join) - ** High frequency usage **
327    */
328
329   clean_channelname(para);
330   chptr = FindChannel(para); 
331
332   if (chptr) {
333     member = find_member_link(chptr, sptr);
334     if (member)
335     { 
336       do_names(sptr, chptr, (showingdelayed?NAMES_DEL:0)|NAMES_ALL);
337       if (!EmptyString(para))
338       {
339         send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
340         return 1;
341       }
342     }
343       else 
344     {
345       /*
346        *  Special Case 3: User isn't on this channel, show all visible users, in 
347        *  non secret channels.
348        */ 
349       do_names(sptr, chptr, (showingdelayed?NAMES_DEL:0)|NAMES_VIS);
350       send_reply(sptr, RPL_ENDOFNAMES, para);
351     } 
352   } else { /* Channel doesn't exist. */ 
353       send_reply(sptr, RPL_ENDOFNAMES, para); 
354   }
355   return 1;
356 }
357
358  
359 /*
360  * ms_names - server message handler
361  *
362  * parv[0] = sender prefix
363  * parv[1] = channel
364  */
365 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
366 {
367   struct Channel *chptr; 
368   struct Channel *ch2ptr; 
369   struct Client *c2ptr;
370   struct Membership* member; 
371   char* s;
372   char* para = parc > 1 ? parv[1] : 0; 
373
374   if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
375     return 0; 
376
377   if (EmptyString(para)) {
378     send_reply(sptr, RPL_ENDOFNAMES, "*");
379     return 0;
380   }
381   else if (*para == '0')
382     *para = '\0';
383   
384   s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
385   if (s) {
386     parv[1] = ++s;
387     m_names(cptr, sptr, parc, parv);
388   }
389  
390   /*
391    * Special Case 1: "/names 0".
392    * Full list as per RFC. 
393    */
394
395   if (!*para) { 
396     int idx; 
397     int mlen;
398     int flag;
399     struct Channel *ch3ptr;
400     char buf[BUFSIZE]; 
401
402     mlen = strlen(cli_name(&me)) + 10 + strlen(cli_name(sptr));
403
404     /* List all visible channels/visible members */ 
405
406     for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
407     { 
408       if (!ShowChannel(sptr, ch2ptr))
409         continue;                 /* Don't show secret chans. */ 
410
411       if (find_channel_member(sptr, ch2ptr))
412       {
413         do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
414       } else { 
415         do_names(sptr, ch2ptr, NAMES_VIS);
416       }
417     } 
418  
419     /* List all remaining users on channel '*' */
420
421     strcpy(buf, "* * :");
422     idx = 5;
423     flag = 0;
424
425     for (c2ptr = GlobalClientList; c2ptr; c2ptr = cli_next(c2ptr))
426     {
427       int showflag = 0;
428
429       if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
430         continue;
431
432       member = cli_user(c2ptr)->channel; 
433
434       while (member)
435       {
436         ch3ptr = member->channel;
437   
438         if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
439           showflag = 1;
440  
441         member = member->next_channel;
442       }
443
444       if (showflag)               /* Have we already shown them? */
445         continue;
446  
447       strcat(buf, cli_name(c2ptr));
448       strcat(buf, " ");
449       idx += strlen(cli_name(c2ptr)) + 1;
450       flag = 1;
451
452       if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
453       {
454         send_reply(sptr, RPL_NAMREPLY, buf);
455         strcpy(buf, "* * :");
456         idx = 5;
457         flag = 0;
458       }
459     }
460     if (flag)
461       send_reply(sptr, RPL_NAMREPLY, buf);
462     send_reply(sptr, RPL_ENDOFNAMES, "*");
463     return 1; 
464   } 
465
466   /*
467    *  Special Case 2: User is on this channel, requesting full names list.
468    *  (As performed with each /join) - ** High frequency usage **
469    */
470
471   clean_channelname(para);
472   chptr = FindChannel(para); 
473
474   if (chptr) {
475     member = find_member_link(chptr, sptr);
476     if (member)
477     { 
478       do_names(sptr, chptr, NAMES_ALL);
479       if (!EmptyString(para))
480       {
481         send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
482         return 1;
483       }
484     }
485       else 
486     {
487       /*
488        *  Special Case 3: User isn't on this channel, show all visible users, in 
489        *  non secret channels.
490        */ 
491       do_names(sptr, chptr, NAMES_VIS);
492     } 
493   } else { /* Channel doesn't exist. */ 
494       send_reply(sptr, RPL_ENDOFNAMES, para); 
495   }
496   return 1;
497 }