Author: Bleep <helveytw@home.com>
[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 #if 0
83 /*
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
87  */
88 #include "handlers.h"
89 #endif /* 0 */
90 #include "channel.h"
91 #include "client.h"
92 #include "hash.h"
93 #include "ircd.h"
94 #include "ircd_reply.h"
95 #include "ircd_string.h"
96 #include "msg.h"
97 #include "numeric.h"
98 #include "numnicks.h"
99 #include "s_user.h"
100 #include "send.h"
101  
102 #include <assert.h>
103 #include <string.h>
104
105 #define NAMES_ALL 1 /* List all users in channel */
106 #define NAMES_VIS 2 /* List only visible users in non-secret channels */
107
108 /*
109  *  Sends a suitably formatted 'names' reply to 'sptr' consisting of nicks within
110  *  'chptr', depending on 'filter'.
111  *
112  *  NAMES_ALL - Lists all users on channel.
113  *  NAMES_VIS - Only list visible (-i) users. --Gte (04/06/2000).
114  *
115  */
116
117 void do_names(struct Client* sptr, struct Channel* chptr, int filter)
118
119   int mlen;
120   int idx;
121   int flag;
122   int needs_space; 
123   int len; 
124   char buf[BUFSIZE];
125   struct Client *c2ptr;
126   struct Membership* member;
127
128   /* Tag Pub/Secret channels accordingly. */
129
130   strcpy(buf, "* ");
131   if (PubChannel(chptr))
132     *buf = '=';
133   else if (SecretChannel(chptr))
134     *buf = '@';
135  
136   len = strlen(chptr->chname);
137   strcpy(buf + 2, chptr->chname);
138   strcpy(buf + 2 + len, " :");
139
140   idx = len + 4;
141   flag = 1;
142   needs_space = 0;
143
144   if (!ShowChannel(sptr, chptr)) /* Don't list private channels unless we are on them. */
145     return;
146
147   /* Iterate over all channel members, and build up the list. */
148
149   mlen = strlen(me.name) + 10 + strlen(sptr->name);
150   
151   for (member = chptr->members; member; member = member->next_member)
152   {
153     c2ptr = member->user;
154
155     if ((filter == NAMES_VIS) && IsInvisible(c2ptr))
156       continue;
157
158     if (IsZombie(member) && member->user != sptr)
159       continue;
160
161     if (needs_space) {
162         strcat(buf, " ");
163       idx++;
164     }
165     needs_space=1;
166     if (IsZombie(member))
167     {
168       strcat(buf, "!");
169       idx++;
170     }
171     else if (IsChanOp(member))
172     {
173       strcat(buf, "@");
174       idx++;
175     }
176     else if (HasVoice(member))
177     {
178       strcat(buf, "+");
179       idx++;
180     }
181     strcat(buf, c2ptr->name);
182     idx += strlen(c2ptr->name) + 1;
183     flag = 1;
184     if (mlen + idx + NICKLEN + 5 > BUFSIZE)
185       /* space, modifier, nick, \r \n \0 */
186     { 
187       sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, sptr->name, buf);
188       strcpy(buf, "* ");
189       ircd_strncpy(buf + 2, chptr->chname, len + 1);
190       buf[len + 2] = 0;
191       strcat(buf, " :");
192       if (PubChannel(chptr))
193         *buf = '=';
194       else if (SecretChannel(chptr))
195         *buf = '@';
196       idx = len + 4;
197       flag = 0;
198       needs_space=0;
199     }
200   }
201   if (flag)
202     send_reply(sptr, RPL_NAMREPLY, buf); 
203 }
204
205 /*
206  * m_names - generic message handler
207  *
208  * parv[0] = sender prefix
209  * parv[1] = channel
210  */
211
212 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
213 {
214   struct Channel *chptr; 
215   struct Channel *ch2ptr; 
216   struct Client *c2ptr;
217   struct Membership* member; 
218   char* s;
219   char* para = parc > 1 ? parv[1] : 0; 
220
221   if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
222     return 0; 
223
224   if (EmptyString(para)) {
225     send_reply(sptr, RPL_ENDOFNAMES, "*");
226     return 0;
227   }
228   else if (*para == '0')
229     *para = '\0';
230   
231   s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. Eww. */
232   if (s) {
233     parv[1] = ++s;
234     m_names(cptr, sptr, parc, parv);
235   }
236  
237   /*
238    * Special Case 1: "/names 0". 
239    * Full list as per RFC. 
240    */
241
242   if (!*para) { 
243     int idx; 
244     int mlen;
245     int flag;
246     struct Channel *ch3ptr;
247     char buf[BUFSIZE]; 
248
249     mlen = strlen(me.name) + 10 + strlen(sptr->name);
250
251     /* List all visible channels/visible members */ 
252
253     for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
254     { 
255       if (!ShowChannel(sptr, ch2ptr))
256         continue;                 /* Don't show secret chans. */ 
257
258       if (find_channel_member(sptr, ch2ptr))
259       {
260         do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
261       } else { 
262         do_names(sptr, ch2ptr, NAMES_VIS);
263       }
264     } 
265
266     /* List all remaining users on channel '*' */
267
268     strcpy(buf, "* * :");
269     idx = 5;
270     flag = 0;
271
272     for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
273     {
274       int showflag = 0;
275
276       if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
277         continue;
278
279       member = c2ptr->user->channel; 
280
281       while (member)
282       {
283         ch3ptr = member->channel;
284   
285         if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
286           showflag = 1;
287  
288         member = member->next_channel;
289       }
290
291       if (showflag)               /* Have we already shown them? */
292         continue;
293  
294       strcat(buf, c2ptr->name);
295       strcat(buf, " ");
296       idx += strlen(c2ptr->name) + 1;
297       flag = 1;
298
299       if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
300       {
301         send_reply(sptr, RPL_NAMREPLY, buf);
302         strcpy(buf, "* * :");
303         idx = 5;
304         flag = 0;
305       }
306     }
307     if (flag)
308       send_reply(sptr, RPL_NAMREPLY, buf);
309     send_reply(sptr, RPL_ENDOFNAMES, "*");
310     return 1; 
311   } 
312
313   /*
314    *  Special Case 2: User is on this channel, requesting full names list.
315    *  (As performed with each /join) - ** High frequency usage **
316    */
317
318   clean_channelname(para);
319   chptr = FindChannel(para); 
320  
321   member = find_member_link(chptr, sptr); 
322   if (member)
323   { 
324     if (chptr) do_names(sptr, chptr, NAMES_ALL);
325     if (!EmptyString(para))
326     {
327       send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
328       return 1;
329     }
330   }
331     else
332   {
333     /*
334      *  Special Case 3: User isn't on this channel, show all visible users, in 
335      *  non secret channels.
336      */
337      
338     if (chptr) do_names(sptr, chptr, NAMES_VIS);
339     if (!EmptyString(para))
340     {
341       send_reply(sptr, RPL_ENDOFNAMES, para);
342       return 1;
343     }
344   }
345   return 1;
346 }
347
348  
349 /*
350  * ms_names - server message handler
351  *
352  * parv[0] = sender prefix
353  * parv[1] = channel
354  */
355 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
356 {
357   struct Channel *chptr; 
358   struct Channel *ch2ptr; 
359   struct Client *c2ptr;
360   struct Membership* member; 
361   char* s;
362   char* para = parc > 1 ? parv[1] : 0; 
363
364   if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
365     return 0; 
366
367   if (EmptyString(para)) {
368     send_reply(sptr, RPL_ENDOFNAMES, "*");
369     return 0;
370   }
371   else if (*para == '0')
372     *para = '\0';
373   
374   s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
375   if (s) {
376     parv[1] = ++s;
377     m_names(cptr, sptr, parc, parv);
378   }
379  
380   /*
381    * Special Case 1: "/names 0".
382    * Full list as per RFC. 
383    */
384
385   if (!*para) { 
386     int idx; 
387     int mlen;
388     int flag;
389     struct Channel *ch3ptr;
390     char buf[BUFSIZE]; 
391
392     mlen = strlen(me.name) + 10 + strlen(sptr->name);
393
394     /* List all visible channels/visible members */ 
395
396     for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
397     { 
398       if (!ShowChannel(sptr, ch2ptr))
399         continue;                 /* Don't show secret chans. */ 
400
401       if (find_channel_member(sptr, ch2ptr))
402       {
403         do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
404       } else { 
405         do_names(sptr, ch2ptr, NAMES_VIS);
406       }
407     } 
408  
409     /* List all remaining users on channel '*' */
410
411     strcpy(buf, "* * :");
412     idx = 5;
413     flag = 0;
414
415     for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
416     {
417       int showflag = 0;
418
419       if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
420         continue;
421
422       member = c2ptr->user->channel; 
423
424       while (member)
425       {
426         ch3ptr = member->channel;
427   
428         if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
429           showflag = 1;
430  
431         member = member->next_channel;
432       }
433
434       if (showflag)               /* Have we already shown them? */
435         continue;
436  
437       strcat(buf, c2ptr->name);
438       strcat(buf, " ");
439       idx += strlen(c2ptr->name) + 1;
440       flag = 1;
441
442       if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
443       {
444         send_reply(sptr, RPL_NAMREPLY, buf);
445         strcpy(buf, "* * :");
446         idx = 5;
447         flag = 0;
448       }
449     }
450     if (flag)
451       send_reply(sptr, RPL_NAMREPLY, buf);
452     send_reply(sptr, RPL_ENDOFNAMES, "*");
453     return 1; 
454   } 
455
456   /*
457    *  Special Case 2: User is on this channel, requesting full names list.
458    *  (As performed with each /join) - ** High frequency usage **
459    */
460
461   clean_channelname(para);
462   chptr = FindChannel(para); 
463  
464   member = find_member_link(chptr, sptr); 
465   if (member)
466   { 
467     if (chptr) do_names(sptr, chptr, NAMES_ALL);
468     if (!EmptyString(para))
469     {
470       send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
471       return 1;
472     }
473
474   }
475     else
476   {
477     /*
478      *  Special Case 3: User isn't on this channel, show all visible users, in 
479      *  non secret channels.
480      */
481      
482     if (chptr) do_names(sptr, chptr, NAMES_VIS);
483     if (!EmptyString(para))
484     {
485       send_reply(sptr, RPL_ENDOFNAMES, para);
486       return 1;
487     }
488   } 
489   return 1;
490 }
491  
492
493 #if 0
494 /*
495  * m_names                              - Added by Jto 27 Apr 1989
496  *
497  * parv[0] = sender prefix
498  * parv[1] = channel
499  */
500 int m_names(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
501 {
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;
508   char buf[BUFSIZE];
509
510   if (parc > 2 && hunt_server(1, cptr, sptr, "%s%s " TOK_NAMES " %s %s", 2, parc, parv)) /* XXX DEAD */
511     return 0;
512
513   mlen = strlen(me.name) + 10 + strlen(sptr->name);
514
515   if (!EmptyString(para))
516   {
517     s = strchr(para, ',');
518     if (s)
519     {
520       parv[1] = ++s;
521       m_names(cptr, sptr, parc, parv);
522     }
523     clean_channelname(para);
524     ch2ptr = FindChannel(para);
525   }
526
527   /*
528    * First, do all visible channels (public and the one user self is)
529    */
530
531   for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
532   {
533     if ((chptr != ch2ptr) && !EmptyString(para))
534       continue;                 /* -- wanted a specific channel */
535     if (!MyConnect(sptr) && EmptyString(para))
536       continue;
537 #ifndef GODMODE
538     if (!ShowChannel(sptr, chptr))
539       continue;                 /* -- users on this are not listed */
540 #endif
541
542     /* Find users on same channel (defined by chptr) */
543
544     strcpy(buf, "* ");
545     len = strlen(chptr->chname);
546     strcpy(buf + 2, chptr->chname);
547     strcpy(buf + 2 + len, " :");
548
549     if (PubChannel(chptr))
550       *buf = '=';
551     else if (SecretChannel(chptr))
552       *buf = '@';
553     idx = len + 4;
554     flag = 1;
555     for (member = chptr->members; member; member = member->next_member)
556     {
557       c2ptr = member->user;
558 #ifndef GODMODE
559       if (sptr != c2ptr && IsInvisible(c2ptr) && !find_channel_member(sptr, chptr))
560         continue;
561 #endif
562       if (IsZombie(member))
563       {
564         if (member->user != sptr)
565           continue;
566         else
567         {
568           strcat(buf, "!");
569           idx++;
570         }
571       }
572       else if (IsChanOp(member))
573       {
574         strcat(buf, "@");
575         idx++;
576       }
577       else if (HasVoice(member))
578       {
579         strcat(buf, "+");
580         idx++;
581       }
582       strcat(buf, c2ptr->name);
583       strcat(buf, " ");
584       idx += strlen(c2ptr->name) + 1;
585       flag = 1;
586 #ifdef GODMODE
587       {
588         char yxx[6];
589         sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
590         assert(c2ptr == findNUser(yxx));
591         sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
592         idx += 6;
593       }
594       if (mlen + idx + NICKLEN + 11 > BUFSIZE)
595 #else
596       if (mlen + idx + NICKLEN + 5 > BUFSIZE)
597 #endif
598         /* space, modifier, nick, \r \n \0 */
599       {
600         sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
601         strcpy(buf, "* ");
602         ircd_strncpy(buf + 2, chptr->chname, len + 1);
603         buf[len + 2] = 0;
604         strcat(buf, " :");
605         if (PubChannel(chptr))
606           *buf = '=';
607         else if (SecretChannel(chptr))
608           *buf = '@';
609         idx = len + 4;
610         flag = 0;
611       }
612     }
613     if (flag)
614       sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
615   }
616   if (!EmptyString(para))
617   {
618     sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], /* XXX DEAD */
619         ch2ptr ? ch2ptr->chname : para);
620     return (1);
621   }
622
623   /* Second, do all non-public, non-secret channels in one big sweep */
624
625   strcpy(buf, "* * :");
626   idx = 5;
627   flag = 0;
628   for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
629   {
630     struct Channel *ch3ptr;
631     int showflag = 0, secret = 0;
632
633 #ifndef GODMODE
634     if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
635 #else
636     if (!IsUser(c2ptr))
637 #endif
638       continue;
639     member = c2ptr->user->channel;
640     /*
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
644      */
645     while (member)
646     {
647       ch3ptr = member->channel;
648 #ifndef GODMODE
649       if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
650 #endif
651         showflag = 1;
652       if (SecretChannel(ch3ptr))
653         secret = 1;
654       member = member->next_channel;
655     }
656     if (showflag)               /* Have we already shown them ? */
657       continue;
658 #ifndef GODMODE
659     if (secret)                 /* On any secret channels ? */
660       continue;
661 #endif
662     strcat(buf, c2ptr->name);
663     strcat(buf, " ");
664     idx += strlen(c2ptr->name) + 1;
665     flag = 1;
666 #ifdef GODMODE
667     {
668       char yxx[6];
669       sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
670       assert(c2ptr == findNUser(yxx));
671       sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
672       idx += 6;
673     }
674 #endif
675 #ifdef GODMODE
676     if (mlen + idx + NICKLEN + 9 > BUFSIZE)
677 #else
678     if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
679 #endif
680     {
681       sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
682       strcpy(buf, "* * :");
683       idx = 5;
684       flag = 0;
685     }
686   }
687   if (flag)
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 */
690   return 1;
691 }
692 #endif /* 0 */
693