Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / channel.c
1 /*
2  * IRC - Internet Relay Chat, ircd/channel.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Co Center
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 1, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * $Id$
21  */
22 #include "channel.h"
23 #include "client.h"
24 #include "hash.h"
25 #include "ircd.h"
26 #include "ircd_alloc.h"
27 #include "ircd_chattr.h"
28 #include "ircd_defs.h"
29 #include "ircd_log.h"
30 #include "ircd_reply.h"
31 #include "ircd_snprintf.h"
32 #include "ircd_string.h"
33 #include "list.h"
34 #include "match.h"
35 #include "msg.h"
36 #include "msgq.h"
37 #include "numeric.h"
38 #include "numnicks.h"
39 #include "querycmds.h"
40 #include "s_bsd.h"
41 #include "s_conf.h"
42 #include "s_debug.h"
43 #include "s_misc.h"
44 #include "s_user.h"
45 #include "send.h"
46 #include "sprintf_irc.h"
47 #include "struct.h"
48 #include "support.h"
49 #include "sys.h"
50 #include "whowas.h"
51
52 #include <assert.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56
57 struct Channel* GlobalChannelList = 0;
58
59 static unsigned int membershipAllocCount;
60 static struct Membership* membershipFreeList;
61
62 static struct SLink *next_overlapped_ban(void);
63 static int del_banid(struct Channel *, char *, int);
64 void del_invite(struct Client *, struct Channel *);
65
66 const char* const PartFmt1     = ":%s " MSG_PART " %s";
67 const char* const PartFmt2     = ":%s " MSG_PART " %s :%s";
68 const char* const PartFmt1serv = "%s%s " TOK_PART " %s";
69 const char* const PartFmt2serv = "%s%s " TOK_PART " %s :%s";
70
71
72 static struct SLink* next_ban;
73 static struct SLink* prev_ban;
74 static struct SLink* removed_bans_list;
75
76 /*
77  * Use a global variable to remember if an oper set a mode on a local channel. Ugly,
78  * but the only way to do it without changing set_mode intensively.
79  */
80 int LocalChanOperMode = 0;
81
82 #if !defined(NDEBUG)
83 /*
84  * return the length (>=0) of a chain of links.
85  */
86 static int list_length(struct SLink *lp)
87 {
88   int count = 0;
89
90   for (; lp; lp = lp->next)
91     ++count;
92   return count;
93 }
94 #endif
95
96 struct Membership* find_member_link(struct Channel* chptr, const struct Client* cptr)
97 {
98   struct Membership *m;
99   assert(0 != cptr);
100   assert(0 != chptr);
101   
102   /* Servers don't have member links */
103   if (IsServer(cptr)||IsMe(cptr))
104      return 0;
105   
106   /* +k users are typically on a LOT of channels.  So we iterate over who
107    * is in the channel.  X/W are +k and are in about 5800 channels each.
108    * however there are typically no more than 1000 people in a channel
109    * at a time.
110    */
111   if (IsChannelService(cptr)) {
112     m = chptr->members;
113     while (m) {
114       assert(m->channel == chptr);
115       if (m->user == cptr)
116         return m;
117       m = m->next_member;
118     }
119   }
120   /* Users on the other hand aren't allowed on more than 15 channels.  50%
121    * of users that are on channels are on 2 or less, 95% are on 7 or less,
122    * and 99% are on 10 or less.
123    */
124   else {
125    m = cptr->user->channel;
126    while (m) {
127      assert(m->user == cptr);
128      if (m->channel == chptr)
129        return m;
130      m = m->next_channel;
131    }
132   }
133   return 0;
134 }
135
136 /*
137  * find_chasing - Find the client structure for a nick name (user)
138  * using history mechanism if necessary. If the client is not found, an error
139  * message (NO SUCH NICK) is generated. If the client was found
140  * through the history, chasing will be 1 and otherwise 0.
141  */
142 struct Client* find_chasing(struct Client* sptr, const char* user, int* chasing)
143 {
144   struct Client* who = FindClient(user);
145
146   if (chasing)
147     *chasing = 0;
148   if (who)
149     return who;
150
151   if (!(who = get_history(user, KILLCHASETIMELIMIT))) {
152     send_reply(sptr, ERR_NOSUCHNICK, user);
153     return 0;
154   }
155   if (chasing)
156     *chasing = 1;
157   return who;
158 }
159
160 /*
161  * Create a string of form "foo!bar@fubar" given foo, bar and fubar
162  * as the parameters.  If NULL, they become "*".
163  */
164 static char *make_nick_user_host(const char *nick, const char *name,
165                                  const char *host)
166 {
167   static char namebuf[NICKLEN + USERLEN + HOSTLEN + 3];
168   sprintf_irc(namebuf, "%s!%s@%s", nick, name, host);
169   return namebuf;
170 }
171
172 /*
173  * Create a string of form "foo!bar@123.456.789.123" given foo, bar and the
174  * IP-number as the parameters.  If NULL, they become "*".
175  */
176 static char *make_nick_user_ip(char *nick, char *name, struct in_addr ip)
177 {
178   static char ipbuf[NICKLEN + USERLEN + 16 + 3];
179   sprintf_irc(ipbuf, "%s!%s@%s", nick, name, ircd_ntoa((const char*) &ip));
180   return ipbuf;
181 }
182
183 #if 0
184 static int DoesOp(const char* modebuf)
185 {
186   assert(0 != modebuf);
187   while (*modebuf) {
188     if (*modebuf == 'o' || *modebuf == 'v')
189       return 1;
190     ++modebuf;
191   }
192   return 0;
193 }
194
195 /*
196  * This function should be removed when all servers are 2.10
197  */
198 static void sendmodeto_one(struct Client* cptr, const char* from,
199                            const char* name, const char* mode,
200                            const char* param, time_t creationtime)
201 {
202   if (IsServer(cptr) && DoesOp(mode) && creationtime)
203     sendto_one(cptr, ":%s MODE %s %s %s " TIME_T_FMT, /* XXX DEAD */
204                from, name, mode, param, creationtime);
205   else
206     sendto_one(cptr, ":%s MODE %s %s %s", from, name, mode, param); /* XXX DEAD */
207 }
208 #endif /* 0 */
209
210 /*
211  * Subtract one user from channel i (and free channel
212  * block, if channel became empty).
213  * Returns: true  (1) if channel still exists
214  *          false (0) if the channel was destroyed
215  */
216 int sub1_from_channel(struct Channel* chptr)
217 {
218   struct SLink *tmp;
219   struct SLink *obtmp;
220
221   if (chptr->users > 1)         /* Can be 0, called for an empty channel too */
222   {
223     assert(0 != chptr->members);
224     --chptr->users;
225     return 1;
226   }
227
228   assert(0 == chptr->members);
229
230   /* Channel became (or was) empty: Remove channel */
231   if (is_listed(chptr))
232   {
233     int i;
234     for (i = 0; i <= HighestFd; i++)
235     {
236       struct Client *acptr = 0;
237       if ((acptr = LocalClientArray[i]) && acptr->listing &&
238           acptr->listing->chptr == chptr)
239       {
240         list_next_channels(acptr, 1);
241         break;                  /* Only one client can list a channel */
242       }
243     }
244   }
245   /*
246    * Now, find all invite links from channel structure
247    */
248   while ((tmp = chptr->invites))
249     del_invite(tmp->value.cptr, chptr);
250
251   tmp = chptr->banlist;
252   while (tmp)
253   {
254     obtmp = tmp;
255     tmp = tmp->next;
256     MyFree(obtmp->value.ban.banstr);
257     MyFree(obtmp->value.ban.who);
258     free_link(obtmp);
259   }
260   if (chptr->prev)
261     chptr->prev->next = chptr->next;
262   else
263     GlobalChannelList = chptr->next;
264   if (chptr->next)
265     chptr->next->prev = chptr->prev;
266   hRemChannel(chptr);
267   --UserStats.channels;
268   /*
269    * make sure that channel actually got removed from hash table
270    */
271   assert(chptr->hnext == chptr);
272   MyFree(chptr);
273   return 0;
274 }
275
276 /*
277  * add_banid
278  *
279  * `cptr' must be the client adding the ban.
280  *
281  * If `change' is true then add `banid' to channel `chptr'.
282  * Returns 0 if the ban was added.
283  * Returns -2 if the ban already existed and was marked CHFL_BURST_BAN_WIPEOUT.
284  * Return -1 otherwise.
285  *
286  * Those bans that overlapped with `banid' are flagged with CHFL_BAN_OVERLAPPED
287  * when `change' is false, otherwise they will be removed from the banlist.
288  * Subsequently calls to next_overlapped_ban() or next_removed_overlapped_ban()
289  * respectively will return these bans until NULL is returned.
290  *
291  * If `firsttime' is true, the ban list as returned by next_overlapped_ban()
292  * is reset (unless a non-zero value is returned, in which case the
293  * CHFL_BAN_OVERLAPPED flag might not have been reset!).
294  *
295  * --Run
296  */
297 int add_banid(struct Client *cptr, struct Channel *chptr, char *banid,
298                      int change, int firsttime)
299 {
300   struct SLink*  ban;
301   struct SLink** banp;
302   int            cnt = 0;
303   int            removed_bans = 0;
304   int            len = strlen(banid);
305
306   if (firsttime)
307   {
308     next_ban = NULL;
309     assert(0 == prev_ban);
310     assert(0 == removed_bans_list);
311   }
312   if (MyUser(cptr))
313     collapse(banid);
314   for (banp = &chptr->banlist; *banp;)
315   {
316     len += strlen((*banp)->value.ban.banstr);
317     ++cnt;
318     if (((*banp)->flags & CHFL_BURST_BAN_WIPEOUT))
319     {
320       if (!strcmp((*banp)->value.ban.banstr, banid))
321       {
322         (*banp)->flags &= ~CHFL_BURST_BAN_WIPEOUT;
323         return -2;
324       }
325     }
326     else if (!mmatch((*banp)->value.ban.banstr, banid))
327       return -1;
328     if (!mmatch(banid, (*banp)->value.ban.banstr))
329     {
330       struct SLink *tmp = *banp;
331       if (change)
332       {
333         if (MyUser(cptr))
334         {
335           cnt--;
336           len -= strlen(tmp->value.ban.banstr);
337         }
338         *banp = tmp->next;
339 #if 0
340         /* Silently remove overlapping bans */
341         MyFree(tmp->value.ban.banstr);
342         MyFree(tmp->value.ban.who);
343         free_link(tmp);
344         tmp = 0;
345 #else
346         /* These will be sent to the user later as -b */
347         tmp->next = removed_bans_list;
348         removed_bans_list = tmp;
349         removed_bans = 1;
350 #endif
351       }
352       else if (!(tmp->flags & CHFL_BURST_BAN_WIPEOUT))
353       {
354         tmp->flags |= CHFL_BAN_OVERLAPPED;
355         if (!next_ban)
356           next_ban = tmp;
357         banp = &tmp->next;
358       }
359       else
360         banp = &tmp->next;
361     }
362     else
363     {
364       if (firsttime)
365         (*banp)->flags &= ~CHFL_BAN_OVERLAPPED;
366       banp = &(*banp)->next;
367     }
368   }
369   if (MyUser(cptr) && !removed_bans && (len > MAXBANLENGTH || (cnt >= MAXBANS)))
370   {
371     send_reply(cptr, ERR_BANLISTFULL, chptr->chname, banid);
372     return -1;
373   }
374   if (change)
375   {
376     char*              ip_start;
377     struct Membership* member;
378     ban = make_link();
379     ban->next = chptr->banlist;
380
381     ban->value.ban.banstr = (char*) MyMalloc(strlen(banid) + 1);
382     assert(0 != ban->value.ban.banstr);
383     strcpy(ban->value.ban.banstr, banid);
384
385     ban->value.ban.who = (char*) MyMalloc(strlen(cptr->name) + 1);
386     assert(0 != ban->value.ban.who);
387     strcpy(ban->value.ban.who, cptr->name);
388
389     ban->value.ban.when = TStime();
390     ban->flags = CHFL_BAN;      /* This bit is never used I think... */
391     if ((ip_start = strrchr(banid, '@')) && check_if_ipmask(ip_start + 1))
392       ban->flags |= CHFL_BAN_IPMASK;
393     chptr->banlist = ban;
394
395     /*
396      * Erase ban-valid-bit
397      */
398     for (member = chptr->members; member; member = member->next_member)
399       ClearBanValid(member);     /* `ban' == channel member ! */
400   }
401   return 0;
402 }
403
404 static struct SLink *next_overlapped_ban(void)
405 {
406   struct SLink *tmp = next_ban;
407   if (tmp)
408   {
409     struct SLink *ban;
410     for (ban = tmp->next; ban; ban = ban->next)
411       if ((ban->flags & CHFL_BAN_OVERLAPPED))
412         break;
413     next_ban = ban;
414   }
415   return tmp;
416 }
417
418 struct SLink *next_removed_overlapped_ban(void)
419 {
420   struct SLink *tmp = removed_bans_list;
421   if (prev_ban)
422   {
423     if (prev_ban->value.ban.banstr)     /* Can be set to NULL in set_mode() */
424       MyFree(prev_ban->value.ban.banstr);
425     MyFree(prev_ban->value.ban.who);
426     free_link(prev_ban);
427     prev_ban = 0;
428   }
429   if (tmp)
430     removed_bans_list = removed_bans_list->next;
431   prev_ban = tmp;
432   return tmp;
433 }
434
435 /*
436  * del_banid
437  *
438  * If `change' is true, delete `banid' from channel `chptr'.
439  * Returns `false' if removal was (or would have been) successful.
440  */
441 static int del_banid(struct Channel *chptr, char *banid, int change)
442 {
443   struct SLink **ban;
444   struct SLink *tmp;
445
446   if (!banid)
447     return -1;
448   for (ban = &(chptr->banlist); *ban; ban = &((*ban)->next)) {
449     if (0 == ircd_strcmp(banid, (*ban)->value.ban.banstr))
450     {
451       tmp = *ban;
452       if (change)
453       {
454         struct Membership* member;
455         *ban = tmp->next;
456         MyFree(tmp->value.ban.banstr);
457         MyFree(tmp->value.ban.who);
458         free_link(tmp);
459         /*
460          * Erase ban-valid-bit, for channel members that are banned
461          */
462         for (member = chptr->members; member; member = member->next_member)
463           if (CHFL_BANVALIDMASK == (member->status & CHFL_BANVALIDMASK))
464             ClearBanValid(member);       /* `tmp' == channel member */
465       }
466       return 0;
467     }
468   }
469   return -1;
470 }
471
472 /*
473  * find_channel_member - returns Membership * if a person is joined and not a zombie
474  */
475 struct Membership* find_channel_member(struct Client* cptr, struct Channel* chptr)
476 {
477   struct Membership* member;
478   assert(0 != chptr);
479
480   member = find_member_link(chptr, cptr);
481   return (member && !IsZombie(member)) ? member : 0;
482 }
483
484 /*
485  * is_banned - a non-zero value if banned else 0.
486  */
487 static int is_banned(struct Client *cptr, struct Channel *chptr,
488                      struct Membership* member)
489 {
490   struct SLink* tmp;
491   char*         s;
492   char*         ip_s = NULL;
493
494   if (!IsUser(cptr))
495     return 0;
496
497   if (member && IsBanValid(member))
498     return IsBanned(member);
499
500   s = make_nick_user_host(cptr->name, cptr->user->username, cptr->user->host);
501
502   for (tmp = chptr->banlist; tmp; tmp = tmp->next) {
503     if ((tmp->flags & CHFL_BAN_IPMASK)) {
504       if (!ip_s)
505         ip_s = make_nick_user_ip(cptr->name, cptr->user->username, cptr->ip);
506       if (match(tmp->value.ban.banstr, ip_s) == 0)
507         break;
508     }
509     else if (match(tmp->value.ban.banstr, s) == 0)
510       break;
511   }
512
513   if (member) {
514     SetBanValid(member);
515     if (tmp) {
516       SetBanned(member);
517       return 1;
518     }
519     else {
520       ClearBanned(member);
521       return 0;
522     }
523   }
524
525   return (tmp != NULL);
526 }
527
528 /*
529  * adds a user to a channel by adding another link to the channels member
530  * chain.
531  */
532 void add_user_to_channel(struct Channel* chptr, struct Client* who,
533                                 unsigned int flags)
534 {
535   assert(0 != chptr);
536   assert(0 != who);
537
538   if (who->user) {
539    
540     struct Membership* member = membershipFreeList;
541     if (member)
542       membershipFreeList = member->next_member;
543     else {
544       member = (struct Membership*) MyMalloc(sizeof(struct Membership));
545       ++membershipAllocCount;
546     }
547
548     assert(0 != member);
549     member->user         = who;
550     member->channel      = chptr;
551     member->status       = flags;
552
553     member->next_member  = chptr->members;
554     if (member->next_member)
555       member->next_member->prev_member = member;
556     member->prev_member  = 0; 
557     chptr->members       = member;
558
559     member->next_channel = who->user->channel;
560     if (member->next_channel)
561       member->next_channel->prev_channel = member;
562     member->prev_channel = 0;
563     who->user->channel = member;
564
565     ++chptr->users;
566     ++who->user->joined;
567   }
568 }
569
570 static int remove_member_from_channel(struct Membership* member)
571 {
572   struct Channel* chptr;
573   assert(0 != member);
574   chptr = member->channel;
575   /*
576    * unlink channel member list
577    */
578   if (member->next_member)
579     member->next_member->prev_member = member->prev_member;
580   if (member->prev_member)
581     member->prev_member->next_member = member->next_member;
582   else
583     member->channel->members = member->next_member; 
584       
585   /*
586    * unlink client channel list
587    */
588   if (member->next_channel)
589     member->next_channel->prev_channel = member->prev_channel;
590   if (member->prev_channel)
591     member->prev_channel->next_channel = member->next_channel;
592   else
593     member->user->user->channel = member->next_channel;
594
595   --member->user->user->joined;
596
597   member->next_member = membershipFreeList;
598   membershipFreeList = member;
599
600   return sub1_from_channel(chptr);
601 }
602
603 static int channel_all_zombies(struct Channel* chptr)
604 {
605   struct Membership* member;
606
607   for (member = chptr->members; member; member = member->next_member) {
608     if (!IsZombie(member))
609       return 0;
610   }
611   return 1;
612 }
613       
614
615 void remove_user_from_channel(struct Client* cptr, struct Channel* chptr)
616 {
617   
618   struct Membership* member;
619   assert(0 != chptr);
620
621   if ((member = find_member_link(chptr, cptr))) {
622     if (remove_member_from_channel(member)) {
623       if (channel_all_zombies(chptr)) {
624         /*
625          * XXX - this looks dangerous but isn't if we got the referential
626          * integrity right for channels
627          */
628         while (remove_member_from_channel(chptr->members))
629           ;
630       }
631     }
632   }
633 }
634
635 void remove_user_from_all_channels(struct Client* cptr)
636 {
637   struct Membership* chan;
638   assert(0 != cptr);
639   assert(0 != cptr->user);
640
641   while ((chan = cptr->user->channel))
642     remove_user_from_channel(cptr, chan->channel);
643 }
644
645 int is_chan_op(struct Client *cptr, struct Channel *chptr)
646 {
647   struct Membership* member;
648   assert(chptr);
649   if ((member = find_member_link(chptr, cptr)))
650     return (!IsZombie(member) && IsChanOp(member));
651
652   return 0;
653 }
654
655 static int is_deopped(struct Client *cptr, struct Channel *chptr)
656 {
657   struct Membership* member;
658
659   assert(0 != chptr);
660   if ((member = find_member_link(chptr, cptr)))
661     return IsDeopped(member);
662
663   return (IsUser(cptr) ? 1 : 0);
664 }
665
666 int is_zombie(struct Client *cptr, struct Channel *chptr)
667 {
668   struct Membership* member;
669
670   assert(0 != chptr);
671
672   if ((member = find_member_link(chptr, cptr)))
673       return IsZombie(member);
674   return 0;
675 }
676
677 int has_voice(struct Client* cptr, struct Channel* chptr)
678 {
679   struct Membership* member;
680
681   assert(0 != chptr);
682   if ((member = find_member_link(chptr, cptr)))
683     return (!IsZombie(member) && HasVoice(member));
684
685   return 0;
686 }
687
688 int member_can_send_to_channel(struct Membership* member)
689 {
690   assert(0 != member);
691
692   if (IsVoicedOrOpped(member))
693     return 1;
694   /*
695    * If it's moderated, and you aren't a priviledged user, you can't
696    * speak.  
697    */
698   if (member->channel->mode.mode & MODE_MODERATED)
699     return 0;
700   /*
701    * If you're banned then you can't speak either.
702    * but because of the amount of CPU time that is_banned chews
703    * we only check it for our clients.
704    */
705   if (MyUser(member->user) && is_banned(member->user, member->channel, member))
706     return 0;
707   return 1;
708 }
709
710 int client_can_send_to_channel(struct Client *cptr, struct Channel *chptr)
711 {
712   struct Membership *member;
713   assert(0 != cptr); 
714   /*
715    * Servers can always speak on channels.
716    */
717   if (IsServer(cptr))
718     return 1;
719
720   member = find_channel_member(cptr, chptr);
721
722   /*
723    * You can't speak if your off channel, if the channel is modeless, or
724    * +n.(no external messages)
725    */
726   if (!member) {
727     if ((chptr->mode.mode & MODE_NOPRIVMSGS) || IsModelessChannel(chptr->chname)) 
728       return 0;
729     else
730       return 1;
731   }
732   return member_can_send_to_channel(member); 
733 }
734
735 /*
736  * find_no_nickchange_channel
737  * if a member and not opped or voiced and banned
738  * return the name of the first channel banned on
739  */
740 const char* find_no_nickchange_channel(struct Client* cptr)
741 {
742   if (MyUser(cptr)) {
743     struct Membership* member;
744     for (member = cptr->user->channel; member; member = member->next_channel) {
745       if (!IsVoicedOrOpped(member) && is_banned(cptr, member->channel, member))
746         return member->channel->chname;
747     }
748   }
749   return 0;
750 }
751
752
753 /*
754  * write the "simple" list of channel modes for channel chptr onto buffer mbuf
755  * with the parameters in pbuf.
756  */
757 void channel_modes(struct Client *cptr, char *mbuf, char *pbuf,
758                           struct Channel *chptr)
759 {
760   assert(0 != mbuf);
761   assert(0 != pbuf);
762   assert(0 != chptr);
763
764   *mbuf++ = '+';
765   if (chptr->mode.mode & MODE_SECRET)
766     *mbuf++ = 's';
767   else if (chptr->mode.mode & MODE_PRIVATE)
768     *mbuf++ = 'p';
769   if (chptr->mode.mode & MODE_MODERATED)
770     *mbuf++ = 'm';
771   if (chptr->mode.mode & MODE_TOPICLIMIT)
772     *mbuf++ = 't';
773   if (chptr->mode.mode & MODE_INVITEONLY)
774     *mbuf++ = 'i';
775   if (chptr->mode.mode & MODE_NOPRIVMSGS)
776     *mbuf++ = 'n';
777   if (chptr->mode.limit) {
778     *mbuf++ = 'l';
779     sprintf_irc(pbuf, "%d", chptr->mode.limit);
780   }
781
782   if (*chptr->mode.key) {
783     *mbuf++ = 'k';
784     if (is_chan_op(cptr, chptr) || IsServer(cptr)) {
785       if (chptr->mode.limit)
786         strcat(pbuf, " ");
787       strcat(pbuf, chptr->mode.key);
788     }
789   }
790   *mbuf = '\0';
791 }
792
793 #if 0
794 static int send_mode_list(struct Client *cptr, char *chname,
795                           time_t creationtime, struct SLink *top,
796                           int mask, char flag)
797 {
798   struct SLink* lp;
799   char*         cp;
800   char*         name;
801   int           count = 0;
802   int           send = 0;
803   int           sent = 0;
804
805   cp = modebuf + strlen(modebuf);
806   if (*parabuf)                 /* mode +l or +k xx */
807     count = 1;
808   for (lp = top; lp; lp = lp->next)
809   {
810     if (!(lp->flags & mask))
811       continue;
812     if (mask == CHFL_BAN)
813       name = lp->value.ban.banstr;
814     else
815       name = lp->value.cptr->name;
816     if (strlen(parabuf) + strlen(name) + 11 < MODEBUFLEN)
817     {
818       strcat(parabuf, " ");
819       strcat(parabuf, name);
820       count++;
821       *cp++ = flag;
822       *cp = '\0';
823     }
824     else if (*parabuf)
825       send = 1;
826     if (count == 6)
827       send = 1;
828     if (send)
829     {
830       /* cptr is always a server! So we send creationtimes */
831       sendmodeto_one(cptr, me.name, chname, modebuf, parabuf, creationtime);
832       sent = 1;
833       send = 0;
834       *parabuf = '\0';
835       cp = modebuf;
836       *cp++ = '+';
837       if (count != 6)
838       {
839         strcpy(parabuf, name);
840         *cp++ = flag;
841       }
842       count = 0;
843       *cp = '\0';
844     }
845   }
846   return sent;
847 }
848
849 #endif /* 0 */
850
851 /*
852  * send "cptr" a full list of the modes for channel chptr.
853  */
854 void send_channel_modes(struct Client *cptr, struct Channel *chptr)
855 {
856   static unsigned int current_flags[4] =
857       { 0, CHFL_CHANOP | CHFL_VOICE, CHFL_VOICE, CHFL_CHANOP };
858   int                first = 1;
859   int                full  = 1;
860   int                flag_cnt = 0;
861   int                new_mode = 0;
862   size_t             len;
863   struct Membership* member;
864   struct SLink*      lp2;
865   char modebuf[MODEBUFLEN];
866   char parabuf[MODEBUFLEN];
867   struct MsgBuf *mb;
868
869   assert(0 != cptr);
870   assert(0 != chptr); 
871
872   if (IsLocalChannel(chptr->chname))
873     return;
874
875   member = chptr->members;
876   lp2 = chptr->banlist;
877
878   *modebuf = *parabuf = '\0';
879   channel_modes(cptr, modebuf, parabuf, chptr);
880
881   for (first = 1; full; first = 0)      /* Loop for multiple messages */
882   {
883     full = 0;                   /* Assume by default we get it
884                                  all in one message */
885
886     /* (Continued) prefix: "<Y> B <channel> <TS>" */
887     /* is there any better way we can do this? */
888     mb = msgq_make(&me, "%C " TOK_BURST " %H %Tu", &me, chptr,
889                    chptr->creationtime);
890
891     if (first && modebuf[1])    /* Add simple modes (iklmnpst)
892                                  if first message */
893     {
894       /* prefix: "<Y> B <channel> <TS>[ <modes>[ <params>]]" */
895       msgq_append(&me, mb, " %s", modebuf);
896
897       if (*parabuf)
898         msgq_append(&me, mb, " %s", parabuf);
899     }
900
901     /*
902      * Attach nicks, comma seperated " nick[:modes],nick[:modes],..."
903      *
904      * Run 4 times over all members, to group the members with the
905      * same mode together
906      */
907     for (first = 1; flag_cnt < 4;
908          member = chptr->members, new_mode = 1, flag_cnt++)
909     {
910       for (; member; member = member->next_member)
911       {
912         if ((member->status & CHFL_VOICED_OR_OPPED) !=
913             current_flags[flag_cnt])
914           continue;             /* Skip members with different flags */
915         if (msgq_bufleft(mb) < NUMNICKLEN + 4)
916           /* The 4 is a possible ",:ov" */
917         {
918           full = 1;           /* Make sure we continue after
919                                  sending it so far */
920           new_mode = 1;       /* Ensure the new BURST line contains the current
921                                  mode. --Gte */
922           break;              /* Do not add this member to this message */
923         }
924         msgq_append(&me, mb, "%c%C", first ? ' ' : ',', member->user);
925         first = 0;              /* From now on, us comma's to add new nicks */
926
927         /*
928          * Do we have a nick with a new mode ?
929          * Or are we starting a new BURST line?
930          */
931         if (new_mode)
932         {
933           new_mode = 0;
934           if (IsVoicedOrOpped(member)) {
935             char tbuf[4] = ":";
936             int loc = 1;
937
938             if (IsChanOp(member))
939               tbuf[loc++] = 'o';
940             if (HasVoice(member))
941               tbuf[loc++] = 'v';
942             tbuf[loc] = '\0';
943             msgq_append(&me, mb, tbuf);
944           }
945         }
946       }
947       if (full)
948         break;
949     }
950
951     if (!full)
952     {
953       /* Attach all bans, space seperated " :%ban ban ..." */
954       for (first = 2; lp2; lp2 = lp2->next)
955       {
956         len = strlen(lp2->value.ban.banstr);
957         if (msgq_bufleft(mb) < len + 1 + first)
958           /* The +1 stands for the added ' '.
959            * The +first stands for the added ":%".
960            */
961         {
962           full = 1;
963           break;
964         }
965         msgq_append(&me, mb, " %s%s", first ? ":%" : "",
966                     lp2->value.ban.banstr);
967         first = 0;
968       }
969     }
970
971     send_buffer(cptr, mb, 0);  /* Send this message */
972     msgq_clean(mb);
973   }                             /* Continue when there was something
974                                  that didn't fit (full==1) */
975 }
976
977 /*
978  * pretty_mask
979  *
980  * by Carlo Wood (Run), 05 Oct 1998.
981  *
982  * Canonify a mask.
983  *
984  * When the nick is longer then NICKLEN, it is cut off (its an error of course).
985  * When the user name or host name are too long (USERLEN and HOSTLEN
986  * respectively) then they are cut off at the start with a '*'.
987  *
988  * The following transformations are made:
989  *
990  * 1)   xxx             -> nick!*@*
991  * 2)   xxx.xxx         -> *!*@host
992  * 3)   xxx!yyy         -> nick!user@*
993  * 4)   xxx@yyy         -> *!user@host
994  * 5)   xxx!yyy@zzz     -> nick!user@host
995  */
996 char *pretty_mask(char *mask)
997 {
998   static char star[2] = { '*', 0 };
999   char *last_dot = NULL;
1000   char *ptr;
1001
1002   /* Case 1: default */
1003   char *nick = mask;
1004   char *user = star;
1005   char *host = star;
1006
1007   /* Do a _single_ pass through the characters of the mask: */
1008   for (ptr = mask; *ptr; ++ptr)
1009   {
1010     if (*ptr == '!')
1011     {
1012       /* Case 3 or 5: Found first '!' (without finding a '@' yet) */
1013       user = ++ptr;
1014       host = star;
1015     }
1016     else if (*ptr == '@')
1017     {
1018       /* Case 4: Found last '@' (without finding a '!' yet) */
1019       nick = star;
1020       user = mask;
1021       host = ++ptr;
1022     }
1023     else if (*ptr == '.')
1024     {
1025       /* Case 2: Found last '.' (without finding a '!' or '@' yet) */
1026       last_dot = ptr;
1027       continue;
1028     }
1029     else
1030       continue;
1031     for (; *ptr; ++ptr)
1032     {
1033       if (*ptr == '@')
1034       {
1035         /* Case 4 or 5: Found last '@' */
1036         host = ptr + 1;
1037       }
1038     }
1039     break;
1040   }
1041   if (user == star && last_dot)
1042   {
1043     /* Case 2: */
1044     nick = star;
1045     user = star;
1046     host = mask;
1047   }
1048   /* Check lengths */
1049   if (nick != star)
1050   {
1051     char *nick_end = (user != star) ? user - 1 : ptr;
1052     if (nick_end - nick > NICKLEN)
1053       nick[NICKLEN] = 0;
1054     *nick_end = 0;
1055   }
1056   if (user != star)
1057   {
1058     char *user_end = (host != star) ? host - 1 : ptr;
1059     if (user_end - user > USERLEN)
1060     {
1061       user = user_end - USERLEN;
1062       *user = '*';
1063     }
1064     *user_end = 0;
1065   }
1066   if (host != star && ptr - host > HOSTLEN)
1067   {
1068     host = ptr - HOSTLEN;
1069     *host = '*';
1070   }
1071   return make_nick_user_host(nick, user, host);
1072 }
1073
1074 static void send_ban_list(struct Client* cptr, struct Channel* chptr)
1075 {
1076   struct SLink* lp;
1077
1078   assert(0 != cptr);
1079   assert(0 != chptr);
1080
1081   for (lp = chptr->banlist; lp; lp = lp->next)
1082     send_reply(cptr, RPL_BANLIST, chptr->chname, lp->value.ban.banstr,
1083                lp->value.ban.who, lp->value.ban.when);
1084
1085   send_reply(cptr, RPL_ENDOFBANLIST, chptr->chname);
1086 }
1087
1088 /* We are now treating the <key> part of /join <channel list> <key> as a key
1089  * ring; that is, we try one key against the actual channel key, and if that
1090  * doesn't work, we try the next one, and so on. -Kev -Texaco
1091  * Returns: 0 on match, 1 otherwise
1092  * This version contributed by SeKs <intru@info.polymtl.ca>
1093  */
1094 static int compall(char *key, char *keyring)
1095 {
1096   char *p1;
1097
1098 top:
1099   p1 = key;                     /* point to the key... */
1100   while (*p1 && *p1 == *keyring)
1101   {                             /* step through the key and ring until they
1102                                    don't match... */
1103     p1++;
1104     keyring++;
1105   }
1106
1107   if (!*p1 && (!*keyring || *keyring == ','))
1108     /* ok, if we're at the end of the and also at the end of one of the keys
1109        in the keyring, we have a match */
1110     return 0;
1111
1112   if (!*keyring)                /* if we're at the end of the key ring, there
1113                                    weren't any matches, so we return 1 */
1114     return 1;
1115
1116   /* Not at the end of the key ring, so step
1117      through to the next key in the ring: */
1118   while (*keyring && *(keyring++) != ',');
1119
1120   goto top;                     /* and check it against the key */
1121 }
1122
1123 int can_join(struct Client *sptr, struct Channel *chptr, char *key)
1124 {
1125   struct SLink *lp;
1126   int overrideJoin = 0;  
1127   
1128   /*
1129    * Now a banned user CAN join if invited -- Nemesi
1130    * Now a user CAN escape channel limit if invited -- bfriendly
1131    * Now a user CAN escape anything if invited -- Isomer
1132    */
1133
1134   for (lp = sptr->user->invited; lp; lp = lp->next)
1135     if (lp->value.chptr == chptr)
1136       return 0;
1137   
1138 #ifdef OPER_WALK_THROUGH_LMODES
1139   /* An oper can force a join on a local channel using "OVERRIDE" as the key. 
1140      a HACK(4) notice will be sent if he would not have been supposed
1141      to join normally. */ 
1142   if (IsOperOnLocalChannel(sptr,chptr->chname) && !BadPtr(key) && compall("OVERRIDE",key) == 0)
1143   {
1144     overrideJoin = MAGIC_OPER_OVERRIDE;
1145   }
1146 #endif
1147
1148   if (chptr->mode.mode & MODE_INVITEONLY)
1149         return overrideJoin + ERR_INVITEONLYCHAN;
1150         
1151   if (chptr->mode.limit && chptr->users >= chptr->mode.limit)
1152         return overrideJoin + ERR_CHANNELISFULL;
1153         
1154   if (is_banned(sptr, chptr, NULL))
1155         return overrideJoin + ERR_BANNEDFROMCHAN;
1156   
1157   /*
1158    * now using compall (above) to test against a whole key ring -Kev
1159    */
1160   if (*chptr->mode.key && (EmptyString(key) || compall(chptr->mode.key, key)))
1161     return overrideJoin + ERR_BADCHANNELKEY;
1162
1163   if (overrideJoin)     
1164         return ERR_DONTCHEAT;
1165         
1166   return 0;
1167 }
1168
1169 /*
1170  * Remove bells and commas from channel name
1171  */
1172 void clean_channelname(char *cn)
1173 {
1174   int i;
1175
1176   for (i = 0; cn[i]; i++) {
1177     if (i >= CHANNELLEN || !IsChannelChar(cn[i])) {
1178       cn[i] = '\0';
1179       return;
1180     }
1181     if (IsChannelLower(cn[i])) {
1182       cn[i] = ToLower(cn[i]);
1183 #ifndef FIXME
1184       /*
1185        * Remove for .08+
1186        * toupper(0xd0)
1187        */
1188       if ((unsigned char)(cn[i]) == 0xd0)
1189         cn[i] = (char) 0xf0;
1190 #endif
1191     }
1192   }
1193 }
1194
1195 /*
1196  *  Get Channel block for i (and allocate a new channel
1197  *  block, if it didn't exists before).
1198  */
1199 struct Channel *get_channel(struct Client *cptr, char *chname, ChannelGetType flag)
1200 {
1201   struct Channel *chptr;
1202   int len;
1203
1204   if (EmptyString(chname))
1205     return NULL;
1206
1207   len = strlen(chname);
1208   if (MyUser(cptr) && len > CHANNELLEN)
1209   {
1210     len = CHANNELLEN;
1211     *(chname + CHANNELLEN) = '\0';
1212   }
1213   if ((chptr = FindChannel(chname)))
1214     return (chptr);
1215   if (flag == CGT_CREATE)
1216   {
1217     chptr = (struct Channel*) MyMalloc(sizeof(struct Channel) + len);
1218     assert(0 != chptr);
1219     ++UserStats.channels;
1220     memset(chptr, 0, sizeof(struct Channel));
1221     strcpy(chptr->chname, chname);
1222     if (GlobalChannelList)
1223       GlobalChannelList->prev = chptr;
1224     chptr->prev = NULL;
1225     chptr->next = GlobalChannelList;
1226     chptr->creationtime = MyUser(cptr) ? TStime() : (time_t) 0;
1227     GlobalChannelList = chptr;
1228     hAddChannel(chptr);
1229   }
1230   return chptr;
1231 }
1232
1233 void add_invite(struct Client *cptr, struct Channel *chptr)
1234 {
1235   struct SLink *inv, **tmp;
1236
1237   del_invite(cptr, chptr);
1238   /*
1239    * Delete last link in chain if the list is max length
1240    */
1241   assert(list_length(cptr->user->invited) == cptr->user->invites);
1242   if (cptr->user->invites>=MAXCHANNELSPERUSER)
1243     del_invite(cptr, cptr->user->invited->value.chptr);
1244   /*
1245    * Add client to channel invite list
1246    */
1247   inv = make_link();
1248   inv->value.cptr = cptr;
1249   inv->next = chptr->invites;
1250   chptr->invites = inv;
1251   /*
1252    * Add channel to the end of the client invite list
1253    */
1254   for (tmp = &(cptr->user->invited); *tmp; tmp = &((*tmp)->next));
1255   inv = make_link();
1256   inv->value.chptr = chptr;
1257   inv->next = NULL;
1258   (*tmp) = inv;
1259   cptr->user->invites++;
1260 }
1261
1262 /*
1263  * Delete Invite block from channel invite list and client invite list
1264  */
1265 void del_invite(struct Client *cptr, struct Channel *chptr)
1266 {
1267   struct SLink **inv, *tmp;
1268
1269   for (inv = &(chptr->invites); (tmp = *inv); inv = &tmp->next)
1270     if (tmp->value.cptr == cptr)
1271     {
1272       *inv = tmp->next;
1273       free_link(tmp);
1274       tmp = 0;
1275       cptr->user->invites--;
1276       break;
1277     }
1278
1279   for (inv = &(cptr->user->invited); (tmp = *inv); inv = &tmp->next)
1280     if (tmp->value.chptr == chptr)
1281     {
1282       *inv = tmp->next;
1283       free_link(tmp);
1284       tmp = 0;
1285       break;
1286     }
1287 }
1288
1289 /* List and skip all channels that are listen */
1290 void list_next_channels(struct Client *cptr, int nr)
1291 {
1292   struct ListingArgs *args = cptr->listing;
1293   struct Channel *chptr = args->chptr;
1294   chptr->mode.mode &= ~MODE_LISTED;
1295   while (is_listed(chptr) || --nr >= 0)
1296   {
1297     for (; chptr; chptr = chptr->next)
1298     {
1299       if (!cptr->user || (SecretChannel(chptr) && !find_channel_member(cptr, chptr)))
1300         continue;
1301       if (chptr->users > args->min_users && chptr->users < args->max_users &&
1302           chptr->creationtime > args->min_time &&
1303           chptr->creationtime < args->max_time &&
1304           (!args->topic_limits || (*chptr->topic &&
1305           chptr->topic_time > args->min_topic_time &&
1306           chptr->topic_time < args->max_topic_time)))
1307       {
1308         if (ShowChannel(cptr,chptr))
1309           send_reply(cptr, RPL_LIST, chptr->chname, chptr->users,
1310                      chptr->topic);
1311         chptr = chptr->next;
1312         break;
1313       }
1314     }
1315     if (!chptr)
1316     {
1317       MyFree(cptr->listing);
1318       cptr->listing = NULL;
1319       send_reply(cptr, RPL_LISTEND);
1320       break;
1321     }
1322   }
1323   if (chptr)
1324   {
1325     cptr->listing->chptr = chptr;
1326     chptr->mode.mode |= MODE_LISTED;
1327   }
1328 }
1329
1330 /*
1331  * Consider:
1332  *
1333  *                     client
1334  *                       |
1335  *                       c
1336  *                       |
1337  *     X --a--> A --b--> B --d--> D
1338  *                       |
1339  *                      who
1340  *
1341  * Where `who' is being KICK-ed by a "KICK" message received by server 'A'
1342  * via 'a', or on server 'B' via either 'b' or 'c', or on server D via 'd'.
1343  *
1344  * a) On server A : set CHFL_ZOMBIE for `who' (lp) and pass on the KICK.
1345  *    Remove the user immedeately when no users are left on the channel.
1346  * b) On server B : remove the user (who/lp) from the channel, send a
1347  *    PART upstream (to A) and pass on the KICK.
1348  * c) KICKed by `client'; On server B : remove the user (who/lp) from the
1349  *    channel, and pass on the KICK.
1350  * d) On server D : remove the user (who/lp) from the channel, and pass on
1351  *    the KICK.
1352  *
1353  * Note:
1354  * - Setting the ZOMBIE flag never hurts, we either remove the
1355  *   client after that or we don't.
1356  * - The KICK message was already passed on, as should be in all cases.
1357  * - `who' is removed in all cases except case a) when users are left.
1358  * - A PART is only sent upstream in case b).
1359  *
1360  * 2 aug 97:
1361  *
1362  *              6
1363  *              |
1364  *  1 --- 2 --- 3 --- 4 --- 5
1365  *        |           |
1366  *      kicker       who
1367  *
1368  * We also need to turn 'who' into a zombie on servers 1 and 6,
1369  * because a KICK from 'who' (kicking someone else in that direction)
1370  * can arrive there afterwards - which should not be bounced itself.
1371  * Therefore case a) also applies for servers 1 and 6.
1372  *
1373  * --Run
1374  */
1375 void make_zombie(struct Membership* member, struct Client* who, struct Client* cptr,
1376                  struct Client* sptr, struct Channel* chptr)
1377 {
1378   assert(0 != member);
1379   assert(0 != who);
1380   assert(0 != cptr);
1381   assert(0 != chptr);
1382
1383   /* Default for case a): */
1384   SetZombie(member);
1385
1386   /* Case b) or c) ?: */
1387   if (MyUser(who))      /* server 4 */
1388   {
1389     if (IsServer(cptr)) /* Case b) ? */
1390       sendcmdto_one(who, CMD_PART, cptr, "%H", chptr);
1391     remove_user_from_channel(who, chptr);
1392     return;
1393   }
1394   if (who->from == cptr)        /* True on servers 1, 5 and 6 */
1395   {
1396     struct Client *acptr = IsServer(sptr) ? sptr : sptr->user->server;
1397     for (; acptr != &me; acptr = acptr->serv->up)
1398       if (acptr == who->user->server)   /* Case d) (server 5) */
1399       {
1400         remove_user_from_channel(who, chptr);
1401         return;
1402       }
1403   }
1404
1405   /* Case a) (servers 1, 2, 3 and 6) */
1406   if (channel_all_zombies(chptr))
1407     remove_user_from_channel(who, chptr);
1408
1409   /* XXX Can't actually call Debug here; if the channel is all zombies,
1410    * chptr will no longer exist when we get here.
1411   Debug((DEBUG_INFO, "%s is now a zombie on %s", who->name, chptr->chname));
1412   */
1413 }
1414
1415 int number_of_zombies(struct Channel *chptr)
1416 {
1417   struct Membership* member;
1418   int                count = 0;
1419
1420   assert(0 != chptr);
1421   for (member = chptr->members; member; member = member->next_member) {
1422     if (IsZombie(member))
1423       ++count;
1424   }
1425   return count;
1426 }
1427
1428 /*
1429  * This helper function builds an argument string in strptr, consisting
1430  * of the original string, a space, and str1 and str2 concatenated (if,
1431  * of course, str2 is not NULL)
1432  */
1433 static void
1434 build_string(char *strptr, int *strptr_i, char *str1, char *str2, char c)
1435 {
1436   if (c)
1437     strptr[(*strptr_i)++] = c;
1438
1439   while (*str1)
1440     strptr[(*strptr_i)++] = *(str1++);
1441
1442   if (str2)
1443     while (*str2)
1444       strptr[(*strptr_i)++] = *(str2++);
1445
1446   strptr[(*strptr_i)] = '\0';
1447 }
1448
1449 /*
1450  * This is the workhorse of our ModeBuf suite; this actually generates the
1451  * output MODE commands, HACK notices, or whatever.  It's pretty complicated.
1452  */
1453 static int
1454 modebuf_flush_int(struct ModeBuf *mbuf, int all)
1455 {
1456   /* we only need the flags that don't take args right now */
1457   static int flags[] = {
1458 /*  MODE_CHANOP,        'o', */
1459 /*  MODE_VOICE,         'v', */
1460     MODE_PRIVATE,       'p',
1461     MODE_SECRET,        's',
1462     MODE_MODERATED,     'm',
1463     MODE_TOPICLIMIT,    't',
1464     MODE_INVITEONLY,    'i',
1465     MODE_NOPRIVMSGS,    'n',
1466 /*  MODE_KEY,           'k', */
1467 /*  MODE_BAN,           'b', */
1468 /*  MODE_LIMIT,         'l', */
1469     0x0, 0x0
1470   };
1471   int i;
1472   int *flag_p;
1473
1474   struct Client *app_source; /* where the MODE appears to come from */
1475
1476   char addbuf[20]; /* accumulates +psmtin, etc. */
1477   int addbuf_i = 0;
1478   char rembuf[20]; /* accumulates -psmtin, etc. */
1479   int rembuf_i = 0;
1480   char *bufptr; /* we make use of indirection to simplify the code */
1481   int *bufptr_i;
1482
1483   char addstr[BUFSIZE]; /* accumulates MODE parameters to add */
1484   int addstr_i;
1485   char remstr[BUFSIZE]; /* accumulates MODE parameters to remove */
1486   int remstr_i;
1487   char *strptr; /* more indirection to simplify the code */
1488   int *strptr_i;
1489
1490   int totalbuflen = BUFSIZE - 200; /* fuzz factor -- don't overrun buffer! */
1491   int tmp;
1492
1493   char limitbuf[20]; /* convert limits to strings */
1494
1495   unsigned int limitdel = MODE_LIMIT;
1496
1497   assert(0 != mbuf);
1498
1499   /* If the ModeBuf is empty, we have nothing to do */
1500   if (mbuf->mb_add == 0 && mbuf->mb_rem == 0 && mbuf->mb_count == 0)
1501     return 0;
1502
1503   /* Ok, if we were given the OPMODE flag, hide the source if its a user */
1504   if (mbuf->mb_dest & MODEBUF_DEST_OPMODE && !IsServer(mbuf->mb_source))
1505     app_source = mbuf->mb_source->user->server;
1506   else
1507     app_source = mbuf->mb_source;
1508
1509   /*
1510    * Account for user we're bouncing; we have to get it in on the first
1511    * bounced MODE, or we could have problems
1512    */
1513   if (mbuf->mb_dest & MODEBUF_DEST_DEOP)
1514     totalbuflen -= 6; /* numeric nick == 5, plus one space */
1515
1516   /* Calculate the simple flags */
1517   for (flag_p = flags; flag_p[0]; flag_p += 2) {
1518     if (*flag_p & mbuf->mb_add)
1519       addbuf[addbuf_i++] = flag_p[1];
1520     else if (*flag_p & mbuf->mb_rem)
1521       rembuf[rembuf_i++] = flag_p[1];
1522   }
1523
1524   /* Now go through the modes with arguments... */
1525   for (i = 0; i < mbuf->mb_count; i++) {
1526     if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1527       bufptr = addbuf;
1528       bufptr_i = &addbuf_i;
1529     } else {
1530       bufptr = rembuf;
1531       bufptr_i = &rembuf_i;
1532     }
1533
1534     if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE)) {
1535       tmp = strlen(MB_CLIENT(mbuf, i)->name);
1536
1537       if ((totalbuflen - IRCD_MAX(5, tmp)) <= 0) /* don't overflow buffer */
1538         MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1539       else {
1540         bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_CHANOP ? 'o' : 'v';
1541         totalbuflen -= IRCD_MAX(5, tmp) + 1;
1542       }
1543     } else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN)) {
1544       tmp = strlen(MB_STRING(mbuf, i));
1545
1546       if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1547         MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1548       else {
1549         bufptr[(*bufptr_i)++] = MB_TYPE(mbuf, i) & MODE_KEY ? 'k' : 'b';
1550         totalbuflen -= tmp + 1;
1551       }
1552     } else if (MB_TYPE(mbuf, i) & MODE_LIMIT) {
1553       /* if it's a limit, we also format the number */
1554       sprintf_irc(limitbuf, "%d", MB_UINT(mbuf, i));
1555
1556       tmp = strlen(limitbuf);
1557
1558       if ((totalbuflen - tmp) <= 0) /* don't overflow buffer */
1559         MB_TYPE(mbuf, i) |= MODE_SAVE; /* save for later */
1560       else {
1561         bufptr[(*bufptr_i)++] = 'l';
1562         totalbuflen -= tmp + 1;
1563       }
1564     }
1565   }
1566
1567   /* terminate the mode strings */
1568   addbuf[addbuf_i] = '\0';
1569   rembuf[rembuf_i] = '\0';
1570
1571   /* If we're building a user visible MODE or HACK... */
1572   if (mbuf->mb_dest & (MODEBUF_DEST_CHANNEL | MODEBUF_DEST_HACK2 |
1573                        MODEBUF_DEST_HACK3   | MODEBUF_DEST_HACK4 |
1574                        MODEBUF_DEST_LOG)) {
1575     /* Set up the parameter strings */
1576     addstr[0] = '\0';
1577     addstr_i = 0;
1578     remstr[0] = '\0';
1579     remstr_i = 0;
1580
1581     for (i = 0; i < mbuf->mb_count; i++) {
1582       if (MB_TYPE(mbuf, i) & MODE_SAVE)
1583         continue;
1584
1585       if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1586         strptr = addstr;
1587         strptr_i = &addstr_i;
1588       } else {
1589         strptr = remstr;
1590         strptr_i = &remstr_i;
1591       }
1592
1593       /* deal with clients... */
1594       if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1595         build_string(strptr, strptr_i, MB_CLIENT(mbuf, i)->name, 0, ' ');
1596
1597       /* deal with strings... */
1598       else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1599         build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1600
1601       /*
1602        * deal with limit; note we cannot include the limit parameter if we're
1603        * removing it
1604        */
1605       else if ((MB_TYPE(mbuf, i) & (MODE_ADD | MODE_LIMIT)) ==
1606                (MODE_ADD | MODE_LIMIT))
1607         build_string(strptr, strptr_i, limitbuf, 0, ' ');
1608     }
1609
1610     /* send the messages off to their destination */
1611     if (mbuf->mb_dest & MODEBUF_DEST_HACK2) {
1612       sendto_opmask_butone(0, SNO_HACK2, "HACK(2): %s MODE %s %s%s%s%s%s%s "
1613                            "[%Tu]", app_source->name, mbuf->mb_channel->chname,
1614                            rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1615                            addbuf, remstr, addstr,
1616                            mbuf->mb_channel->creationtime);
1617       sendcmdto_serv_butone(&me, CMD_DESYNCH, mbuf->mb_connect,
1618                             ":HACK: %s MODE %s %s%s%s%s%s%s [%Tu]",
1619                             app_source->name, mbuf->mb_channel->chname,
1620                             rembuf_i ? "-" : "", rembuf,
1621                             addbuf_i ? "+" : "", addbuf, remstr, addstr,
1622                             mbuf->mb_channel->creationtime);
1623     }
1624
1625     if (mbuf->mb_dest & MODEBUF_DEST_HACK3)
1626       sendto_opmask_butone(0, SNO_HACK3, "BOUNCE or HACK(3): %s MODE %s "
1627                            "%s%s%s%s%s%s [%Tu]", app_source->name,
1628                            mbuf->mb_channel->chname, rembuf_i ? "-" : "",
1629                            rembuf, addbuf_i ? "+" : "", addbuf, remstr, addstr,
1630                            mbuf->mb_channel->creationtime);
1631
1632     if (mbuf->mb_dest & MODEBUF_DEST_HACK4)
1633       sendto_opmask_butone(0, SNO_HACK4, "HACK(4): %s MODE %s %s%s%s%s%s%s "
1634                            "[%Tu]", app_source->name, mbuf->mb_channel->chname,
1635                            rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1636                            addbuf, remstr, addstr,
1637                            mbuf->mb_channel->creationtime);
1638
1639     if (mbuf->mb_dest & MODEBUF_DEST_LOG)
1640       log_write(LS_OPERMODE, L_INFO, LOG_NOSNOTICE,
1641                 "%#C OPMODE %H %s%s%s%s%s%s", mbuf->mb_source,
1642                 mbuf->mb_channel, rembuf_i ? "-" : "", rembuf,
1643                 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1644
1645     if (mbuf->mb_dest & MODEBUF_DEST_CHANNEL)
1646       sendcmdto_channel_butserv(app_source, CMD_MODE, mbuf->mb_channel,
1647                                 "%H %s%s%s%s%s%s", mbuf->mb_channel,
1648                                 rembuf_i ? "-" : "", rembuf,
1649                                 addbuf_i ? "+" : "", addbuf, remstr, addstr);
1650   }
1651
1652   /* Now are we supposed to propagate to other servers? */
1653   if (mbuf->mb_dest & MODEBUF_DEST_SERVER) {
1654     /* set up parameter string */
1655     addstr[0] = '\0';
1656     addstr_i = 0;
1657     remstr[0] = '\0';
1658     remstr_i = 0;
1659
1660     /*
1661      * limit is supressed if we're removing it; we have to figure out which
1662      * direction is the direction for it to be removed, though...
1663      */
1664     limitdel |= (mbuf->mb_dest & MODEBUF_DEST_HACK2) ? MODE_DEL : MODE_ADD;
1665
1666     for (i = 0; i < mbuf->mb_count; i++) {
1667       if (MB_TYPE(mbuf, i) & MODE_SAVE)
1668         continue;
1669
1670       if (MB_TYPE(mbuf, i) & MODE_ADD) { /* adding or removing? */
1671         strptr = addstr;
1672         strptr_i = &addstr_i;
1673       } else {
1674         strptr = remstr;
1675         strptr_i = &remstr_i;
1676       }
1677
1678       /* deal with modes that take clients */
1679       if (MB_TYPE(mbuf, i) & (MODE_CHANOP | MODE_VOICE))
1680         build_string(strptr, strptr_i, NumNick(MB_CLIENT(mbuf, i)), ' ');
1681
1682       /* deal with modes that take strings */
1683       else if (MB_TYPE(mbuf, i) & (MODE_KEY | MODE_BAN))
1684         build_string(strptr, strptr_i, MB_STRING(mbuf, i), 0, ' ');
1685
1686       /*
1687        * deal with the limit.  Logic here is complicated; if HACK2 is set,
1688        * we're bouncing the mode, so sense is reversed, and we have to
1689        * include the original limit if it looks like it's being removed
1690        */
1691       else if ((MB_TYPE(mbuf, i) & limitdel) == limitdel)
1692         build_string(strptr, strptr_i, limitbuf, 0, ' ');
1693     }
1694
1695     /* we were told to deop the source */
1696     if (mbuf->mb_dest & MODEBUF_DEST_DEOP) {
1697       addbuf[addbuf_i++] = 'o'; /* remember, sense is reversed */
1698       addbuf[addbuf_i] = '\0'; /* terminate the string... */
1699       build_string(addstr, &addstr_i, NumNick(mbuf->mb_source), ' ');
1700
1701       /* mark that we've done this, so we don't do it again */
1702       mbuf->mb_dest &= ~MODEBUF_DEST_DEOP;
1703     }
1704
1705     if (mbuf->mb_dest & MODEBUF_DEST_OPMODE) {
1706       /* If OPMODE was set, we're propagating the mode as an OPMODE message */
1707       sendcmdto_serv_butone(mbuf->mb_source, CMD_OPMODE, mbuf->mb_connect,
1708                             "%H %s%s%s%s%s%s", mbuf->mb_channel,
1709                             rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1710                             addbuf, remstr, addstr);
1711     } else if (mbuf->mb_dest & MODEBUF_DEST_BOUNCE) {
1712       /*
1713        * If HACK2 was set, we're bouncing; we send the MODE back to the
1714        * connection we got it from with the senses reversed and a TS of 0;
1715        * origin is us
1716        */
1717       sendcmdto_one(&me, CMD_MODE, mbuf->mb_connect, "%H %s%s%s%s%s%s %Tu",
1718                     mbuf->mb_channel, addbuf_i ? "-" : "", addbuf,
1719                     rembuf_i ? "+" : "", rembuf, addstr, remstr,
1720                     mbuf->mb_channel->creationtime);
1721     } else {
1722       /*
1723        * We're propagating a normal MODE command to the rest of the network;
1724        * we send the actual channel TS unless this is a HACK3 or a HACK4
1725        */
1726       if (IsServer(mbuf->mb_source))
1727         sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1728                               "%H %s%s%s%s%s%s %Tu", mbuf->mb_channel,
1729                               rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1730                               addbuf, remstr, addstr,
1731                               (mbuf->mb_dest & MODEBUF_DEST_HACK4) ? 0 :
1732                               mbuf->mb_channel->creationtime);
1733       else
1734         sendcmdto_serv_butone(mbuf->mb_source, CMD_MODE, mbuf->mb_connect,
1735                               "%H %s%s%s%s%s%s", mbuf->mb_channel,
1736                               rembuf_i ? "-" : "", rembuf, addbuf_i ? "+" : "",
1737                               addbuf, remstr, addstr);
1738     }
1739   }
1740
1741   /* We've drained the ModeBuf... */
1742   mbuf->mb_add = 0;
1743   mbuf->mb_rem = 0;
1744   mbuf->mb_count = 0;
1745
1746   /* reinitialize the mode-with-arg slots */
1747   for (i = 0; i < MAXMODEPARAMS; i++) {
1748     /* If we saved any, pack them down */
1749     if (MB_TYPE(mbuf, i) & MODE_SAVE) {
1750       mbuf->mb_modeargs[mbuf->mb_count] = mbuf->mb_modeargs[i];
1751       MB_TYPE(mbuf, mbuf->mb_count) &= ~MODE_SAVE; /* don't save anymore */
1752
1753       if (mbuf->mb_count++ == i) /* don't overwrite our hard work */
1754         continue;
1755     } else if (MB_TYPE(mbuf, i) & MODE_FREE)
1756       MyFree(MB_STRING(mbuf, i)); /* free string if needed */
1757
1758     MB_TYPE(mbuf, i) = 0;
1759     MB_UINT(mbuf, i) = 0;
1760   }
1761
1762   /* If we're supposed to flush it all, do so--all hail tail recursion */
1763   if (all && mbuf->mb_count)
1764     return modebuf_flush_int(mbuf, 1);
1765
1766   return 0;
1767 }
1768
1769 /*
1770  * This routine just initializes a ModeBuf structure with the information
1771  * needed and the options given.
1772  */
1773 void
1774 modebuf_init(struct ModeBuf *mbuf, struct Client *source,
1775              struct Client *connect, struct Channel *chan, unsigned int dest)
1776 {
1777   int i;
1778
1779   assert(0 != mbuf);
1780   assert(0 != source);
1781   assert(0 != chan);
1782   assert(0 != dest);
1783
1784   mbuf->mb_add = 0;
1785   mbuf->mb_rem = 0;
1786   mbuf->mb_source = source;
1787   mbuf->mb_connect = connect;
1788   mbuf->mb_channel = chan;
1789   mbuf->mb_dest = dest;
1790   mbuf->mb_count = 0;
1791
1792   /* clear each mode-with-parameter slot */
1793   for (i = 0; i < MAXMODEPARAMS; i++) {
1794     MB_TYPE(mbuf, i) = 0;
1795     MB_UINT(mbuf, i) = 0;
1796   }
1797 }
1798
1799 /*
1800  * This routine simply adds modes to be added or deleted; do a binary OR
1801  * with either MODE_ADD or MODE_DEL
1802  */
1803 void
1804 modebuf_mode(struct ModeBuf *mbuf, unsigned int mode)
1805 {
1806   assert(0 != mbuf);
1807   assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1808
1809   mode &= (MODE_ADD | MODE_DEL | MODE_PRIVATE | MODE_SECRET | MODE_MODERATED |
1810            MODE_TOPICLIMIT | MODE_INVITEONLY | MODE_NOPRIVMSGS);
1811
1812   if (!(mode & ~(MODE_ADD | MODE_DEL))) /* don't add empty modes... */
1813     return;
1814
1815   if (mode & MODE_ADD) {
1816     mbuf->mb_rem &= ~mode;
1817     mbuf->mb_add |= mode;
1818   } else {
1819     mbuf->mb_add &= ~mode;
1820     mbuf->mb_rem |= mode;
1821   }
1822 }
1823
1824 /*
1825  * This routine adds a mode to be added or deleted that takes a unsigned
1826  * int parameter; mode may *only* be the relevant mode flag ORed with one
1827  * of MODE_ADD or MODE_DEL
1828  */
1829 void
1830 modebuf_mode_uint(struct ModeBuf *mbuf, unsigned int mode, unsigned int uint)
1831 {
1832   assert(0 != mbuf);
1833   assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1834
1835   MB_TYPE(mbuf, mbuf->mb_count) = mode;
1836   MB_UINT(mbuf, mbuf->mb_count) = uint;
1837
1838   /* when we've reached the maximal count, flush the buffer */
1839   if (++mbuf->mb_count >=
1840       (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1841     modebuf_flush_int(mbuf, 0);
1842 }
1843
1844 /*
1845  * This routine adds a mode to be added or deleted that takes a string
1846  * parameter; mode may *only* be the relevant mode flag ORed with one of
1847  * MODE_ADD or MODE_DEL
1848  */
1849 void
1850 modebuf_mode_string(struct ModeBuf *mbuf, unsigned int mode, char *string,
1851                     int free)
1852 {
1853   assert(0 != mbuf);
1854   assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1855
1856   MB_TYPE(mbuf, mbuf->mb_count) = mode | (free ? MODE_FREE : 0);
1857   MB_STRING(mbuf, mbuf->mb_count) = string;
1858
1859   /* when we've reached the maximal count, flush the buffer */
1860   if (++mbuf->mb_count >=
1861       (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1862     modebuf_flush_int(mbuf, 0);
1863 }
1864
1865 /*
1866  * This routine adds a mode to be added or deleted that takes a client
1867  * parameter; mode may *only* be the relevant mode flag ORed with one of
1868  * MODE_ADD or MODE_DEL
1869  */
1870 void
1871 modebuf_mode_client(struct ModeBuf *mbuf, unsigned int mode,
1872                     struct Client *client)
1873 {
1874   assert(0 != mbuf);
1875   assert(0 != (mode & (MODE_ADD | MODE_DEL)));
1876
1877   MB_TYPE(mbuf, mbuf->mb_count) = mode;
1878   MB_CLIENT(mbuf, mbuf->mb_count) = client;
1879
1880   /* when we've reached the maximal count, flush the buffer */
1881   if (++mbuf->mb_count >=
1882       (MAXMODEPARAMS - (mbuf->mb_dest & MODEBUF_DEST_DEOP ? 1 : 0)))
1883     modebuf_flush_int(mbuf, 0);
1884 }
1885
1886 /*
1887  * This is the exported binding for modebuf_flush()
1888  */
1889 int
1890 modebuf_flush(struct ModeBuf *mbuf)
1891 {
1892   return modebuf_flush_int(mbuf, 1);
1893 }
1894
1895 /*
1896  * This extracts the simple modes contained in mbuf
1897  */
1898 void
1899 modebuf_extract(struct ModeBuf *mbuf, char *buf)
1900 {
1901   static int flags[] = {
1902 /*  MODE_CHANOP,        'o', */
1903 /*  MODE_VOICE,         'v', */
1904     MODE_PRIVATE,       'p',
1905     MODE_SECRET,        's',
1906     MODE_MODERATED,     'm',
1907     MODE_TOPICLIMIT,    't',
1908     MODE_INVITEONLY,    'i',
1909     MODE_NOPRIVMSGS,    'n',
1910     MODE_KEY,           'k',
1911 /*  MODE_BAN,           'b', */
1912     MODE_LIMIT,         'l',
1913     0x0, 0x0
1914   };
1915   unsigned int add;
1916   int i, bufpos = 0, len;
1917   int *flag_p;
1918   char *key = 0, limitbuf[20];
1919
1920   assert(0 != mbuf);
1921   assert(0 != buf);
1922
1923   buf[0] = '\0';
1924
1925   add = mbuf->mb_add;
1926
1927   for (i = 0; i < mbuf->mb_count; i++) { /* find keys and limits */
1928     if (MB_TYPE(mbuf, i) & MODE_ADD) {
1929       add |= MB_TYPE(mbuf, i) & (MODE_KEY | MODE_LIMIT);
1930
1931       if (MB_TYPE(mbuf, i) & MODE_KEY) /* keep strings */
1932         key = MB_STRING(mbuf, i);
1933       else if (MB_TYPE(mbuf, i) & MODE_LIMIT)
1934         ircd_snprintf(0, limitbuf, sizeof(limitbuf), "%d", MB_UINT(mbuf, i));
1935     }
1936   }
1937
1938   if (!add)
1939     return;
1940
1941   buf[bufpos++] = '+'; /* start building buffer */
1942
1943   for (flag_p = flags; flag_p[0]; flag_p += 2)
1944     if (*flag_p & add)
1945       buf[bufpos++] = flag_p[1];
1946
1947   for (i = 0, len = bufpos; i < len; i++) {
1948     if (buf[i] == 'k')
1949       build_string(buf, &bufpos, key, 0, ' ');
1950     else if (buf[i] == 'l')
1951       build_string(buf, &bufpos, limitbuf, 0, ' ');
1952   }
1953
1954   buf[bufpos] = '\0';
1955
1956   return;
1957 }
1958
1959 /*
1960  * Simple function to invalidate bans
1961  */
1962 void
1963 mode_ban_invalidate(struct Channel *chan)
1964 {
1965   struct Membership *member;
1966
1967   for (member = chan->members; member; member = member->next_member)
1968     ClearBanValid(member);
1969 }
1970
1971 /*
1972  * Simple function to drop invite structures
1973  */
1974 void
1975 mode_invite_clear(struct Channel *chan)
1976 {
1977   while (chan->invites)
1978     del_invite(chan->invites->value.cptr, chan);
1979 }
1980
1981 /* What we've done for mode_parse so far... */
1982 #define DONE_LIMIT      0x01    /* We've set the limit */
1983 #define DONE_KEY        0x02    /* We've set the key */
1984 #define DONE_BANLIST    0x04    /* We've sent the ban list */
1985 #define DONE_NOTOPER    0x08    /* We've sent a "Not oper" error */
1986 #define DONE_BANCLEAN   0x10    /* We've cleaned bans... */
1987
1988 struct ParseState {
1989   struct ModeBuf *mbuf;
1990   struct Client *cptr;
1991   struct Client *sptr;
1992   struct Channel *chptr;
1993   int parc;
1994   char **parv;
1995   unsigned int flags;
1996   unsigned int dir;
1997   unsigned int done;
1998   unsigned int add;
1999   unsigned int del;
2000   int args_used;
2001   int max_args;
2002   int numbans;
2003   struct SLink banlist[MAXPARA];
2004   struct {
2005     unsigned int flag;
2006     struct Client *client;
2007   } cli_change[MAXPARA];
2008 };
2009
2010 /*
2011  * Here's a helper function to deal with sending along "Not oper" or
2012  * "Not member" messages
2013  */
2014 static void
2015 send_notoper(struct ParseState *state)
2016 {
2017   if (state->done & DONE_NOTOPER)
2018     return;
2019
2020   send_reply(state->sptr, (state->flags & MODE_PARSE_NOTOPER) ?
2021              ERR_CHANOPRIVSNEEDED : ERR_NOTONCHANNEL, state->chptr->chname);
2022
2023   state->done |= DONE_NOTOPER;
2024 }
2025
2026 /*
2027  * Helper function to convert limits
2028  */
2029 static void
2030 mode_parse_limit(struct ParseState *state, int *flag_p)
2031 {
2032   unsigned int t_limit;
2033
2034   if (state->dir == MODE_ADD) { /* convert arg only if adding limit */
2035     if (MyUser(state->sptr) && state->max_args <= 0) /* too many args? */
2036       return;
2037
2038     if (state->parc <= 0) { /* warn if not enough args */
2039       if (MyUser(state->sptr))
2040         need_more_params(state->sptr, "MODE +l");
2041       return;
2042     }
2043
2044     t_limit = atoi(state->parv[state->args_used++]); /* grab arg */
2045     state->parc--;
2046     state->max_args--;
2047
2048     if (!(state->flags & MODE_PARSE_WIPEOUT) &&
2049         (!t_limit || t_limit == state->chptr->mode.limit))
2050       return;
2051   } else
2052     t_limit = state->chptr->mode.limit;
2053
2054   /* If they're not an oper, they can't change modes */
2055   if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2056     send_notoper(state);
2057     return;
2058   }
2059
2060   if (state->done & DONE_LIMIT) /* allow limit to be set only once */
2061     return;
2062   state->done |= DONE_LIMIT;
2063
2064   if (!state->mbuf)
2065     return;
2066
2067   modebuf_mode_uint(state->mbuf, state->dir | flag_p[0], t_limit);
2068
2069   if (state->flags & MODE_PARSE_SET) { /* set the limit */
2070     if (state->dir & MODE_ADD) {
2071       state->chptr->mode.mode |= flag_p[0];
2072       state->chptr->mode.limit = t_limit;
2073     } else {
2074       state->chptr->mode.mode &= ~flag_p[0];
2075       state->chptr->mode.limit = 0;
2076     }
2077   }
2078 }
2079
2080 /*
2081  * Helper function to convert keys
2082  */
2083 static void
2084 mode_parse_key(struct ParseState *state, int *flag_p)
2085 {
2086   char *t_str, *s;
2087   int t_len;
2088
2089   if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2090     return;
2091
2092   if (state->parc <= 0) { /* warn if not enough args */
2093     if (MyUser(state->sptr))
2094       need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2095                        "MODE -k");
2096     return;
2097   }
2098
2099   t_str = state->parv[state->args_used++]; /* grab arg */
2100   state->parc--;
2101   state->max_args--;
2102
2103   /* If they're not an oper, they can't change modes */
2104   if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2105     send_notoper(state);
2106     return;
2107   }
2108
2109   if (state->done & DONE_KEY) /* allow key to be set only once */
2110     return;
2111   state->done |= DONE_KEY;
2112
2113   t_len = KEYLEN + 1;
2114
2115   /* clean up the key string */
2116   s = t_str;
2117   while (*++s > ' ' && *s != ':' && --t_len)
2118     ;
2119   *s = '\0';
2120
2121   if (!*t_str) { /* warn if empty */
2122     if (MyUser(state->sptr))
2123       need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +k" :
2124                        "MODE -k");
2125     return;
2126   }
2127
2128   if (!state->mbuf)
2129     return;
2130
2131   /* can't add a key if one is set, nor can one remove the wrong key */
2132   if (!(state->flags & MODE_PARSE_FORCE))
2133     if ((state->dir == MODE_ADD && *state->chptr->mode.key) ||
2134         (state->dir == MODE_DEL &&
2135          ircd_strcmp(state->chptr->mode.key, t_str))) {
2136       send_reply(state->sptr, ERR_KEYSET, state->chptr->chname);
2137       return;
2138     }
2139
2140   if (!(state->flags & MODE_PARSE_WIPEOUT) && state->dir == MODE_ADD &&
2141       !ircd_strcmp(state->chptr->mode.key, t_str))
2142     return; /* no key change */
2143
2144   if (state->flags & MODE_PARSE_BOUNCE) {
2145     if (*state->chptr->mode.key) /* reset old key */
2146       modebuf_mode_string(state->mbuf, MODE_DEL | flag_p[0],
2147                           state->chptr->mode.key, 0);
2148     else /* remove new bogus key */
2149       modebuf_mode_string(state->mbuf, MODE_ADD | flag_p[0], t_str, 0);
2150   } else /* send new key */
2151     modebuf_mode_string(state->mbuf, state->dir | flag_p[0], t_str, 0);
2152
2153   if (state->flags & MODE_PARSE_SET) {
2154     if (state->dir == MODE_ADD) /* set the new key */
2155       ircd_strncpy(state->chptr->mode.key, t_str, KEYLEN);
2156     else /* remove the old key */
2157       *state->chptr->mode.key = '\0';
2158   }
2159 }
2160
2161 /*
2162  * Helper function to convert bans
2163  */
2164 static void
2165 mode_parse_ban(struct ParseState *state, int *flag_p)
2166 {
2167   char *t_str, *s;
2168   struct SLink *ban, *newban = 0;
2169
2170   if (state->parc <= 0) { /* Not enough args, send ban list */
2171     if (MyUser(state->sptr) && !(state->done & DONE_BANLIST)) {
2172       send_ban_list(state->sptr, state->chptr);
2173       state->done |= DONE_BANLIST;
2174     }
2175
2176     return;
2177   }
2178
2179   if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2180     return;
2181
2182   t_str = state->parv[state->args_used++]; /* grab arg */
2183   state->parc--;
2184   state->max_args--;
2185
2186   /* If they're not an oper, they can't change modes */
2187   if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2188     send_notoper(state);
2189     return;
2190   }
2191
2192   if ((s = strchr(t_str, ' ')))
2193     *s = '\0';
2194
2195   if (!*t_str || *t_str == ':') { /* warn if empty */
2196     if (MyUser(state->sptr))
2197       need_more_params(state->sptr, state->dir == MODE_ADD ? "MODE +b" :
2198                        "MODE -b");
2199     return;
2200   }
2201
2202   t_str = collapse(pretty_mask(t_str));
2203
2204   /* remember the ban for the moment... */
2205   if (state->dir == MODE_ADD) {
2206     newban = state->banlist + (state->numbans++);
2207     newban->next = 0;
2208
2209     DupString(newban->value.ban.banstr, t_str);
2210     newban->value.ban.who = state->sptr->name;
2211     newban->value.ban.when = TStime();
2212
2213     newban->flags = CHFL_BAN | MODE_ADD;
2214
2215     if ((s = strrchr(t_str, '@')) && check_if_ipmask(s + 1))
2216       newban->flags |= CHFL_BAN_IPMASK;
2217   }
2218
2219   if (!state->chptr->banlist) {
2220     state->chptr->banlist = newban; /* add our ban with its flags */
2221     state->done |= DONE_BANCLEAN;
2222     return;
2223   }
2224
2225   /* Go through all bans */
2226   for (ban = state->chptr->banlist; ban; ban = ban->next) {
2227     /* first, clean the ban flags up a bit */
2228     if (!(state->done & DONE_BANCLEAN))
2229       /* Note: We're overloading *lots* of bits here; be careful! */
2230       ban->flags &= ~(MODE_ADD | MODE_DEL | CHFL_BAN_OVERLAPPED);
2231
2232     /* Bit meanings:
2233      *
2234      * MODE_ADD            - Ban was added; if we're bouncing modes,
2235      *                       then we'll remove it below; otherwise,
2236      *                       we'll have to allocate a real ban
2237      *
2238      * MODE_DEL            - Ban was marked for deletion; if we're
2239      *                       bouncing modes, we'll have to re-add it,
2240      *                       otherwise, we'll have to remove it
2241      *
2242      * CHFL_BAN_OVERLAPPED - The ban we added turns out to overlap
2243      *                       with a ban already set; if we're
2244      *                       bouncing modes, we'll have to bounce
2245      *                       this one; otherwise, we'll just ignore
2246      *                       it when we process added bans
2247      */
2248
2249     if (state->dir == MODE_DEL && !ircd_strcmp(ban->value.ban.banstr, t_str)) {
2250       ban->flags |= MODE_DEL; /* delete one ban */
2251
2252       if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2253         break;
2254     } else if (state->dir == MODE_ADD) {
2255       /* if the ban already exists, don't worry about it */
2256       if (!ircd_strcmp(ban->value.ban.banstr, t_str)) {
2257         if (state->done & DONE_BANCLEAN) /* If we're cleaning, finish */
2258           break;
2259         continue;
2260       } else if (!mmatch(ban->value.ban.banstr, t_str)) {
2261         if (!(ban->flags & MODE_DEL))
2262           newban->flags |= CHFL_BAN_OVERLAPPED; /* our ban overlaps */
2263       } else if (!mmatch(t_str, ban->value.ban.banstr))
2264         ban->flags |= MODE_DEL; /* mark ban for deletion: overlapping */
2265
2266       if (!ban->next) {
2267         ban->next = newban; /* add our ban with its flags */
2268         break; /* get out of loop */
2269       }
2270     }
2271   }
2272   state->done |= DONE_BANCLEAN;
2273 }
2274
2275 /*
2276  * This is the bottom half of the ban processor
2277  */
2278 static void
2279 mode_process_bans(struct ParseState *state)
2280 {
2281   struct SLink *ban, *newban, *prevban, *nextban;
2282   int count = 0;
2283   int len = 0;
2284   int banlen;
2285   int changed = 0;
2286
2287   for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) {
2288     count++;
2289     banlen = strlen(ban->value.ban.banstr);
2290     len += banlen;
2291     nextban = ban->next;
2292
2293     if ((ban->flags & (MODE_DEL | MODE_ADD)) == (MODE_DEL | MODE_ADD)) {
2294       if (prevban)
2295         prevban->next = 0; /* Break the list; ban isn't a real ban */
2296       else
2297         state->chptr->banlist = 0;
2298
2299       count--;
2300       len -= banlen;
2301
2302       MyFree(ban->value.ban.banstr);
2303
2304       continue;
2305     } else if (ban->flags & MODE_DEL) { /* Deleted a ban? */
2306       modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
2307                           ban->value.ban.banstr,
2308                           state->flags & MODE_PARSE_SET);
2309
2310       if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
2311         if (prevban) /* clip it out of the list... */
2312           prevban->next = ban->next;
2313         else
2314           state->chptr->banlist = ban->next;
2315
2316         count--;
2317         len -= banlen;
2318
2319         MyFree(ban->value.ban.who);
2320         free_link(ban);
2321
2322         changed++;
2323         continue; /* next ban; keep prevban like it is */
2324       } else
2325         ban->flags &= (CHFL_BAN | CHFL_BAN_IPMASK); /* unset other flags */
2326     } else if (ban->flags & MODE_ADD) { /* adding a ban? */
2327       if (prevban)
2328         prevban->next = 0; /* Break the list; ban isn't a real ban */
2329       else
2330         state->chptr->banlist = 0;
2331
2332       /* If we're supposed to ignore it, do so. */
2333       if (ban->flags & CHFL_BAN_OVERLAPPED &&
2334           !(state->flags & MODE_PARSE_BOUNCE)) {
2335         count--;
2336         len -= banlen;
2337
2338         MyFree(ban->value.ban.banstr);
2339       } else {
2340         if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
2341             (len > MAXBANLENGTH || count >= MAXBANS)) {
2342           send_reply(state->sptr, ERR_BANLISTFULL, state->chptr->chname,
2343                      ban->value.ban.banstr);
2344           count--;
2345           len -= banlen;
2346
2347           MyFree(ban->value.ban.banstr);
2348         } else {
2349           /* add the ban to the buffer */
2350           modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
2351                               ban->value.ban.banstr,
2352                               !(state->flags & MODE_PARSE_SET));
2353
2354           if (state->flags & MODE_PARSE_SET) { /* create a new ban */
2355             newban = make_link();
2356             newban->value.ban.banstr = ban->value.ban.banstr;
2357             DupString(newban->value.ban.who, ban->value.ban.who);
2358             newban->value.ban.when = ban->value.ban.when;
2359             newban->flags = ban->flags & (CHFL_BAN | CHFL_BAN_IPMASK);
2360
2361             newban->next = state->chptr->banlist; /* and link it in */
2362             state->chptr->banlist = newban;
2363
2364             changed++;
2365           }
2366         }
2367       }
2368     }
2369
2370     prevban = ban;
2371   } /* for (prevban = 0, ban = state->chptr->banlist; ban; ban = nextban) { */
2372
2373   if (changed) /* if we changed the ban list, we must invalidate the bans */
2374     mode_ban_invalidate(state->chptr);
2375 }
2376
2377 /*
2378  * Helper function to process client changes
2379  */
2380 static void
2381 mode_parse_client(struct ParseState *state, int *flag_p)
2382 {
2383   char *t_str;
2384   struct Client *acptr;
2385   int i;
2386
2387   if (MyUser(state->sptr) && state->max_args <= 0) /* drop if too many args */
2388     return;
2389
2390   if (state->parc <= 0) /* return if not enough args */
2391     return;
2392
2393   t_str = state->parv[state->args_used++]; /* grab arg */
2394   state->parc--;
2395   state->max_args--;
2396
2397   /* If they're not an oper, they can't change modes */
2398   if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2399     send_notoper(state);
2400     return;
2401   }
2402
2403   if (MyUser(state->sptr)) /* find client we're manipulating */
2404     acptr = find_chasing(state->sptr, t_str, NULL);
2405   else
2406     acptr = findNUser(t_str);
2407
2408   if (!acptr)
2409     return; /* find_chasing() already reported an error to the user */
2410
2411   for (i = 0; i < MAXPARA; i++) /* find an element to stick them in */
2412     if (!state->cli_change[i].flag || (state->cli_change[i].client == acptr &&
2413                                        state->cli_change[i].flag & flag_p[0]))
2414       break; /* found a slot */
2415
2416   /* Store what we're doing to them */
2417   state->cli_change[i].flag = state->dir | flag_p[0];
2418   state->cli_change[i].client = acptr;
2419 }
2420
2421 /*
2422  * Helper function to process the changed client list
2423  */
2424 static void
2425 mode_process_clients(struct ParseState *state)
2426 {
2427   int i;
2428   struct Membership *member;
2429
2430   for (i = 0; state->cli_change[i].flag; i++) {
2431     assert(0 != state->cli_change[i].client);
2432
2433     /* look up member link */
2434     if (!(member = find_member_link(state->chptr,
2435                                     state->cli_change[i].client)) ||
2436         (MyUser(state->sptr) && IsZombie(member))) {
2437       if (MyUser(state->sptr))
2438         send_reply(state->sptr, ERR_USERNOTINCHANNEL,
2439                    state->cli_change[i].client->name, state->chptr->chname);
2440       continue;
2441     }
2442
2443     if ((state->cli_change[i].flag & MODE_ADD &&
2444          (state->cli_change[i].flag & member->status)) ||
2445         (state->cli_change[i].flag & MODE_DEL &&
2446          !(state->cli_change[i].flag & member->status)))
2447       continue; /* no change made, don't do anything */
2448
2449     /* see if the deop is allowed */
2450     if ((state->cli_change[i].flag & (MODE_DEL | MODE_CHANOP)) ==
2451         (MODE_DEL | MODE_CHANOP)) {
2452       /* prevent +k users from being deopped */
2453       if (IsChannelService(state->cli_change[i].client)) {
2454         if (state->flags & MODE_PARSE_FORCE) /* it was forced */
2455           sendto_opmask_butone(0, SNO_HACK4, "Deop of +k user on %H by %s",
2456                                state->chptr,
2457                                (IsServer(state->sptr) ? state->sptr->name :
2458                                 state->sptr->user->server->name));
2459
2460         else if (MyUser(state->sptr) && state->flags & MODE_PARSE_SET) {
2461           send_reply(state->sptr, ERR_ISCHANSERVICE,
2462                      state->cli_change[i].client->name, state->chptr->chname);
2463           continue;
2464         }
2465       }
2466
2467 #ifdef NO_OPER_DEOP_LCHAN
2468       /* don't allow local opers to be deopped on local channels */
2469       if (MyUser(state->sptr) && state->cli_change[i].client != state->sptr &&
2470           IsOperOnLocalChannel(state->cli_change[i].client,
2471                                state->chptr->chname)) {
2472         send_reply(state->sptr, ERR_ISOPERLCHAN,
2473                    state->cli_change[i].client->name, state->chptr->chname);
2474         continue;
2475       }
2476 #endif
2477     }
2478
2479     /* accumulate the change */
2480     modebuf_mode_client(state->mbuf, state->cli_change[i].flag,
2481                         state->cli_change[i].client);
2482
2483     /* actually effect the change */
2484     if (state->flags & MODE_PARSE_SET) {
2485       if (state->cli_change[i].flag & MODE_ADD) {
2486         member->status |= (state->cli_change[i].flag &
2487                            (MODE_CHANOP | MODE_VOICE));
2488         if (state->cli_change[i].flag & MODE_CHANOP)
2489           ClearDeopped(member);
2490       } else
2491         member->status &= ~(state->cli_change[i].flag &
2492                             (MODE_CHANOP | MODE_VOICE));
2493     }
2494   } /* for (i = 0; state->cli_change[i].flags; i++) { */
2495 }
2496
2497 /*
2498  * Helper function to process the simple modes
2499  */
2500 static void
2501 mode_parse_mode(struct ParseState *state, int *flag_p)
2502 {
2503   /* If they're not an oper, they can't change modes */
2504   if (state->flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER)) {
2505     send_notoper(state);
2506     return;
2507   }
2508
2509   if (!state->mbuf)
2510     return;
2511
2512   if (state->dir == MODE_ADD) {
2513     state->add |= flag_p[0];
2514     state->del &= ~flag_p[0];
2515
2516     if (flag_p[0] & MODE_SECRET) {
2517       state->add &= ~MODE_PRIVATE;
2518       state->del |= MODE_PRIVATE;
2519     } else if (flag_p[0] & MODE_PRIVATE) {
2520       state->add &= ~MODE_SECRET;
2521       state->del |= MODE_SECRET;
2522     }
2523   } else {
2524     state->add &= ~flag_p[0];
2525     state->del |= flag_p[0];
2526   }
2527
2528   assert(0 == (state->add & state->del));
2529   assert((MODE_SECRET | MODE_PRIVATE) !=
2530          (state->add & (MODE_SECRET | MODE_PRIVATE)));
2531 }
2532
2533 /*
2534  * This routine is intended to parse MODE or OPMODE commands and effect the
2535  * changes (or just build the bounce buffer).  We pass the starting offset
2536  * as a 
2537  */
2538 int
2539 mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
2540            struct Channel *chptr, int parc, char *parv[], unsigned int flags)
2541 {
2542   static int chan_flags[] = {
2543     MODE_CHANOP,        'o',
2544     MODE_VOICE,         'v',
2545     MODE_PRIVATE,       'p',
2546     MODE_SECRET,        's',
2547     MODE_MODERATED,     'm',
2548     MODE_TOPICLIMIT,    't',
2549     MODE_INVITEONLY,    'i',
2550     MODE_NOPRIVMSGS,    'n',
2551     MODE_KEY,           'k',
2552     MODE_BAN,           'b',
2553     MODE_LIMIT,         'l',
2554     MODE_ADD,           '+',
2555     MODE_DEL,           '-',
2556     0x0, 0x0
2557   };
2558   int i;
2559   int *flag_p;
2560   unsigned int t_mode;
2561   char *modestr;
2562   struct ParseState state;
2563
2564   assert(0 != cptr);
2565   assert(0 != sptr);
2566   assert(0 != chptr);
2567   assert(0 != parc);
2568   assert(0 != parv);
2569
2570   state.mbuf = mbuf;
2571   state.cptr = cptr;
2572   state.sptr = sptr;
2573   state.chptr = chptr;
2574   state.parc = parc;
2575   state.parv = parv;
2576   state.flags = flags;
2577   state.dir = MODE_ADD;
2578   state.done = 0;
2579   state.add = 0;
2580   state.del = 0;
2581   state.args_used = 0;
2582   state.max_args = MAXMODEPARAMS;
2583   state.numbans = 0;
2584
2585   for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
2586     state.banlist[i].next = 0;
2587     state.banlist[i].value.ban.banstr = 0;
2588     state.banlist[i].value.ban.who = 0;
2589     state.banlist[i].value.ban.when = 0;
2590     state.banlist[i].flags = 0;
2591     state.cli_change[i].flag = 0;
2592     state.cli_change[i].client = 0;
2593   }
2594
2595   modestr = state.parv[state.args_used++];
2596   state.parc--;
2597
2598   while (*modestr) {
2599     for (; *modestr; modestr++) {
2600       for (flag_p = chan_flags; flag_p[0]; flag_p += 2) /* look up flag */
2601         if (flag_p[1] == *modestr)
2602           break;
2603
2604       if (!flag_p[0]) { /* didn't find it?  complain and continue */
2605         if (MyUser(state.sptr))
2606           send_reply(state.sptr, ERR_UNKNOWNMODE, *modestr);
2607         continue;
2608       }
2609
2610       switch (*modestr) {
2611       case '+': /* switch direction to MODE_ADD */
2612       case '-': /* switch direction to MODE_DEL */
2613         state.dir = flag_p[0];
2614         break;
2615
2616       case 'l': /* deal with limits */
2617         mode_parse_limit(&state, flag_p);
2618         break;
2619
2620       case 'k': /* deal with keys */
2621         mode_parse_key(&state, flag_p);
2622         break;
2623
2624       case 'b': /* deal with bans */
2625         mode_parse_ban(&state, flag_p);
2626         break;
2627
2628       case 'o': /* deal with ops/voice */
2629       case 'v':
2630         mode_parse_client(&state, flag_p);
2631         break;
2632
2633       default: /* deal with other modes */
2634         mode_parse_mode(&state, flag_p);
2635         break;
2636       } /* switch (*modestr) { */
2637     } /* for (; *modestr; modestr++) { */
2638
2639     if (state.flags & MODE_PARSE_BURST)
2640       break; /* don't interpret any more arguments */
2641
2642     if (state.parc > 0) { /* process next argument in string */
2643       modestr = state.parv[state.args_used++];
2644       state.parc--;
2645
2646       /* is it a TS? */
2647       if (IsServer(state.sptr) && !state.parc && IsDigit(*modestr)) {
2648         time_t recv_ts;
2649
2650         if (!(state.flags & MODE_PARSE_SET))      /* don't set earlier TS if */
2651           break;                     /* we're then going to bounce the mode! */
2652
2653         recv_ts = atoi(modestr);
2654
2655         if (recv_ts && recv_ts < state.chptr->creationtime)
2656           state.chptr->creationtime = recv_ts; /* respect earlier TS */
2657
2658         break; /* break out of while loop */
2659       } else if (state.flags & MODE_PARSE_STRICT ||
2660                  (MyUser(state.sptr) && state.max_args <= 0)) {
2661         state.parc++; /* we didn't actually gobble the argument */
2662         state.args_used--;
2663         break; /* break out of while loop */
2664       }
2665     }
2666   } /* while (*modestr) { */
2667
2668   /*
2669    * the rest of the function finishes building resultant MODEs; if the
2670    * origin isn't a member or an oper, skip it.
2671    */
2672   if (!state.mbuf || state.flags & (MODE_PARSE_NOTOPER | MODE_PARSE_NOTMEMBER))
2673     return state.args_used; /* tell our parent how many args we gobbled */
2674
2675   t_mode = state.chptr->mode.mode;
2676
2677   if (state.del & t_mode) { /* delete any modes to be deleted... */
2678     modebuf_mode(state.mbuf, MODE_DEL | (state.del & t_mode));
2679
2680     t_mode &= ~state.del;
2681   }
2682   if (state.add & ~t_mode) { /* add any modes to be added... */
2683     modebuf_mode(state.mbuf, MODE_ADD | (state.add & ~t_mode));
2684
2685     t_mode |= state.add;
2686   }
2687
2688   if (state.flags & MODE_PARSE_SET) { /* set the channel modes */
2689     if ((state.chptr->mode.mode & MODE_INVITEONLY) &&
2690         !(t_mode & MODE_INVITEONLY))
2691       mode_invite_clear(state.chptr);
2692
2693     state.chptr->mode.mode = t_mode;
2694   }
2695
2696   if (state.flags & MODE_PARSE_WIPEOUT) {
2697     if (state.chptr->mode.limit && !(state.done & DONE_LIMIT))
2698       modebuf_mode_uint(state.mbuf, MODE_DEL | MODE_LIMIT,
2699                         state.chptr->mode.limit);
2700     if (*state.chptr->mode.key && !(state.done & DONE_KEY))
2701       modebuf_mode_string(state.mbuf, MODE_DEL | MODE_KEY,
2702                           state.chptr->mode.key, 0);
2703   }
2704
2705   if (state.done & DONE_BANCLEAN) /* process bans */
2706     mode_process_bans(&state);
2707
2708   /* process client changes */
2709   if (state.cli_change[0].flag)
2710     mode_process_clients(&state);
2711
2712   return state.args_used; /* tell our parent how many args we gobbled */
2713 }
2714
2715 /*
2716  * Initialize a join buffer
2717  */
2718 void
2719 joinbuf_init(struct JoinBuf *jbuf, struct Client *source,
2720              struct Client *connect, unsigned int type, char *comment,
2721              time_t create)
2722 {
2723   int i;
2724
2725   assert(0 != jbuf);
2726   assert(0 != source);
2727   assert(0 != connect);
2728
2729   jbuf->jb_source = source; /* just initialize struct JoinBuf */
2730   jbuf->jb_connect = connect;
2731   jbuf->jb_type = type;
2732   jbuf->jb_comment = comment;
2733   jbuf->jb_create = create;
2734   jbuf->jb_count = 0;
2735   jbuf->jb_strlen = (((type == JOINBUF_TYPE_JOIN ||
2736                        type == JOINBUF_TYPE_PART ||
2737                        type == JOINBUF_TYPE_PARTALL) ?
2738                       STARTJOINLEN : STARTCREATELEN) +
2739                      (comment ? strlen(comment) + 2 : 0));
2740
2741   for (i = 0; i < MAXJOINARGS; i++)
2742     jbuf->jb_channels[i] = 0;
2743 }
2744
2745 /*
2746  * Add a channel to the join buffer
2747  */
2748 void
2749 joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
2750 {
2751   unsigned int len;
2752
2753   assert(0 != jbuf);
2754
2755   if (!chan) {
2756     if (jbuf->jb_type == JOINBUF_TYPE_JOIN)
2757       sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect, "0");
2758
2759     return;
2760   }
2761
2762   if (jbuf->jb_type == JOINBUF_TYPE_PART ||
2763       jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
2764     /* Send notification to channel */
2765     if (!(flags & CHFL_ZOMBIE))
2766       sendcmdto_channel_butserv(jbuf->jb_source, CMD_PART, chan,
2767                                 (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2768                                 ":%H" : "%H :%s", chan, jbuf->jb_comment);
2769     else if (MyUser(jbuf->jb_source))
2770       sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
2771                     (flags & CHFL_BANNED || !jbuf->jb_comment) ?
2772                     ":%H" : "%H :%s", chan, jbuf->jb_comment);
2773     /* XXX: Shouldn't we send a PART here anyway? */
2774     /* to users on the channel?  Why?  From their POV, the user isn't on
2775      * the channel anymore anyway.  We don't send to servers until below,
2776      * when we gang all the channel parts together.  Note that this is
2777      * exactly the same logic, albeit somewhat more concise, as was in
2778      * the original m_part.c */
2779
2780     if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2781         IsLocalChannel(chan->chname)) /* got to remove user here */
2782       remove_user_from_channel(jbuf->jb_source, chan);
2783   } else {
2784     /* Add user to channel */
2785     add_user_to_channel(chan, jbuf->jb_source, flags);
2786
2787     /* send notification to all servers */
2788     if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !IsLocalChannel(chan->chname))
2789       sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
2790                             "%H %Tu", chan, chan->creationtime);
2791
2792     /* Send the notification to the channel */
2793     sendcmdto_channel_butserv(jbuf->jb_source, CMD_JOIN, chan, ":%H", chan);
2794
2795     /* send an op, too, if needed */
2796     if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE &&
2797         !IsModelessChannel(chan->chname))
2798       sendcmdto_channel_butserv(jbuf->jb_source, CMD_MODE, chan, "%H +o %C",
2799                                 chan, jbuf->jb_source);
2800   }
2801
2802   if (jbuf->jb_type == JOINBUF_TYPE_PARTALL || IsLocalChannel(chan->chname))
2803     return; /* don't send to remote */
2804
2805   /* figure out if channel name will cause buffer to be overflowed */
2806   len = chan ? strlen(chan->chname) + 1 : 2;
2807   if (jbuf->jb_strlen + len > BUFSIZE)
2808     joinbuf_flush(jbuf);
2809
2810   /* add channel to list of channels to send and update counts */
2811   jbuf->jb_channels[jbuf->jb_count++] = chan;
2812   jbuf->jb_strlen += len;
2813
2814   /* if we've used up all slots, flush */
2815   if (jbuf->jb_count >= MAXJOINARGS)
2816     joinbuf_flush(jbuf);
2817 }
2818
2819 /*
2820  * Flush the channel list to remote servers
2821  */
2822 int
2823 joinbuf_flush(struct JoinBuf *jbuf)
2824 {
2825   char chanlist[BUFSIZE];
2826   int chanlist_i = 0;
2827   int i;
2828
2829   if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
2830       jbuf->jb_type == JOINBUF_TYPE_JOIN)
2831     return 0; /* no joins to process */
2832
2833   for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
2834     build_string(chanlist, &chanlist_i,
2835                  jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
2836                  i == 0 ? '\0' : ',');
2837     if (JOINBUF_TYPE_PART == jbuf->jb_type)
2838       /* Remove user from channel */
2839       remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
2840
2841     jbuf->jb_channels[i] = 0; /* mark slot empty */
2842   }
2843
2844   jbuf->jb_count = 0; /* reset base counters */
2845   jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
2846                       STARTJOINLEN : STARTCREATELEN) +
2847                      (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
2848
2849   /* and send the appropriate command */
2850   switch (jbuf->jb_type) {
2851   case JOINBUF_TYPE_CREATE:
2852     sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
2853                           "%s %Tu", chanlist, jbuf->jb_create);
2854     break;
2855
2856   case JOINBUF_TYPE_PART:
2857     sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
2858                           jbuf->jb_comment ? "%s :%s" : "%s", chanlist,
2859                           jbuf->jb_comment);
2860     break;
2861   }
2862
2863   return 0;
2864 }