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   return 1;
348 }
349
350  
351 /*
352  * ms_names - server message handler
353  *
354  * parv[0] = sender prefix
355  * parv[1] = channel
356  */
357 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
358 {
359   struct Channel *chptr; 
360   struct Channel *ch2ptr; 
361   struct Client *c2ptr;
362   struct Membership* member; 
363   char* s;
364   char* para = parc > 1 ? parv[1] : 0; 
365
366   if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc,
367                                   parv))
368     return 0; 
369
370   if (EmptyString(para))
371     return 0;
372   else if (*para == '0')
373     *para = '\0';
374   
375   s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
376   if (s) {
377     parv[1] = ++s;
378     m_names(cptr, sptr, parc, parv);
379   }
380  
381   /*
382    * Special Case 1: "/names 0".
383    * Full list as per RFC. 
384    */
385
386   if (!*para) { 
387     int idx; 
388     int mlen;
389     int flag;
390     struct Channel *ch3ptr;
391     char buf[BUFSIZE]; 
392
393     mlen = strlen(me.name) + 10 + strlen(sptr->name);
394
395     /* List all visible channels/visible members */ 
396
397     for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
398     { 
399       if (!ShowChannel(sptr, ch2ptr))
400         continue;                 /* Don't show secret chans. */ 
401
402       if (find_channel_member(sptr, ch2ptr))
403       {
404         do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
405       } else { 
406         do_names(sptr, ch2ptr, NAMES_VIS);
407       }
408     } 
409  
410     /* List all remaining users on channel '*' */
411
412     strcpy(buf, "* * :");
413     idx = 5;
414     flag = 0;
415
416     for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
417     {
418       int showflag = 0;
419
420       if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
421         continue;
422
423       member = c2ptr->user->channel; 
424
425       while (member)
426       {
427         ch3ptr = member->channel;
428   
429         if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
430           showflag = 1;
431  
432         member = member->next_channel;
433       }
434
435       if (showflag)               /* Have we already shown them? */
436         continue;
437  
438       strcat(buf, c2ptr->name);
439       strcat(buf, " ");
440       idx += strlen(c2ptr->name) + 1;
441       flag = 1;
442
443       if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
444       {
445         send_reply(sptr, RPL_NAMREPLY, buf);
446         strcpy(buf, "* * :");
447         idx = 5;
448         flag = 0;
449       }
450     }
451     if (flag)
452       send_reply(sptr, RPL_NAMREPLY, buf);
453     send_reply(sptr, RPL_ENDOFNAMES, "*");
454     return 1; 
455   } 
456
457   /*
458    *  Special Case 2: User is on this channel, requesting full names list.
459    *  (As performed with each /join) - ** High frequency usage **
460    */
461
462   clean_channelname(para);
463   chptr = FindChannel(para); 
464  
465   member = find_member_link(chptr, sptr); 
466   if (member)
467   { 
468     if (chptr) do_names(sptr, chptr, NAMES_ALL);
469     if (!EmptyString(para))
470     {
471       send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
472       return 1;
473     }
474
475   }
476     else
477   {
478     /*
479      *  Special Case 3: User isn't on this channel, show all visible users, in 
480      *  non secret channels.
481      */
482      
483     if (chptr) do_names(sptr, chptr, NAMES_VIS);
484     if (!EmptyString(para))
485     {
486       send_reply(sptr, RPL_ENDOFNAMES, para);
487       return 1;
488     }
489   } 
490   return 1;
491 }
492  
493
494 #if 0
495 /*
496  * m_names                              - Added by Jto 27 Apr 1989
497  *
498  * parv[0] = sender prefix
499  * parv[1] = channel
500  */
501 int m_names(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
502 {
503   struct Channel *chptr;
504   struct Client *c2ptr;
505   struct Membership* member;
506   struct Channel *ch2ptr = 0;
507   int idx, flag, len, mlen;
508   char *s, *para = parc > 1 ? parv[1] : 0;
509   char buf[BUFSIZE];
510
511   if (parc > 2 && hunt_server(1, cptr, sptr, "%s%s " TOK_NAMES " %s %s", 2, parc, parv)) /* XXX DEAD */
512     return 0;
513
514   mlen = strlen(me.name) + 10 + strlen(sptr->name);
515
516   if (!EmptyString(para))
517   {
518     s = strchr(para, ',');
519     if (s)
520     {
521       parv[1] = ++s;
522       m_names(cptr, sptr, parc, parv);
523     }
524     clean_channelname(para);
525     ch2ptr = FindChannel(para);
526   }
527
528   /*
529    * First, do all visible channels (public and the one user self is)
530    */
531
532   for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
533   {
534     if ((chptr != ch2ptr) && !EmptyString(para))
535       continue;                 /* -- wanted a specific channel */
536     if (!MyConnect(sptr) && EmptyString(para))
537       continue;
538 #ifndef GODMODE
539     if (!ShowChannel(sptr, chptr))
540       continue;                 /* -- users on this are not listed */
541 #endif
542
543     /* Find users on same channel (defined by chptr) */
544
545     strcpy(buf, "* ");
546     len = strlen(chptr->chname);
547     strcpy(buf + 2, chptr->chname);
548     strcpy(buf + 2 + len, " :");
549
550     if (PubChannel(chptr))
551       *buf = '=';
552     else if (SecretChannel(chptr))
553       *buf = '@';
554     idx = len + 4;
555     flag = 1;
556     for (member = chptr->members; member; member = member->next_member)
557     {
558       c2ptr = member->user;
559 #ifndef GODMODE
560       if (sptr != c2ptr && IsInvisible(c2ptr) && !find_channel_member(sptr, chptr))
561         continue;
562 #endif
563       if (IsZombie(member))
564       {
565         if (member->user != sptr)
566           continue;
567         else
568         {
569           strcat(buf, "!");
570           idx++;
571         }
572       }
573       else if (IsChanOp(member))
574       {
575         strcat(buf, "@");
576         idx++;
577       }
578       else if (HasVoice(member))
579       {
580         strcat(buf, "+");
581         idx++;
582       }
583       strcat(buf, c2ptr->name);
584       strcat(buf, " ");
585       idx += strlen(c2ptr->name) + 1;
586       flag = 1;
587 #ifdef GODMODE
588       {
589         char yxx[6];
590         sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
591         assert(c2ptr == findNUser(yxx));
592         sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
593         idx += 6;
594       }
595       if (mlen + idx + NICKLEN + 11 > BUFSIZE)
596 #else
597       if (mlen + idx + NICKLEN + 5 > BUFSIZE)
598 #endif
599         /* space, modifier, nick, \r \n \0 */
600       {
601         sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
602         strcpy(buf, "* ");
603         ircd_strncpy(buf + 2, chptr->chname, len + 1);
604         buf[len + 2] = 0;
605         strcat(buf, " :");
606         if (PubChannel(chptr))
607           *buf = '=';
608         else if (SecretChannel(chptr))
609           *buf = '@';
610         idx = len + 4;
611         flag = 0;
612       }
613     }
614     if (flag)
615       sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
616   }
617   if (!EmptyString(para))
618   {
619     sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], /* XXX DEAD */
620         ch2ptr ? ch2ptr->chname : para);
621     return (1);
622   }
623
624   /* Second, do all non-public, non-secret channels in one big sweep */
625
626   strcpy(buf, "* * :");
627   idx = 5;
628   flag = 0;
629   for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
630   {
631     struct Channel *ch3ptr;
632     int showflag = 0, secret = 0;
633
634 #ifndef GODMODE
635     if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
636 #else
637     if (!IsUser(c2ptr))
638 #endif
639       continue;
640     member = c2ptr->user->channel;
641     /*
642      * Don't show a client if they are on a secret channel or when
643      * they are on a channel sptr is on since they have already
644      * been show earlier. -avalon
645      */
646     while (member)
647     {
648       ch3ptr = member->channel;
649 #ifndef GODMODE
650       if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
651 #endif
652         showflag = 1;
653       if (SecretChannel(ch3ptr))
654         secret = 1;
655       member = member->next_channel;
656     }
657     if (showflag)               /* Have we already shown them ? */
658       continue;
659 #ifndef GODMODE
660     if (secret)                 /* On any secret channels ? */
661       continue;
662 #endif
663     strcat(buf, c2ptr->name);
664     strcat(buf, " ");
665     idx += strlen(c2ptr->name) + 1;
666     flag = 1;
667 #ifdef GODMODE
668     {
669       char yxx[6];
670       sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
671       assert(c2ptr == findNUser(yxx));
672       sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
673       idx += 6;
674     }
675 #endif
676 #ifdef GODMODE
677     if (mlen + idx + NICKLEN + 9 > BUFSIZE)
678 #else
679     if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
680 #endif
681     {
682       sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
683       strcpy(buf, "* * :");
684       idx = 5;
685       flag = 0;
686     }
687   }
688   if (flag)
689     sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
690   sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], "*"); /* XXX DEAD */
691   return 1;
692 }
693 #endif /* 0 */
694