Author: Kev <klmitch@mit.edu>
[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       send_reply(sptr, RPL_NAMREPLY, 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,
222                                   parv))
223     return 0; 
224
225   if (EmptyString(para))
226     return 0;
227   else if (*para == '0')
228     *para = '\0';
229   
230   s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
231   if (s) {
232     parv[1] = ++s;
233     m_names(cptr, sptr, parc, parv);
234   }
235  
236   /*
237    * Special Case 1: "/names 0". 
238    * Full list as per RFC. 
239    */
240
241   if (!*para) { 
242     int idx; 
243     int mlen;
244     int flag;
245     struct Channel *ch3ptr;
246     char buf[BUFSIZE]; 
247
248     mlen = strlen(me.name) + 10 + strlen(sptr->name);
249
250     /* List all visible channels/visible members */ 
251
252     for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
253     { 
254       if (!ShowChannel(sptr, ch2ptr))
255         continue;                 /* Don't show secret chans. */ 
256
257       if (find_channel_member(sptr, ch2ptr))
258       {
259         do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
260       } else { 
261         do_names(sptr, ch2ptr, NAMES_VIS);
262       }
263     } 
264
265     /* List all remaining users on channel '*' */
266
267     strcpy(buf, "* * :");
268     idx = 5;
269     flag = 0;
270
271     for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
272     {
273       int showflag = 0;
274
275       if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
276         continue;
277
278       member = c2ptr->user->channel; 
279
280       while (member)
281       {
282         ch3ptr = member->channel;
283   
284         if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
285           showflag = 1;
286  
287         member = member->next_channel;
288       }
289
290       if (showflag)               /* Have we already shown them? */
291         continue;
292  
293       strcat(buf, c2ptr->name);
294       strcat(buf, " ");
295       idx += strlen(c2ptr->name) + 1;
296       flag = 1;
297
298       if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
299       {
300         send_reply(sptr, RPL_NAMREPLY, buf);
301         strcpy(buf, "* * :");
302         idx = 5;
303         flag = 0;
304       }
305     }
306     if (flag)
307       send_reply(sptr, RPL_NAMREPLY, buf);
308     send_reply(sptr, RPL_ENDOFNAMES, "*");
309     return 1; 
310   } 
311
312   /*
313    *  Special Case 2: User is on this channel, requesting full names list.
314    *  (As performed with each /join) - ** High frequency usage **
315    */
316
317   clean_channelname(para);
318   chptr = FindChannel(para); 
319  
320   member = find_member_link(chptr, sptr); 
321   if (member)
322   { 
323     if (chptr) do_names(sptr, chptr, NAMES_ALL);
324     if (!EmptyString(para))
325     {
326       send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
327       return 1;
328     }
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,
365                                   parv))
366     return 0; 
367
368   if (EmptyString(para))
369     return 0;
370   else if (*para == '0')
371     *para = '\0';
372   
373   s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
374   if (s) {
375     parv[1] = ++s;
376     m_names(cptr, sptr, parc, parv);
377   }
378  
379   /*
380    * Special Case 1: "/names 0".
381    * Full list as per RFC. 
382    */
383
384   if (!*para) { 
385     int idx; 
386     int mlen;
387     int flag;
388     struct Channel *ch3ptr;
389     char buf[BUFSIZE]; 
390
391     mlen = strlen(me.name) + 10 + strlen(sptr->name);
392
393     /* List all visible channels/visible members */ 
394
395     for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
396     { 
397       if (!ShowChannel(sptr, ch2ptr))
398         continue;                 /* Don't show secret chans. */ 
399
400       if (find_channel_member(sptr, ch2ptr))
401       {
402         do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
403       } else { 
404         do_names(sptr, ch2ptr, NAMES_VIS);
405       }
406     } 
407  
408     /* List all remaining users on channel '*' */
409
410     strcpy(buf, "* * :");
411     idx = 5;
412     flag = 0;
413
414     for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
415     {
416       int showflag = 0;
417
418       if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
419         continue;
420
421       member = c2ptr->user->channel; 
422
423       while (member)
424       {
425         ch3ptr = member->channel;
426   
427         if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
428           showflag = 1;
429  
430         member = member->next_channel;
431       }
432
433       if (showflag)               /* Have we already shown them? */
434         continue;
435  
436       strcat(buf, c2ptr->name);
437       strcat(buf, " ");
438       idx += strlen(c2ptr->name) + 1;
439       flag = 1;
440
441       if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
442       {
443         send_reply(sptr, RPL_NAMREPLY, buf);
444         strcpy(buf, "* * :");
445         idx = 5;
446         flag = 0;
447       }
448     }
449     if (flag)
450       send_reply(sptr, RPL_NAMREPLY, buf);
451     send_reply(sptr, RPL_ENDOFNAMES, "*");
452     return 1; 
453   } 
454
455   /*
456    *  Special Case 2: User is on this channel, requesting full names list.
457    *  (As performed with each /join) - ** High frequency usage **
458    */
459
460   clean_channelname(para);
461   chptr = FindChannel(para); 
462  
463   member = find_member_link(chptr, sptr); 
464   if (member)
465   { 
466     if (chptr) 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   }
474     else
475   {
476     /*
477      *  Special Case 3: User isn't on this channel, show all visible users, in 
478      *  non secret channels.
479      */
480      
481     if (chptr) do_names(sptr, chptr, NAMES_VIS);
482     if (!EmptyString(para))
483     {
484       send_reply(sptr, RPL_ENDOFNAMES, para);
485       return 1;
486     }
487   } 
488   return 1;
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