56cc590a7538770287dd7b1fee510e9ad120b922
[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  *
112  */
113
114 void do_names(struct Client* sptr, struct Channel* chptr, int filter)
115
116   int mlen;
117   int idx;
118   int flag;
119   int needs_space; 
120   int len; 
121   char buf[BUFSIZE];
122   struct Client *c2ptr;
123   struct Membership* member;
124
125   /* Tag Pub/Secret channels accordingly. */
126
127   strcpy(buf, "* ");
128   if (PubChannel(chptr))
129     *buf = '=';
130   else if (SecretChannel(chptr))
131     *buf = '@';
132  
133   len = strlen(chptr->chname);
134   strcpy(buf + 2, chptr->chname);
135   strcpy(buf + 2 + len, " :");
136
137   idx = len + 4;
138   flag = 1;
139   needs_space = 0;
140
141   if (!ShowChannel(sptr, chptr)) /* Don't list private channels unless we are on them. */
142     return;
143
144   /* Iterate over all channel members, and build up the list. */
145
146   mlen = strlen(me.name) + 10 + strlen(sptr->name);
147   
148   for (member = chptr->members; member; member = member->next_member)
149   {
150     c2ptr = member->user;
151
152     if ((filter == NAMES_VIS) && IsInvisible(c2ptr))
153       continue;
154
155     if (IsZombie(member) && member->user != sptr)
156       continue;
157
158     if (needs_space) {
159         strcat(buf, " ");
160       idx++;
161     }
162     needs_space=1;
163     if (IsZombie(member))
164     {
165       strcat(buf, "!");
166       idx++;
167     }
168     else if (IsChanOp(member))
169     {
170       strcat(buf, "@");
171       idx++;
172     }
173     else if (HasVoice(member))
174     {
175       strcat(buf, "+");
176       idx++;
177     }
178     strcat(buf, c2ptr->name);
179     idx += strlen(c2ptr->name) + 1;
180     flag = 1;
181     if (mlen + idx + NICKLEN + 5 > BUFSIZE)
182       /* space, modifier, nick, \r \n \0 */
183     { 
184       sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, sptr->name, buf);
185       strcpy(buf, "* ");
186       ircd_strncpy(buf + 2, chptr->chname, len + 1);
187       buf[len + 2] = 0;
188       strcat(buf, " :");
189       if (PubChannel(chptr))
190         *buf = '=';
191       else if (SecretChannel(chptr))
192         *buf = '@';
193       idx = len + 4;
194       flag = 0;
195       needs_space=0;
196     }
197   }
198   if (flag)
199     send_reply(sptr, RPL_NAMREPLY, buf); 
200 }
201
202 /*
203  * m_names - generic message handler
204  *
205  * parv[0] = sender prefix
206  * parv[1] = channel
207  */
208
209 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
210 {
211   struct Channel *chptr; 
212   struct Channel *ch2ptr; 
213   struct Client *c2ptr;
214   struct Membership* member; 
215   char* s;
216   char* para = parc > 1 ? parv[1] : 0; 
217
218   if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
219     return 0; 
220
221   if (EmptyString(para)) {
222     send_reply(sptr, RPL_ENDOFNAMES, "*");
223     return 0;
224   }
225   else if (*para == '0')
226     *para = '\0';
227   
228   s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. Eww. */
229   if (s) {
230     parv[1] = ++s;
231     m_names(cptr, sptr, parc, parv);
232   }
233  
234   /*
235    * Special Case 1: "/names 0". 
236    * Full list as per RFC. 
237    */
238
239   if (!*para) { 
240     int idx; 
241     int mlen;
242     int flag;
243     struct Channel *ch3ptr;
244     char buf[BUFSIZE]; 
245
246     mlen = strlen(me.name) + 10 + strlen(sptr->name);
247
248     /* List all visible channels/visible members */ 
249
250     for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
251     { 
252       if (!ShowChannel(sptr, ch2ptr))
253         continue;                 /* Don't show secret chans. */ 
254
255       if (find_channel_member(sptr, ch2ptr))
256       {
257         do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
258       } else { 
259         do_names(sptr, ch2ptr, NAMES_VIS);
260       }
261     } 
262
263     /* List all remaining users on channel '*' */
264
265     strcpy(buf, "* * :");
266     idx = 5;
267     flag = 0;
268
269     for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
270     {
271       int showflag = 0;
272
273       if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
274         continue;
275
276       member = c2ptr->user->channel; 
277
278       while (member)
279       {
280         ch3ptr = member->channel;
281   
282         if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
283           showflag = 1;
284  
285         member = member->next_channel;
286       }
287
288       if (showflag)               /* Have we already shown them? */
289         continue;
290  
291       strcat(buf, c2ptr->name);
292       strcat(buf, " ");
293       idx += strlen(c2ptr->name) + 1;
294       flag = 1;
295
296       if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
297       {
298         send_reply(sptr, RPL_NAMREPLY, buf);
299         strcpy(buf, "* * :");
300         idx = 5;
301         flag = 0;
302       }
303     }
304     if (flag)
305       send_reply(sptr, RPL_NAMREPLY, buf);
306     send_reply(sptr, RPL_ENDOFNAMES, "*");
307     return 1; 
308   } 
309
310   /*
311    *  Special Case 2: User is on this channel, requesting full names list.
312    *  (As performed with each /join) - ** High frequency usage **
313    */
314
315   clean_channelname(para);
316   chptr = FindChannel(para); 
317
318   if (chptr) {
319     member = find_member_link(chptr, sptr);
320     if (member)
321     { 
322       do_names(sptr, chptr, NAMES_ALL);
323       if (!EmptyString(para))
324       {
325         send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
326         return 1;
327       }
328     }
329       else 
330     {
331       /*
332        *  Special Case 3: User isn't on this channel, show all visible users, in 
333        *  non secret channels.
334        */ 
335       do_names(sptr, chptr, NAMES_VIS);
336     } 
337   } else { /* Channel doesn't exist. */ 
338       send_reply(sptr, RPL_ENDOFNAMES, para); 
339   }
340   return 1;
341 }
342
343  
344 /*
345  * ms_names - server message handler
346  *
347  * parv[0] = sender prefix
348  * parv[1] = channel
349  */
350 int ms_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
351 {
352   struct Channel *chptr; 
353   struct Channel *ch2ptr; 
354   struct Client *c2ptr;
355   struct Membership* member; 
356   char* s;
357   char* para = parc > 1 ? parv[1] : 0; 
358
359   if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
360     return 0; 
361
362   if (EmptyString(para)) {
363     send_reply(sptr, RPL_ENDOFNAMES, "*");
364     return 0;
365   }
366   else if (*para == '0')
367     *para = '\0';
368   
369   s = strchr(para, ','); /* Recursively call m_names for each comma-seperated channel. */
370   if (s) {
371     parv[1] = ++s;
372     m_names(cptr, sptr, parc, parv);
373   }
374  
375   /*
376    * Special Case 1: "/names 0".
377    * Full list as per RFC. 
378    */
379
380   if (!*para) { 
381     int idx; 
382     int mlen;
383     int flag;
384     struct Channel *ch3ptr;
385     char buf[BUFSIZE]; 
386
387     mlen = strlen(me.name) + 10 + strlen(sptr->name);
388
389     /* List all visible channels/visible members */ 
390
391     for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
392     { 
393       if (!ShowChannel(sptr, ch2ptr))
394         continue;                 /* Don't show secret chans. */ 
395
396       if (find_channel_member(sptr, ch2ptr))
397       {
398         do_names(sptr, ch2ptr, NAMES_ALL); /* Full list if we're in this chan. */
399       } else { 
400         do_names(sptr, ch2ptr, NAMES_VIS);
401       }
402     } 
403  
404     /* List all remaining users on channel '*' */
405
406     strcpy(buf, "* * :");
407     idx = 5;
408     flag = 0;
409
410     for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
411     {
412       int showflag = 0;
413
414       if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
415         continue;
416
417       member = c2ptr->user->channel; 
418
419       while (member)
420       {
421         ch3ptr = member->channel;
422   
423         if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
424           showflag = 1;
425  
426         member = member->next_channel;
427       }
428
429       if (showflag)               /* Have we already shown them? */
430         continue;
431  
432       strcat(buf, c2ptr->name);
433       strcat(buf, " ");
434       idx += strlen(c2ptr->name) + 1;
435       flag = 1;
436
437       if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
438       {
439         send_reply(sptr, RPL_NAMREPLY, buf);
440         strcpy(buf, "* * :");
441         idx = 5;
442         flag = 0;
443       }
444     }
445     if (flag)
446       send_reply(sptr, RPL_NAMREPLY, buf);
447     send_reply(sptr, RPL_ENDOFNAMES, "*");
448     return 1; 
449   } 
450
451   /*
452    *  Special Case 2: User is on this channel, requesting full names list.
453    *  (As performed with each /join) - ** High frequency usage **
454    */
455
456   clean_channelname(para);
457   chptr = FindChannel(para); 
458
459   if (chptr) {
460     member = find_member_link(chptr, sptr);
461     if (member)
462     { 
463       do_names(sptr, chptr, NAMES_ALL);
464       if (!EmptyString(para))
465       {
466         send_reply(sptr, RPL_ENDOFNAMES, chptr ? chptr->chname : para);
467         return 1;
468       }
469     }
470       else 
471     {
472       /*
473        *  Special Case 3: User isn't on this channel, show all visible users, in 
474        *  non secret channels.
475        */ 
476       do_names(sptr, chptr, NAMES_VIS);
477     } 
478   } else { /* Channel doesn't exist. */ 
479       send_reply(sptr, RPL_ENDOFNAMES, para); 
480   }
481   return 1;
482 }
483  
484
485 #if 0
486 /*
487  * m_names                              - Added by Jto 27 Apr 1989
488  *
489  * parv[0] = sender prefix
490  * parv[1] = channel
491  */
492 int m_names(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
493 {
494   struct Channel *chptr;
495   struct Client *c2ptr;
496   struct Membership* member;
497   struct Channel *ch2ptr = 0;
498   int idx, flag, len, mlen;
499   char *s, *para = parc > 1 ? parv[1] : 0;
500   char buf[BUFSIZE];
501
502   if (parc > 2 && hunt_server(1, cptr, sptr, "%s%s " TOK_NAMES " %s %s", 2, parc, parv)) /* XXX DEAD */
503     return 0;
504
505   mlen = strlen(me.name) + 10 + strlen(sptr->name);
506
507   if (!EmptyString(para))
508   {
509     s = strchr(para, ',');
510     if (s)
511     {
512       parv[1] = ++s;
513       m_names(cptr, sptr, parc, parv);
514     }
515     clean_channelname(para);
516     ch2ptr = FindChannel(para);
517   }
518
519   /*
520    * First, do all visible channels (public and the one user self is)
521    */
522
523   for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
524   {
525     if ((chptr != ch2ptr) && !EmptyString(para))
526       continue;                 /* -- wanted a specific channel */
527     if (!MyConnect(sptr) && EmptyString(para))
528       continue;
529 #ifndef GODMODE
530     if (!ShowChannel(sptr, chptr))
531       continue;                 /* -- users on this are not listed */
532 #endif
533
534     /* Find users on same channel (defined by chptr) */
535
536     strcpy(buf, "* ");
537     len = strlen(chptr->chname);
538     strcpy(buf + 2, chptr->chname);
539     strcpy(buf + 2 + len, " :");
540
541     if (PubChannel(chptr))
542       *buf = '=';
543     else if (SecretChannel(chptr))
544       *buf = '@';
545     idx = len + 4;
546     flag = 1;
547     for (member = chptr->members; member; member = member->next_member)
548     {
549       c2ptr = member->user;
550 #ifndef GODMODE
551       if (sptr != c2ptr && IsInvisible(c2ptr) && !find_channel_member(sptr, chptr))
552         continue;
553 #endif
554       if (IsZombie(member))
555       {
556         if (member->user != sptr)
557           continue;
558         else
559         {
560           strcat(buf, "!");
561           idx++;
562         }
563       }
564       else if (IsChanOp(member))
565       {
566         strcat(buf, "@");
567         idx++;
568       }
569       else if (HasVoice(member))
570       {
571         strcat(buf, "+");
572         idx++;
573       }
574       strcat(buf, c2ptr->name);
575       strcat(buf, " ");
576       idx += strlen(c2ptr->name) + 1;
577       flag = 1;
578 #ifdef GODMODE
579       {
580         char yxx[6];
581         sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
582         assert(c2ptr == findNUser(yxx));
583         sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
584         idx += 6;
585       }
586       if (mlen + idx + NICKLEN + 11 > BUFSIZE)
587 #else
588       if (mlen + idx + NICKLEN + 5 > BUFSIZE)
589 #endif
590         /* space, modifier, nick, \r \n \0 */
591       {
592         sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
593         strcpy(buf, "* ");
594         ircd_strncpy(buf + 2, chptr->chname, len + 1);
595         buf[len + 2] = 0;
596         strcat(buf, " :");
597         if (PubChannel(chptr))
598           *buf = '=';
599         else if (SecretChannel(chptr))
600           *buf = '@';
601         idx = len + 4;
602         flag = 0;
603       }
604     }
605     if (flag)
606       sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
607   }
608   if (!EmptyString(para))
609   {
610     sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], /* XXX DEAD */
611         ch2ptr ? ch2ptr->chname : para);
612     return (1);
613   }
614
615   /* Second, do all non-public, non-secret channels in one big sweep */
616
617   strcpy(buf, "* * :");
618   idx = 5;
619   flag = 0;
620   for (c2ptr = GlobalClientList; c2ptr; c2ptr = c2ptr->next)
621   {
622     struct Channel *ch3ptr;
623     int showflag = 0, secret = 0;
624
625 #ifndef GODMODE
626     if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
627 #else
628     if (!IsUser(c2ptr))
629 #endif
630       continue;
631     member = c2ptr->user->channel;
632     /*
633      * Don't show a client if they are on a secret channel or when
634      * they are on a channel sptr is on since they have already
635      * been show earlier. -avalon
636      */
637     while (member)
638     {
639       ch3ptr = member->channel;
640 #ifndef GODMODE
641       if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
642 #endif
643         showflag = 1;
644       if (SecretChannel(ch3ptr))
645         secret = 1;
646       member = member->next_channel;
647     }
648     if (showflag)               /* Have we already shown them ? */
649       continue;
650 #ifndef GODMODE
651     if (secret)                 /* On any secret channels ? */
652       continue;
653 #endif
654     strcat(buf, c2ptr->name);
655     strcat(buf, " ");
656     idx += strlen(c2ptr->name) + 1;
657     flag = 1;
658 #ifdef GODMODE
659     {
660       char yxx[6];
661       sprintf_irc(yxx, "%s%s", NumNick(c2ptr));
662       assert(c2ptr == findNUser(yxx));
663       sprintf_irc(buf + strlen(buf), "(%s) ", yxx);
664       idx += 6;
665     }
666 #endif
667 #ifdef GODMODE
668     if (mlen + idx + NICKLEN + 9 > BUFSIZE)
669 #else
670     if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
671 #endif
672     {
673       sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
674       strcpy(buf, "* * :");
675       idx = 5;
676       flag = 0;
677     }
678   }
679   if (flag)
680     sendto_one(sptr, rpl_str(RPL_NAMREPLY), me.name, parv[0], buf); /* XXX DEAD */
681   sendto_one(sptr, rpl_str(RPL_ENDOFNAMES), me.name, parv[0], "*"); /* XXX DEAD */
682   return 1;
683 }
684 #endif /* 0 */
685