Author: Isomer <isomer@coders.net>
[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 /*
106  *  Sends a suitably formatted 'names' reply to 'sptr' consisting of nicks within
107  *  'chptr', depending on 'filter'.
108  *
109  *  NAMES_ALL - Lists all users on channel.
110  *  NAMES_VIS - Only list visible (-i) users. --Gte (04/06/2000).
111  *  NAMES_EON - When OR'd with the other two, adds an 'End of Names' numeric
112  *              used by m_join
113  *
114  */
115
116 void do_names(struct Client* sptr, struct Channel* chptr, int filter)
117
118   int mlen;
119   int idx;
120   int flag;
121   int needs_space; 
122   int len; 
123   char buf[BUFSIZE];
124   struct Client *c2ptr;
125   struct Membership* member;
126   
127   assert(chptr);
128   assert(sptr);
129   assert((filter&NAMES_ALL) != (filter&NAMES_VIS));
130
131   /* Tag Pub/Secret channels accordingly. */
132
133   strcpy(buf, "* ");
134   if (PubChannel(chptr))
135     *buf = '=';
136   else if (SecretChannel(chptr))
137     *buf = '@';
138  
139   len = strlen(chptr->chname);
140   strcpy(buf + 2, chptr->chname);
141   strcpy(buf + 2 + len, " :");
142
143   idx = len + 4;
144   flag = 1;
145   needs_space = 0;
146
147   if (!ShowChannel(sptr, chptr)) /* Don't list private channels unless we are on them. */
148     return;
149
150   /* Iterate over all channel members, and build up the list. */
151
152   mlen = strlen(me.name) + 10 + strlen(sptr->name);
153   
154   for (member = chptr->members; member; member = member->next_member)
155   {
156     c2ptr = member->user;
157
158     if (((filter&NAMES_VIS)!=0) && IsInvisible(c2ptr))
159       continue;
160
161     if (IsZombie(member) && member->user != sptr)
162       continue;
163
164     if (needs_space) {
165         strcat(buf, " ");
166       idx++;
167     }
168     needs_space=1;
169     if (IsZombie(member))
170     {
171       strcat(buf, "!");
172       idx++;
173     }
174     else if (IsChanOp(member))
175     {
176       strcat(buf, "@");
177       idx++;
178     }
179     else if (HasVoice(member))
180     {
181       strcat(buf, "+");
182       idx++;
183     }
184     strcat(buf, c2ptr->name);
185     idx += strlen(c2ptr->name) + 1;
186     flag = 1;
187     if (mlen + idx + NICKLEN + 5 > BUFSIZE)
188       /* space, modifier, nick, \r \n \0 */
189     { 
190       sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, sptr->name, buf);
191       strcpy(buf, "* ");
192       ircd_strncpy(buf + 2, chptr->chname, len + 1);
193       buf[len + 2] = 0;
194       strcat(buf, " :");
195       if (PubChannel(chptr))
196         *buf = '=';
197       else if (SecretChannel(chptr))
198         *buf = '@';
199       idx = len + 4;
200       flag = 0;
201       needs_space=0;
202     }
203   }
204   if (flag)
205     send_reply(sptr, RPL_NAMREPLY, buf); 
206   if (filter&NAMES_EON)
207     send_reply(sptr, RPL_ENDOFNAMES, chptr->chname);
208 }
209
210 /*
211  * m_names - generic message handler
212  *
213  * parv[0] = sender prefix
214  * parv[1] = channel
215  */
216
217 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
218 {
219   struct Channel *chptr; 
220   struct Channel *ch2ptr; 
221   struct Client *c2ptr;
222   struct Membership* member; 
223   char* s;
224   char* para = parc > 1 ? parv[1] : 0; 
225
226   if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
227     return 0; 
228
229   if (EmptyString(para)) {
230     send_reply(sptr, RPL_ENDOFNAMES, "*");
231     return 0;
232   }
233   else if (*para == '0')
234     *para = '\0';
235   
236   s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. Eww. */
237   if (s) {
238     parv[1] = ++s;
239     m_names(cptr, sptr, parc, parv);
240   }
241  
242   /*
243    * Special Case 1: "/names 0". 
244    * Full list as per RFC. 
245    */
246
247   if (!*para) { 
248     int idx; 
249     int mlen;
250     int flag;
251     struct Channel *ch3ptr;
252     char buf[BUFSIZE]; 
253
254     mlen = strlen(me.name) + 10 + strlen(sptr->name);
255
256     /* List all visible channels/visible members */ 
257
258     for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
259     { 
260       if (!ShowChannel(sptr, ch2ptr))
261         continue;                 /* Don't show secret chans. */ 
262
263       if (find_channel_member(sptr, ch2ptr))
264       {
265         do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
266       } else { 
267         do_names(sptr, ch2ptr, NAMES_VIS);
268       }
269     } 
270
271     /* List all remaining users on channel '*' */
272
273     strcpy(buf, "* * :");
274     idx = 5;
275     flag = 0;
276
277     for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
278     {
279       int showflag = 0;
280
281       if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
282         continue;
283
284       member = c2ptr->user->channel; 
285
286       while (member)
287       {
288         ch3ptr = member->channel;
289   
290         if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
291           showflag = 1;
292  
293         member = member->next_channel;
294       }
295
296       if (showflag)               /* Have we already shown them? */
297         continue;
298  
299       strcat(buf, c2ptr->name);
300       strcat(buf, " ");
301       idx += strlen(c2ptr->name) + 1;
302       flag = 1;
303
304       if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
305       {
306         send_reply(sptr, RPL_NAMREPLY, buf);
307         strcpy(buf, "* * :");
308         idx = 5;
309         flag = 0;
310       }
311     }
312     if (flag)
313       send_reply(sptr, RPL_NAMREPLY, buf);
314     send_reply(sptr, RPL_ENDOFNAMES, "*");
315     return 1; 
316   } 
317
318   /*
319    *  Special Case 2: User is on this channel, requesting full names list.
320    *  (As performed with each /join) - ** High frequency usage **
321    */
322
323   clean_channelname(para);
324   chptr = FindChannel(para); 
325
326   if (chptr) {
327     member = find_member_link(chptr, sptr);
328     if (member)
329     { 
330       do_names(sptr, chptr, NAMES_ALL);
331       if (!EmptyString(para))
332       {
333         send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
334         return 1;
335       }
336     }
337       else 
338     {
339       /*
340        *  Special Case 3: User isn't on this channel, show all visible users, in 
341        *  non secret channels.
342        */ 
343       do_names(sptr, chptr, NAMES_VIS);
344     } 
345   } else { /* Channel doesn't exist. */ 
346       send_reply(sptr, RPL_ENDOFNAMES, para); 
347   }
348   return 1;
349 }
350
351  
352 /*
353  * ms_names - server message handler
354  *
355  * parv[0] = sender prefix
356  * parv[1] = channel
357  */
358 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
359 {
360   struct Channel *chptr; 
361   struct Channel *ch2ptr; 
362   struct Client *c2ptr;
363   struct Membership* member; 
364   char* s;
365   char* para = parc > 1 ? parv[1] : 0; 
366
367   if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
368     return 0; 
369
370   if (EmptyString(para)) {
371     send_reply(sptr, RPL_ENDOFNAMES, "*");
372     return 0;
373   }
374   else if (*para == '0')
375     *para = '\0';
376   
377   s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
378   if (s) {
379     parv[1] = ++s;
380     m_names(cptr, sptr, parc, parv);
381   }
382  
383   /*
384    * Special Case 1: "/names 0".
385    * Full list as per RFC. 
386    */
387
388   if (!*para) { 
389     int idx; 
390     int mlen;
391     int flag;
392     struct Channel *ch3ptr;
393     char buf[BUFSIZE]; 
394
395     mlen = strlen(me.name) + 10 + strlen(sptr->name);
396
397     /* List all visible channels/visible members */ 
398
399     for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
400     { 
401       if (!ShowChannel(sptr, ch2ptr))
402         continue;                 /* Don't show secret chans. */ 
403
404       if (find_channel_member(sptr, ch2ptr))
405       {
406         do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
407       } else { 
408         do_names(sptr, ch2ptr, NAMES_VIS);
409       }
410     } 
411  
412     /* List all remaining users on channel '*' */
413
414     strcpy(buf, "* * :");
415     idx = 5;
416     flag = 0;
417
418     for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
419     {
420       int showflag = 0;
421
422       if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
423         continue;
424
425       member = c2ptr->user->channel; 
426
427       while (member)
428       {
429         ch3ptr = member->channel;
430   
431         if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
432           showflag = 1;
433  
434         member = member->next_channel;
435       }
436
437       if (showflag)               /* Have we already shown them? */
438         continue;
439  
440       strcat(buf, c2ptr->name);
441       strcat(buf, " ");
442       idx += strlen(c2ptr->name) + 1;
443       flag = 1;
444
445       if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
446       {
447         send_reply(sptr, RPL_NAMREPLY, buf);
448         strcpy(buf, "* * :");
449         idx = 5;
450         flag = 0;
451       }
452     }
453     if (flag)
454       send_reply(sptr, RPL_NAMREPLY, buf);
455     send_reply(sptr, RPL_ENDOFNAMES, "*");
456     return 1; 
457   } 
458
459   /*
460    *  Special Case 2: User is on this channel, requesting full names list.
461    *  (As performed with each /join) - ** High frequency usage **
462    */
463
464   clean_channelname(para);
465   chptr = FindChannel(para); 
466
467   if (chptr) {
468     member = find_member_link(chptr, sptr);
469     if (member)
470     { 
471       do_names(sptr, chptr, NAMES_ALL);
472       if (!EmptyString(para))
473       {
474         send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
475         return 1;
476       }
477     }
478       else 
479     {
480       /*
481        *  Special Case 3: User isn't on this channel, show all visible users, in 
482        *  non secret channels.
483        */ 
484       do_names(sptr, chptr, NAMES_VIS);
485     } 
486   } else { /* Channel doesn't exist. */ 
487       send_reply(sptr, RPL_ENDOFNAMES, para); 
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