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