Author: Greg Sikorski <gte@atomicrevs.demon.co.uk>
[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 (needs_space) {
159         strcat(buf, " ");
160       idx++;
161     }
162     needs_space=1;
163     if (IsZombie(member))
164     {
165       if (member->user != sptr)
166         continue;
167       else
168       {
169         strcat(buf, "!");
170         idx++;
171       }
172     }
173     else if (IsChanOp(member))
174     {
175       strcat(buf, "@");
176       idx++;
177     }
178     else if (HasVoice(member))
179     {
180       strcat(buf, "+");
181       idx++;
182     }
183     strcat(buf, c2ptr->name);
184     idx += strlen(c2ptr->name) + 1;
185     flag = 1;
186     if (mlen + idx + NICKLEN + 5 > BUFSIZE)
187       /* space, modifier, nick, \r \n \0 */
188     {
189       send_reply(sptr, RPL_NAMREPLY, buf);
190       strcpy(buf, "* ");
191       ircd_strncpy(buf + 2, chptr->chname, len + 1);
192       buf[len + 2] = 0;
193       strcat(buf, " :");
194       if (PubChannel(chptr))
195         *buf = '=';
196       else if (SecretChannel(chptr))
197         *buf = '@';
198       idx = len + 4;
199       flag = 0;
200       needs_space=0;
201     }
202   }
203   if (flag)
204     send_reply(sptr, RPL_NAMREPLY, buf); 
205 }
206
207 /*
208  * m_names - generic message handler
209  *
210  * parv[0] = sender prefix
211  * parv[1] = channel
212  */
213
214 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
215 {
216   struct Channel *chptr; 
217   struct Channel *ch2ptr; 
218   struct Client *c2ptr;
219   struct Membership* member; 
220   char* s;
221   char* para = parc > 1 ? parv[1] : 0; 
222
223   if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc,
224                                   parv))
225     return 0; 
226
227   if (EmptyString(para))
228     return 0;
229   else if (*para == '0')
230     *para = '\0';
231   
232   s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
233   if (s) {
234     parv[1] = ++s;
235     m_names(cptr, sptr, parc, parv);
236   }
237  
238   /*
239    * Special Case 1: "/names 0". 
240    * Full list as per RFC. 
241    */
242
243   if (!*para) { 
244     int idx; 
245     int mlen;
246     int flag;
247     struct Channel *ch3ptr;
248     char buf[BUFSIZE]; 
249
250     mlen = strlen(me.name) + 10 + strlen(sptr->name);
251
252     /* List all visible channels/visible members */ 
253
254     for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
255     { 
256       if (!ShowChannel(sptr, ch2ptr))
257         continue;                 /* Don't show secret chans. */ 
258
259       if (find_channel_member(sptr, ch2ptr))
260       {
261         do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
262       } else { 
263         do_names(sptr, ch2ptr, NAMES_VIS);
264       }
265     } 
266
267     /* List all remaining users on channel '*' */
268
269     strcpy(buf, "* * :");
270     idx = 5;
271     flag = 0;
272
273     for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
274     {
275       int showflag = 0;
276
277       if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
278         continue;
279
280       member = c2ptr->user->channel; 
281
282       while (member)
283       {
284         ch3ptr = member->channel;
285   
286         if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
287           showflag = 1;
288  
289         member = member->next_channel;
290       }
291
292       if (showflag)               /* Have we already shown them? */
293         continue;
294  
295       strcat(buf, c2ptr->name);
296       strcat(buf, " ");
297       idx += strlen(c2ptr->name) + 1;
298       flag = 1;
299
300       if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
301       {
302         send_reply(sptr, RPL_NAMREPLY, buf);
303         strcpy(buf, "* * :");
304         idx = 5;
305         flag = 0;
306       }
307     }
308     if (flag)
309       send_reply(sptr, RPL_NAMREPLY, buf);
310     send_reply(sptr, RPL_ENDOFNAMES, "*");
311     return 1; 
312   } 
313
314   /*
315    *  Special Case 2: User is on this channel, requesting full names list.
316    *  (As performed with each /join) - ** High frequency usage **
317    */
318
319   clean_channelname(para);
320   chptr = FindChannel(para); 
321  
322   member = find_member_link(chptr, sptr); 
323   if (member)
324   { 
325     if (chptr) do_names(sptr, chptr, NAMES_ALL);
326     if (!EmptyString(para))
327     {
328       send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
329       return 1;
330     }
331
332   }
333     else
334   {
335     /*
336      *  Special Case 3: User isn't on this channel, show all visible users, in 
337      *  non secret channels.
338      */
339      
340     if (chptr) do_names(sptr, chptr, NAMES_VIS);
341     if (!EmptyString(para))
342     {
343       send_reply(sptr, RPL_ENDOFNAMES, para);
344       return 1;
345     }
346   } 
347 }
348
349  
350 /*
351  * ms_names - server message handler
352  *
353  * parv[0] = sender prefix
354  * parv[1] = channel
355  */
356 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
357 {
358   struct Channel *chptr; 
359   struct Channel *ch2ptr; 
360   struct Client *c2ptr;
361   struct Membership* member; 
362   char* s;
363   char* para = parc > 1 ? parv[1] : 0; 
364
365   if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc,
366                                   parv))
367     return 0; 
368
369   if (EmptyString(para))
370     return 0;
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 }
490  
491
492 #if 0
493 /*
494  * m_names                              - Added by Jto 27 Apr 1989
495  *
496  * parv[0] = sender prefix
497  * parv[1] = channel
498  */
499 int m_names(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
500 {
501   struct Channel *chptr;
502   struct Client *c2ptr;
503   struct Membership* member;
504   struct Channel *ch2ptr = 0;
505   int idx, flag, len, mlen;
506   char *s, *para = parc > 1 ? parv[1] : 0;
507   char buf[BUFSIZE];
508
509   if (parc > 2 && hunt_server(1, cptr, sptr, "%s%s " TOK_NAMES " %s %s", 2, parc, parv)) /* XXX DEAD */
510     return 0;
511
512   mlen = strlen(me.name) + 10 + strlen(sptr->name);
513
514   if (!EmptyString(para))
515   {
516     s = strchr(para, ',');
517     if (s)
518     {
519       parv[1] = ++s;
520       m_names(cptr, sptr, parc, parv);
521     }
522     clean_channelname(para);
523     ch2ptr = FindChannel(para);
524   }
525
526   /*
527    * First, do all visible channels (public and the one user self is)
528    */
529
530   for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
531   {
532     if ((chptr != ch2ptr) && !EmptyString(para))
533       continue;                 /* -- wanted a specific channel */
534     if (!MyConnect(sptr) && EmptyString(para))
535       continue;
536 #ifndef GODMODE
537     if (!ShowChannel(sptr, chptr))
538       continue;                 /* -- users on this are not listed */
539 #endif
540
541     /* Find users on same channel (defined by chptr) */
542
543     strcpy(buf, "* ");
544     len = strlen(chptr->chname);
545     strcpy(buf + 2, chptr->chname);
546     strcpy(buf + 2 + len, " :");
547
548     if (PubChannel(chptr))
549       *buf = '=';
550     else if (SecretChannel(chptr))
551       *buf = '@';
552     idx = len + 4;
553     flag = 1;
554     for (member = chptr->members; member; member = member->next_member)
555     {
556       c2ptr = member->user;
557 #ifndef GODMODE
558       if (sptr != c2ptr && IsInvisible(c2ptr) && !find_channel_member(sptr, chptr))
559         continue;
560 #endif
561       if (IsZombie(member))
562       {
563         if (member->user != sptr)
564           continue;
565         else
566         {
567           strcat(buf, "!");
568           idx++;
569         }
570       }
571       else if (IsChanOp(member))
572       {
573         strcat(buf, "@");
574         idx++;
575       }
576       else if (HasVoice(member))
577       {
578         strcat(buf, "+");
579         idx++;
580       }
581       strcat(buf, c2ptr->name);
582       strcat(buf, " ");
583       idx += strlen(c2ptr->name) + 1;
584       flag = 1;
585 #ifdef GODMODE
586       {
587         char yxx[6];
588         sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
589         assert(c2ptr == findNUser(yxx));
590         sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
591         idx += 6;
592       }
593       if (mlen + idx + NICKLEN + 11 > BUFSIZE)
594 #else
595       if (mlen + idx + NICKLEN + 5 > BUFSIZE)
596 #endif
597         /* space, modifier, nick, \r \n \0 */
598       {
599         sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
600         strcpy(buf, "* ");
601         ircd_strncpy(buf + 2, chptr->chname, len + 1);
602         buf[len + 2] = 0;
603         strcat(buf, " :");
604         if (PubChannel(chptr))
605           *buf = '=';
606         else if (SecretChannel(chptr))
607           *buf = '@';
608         idx = len + 4;
609         flag = 0;
610       }
611     }
612     if (flag)
613       sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
614   }
615   if (!EmptyString(para))
616   {
617     sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], /* XXX DEAD */
618         ch2ptr ? ch2ptr->chname : para);
619     return (1);
620   }
621
622   /* Second, do all non-public, non-secret channels in one big sweep */
623
624   strcpy(buf, "* * :");
625   idx = 5;
626   flag = 0;
627   for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
628   {
629     struct Channel *ch3ptr;
630     int showflag = 0, secret = 0;
631
632 #ifndef GODMODE
633     if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
634 #else
635     if (!IsUser(c2ptr))
636 #endif
637       continue;
638     member = c2ptr->user->channel;
639     /*
640      * Don't show a client if they are on a secret channel or when
641      * they are on a channel sptr is on since they have already
642      * been show earlier. -avalon
643      */
644     while (member)
645     {
646       ch3ptr = member->channel;
647 #ifndef GODMODE
648       if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
649 #endif
650         showflag = 1;
651       if (SecretChannel(ch3ptr))
652         secret = 1;
653       member = member->next_channel;
654     }
655     if (showflag)               /* Have we already shown them ? */
656       continue;
657 #ifndef GODMODE
658     if (secret)                 /* On any secret channels ? */
659       continue;
660 #endif
661     strcat(buf, c2ptr->name);
662     strcat(buf, " ");
663     idx += strlen(c2ptr->name) + 1;
664     flag = 1;
665 #ifdef GODMODE
666     {
667       char yxx[6];
668       sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
669       assert(c2ptr == findNUser(yxx));
670       sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
671       idx += 6;
672     }
673 #endif
674 #ifdef GODMODE
675     if (mlen + idx + NICKLEN + 9 > BUFSIZE)
676 #else
677     if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
678 #endif
679     {
680       sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
681       strcpy(buf, "* * :");
682       idx = 5;
683       flag = 0;
684     }
685   }
686   if (flag)
687     sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
688   sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], "*"); /* XXX DEAD */
689   return 1;
690 }
691 #endif /* 0 */
692