]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/mode.cpp
Major optimizations!
[user/henk/code/inspircd.git] / src / mode.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "inspircd.h"
18 #include "inspircd_io.h"
19 #include "inspircd_util.h"
20 #include "inspircd_config.h"
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
26 #include <cstdio>
27 #include <time.h>
28 #include <string>
29 #ifdef GCC3
30 #include <ext/hash_map>
31 #else
32 #include <hash_map>
33 #endif
34 #include <map>
35 #include <sstream>
36 #include <vector>
37 #include <errno.h>
38 #include <deque>
39 #include <errno.h>
40 #include <unistd.h>
41 #include <sched.h>
42 #include "connection.h"
43 #include "users.h"
44 #include "servers.h"
45 #include "ctables.h"
46 #include "globals.h"
47 #include "modules.h"
48 #include "dynamic.h"
49 #include "wildcard.h"
50 #include "message.h"
51 #include "commands.h"
52 #include "xline.h"
53 #include "inspstring.h"
54
55 using namespace std;
56
57 extern int MODCOUNT;
58 extern std::vector<Module*> modules;
59 extern std::vector<ircd_module*> factory;
60 extern std::vector<std::string> module_names;
61
62
63 extern int LogLevel;
64 extern char ServerName[MAXBUF];
65 extern char Network[MAXBUF];
66 extern char ServerDesc[MAXBUF];
67 extern char AdminName[MAXBUF];
68 extern char AdminEmail[MAXBUF];
69 extern char AdminNick[MAXBUF];
70 extern char diepass[MAXBUF];
71 extern char restartpass[MAXBUF];
72 extern char motd[MAXBUF];
73 extern char rules[MAXBUF];
74 extern char list[MAXBUF];
75 extern char PrefixQuit[MAXBUF];
76 extern char DieValue[MAXBUF];
77
78 extern bool AllowHalfop;
79 extern bool AllowProtect;
80 extern bool AllowFounder;
81
82 extern time_t TIME;
83
84 char* give_ops(userrec *user,char *dest,chanrec *chan,int status)
85 {
86         userrec *d;
87         int i;
88         
89         if ((!user) || (!dest) || (!chan))
90         {
91                 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
92                 return NULL;
93         }
94
95         if (!isnick(dest))
96         {
97                 log(DEFAULT,"the target nickname given to give_ops was invalid");
98                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
99                 return NULL;
100         }
101         d = Find(dest);
102         if (!d)
103         {
104                 log(DEFAULT,"the target nickname given to give_ops couldnt be found");
105                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
106                 return NULL;
107         }
108         else
109         {
110
111                 int MOD_RESULT = 0;
112                 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
113                 
114                 if (MOD_RESULT == ACR_DENY)
115                         return NULL;
116                 if (MOD_RESULT == ACR_DEFAULT)
117                 {
118                         if ((status < STATUS_OP) && (!is_uline(user->server)))
119                         {
120                                 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
121                                 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
122                                 return NULL;
123                         }
124                 }
125
126
127                 for (int i = 0; i != MAXCHANS; i++)
128                 {
129                         if ((d->chans[i].channel != NULL) && (chan != NULL))
130                         if (!strcasecmp(d->chans[i].channel->name,chan->name))
131                         {
132                         if (d->chans[i].uc_modes & UCMODE_OP)
133                                 {
134                                         /* mode already set on user, dont allow multiple */
135                                         log(DEFAULT,"The target user given to give_ops was already opped on the channel");
136                                         return NULL;
137                                 }
138                                 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
139                                 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
140                                 return d->nick;
141                         }
142                 }
143                 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
144         }
145         return NULL;
146 }
147
148 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
149 {
150         userrec *d;
151         int i;
152         
153         if ((!user) || (!dest) || (!chan))
154         {
155                 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
156                 return NULL;
157         }
158
159         d = Find(dest);
160         if (!isnick(dest))
161         {
162                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
163                 return NULL;
164         }
165         if (!d)
166         {
167                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
168                 return NULL;
169         }
170         else
171         {
172                 int MOD_RESULT = 0;
173                 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
174                 
175                 if (MOD_RESULT == ACR_DENY)
176                         return NULL;
177                 if (MOD_RESULT == ACR_DEFAULT)
178                 {
179                         if ((status < STATUS_OP) && (!is_uline(user->server)))
180                         {
181                                 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
182                                 return NULL;
183                         }
184                 }
185
186                 for (int i = 0; i != MAXCHANS; i++)
187                 {
188                         if ((d->chans[i].channel != NULL) && (chan != NULL))
189                         if (!strcasecmp(d->chans[i].channel->name,chan->name))
190                         {
191                                 if (d->chans[i].uc_modes & UCMODE_HOP)
192                                 {
193                                         /* mode already set on user, dont allow multiple */
194                                         return NULL;
195                                 }
196                                 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
197                                 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
198                                 return d->nick;
199                         }
200                 }
201         }
202         return NULL;
203 }
204
205 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
206 {
207         userrec *d;
208         int i;
209         
210         if ((!user) || (!dest) || (!chan))
211         {
212                 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
213                 return NULL;
214         }
215
216         d = Find(dest);
217         if (!isnick(dest))
218         {
219                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
220                 return NULL;
221         }
222         if (!d)
223         {
224                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
225                 return NULL;
226         }
227         else
228         {
229                 int MOD_RESULT = 0;
230                 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
231                 
232                 if (MOD_RESULT == ACR_DENY)
233                         return NULL;
234                 if (MOD_RESULT == ACR_DEFAULT)
235                 {
236                         if ((status < STATUS_HOP) && (!is_uline(user->server)))
237                         {
238                                 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
239                                 return NULL;
240                         }
241                 }
242
243                 for (int i = 0; i != MAXCHANS; i++)
244                 {
245                         if ((d->chans[i].channel != NULL) && (chan != NULL))
246                         if (!strcasecmp(d->chans[i].channel->name,chan->name))
247                         {
248                                 if (d->chans[i].uc_modes & UCMODE_VOICE)
249                                 {
250                                         /* mode already set on user, dont allow multiple */
251                                         return NULL;
252                                 }
253                                 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
254                                 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
255                                 return d->nick;
256                         }
257                 }
258         }
259         return NULL;
260 }
261
262 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
263 {
264         userrec *d;
265         int i;
266         
267         if ((!user) || (!dest) || (!chan))
268         {
269                 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
270                 return NULL;
271         }
272
273         d = Find(dest);
274         if (!isnick(dest))
275         {
276                 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
277                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
278                 return NULL;
279         }
280         if (!d)
281         {
282                 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
283                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
284                 return NULL;
285         }
286         else
287         {
288                 int MOD_RESULT = 0;
289                 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
290                 
291                 if (MOD_RESULT == ACR_DENY)
292                         return NULL;
293                 if (MOD_RESULT == ACR_DEFAULT)
294                 {
295                         if ((status < STATUS_OP) && (!is_uline(user->server)))
296                         {
297                                 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
298                                 return NULL;
299                         }
300                 }
301
302                 for (int i = 0; i != MAXCHANS; i++)
303                 {
304                         if ((d->chans[i].channel != NULL) && (chan != NULL))
305                         if (!strcasecmp(d->chans[i].channel->name,chan->name))
306                         {
307                                 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
308                                 {
309                                         /* mode already set on user, dont allow multiple */
310                                         return NULL;
311                                 }
312                                 d->chans[i].uc_modes ^= UCMODE_OP;
313                                 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
314                                 return d->nick;
315                         }
316                 }
317                 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
318         }
319         return NULL;
320 }
321
322 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
323 {
324         userrec *d;
325         int i;
326         
327         if ((!user) || (!dest) || (!chan))
328         {
329                 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
330                 return NULL;
331         }
332
333         d = Find(dest);
334         if (!isnick(dest))
335         {
336                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
337                 return NULL;
338         }
339         if (!d)
340         {
341                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
342                 return NULL;
343         }
344         else
345         {
346                 int MOD_RESULT = 0;
347                 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
348                 
349                 if (MOD_RESULT == ACR_DENY)
350                         return NULL;
351                 if (MOD_RESULT == ACR_DEFAULT)
352                 {
353                         if ((status < STATUS_OP) && (!is_uline(user->server)))
354                         {
355                                 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
356                                 return NULL;
357                         }
358                 }
359
360                 for (int i = 0; i != MAXCHANS; i++)
361                 {
362                         if ((d->chans[i].channel != NULL) && (chan != NULL))
363                         if (!strcasecmp(d->chans[i].channel->name,chan->name))
364                         {
365                                 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
366                                 {
367                                         /* mode already set on user, dont allow multiple */
368                                         return NULL;
369                                 }
370                                 d->chans[i].uc_modes ^= UCMODE_HOP;
371                                 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
372                                 return d->nick;
373                         }
374                 }
375         }
376         return NULL;
377 }
378
379 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
380 {
381         userrec *d;
382         int i;
383         
384         if ((!user) || (!dest) || (!chan))
385         {
386                 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
387                 return NULL;
388         }
389
390         d = Find(dest);
391         if (!isnick(dest))
392         {
393                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
394                 return NULL;
395         }
396         if (!d)
397         {
398                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
399                 return NULL;
400         }
401         else
402         {
403                 int MOD_RESULT = 0;
404                 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
405                 
406                 if (MOD_RESULT == ACR_DENY)
407                         return NULL;
408                 if (MOD_RESULT == ACR_DEFAULT)
409                 {
410                         if ((status < STATUS_HOP) && (!is_uline(user->server)))
411                         {
412                                 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
413                                 return NULL;
414                         }
415                 }
416
417                 for (int i = 0; i != MAXCHANS; i++)
418                 {
419                         if ((d->chans[i].channel != NULL) && (chan != NULL))
420                         if (!strcasecmp(d->chans[i].channel->name,chan->name))
421                         {
422                                 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
423                                 {
424                                         /* mode already set on user, dont allow multiple */
425                                         return NULL;
426                                 }
427                                 d->chans[i].uc_modes ^= UCMODE_VOICE;
428                                 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
429                                 return d->nick;
430                         }
431                 }
432         }
433         return NULL;
434 }
435
436 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
437 {
438         if ((!user) || (!dest) || (!chan)) {
439                 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
440                 return NULL;
441         }
442
443         BanItem b;
444         if ((!user) || (!dest) || (!chan))
445                 return NULL;
446         if (strchr(dest,'!')==0)
447                 return NULL;
448         if (strchr(dest,'@')==0)
449                 return NULL;
450         for (int i = 0; i < strlen(dest); i++)
451                 if (dest[i] < 32)
452                         return NULL;
453         for (int i = 0; i < strlen(dest); i++)
454                 if (dest[i] > 126)
455                         return NULL;
456         int c = 0;
457         for (int i = 0; i < strlen(dest); i++)
458                 if (dest[i] == '!')
459                         c++;
460         if (c>1)
461                 return NULL;
462         c = 0;
463         for (int i = 0; i < strlen(dest); i++)
464                 if (dest[i] == '@')
465                         c++;
466         if (c>1)
467                 return NULL;
468
469         long maxbans = GetMaxBans(chan->name);
470         if (chan->bans.size() > maxbans)
471         {
472                 WriteServ(user->fd,"478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)",user->nick, chan->name,chan->name,maxbans);
473                 return NULL;
474         }
475
476         log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
477
478         TidyBan(dest);
479         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
480         {
481                 if (!strcasecmp(i->data,dest))
482                 {
483                         // dont allow a user to set the same ban twice
484                         return NULL;
485                 }
486         }
487
488         b.set_time = TIME;
489         strncpy(b.data,dest,MAXBUF);
490         strncpy(b.set_by,user->nick,NICKMAX);
491         chan->bans.push_back(b);
492         return dest;
493 }
494
495 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
496 {
497         if ((!user) || (!dest) || (!chan)) {
498                 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
499                 return 0;
500         }
501
502         log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
503         for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
504         {
505                 if (!strcasecmp(i->data,dest))
506                 {
507                         chan->bans.erase(i);
508                         return dest;
509                 }
510         }
511         return NULL;
512 }
513
514 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
515 {
516         if (!parameters) {
517                 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
518                 return;
519         }
520
521         char modelist[MAXBUF];
522         char outlist[MAXBUF];
523         char outstr[MAXBUF];
524         char outpars[32][MAXBUF];
525         int param = 2;
526         int pc = 0;
527         int ptr = 0;
528         int mdir = 1;
529         char* r = NULL;
530         bool k_set = false, l_set = false;
531
532         if (pcnt < 2)
533         {
534                 return;
535         }
536
537         int MOD_RESULT = 0;
538         FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
539         
540         if (MOD_RESULT == ACR_DENY)
541                 return;
542
543         log(DEBUG,"process_modes: start: parameters=%d",pcnt);
544
545         strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
546                                                  * parameters[2] onwards are parameters for
547                                                  * modes that require them :) */
548         strlcpy(outlist,"+",MAXBUF);
549         mdir = 1;
550
551         log(DEBUG,"process_modes: modelist: %s",modelist);
552
553         for (ptr = 0; ptr < strlen(modelist); ptr++)
554         {
555                 r = NULL;
556
557                 {
558                         log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
559                         char modechar = modelist[ptr];
560                         switch (modelist[ptr])
561                         {
562                                 case '-':
563                                         if (mdir != 0)
564                                         {
565                                                 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
566                                                 {
567                                                         outlist[strlen(outlist)-1] = '-';
568                                                 }
569                                                 else
570                                                 {
571                                                         strcat(outlist,"-");
572                                                 }
573                                         }
574                                         mdir = 0;
575                                         
576                                 break;                  
577
578                                 case '+':
579                                         if (mdir != 1)
580                                         {
581                                                 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
582                                                 {
583                                                         outlist[strlen(outlist)-1] = '+';
584                                                 }
585                                                 else
586                                                 {
587                                                         strcat(outlist,"+");
588                                                 }
589                                         }
590                                         mdir = 1;
591                                 break;
592
593                                 case 'o':
594                                         log(DEBUG,"Ops");
595                                         if ((param >= pcnt)) break;
596                                         log(DEBUG,"Enough parameters left");
597                                         if (mdir == 1)
598                                         {
599                                                 log(DEBUG,"calling give_ops");
600                                                 r = give_ops(user,parameters[param++],chan,status);
601                                         }
602                                         else
603                                         {
604                                                 log(DEBUG,"calling take_ops");
605                                                 r = take_ops(user,parameters[param++],chan,status);
606                                         }
607                                         if (r)
608                                         {
609                                                 strlcat(outlist,"o",MAXBUF);
610                                                 strlcpy(outpars[pc++],r,MAXBUF);
611                                         }
612                                 break;
613                         
614                                 case 'h':
615                                         if (((param >= pcnt)) || (!AllowHalfop)) break;
616                                         if (mdir == 1)
617                                         {
618                                                 r = give_hops(user,parameters[param++],chan,status);
619                                         }
620                                         else
621                                         {
622                                                 r = take_hops(user,parameters[param++],chan,status);
623                                         }
624                                         if (r)
625                                         {
626                                                 strlcat(outlist,"h",MAXBUF);
627                                                 strlcpy(outpars[pc++],r,MAXBUF);
628                                         }
629                                 break;
630                         
631                                 
632                                 case 'v':
633                                         if ((param >= pcnt)) break;
634                                         if (mdir == 1)
635                                         {
636                                                 r = give_voice(user,parameters[param++],chan,status);
637                                         }
638                                         else
639                                         {
640                                                 r = take_voice(user,parameters[param++],chan,status);
641                                         }
642                                         if (r)
643                                         {
644                                                 strlcat(outlist,"v",MAXBUF);
645                                                 strlcpy(outpars[pc++],r,MAXBUF);
646                                         }
647                                 break;
648                                 
649                                 case 'b':
650                                         if ((param >= pcnt)) break;
651                                         if (mdir == 1)
652                                         {
653                                                 r = add_ban(user,parameters[param++],chan,status);
654                                         }
655                                         else
656                                         {
657                                                 r = take_ban(user,parameters[param++],chan,status);
658                                         }
659                                         if (r)
660                                         {
661                                                 strlcat(outlist,"b",MAXBUF);
662                                                 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
663                                         }
664                                 break;
665
666
667                                 case 'k':
668                                         if ((param >= pcnt))
669                                                 break;
670
671                                         if (mdir == 1)
672                                         {
673                                                 if (k_set)
674                                                         break;
675                                                 
676                                                 if (!strcmp(chan->key,""))
677                                                 {
678                                                         strcat(outlist,"k");
679                                                         char key[MAXBUF];
680                                                         strlcpy(key,parameters[param++],MAXBUF);
681                                                         if (strlen(key)>32) {
682                                                                 key[31] = '\0';
683                                                         }
684                                                         strlcpy(outpars[pc++],key,MAXBUF);
685                                                         strlcpy(chan->key,key,MAXBUF);
686                                                         k_set = true;
687                                                 }
688                                         }
689                                         else
690                                         {
691                                                 /* checks on -k are case sensitive and only accurate to the
692                                                    first 32 characters */
693                                                 char key[MAXBUF];
694                                                 strlcpy(key,parameters[param++],MAXBUF);
695                                                 if (strlen(key)>32) {
696                                                         key[31] = '\0';
697                                                 }
698                                                 /* only allow -k if correct key given */
699                                                 if (!strcmp(chan->key,key))
700                                                 {
701                                                         strlcat(outlist,"k",MAXBUF);
702                                                         strlcpy(chan->key,"",MAXBUF);
703                                                         strlcpy(outpars[pc++],key,MAXBUF);
704                                                 }
705                                         }
706                                 break;
707                                 
708                                 case 'l':
709                                         if (mdir == 0)
710                                         {
711                                                 if (chan->limit)
712                                                 {
713                                                         strcat(outlist,"l");
714                                                         chan->limit = 0;
715                                                 }
716                                         }
717                                         
718                                         if ((param >= pcnt)) break;
719                                         if (mdir == 1)
720                                         {
721                                                 if (l_set)
722                                                         break;
723                                                 
724                                                 bool invalid = false;
725                                                 for (int i = 0; i < strlen(parameters[param]); i++)
726                                                 {
727                                                         if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
728                                                         {
729                                                                 invalid = true;
730                                                         }
731                                                 }
732                                                 if (atoi(parameters[param]) < 1)
733                                                 {
734                                                         invalid = true;
735                                                 }
736
737                                                 if (invalid)
738                                                         break;
739                                                 
740                                                 chan->limit = atoi(parameters[param]);
741                                                 
742                                                 // reported by mech: large values cause underflow
743                                                 if (chan->limit < 0)
744                                                         chan->limit = 0x7FFFFF;
745                                                         
746                                                 if (chan->limit)
747                                                 {
748                                                         strlcat(outlist,"l",MAXBUF);
749                                                         strlcpy(outpars[pc++],parameters[param++],MAXBUF);
750                                                         l_set = true;
751                                                 }
752                                         }
753                                 break;
754                                 
755                                 case 'i':
756                                         if (chan->inviteonly != mdir)
757                                         {
758                                                 strlcat(outlist,"i",MAXBUF);
759                                         }
760                                         chan->inviteonly = mdir;
761                                 break;
762                                 
763                                 case 't':
764                                         if (chan->topiclock != mdir)
765                                         {
766                                                 strlcat(outlist,"t",MAXBUF);
767                                         }
768                                         chan->topiclock = mdir;
769                                 break;
770                                 
771                                 case 'n':
772                                         if (chan->noexternal != mdir)
773                                         {
774                                                 strlcat(outlist,"n",MAXBUF);
775                                         }
776                                         chan->noexternal = mdir;
777                                 break;
778                                 
779                                 case 'm':
780                                         if (chan->moderated != mdir)
781                                         {
782                                                 strlcat(outlist,"m",MAXBUF);
783                                         }
784                                         chan->moderated = mdir;
785                                 break;
786                                 
787                                 case 's':
788                                         if (chan->secret != mdir)
789                                         {
790                                                 strcat(outlist,"s");
791                                                 if (chan->c_private)
792                                                 {
793                                                         chan->c_private = 0;
794                                                         if (mdir)
795                                                         {
796                                                                 strlcat(outlist,"-p+",MAXBUF);
797                                                         }
798                                                         else
799                                                         {
800                                                                 strlcat(outlist,"+p-",MAXBUF);
801                                                         }
802                                                 }
803                                         }
804                                         chan->secret = mdir;
805                                 break;
806                                 
807                                 case 'p':
808                                         if (chan->c_private != mdir)
809                                         {
810                                                 strlcat(outlist,"p",MAXBUF);
811                                                 if (chan->secret)
812                                                 {
813                                                         chan->secret = 0;
814                                                         if (mdir)
815                                                         {
816                                                                 strlcat(outlist,"-s+",MAXBUF);
817                                                         }
818                                                         else
819                                                         {
820                                                                 strlcat(outlist,"+s-",MAXBUF);
821                                                         }
822                                                 }
823                                         }
824                                         chan->c_private = mdir;
825                                 break;
826                                 
827                                 default:
828                                         log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
829                                         string_list p;
830                                         p.clear();
831                                         if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
832                                         {
833                                                 if (!ModeIsListMode(modechar,MT_CHANNEL))
834                                                 {
835                                                         log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
836                                                         break;
837                                                 }
838                                         }
839                                         if (ModeDefined(modechar,MT_CHANNEL))
840                                         {
841                                                 log(DEBUG,"A module has claimed this mode");
842                                                 if (param<pcnt)
843                                                 {
844                                                         if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
845                                                         {
846                                                                 p.push_back(parameters[param]);
847                                                         }
848                                                         if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
849                                                         {
850                                                                 p.push_back(parameters[param]);
851                                                         }
852                                                 }
853                                                 bool handled = false;
854                                                 if (param>=pcnt)
855                                                 {
856                                                         // we're supposed to have a parameter, but none was given... so dont handle the mode.
857                                                         if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))) 
858                                                         {
859                                                                 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
860                                                                 handled = true;
861                                                                 param++;
862                                                         }
863                                                 }
864                                                 for (int i = 0; i <= MODCOUNT; i++)
865                                                 {
866                                                         if (!handled)
867                                                         {
868                                                                 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
869                                                                 if (t != 0)
870                                                                 {
871                                                                         log(DEBUG,"OnExtendedMode returned nonzero for a module");
872                                                                         char app[] = {modechar, 0};
873                                                                         if (ModeIsListMode(modechar,MT_CHANNEL))
874                                                                         {
875                                                                                 if (t == -1)
876                                                                                 {
877                                                                                         pc++;
878                                                                                 }
879                                                                                 else
880                                                                                 {
881                                                                                         if (ptr>0)
882                                                                                         {
883                                                                                                 strlcat(outlist, app,MAXBUF);
884                                                                                         }
885                                                                                         strlcpy(outpars[pc++],parameters[param++],MAXBUF);
886                                                                                 }
887                                                                         }
888                                                                         else
889                                                                         {
890                                                                                 if (ptr>0)
891                                                                                 {
892                                                                                         if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
893                                                                                         {
894                                                                                                 strlcat(outlist, app,MAXBUF);
895                                                                                         }
896                                                                                         else if (!strchr(outlist,modechar))
897                                                                                         {
898                                                                                                 strlcat(outlist, app,MAXBUF);
899                                                                                         }
900                                                                                 }
901                                                                                 chan->SetCustomMode(modechar,mdir);
902                                                                                 // include parameters in output if mode has them
903                                                                                 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
904                                                                                 {
905                                                                                         chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
906                                                                                         strlcpy(outpars[pc++],parameters[param++],MAXBUF);
907                                                                                 }
908                                                                         }
909                                                                         // break, because only one module can handle the mode.
910                                                                         handled = true;
911                                                                 }
912                                                         }
913                                                 }
914                                         }
915                                         else
916                                         {
917                                                 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
918                                         }
919                                 break;
920                                 
921                         }
922                 }
923         }
924
925         /* this ensures only the *valid* modes are sent out onto the network */
926         while ((outlist[strlen(outlist)-1] == '-') || (outlist[strlen(outlist)-1] == '+'))
927         {
928                 outlist[strlen(outlist)-1] = '\0';
929         }
930         if (strcmp(outlist,""))
931         {
932                 strlcpy(outstr,outlist,MAXBUF);
933                 for (ptr = 0; ptr < pc; ptr++)
934                 {
935                         strlcat(outstr," ",MAXBUF);
936                         strlcat(outstr,outpars[ptr],MAXBUF);
937                 }
938                 if (local)
939                 {
940                         log(DEBUG,"Local mode change");
941                         WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
942                 }
943                 else
944                 {
945                         if (servermode)
946                         {
947                                 if (!silent)
948                                 {
949                                         WriteChannelWithServ(ServerName,chan,user,"MODE %s %s",chan->name,outstr);
950                                         // M token for a usermode must go to all servers
951                                         char buffer[MAXBUF];
952                                         snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
953                                         NetSendToAll(buffer);
954                                 }
955                                         
956                         }
957                         else
958                         {
959                                 if (!silent)
960                                 {
961                                         WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
962                                         // M token for a usermode must go to all servers
963                                         char buffer[MAXBUF];
964                                         snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
965                                         NetSendToAll(buffer);
966                                 }
967                         }
968                 }
969         }
970 }
971
972 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
973
974 bool allowed_umode(char umode, char* sourcemodes,bool adding)
975 {
976         log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
977         // RFC1459 specified modes
978         if ((umode == 'w') || (umode == 's') || (umode == 'i'))
979         {
980                 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
981                 return true;
982         }
983         
984         // user may not +o themselves or others, but an oper may de-oper other opers or themselves
985         if ((strchr(sourcemodes,'o')) && (!adding))
986         {
987                 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
988                 return true;
989         }
990         else if (umode == 'o')
991         {
992                 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
993                 return false;
994         }
995         
996         // process any module-defined modes that need oper
997         if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
998         {
999                 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1000                 return true;
1001         }
1002         else
1003         if (ModeDefined(umode,MT_CLIENT))
1004         {
1005                 // process any module-defined modes that don't need oper
1006                 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1007                 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1008                 {
1009                         // no, this mode needs oper, and this user 'aint got what it takes!
1010                         return false;
1011                 }
1012                 return true;
1013         }
1014
1015         // anything else - return false.
1016         log(DEBUG,"umode %c not known by any ruleset",umode);
1017         return false;
1018 }
1019
1020 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1021 {
1022         userrec* s2;
1023         bool faked = false;
1024         if (!source)
1025         {
1026                 s2 = new userrec;
1027                 strlcpy(s2->nick,ServerName,NICKMAX);
1028                 strcpy(s2->modes,"o");
1029                 s2->fd = -1;
1030                 source = s2;
1031                 faked = true;
1032         }
1033         string_list p;
1034         p.clear();
1035         if (ModeDefined(umode,MT_CLIENT))
1036         {
1037                 for (int i = 0; i <= MODCOUNT; i++)
1038                 {
1039                         if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1040                         {
1041                                 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1042                                 return true;
1043                         }
1044                 }
1045                 log(DEBUG,"No module claims umode %c",umode);
1046                 if (faked)
1047                 {
1048                         delete s2;
1049                         source = NULL;
1050                 }
1051                 return false;
1052         }
1053         else
1054         {
1055                 if (faked)
1056                 {
1057                         delete s2;
1058                         source = NULL;
1059                 }
1060                 return false;
1061         }
1062 }
1063
1064 void handle_mode(char **parameters, int pcnt, userrec *user)
1065 {
1066         chanrec* Ptr;
1067         userrec* dest;
1068         int can_change,i;
1069         int direction = 1;
1070         char outpars[MAXBUF];
1071
1072         dest = Find(parameters[0]);
1073
1074         if (!user)
1075         {
1076                 return;
1077         }
1078
1079         if ((dest) && (pcnt == 1))
1080         {
1081                 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1082                 return;
1083         }
1084
1085         if ((dest) && (pcnt > 1))
1086         {
1087                 char dmodes[MAXBUF];
1088                 strlcpy(dmodes,dest->modes,MAXBUF);
1089                 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1090         
1091                 can_change = 0;
1092                 if (user != dest)
1093                 {
1094                         if (strchr(user->modes,'o'))
1095                         {
1096                                 can_change = 1;
1097                         }
1098                 }
1099                 else
1100                 {
1101                         can_change = 1;
1102                 }
1103                 if (!can_change)
1104                 {
1105                         WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1106                         return;
1107                 }
1108                 
1109                 strcpy(outpars,"+");
1110                 direction = 1;
1111
1112                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1113                         return;
1114
1115                 for (int i = 0; i < strlen(parameters[1]); i++)
1116                 {
1117                         if (parameters[1][i] == '+')
1118                         {
1119                                 if (direction != 1)
1120                                 {
1121                                         if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1122                                         {
1123                                                 outpars[strlen(outpars)-1] = '+';
1124                                         }
1125                                         else
1126                                         {
1127                                                 strcat(outpars,"+");
1128                                         }
1129                                 }
1130                                 direction = 1;
1131                         }
1132                         else
1133                         if (parameters[1][i] == '-')
1134                         {
1135                                 if (direction != 0)
1136                                 {
1137                                         if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1138                                         {
1139                                                 outpars[strlen(outpars)-1] = '-';
1140                                         }
1141                                         else
1142                                         {
1143                                                 strcat(outpars,"-");
1144                                         }
1145                                 }
1146                                 direction = 0;
1147                         }
1148                         else
1149                         {
1150                                 can_change = 0;
1151                                 if (strchr(user->modes,'o'))
1152                                 {
1153                                         can_change = 1;
1154                                 }
1155                                 else
1156                                 {
1157                                         if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1158                                         {
1159                                                 can_change = 1;
1160                                         }
1161                                 }
1162                                 if (can_change)
1163                                 {
1164                                         if (direction == 1)
1165                                         {
1166                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1167                                                 {
1168                                                         char umode = parameters[1][i];
1169                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1170                                                         {
1171                                                                 dmodes[strlen(dmodes)+1]='\0';
1172                                                                 dmodes[strlen(dmodes)] = parameters[1][i];
1173                                                                 outpars[strlen(outpars)+1]='\0';
1174                                                                 outpars[strlen(outpars)] = parameters[1][i];
1175                                                         }
1176                                                 }
1177                                         }
1178                                         else
1179                                         {
1180                                                 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1181                                                 {
1182                                                         char umode = parameters[1][i];
1183                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1184                                                         {
1185                                                                 int q = 0;
1186                                                                 char temp[MAXBUF];      
1187                                                                 char moo[MAXBUF];       
1188
1189                                                                 outpars[strlen(outpars)+1]='\0';
1190                                                                 outpars[strlen(outpars)] = parameters[1][i];
1191                                                         
1192                                                                 strcpy(temp,"");
1193                                                                 for (q = 0; q < strlen(dmodes); q++)
1194                                                                 {
1195                                                                         if (dmodes[q] != parameters[1][i])
1196                                                                         {
1197                                                                                 moo[0] = dmodes[q];
1198                                                                                 moo[1] = '\0';
1199                                                                                 strlcat(temp,moo,MAXBUF);
1200                                                                         }
1201                                                                 }
1202                                                                 strlcpy(dmodes,temp,MAXBUF);
1203                                                         }
1204                                                 }
1205                                         }
1206                                 }
1207                         }
1208                 }
1209                 if (strlen(outpars))
1210                 {
1211                         char b[MAXBUF];
1212                         strlcpy(b,"",MAXBUF);
1213                         int z = 0;
1214                         int i = 0;
1215                         while (i < strlen (outpars))
1216                         {
1217                                 b[z++] = outpars[i++];
1218                                 b[z] = '\0';
1219                                 if (i<strlen(outpars)-1)
1220                                 {
1221                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1222                                         {
1223                                                 // someones playing silly buggers and trying
1224                                                 // to put a +- or -+ into the line...
1225                                                 i++;
1226                                         }
1227                                 }
1228                                 if (i == strlen(outpars)-1)
1229                                 {
1230                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1231                                         {
1232                                                 i++;
1233                                         }
1234                                 }
1235                         }
1236
1237                         z = strlen(b)-1;
1238                         if ((b[z] == '-') || (b[z] == '+'))
1239                                 b[z] == '\0';
1240
1241                         if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1242                                 return;
1243
1244                         WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1245
1246                         // M token for a usermode must go to all servers
1247                         char buffer[MAXBUF];
1248                         snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1249                         NetSendToAll(buffer);
1250
1251                         if (strlen(dmodes)>MAXMODES)
1252                         {
1253                                 dmodes[MAXMODES-1] = '\0';
1254                         }
1255                         log(DEBUG,"Stripped mode line");
1256                         log(DEBUG,"Line dest is now %s",dmodes);
1257                         strlcpy(dest->modes,dmodes,MAXMODES);
1258
1259                 }
1260
1261                 return;
1262         }
1263         
1264         Ptr = FindChan(parameters[0]);
1265         if (Ptr)
1266         {
1267                 if (pcnt == 1)
1268                 {
1269                         /* just /modes #channel */
1270                         WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1271                         WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1272                         return;
1273                 }
1274                 else
1275                 if (pcnt == 2)
1276                 {
1277                         if ((!strcmp(parameters[1],"+b")) || (!strcmp(parameters[1],"b")))
1278                         {
1279
1280                                 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1281                                 {
1282                                         WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1283                                 }
1284                                 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1285                                 return;
1286                         }
1287                 }
1288
1289                 if ((Ptr) && (!has_channel(user,Ptr)))
1290                 {
1291                         WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1292                         return;
1293                 }
1294
1295                 if (Ptr)
1296                 {
1297                         int MOD_RESULT = 0;
1298                         FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1299                         
1300                         if (MOD_RESULT == ACR_DENY)
1301                                 return;
1302                         if (MOD_RESULT == ACR_DEFAULT)
1303                         {
1304                                 if (cstatus(user,Ptr) < STATUS_HOP)
1305                                 {
1306                                         WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1307                                         return;
1308                                 }
1309                         }
1310
1311                         process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1312                 }
1313         }
1314         else
1315         {
1316                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
1317         }
1318 }
1319
1320
1321
1322
1323 void server_mode(char **parameters, int pcnt, userrec *user)
1324 {
1325         chanrec* Ptr;
1326         userrec* dest;
1327         int can_change,i;
1328         int direction = 1;
1329         char outpars[MAXBUF];
1330
1331         dest = Find(parameters[0]);
1332         
1333         // fix: ChroNiCk found this - we cant use this as debug if its null!
1334         if (dest)
1335         {
1336                 log(DEBUG,"server_mode on %s",dest->nick);
1337         }
1338
1339         if ((dest) && (pcnt > 1))
1340         {
1341                 log(DEBUG,"params > 1");
1342
1343                 char dmodes[MAXBUF];
1344                 strlcpy(dmodes,dest->modes,MAXBUF);
1345
1346                 strcpy(outpars,"+");
1347                 direction = 1;
1348
1349                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1350                         return;
1351
1352                 for (int i = 0; i < strlen(parameters[1]); i++)
1353                 {
1354                         if (parameters[1][i] == '+')
1355                         {
1356                                 if (direction != 1)
1357                                 {
1358                                         if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1359                                         {
1360                                                 outpars[strlen(outpars)-1] = '+';
1361                                         }
1362                                         else
1363                                         {
1364                                                 strcat(outpars,"+");
1365                                         }
1366                                 }
1367                                 direction = 1;
1368                         }
1369                         else
1370                         if (parameters[1][i] == '-')
1371                         {
1372                                 if (direction != 0)
1373                                 {
1374                                         if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1375                                         {
1376                                                 outpars[strlen(outpars)-1] = '-';
1377                                         }
1378                                         else
1379                                         {
1380                                                 strcat(outpars,"-");
1381                                         }
1382                                 }
1383                                 direction = 0;
1384                         }
1385                         else
1386                         {
1387                                 log(DEBUG,"begin mode processing entry");
1388                                 can_change = 1;
1389                                 if (can_change)
1390                                 {
1391                                         if (direction == 1)
1392                                         {
1393                                                 log(DEBUG,"umode %c being added",parameters[1][i]);
1394                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1395                                                 {
1396                                                         char umode = parameters[1][i];
1397                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1398                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1399                                                         {
1400                                                                 dmodes[strlen(dmodes)+1]='\0';
1401                                                                 dmodes[strlen(dmodes)] = parameters[1][i];
1402                                                                 outpars[strlen(outpars)+1]='\0';
1403                                                                 outpars[strlen(outpars)] = parameters[1][i];
1404                                                         }
1405                                                 }
1406                                         }
1407                                         else
1408                                         {
1409                                                 // can only remove a mode they already have
1410                                                 log(DEBUG,"umode %c being removed",parameters[1][i]);
1411                                                 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1412                                                 {
1413                                                         char umode = parameters[1][i];
1414                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1415                                                         if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1416                                                         {
1417                                                                 int q = 0;
1418                                                                 char temp[MAXBUF];
1419                                                                 char moo[MAXBUF];       
1420
1421                                                                 outpars[strlen(outpars)+1]='\0';
1422                                                                 outpars[strlen(outpars)] = parameters[1][i];
1423                                                         
1424                                                                 strcpy(temp,"");
1425                                                                 for (q = 0; q < strlen(dmodes); q++)
1426                                                                 {
1427                                                                         if (dmodes[q] != parameters[1][i])
1428                                                                         {
1429                                                                                 moo[0] = dmodes[q];
1430                                                                                 moo[1] = '\0';
1431                                                                                 strlcat(temp,moo,MAXBUF);
1432                                                                         }
1433                                                                 }
1434                                                                 strlcpy(dmodes,temp,MAXBUF);
1435                                                         }
1436                                                 }
1437                                         }
1438                                 }
1439                         }
1440                 }
1441                 if (strlen(outpars))
1442                 {
1443                         char b[MAXBUF];
1444                         strlcpy(b,"",MAXBUF);
1445                         int z = 0;
1446                         int i = 0;
1447                         while (i < strlen (outpars))
1448                         {
1449                                 b[z++] = outpars[i++];
1450                                 b[z] = '\0';
1451                                 if (i<strlen(outpars)-1)
1452                                 {
1453                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1454                                         {
1455                                                 // someones playing silly buggers and trying
1456                                                 // to put a +- or -+ into the line...
1457                                                 i++;
1458                                         }
1459                                 }
1460                                 if (i == strlen(outpars)-1)
1461                                 {
1462                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1463                                         {
1464                                                 i++;
1465                                         }
1466                                 }
1467                         }
1468
1469                         z = strlen(b)-1;
1470                         if ((b[z] == '-') || (b[z] == '+'))
1471                                 b[z] == '\0';
1472
1473                         if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1474                                 return;
1475
1476                         WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1477
1478                         // M token for a usermode must go to all servers
1479                         char buffer[MAXBUF];
1480                         snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1481                         NetSendToAll(buffer);
1482                         
1483                         if (strlen(dmodes)>MAXMODES)
1484                         {
1485                                 dmodes[MAXMODES-1] = '\0';
1486                         }
1487                         log(DEBUG,"Stripped mode line");
1488                         log(DEBUG,"Line dest is now %s",dmodes);
1489                         strlcpy(dest->modes,dmodes,MAXMODES);
1490
1491                 }
1492
1493                 return;
1494         }
1495         
1496         Ptr = FindChan(parameters[0]);
1497         if (Ptr)
1498         {
1499                 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1500         }
1501         else
1502         {
1503                 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
1504         }
1505 }
1506
1507
1508
1509 void merge_mode(char **parameters, int pcnt)
1510 {
1511         chanrec* Ptr;
1512         userrec* dest;
1513         int can_change,i;
1514         int direction = 1;
1515         char outpars[MAXBUF];
1516
1517         dest = Find(parameters[0]);
1518         
1519         // fix: ChroNiCk found this - we cant use this as debug if its null!
1520         if (dest)
1521         {
1522                 log(DEBUG,"merge_mode on %s",dest->nick);
1523         }
1524
1525         if ((dest) && (pcnt > 1))
1526         {
1527                 log(DEBUG,"params > 1");
1528
1529                 char dmodes[MAXBUF];
1530                 strlcpy(dmodes,dest->modes,MAXBUF);
1531
1532                 strcpy(outpars,"+");
1533                 direction = 1;
1534
1535                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1536                         return;
1537
1538                 for (int i = 0; i < strlen(parameters[1]); i++)
1539                 {
1540                         if (parameters[1][i] == '+')
1541                         {
1542                                 if (direction != 1)
1543                                 {
1544                                         if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1545                                         {
1546                                                 outpars[strlen(outpars)-1] = '+';
1547                                         }
1548                                         else
1549                                         {
1550                                                 strcat(outpars,"+");
1551                                         }
1552                                 }
1553                                 direction = 1;
1554                         }
1555                         else
1556                         if (parameters[1][i] == '-')
1557                         {
1558                                 if (direction != 0)
1559                                 {
1560                                         if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1561                                         {
1562                                                 outpars[strlen(outpars)-1] = '-';
1563                                         }
1564                                         else
1565                                         {
1566                                                 strcat(outpars,"-");
1567                                         }
1568                                 }
1569                                 direction = 0;
1570                         }
1571                         else
1572                         {
1573                                 log(DEBUG,"begin mode processing entry");
1574                                 can_change = 1;
1575                                 if (can_change)
1576                                 {
1577                                         if (direction == 1)
1578                                         {
1579                                                 log(DEBUG,"umode %c being added",parameters[1][i]);
1580                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1581                                                 {
1582                                                         char umode = parameters[1][i];
1583                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1584                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1585                                                         {
1586                                                                 dmodes[strlen(dmodes)+1]='\0';
1587                                                                 dmodes[strlen(dmodes)] = parameters[1][i];
1588                                                                 outpars[strlen(outpars)+1]='\0';
1589                                                                 outpars[strlen(outpars)] = parameters[1][i];
1590                                                         }
1591                                                 }
1592                                         }
1593                                         else
1594                                         {
1595                                                 // can only remove a mode they already have
1596                                                 log(DEBUG,"umode %c being removed",parameters[1][i]);
1597                                                 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1598                                                 {
1599                                                         char umode = parameters[1][i];
1600                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1601                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1602                                                         {
1603                                                                 int q = 0;
1604                                                                 char temp[MAXBUF];
1605                                                                 char moo[MAXBUF];       
1606
1607                                                                 outpars[strlen(outpars)+1]='\0';
1608                                                                 outpars[strlen(outpars)] = parameters[1][i];
1609                                                         
1610                                                                 strcpy(temp,"");
1611                                                                 for (q = 0; q < strlen(dmodes); q++)
1612                                                                 {
1613                                                                         if (dmodes[q] != parameters[1][i])
1614                                                                         {
1615                                                                                 moo[0] = dmodes[q];
1616                                                                                 moo[1] = '\0';
1617                                                                                 strlcat(temp,moo,MAXBUF);
1618                                                                         }
1619                                                                 }
1620                                                                 strlcpy(dmodes,temp,MAXBUF);
1621                                                         }
1622                                                 }
1623                                         }
1624                                 }
1625                         }
1626                 }
1627                 if (strlen(outpars))
1628                 {
1629                         char b[MAXBUF];
1630                         strcpy(b,"");
1631                         int z = 0;
1632                         int i = 0;
1633                         while (i < strlen (outpars))
1634                         {
1635                                 b[z++] = outpars[i++];
1636                                 b[z] = '\0';
1637                                 if (i<strlen(outpars)-1)
1638                                 {
1639                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1640                                         {
1641                                                 // someones playing silly buggers and trying
1642                                                 // to put a +- or -+ into the line...
1643                                                 i++;
1644                                         }
1645                                 }
1646                                 if (i == strlen(outpars)-1)
1647                                 {
1648                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1649                                         {
1650                                                 i++;
1651                                         }
1652                                 }
1653                         }
1654
1655                         z = strlen(b)-1;
1656                         if ((b[z] == '-') || (b[z] == '+'))
1657                                 b[z] == '\0';
1658
1659                         if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1660                                 return;
1661
1662                         if (strlen(dmodes)>MAXMODES)
1663                         {
1664                                 dmodes[MAXMODES-1] = '\0';
1665                         }
1666                         log(DEBUG,"Stripped mode line");
1667                         log(DEBUG,"Line dest is now %s",dmodes);
1668                         strlcpy(dest->modes,dmodes,MAXMODES);
1669
1670                 }
1671
1672                 return;
1673         }
1674         
1675         Ptr = FindChan(parameters[0]);
1676         if (Ptr)
1677         {
1678                 userrec s2;
1679                 strlcpy(s2.nick,ServerName,NICKMAX);
1680                 strcpy(s2.modes,"o");
1681                 s2.fd = -1;
1682                 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1683         }
1684 }
1685
1686
1687 void merge_mode2(char **parameters, int pcnt, userrec* user)
1688 {
1689         chanrec* Ptr;
1690         userrec* dest;
1691         int can_change,i;
1692         int direction = 1;
1693         char outpars[MAXBUF];
1694
1695         dest = Find(parameters[0]);
1696         
1697         // fix: ChroNiCk found this - we cant use this as debug if its null!
1698         if (dest)
1699         {
1700                 log(DEBUG,"merge_mode on %s",dest->nick);
1701         }
1702
1703         if ((dest) && (pcnt > 1))
1704         {
1705                 log(DEBUG,"params > 1");
1706
1707                 char dmodes[MAXBUF];
1708                 strlcpy(dmodes,dest->modes,MAXBUF);
1709
1710                 strcpy(outpars,"+");
1711                 direction = 1;
1712
1713                 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1714                 {
1715                         // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1716                         log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1717                 }
1718                 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1719                 return;
1720
1721                 for (int i = 0; i < strlen(parameters[1]); i++)
1722                 {
1723                         if (parameters[1][i] == '+')
1724                         {
1725                                 if (direction != 1)
1726                                 {
1727                                         if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1728                                         {
1729                                                 outpars[strlen(outpars)-1] = '+';
1730                                         }
1731                                         else
1732                                         {
1733                                                 strcat(outpars,"+");
1734                                         }
1735                                 }
1736                                 direction = 1;
1737                         }
1738                         else
1739                         if (parameters[1][i] == '-')
1740                         {
1741                                 if (direction != 0)
1742                                 {
1743                                         if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1744                                         {
1745                                                 outpars[strlen(outpars)-1] = '-';
1746                                         }
1747                                         else
1748                                         {
1749                                                 strcat(outpars,"-");
1750                                         }
1751                                 }
1752                                 direction = 0;
1753                         }
1754                         else
1755                         {
1756                                 log(DEBUG,"begin mode processing entry");
1757                                 can_change = 1;
1758                                 if (can_change)
1759                                 {
1760                                         if (direction == 1)
1761                                         {
1762                                                 log(DEBUG,"umode %c being added",parameters[1][i]);
1763                                                 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1764                                                 {
1765                                                         char umode = parameters[1][i];
1766                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1767                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1768                                                         {
1769                                                                 dmodes[strlen(dmodes)+1]='\0';
1770                                                                 dmodes[strlen(dmodes)] = parameters[1][i];
1771                                                                 outpars[strlen(outpars)+1]='\0';
1772                                                                 outpars[strlen(outpars)] = parameters[1][i];
1773                                                                 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1774                                                         }
1775                                                 }
1776                                         }
1777                                         else
1778                                         {
1779                                                 // can only remove a mode they already have
1780                                                 log(DEBUG,"umode %c being removed",parameters[1][i]);
1781                                                 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1782                                                 {
1783                                                         char umode = parameters[1][i];
1784                                                         log(DEBUG,"umode %c is an allowed umode",umode);
1785                                                         if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1786                                                         {
1787                                                                 int q = 0;
1788                                                                 char temp[MAXBUF];
1789                                                                 char moo[MAXBUF];       
1790
1791                                                                 outpars[strlen(outpars)+1]='\0';
1792                                                                 outpars[strlen(outpars)] = parameters[1][i];
1793                                                         
1794                                                                 strcpy(temp,"");
1795                                                                 for (q = 0; q < strlen(dmodes); q++)
1796                                                                 {
1797                                                                         if (dmodes[q] != parameters[1][i])
1798                                                                         {
1799                                                                                 moo[0] = dmodes[q];
1800                                                                                 moo[1] = '\0';
1801                                                                                 strlcat(temp,moo,MAXBUF);
1802                                                                         }
1803                                                                 }
1804                                                                 strlcpy(dmodes,temp,MAXBUF);
1805                                                         }
1806                                                 }
1807                                         }
1808                                 }
1809                         }
1810                 }
1811                 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1812                 if (strlen(outpars))
1813                 {
1814                         char b[MAXBUF];
1815                         strcpy(b,"");
1816                         int z = 0;
1817                         int i = 0;
1818                         while (i < strlen (outpars))
1819                         {
1820                                 b[z++] = outpars[i++];
1821                                 b[z] = '\0';
1822                                 if (i<strlen(outpars)-1)
1823                                 {
1824                                         if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1825                                         {
1826                                                 // someones playing silly buggers and trying
1827                                                 // to put a +- or -+ into the line...
1828                                                 i++;
1829                                         }
1830                                 }
1831                                 if (i == strlen(outpars)-1)
1832                                 {
1833                                         if ((outpars[i] == '-') || (outpars[i] == '+'))
1834                                         {
1835                                                 i++;
1836                                         }
1837                                 }
1838                         }
1839
1840                         z = strlen(b)-1;
1841                         if ((b[z] == '-') || (b[z] == '+'))
1842                                 b[z] == '\0';
1843
1844                         if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1845                                 return;
1846
1847                         // was sending 'b'
1848                         WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
1849                         log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
1850
1851                         if (strlen(dmodes)>MAXMODES)
1852                         {
1853                                 dmodes[MAXMODES-1] = '\0';
1854                         }
1855                         log(DEBUG,"Stripped mode line");
1856                         log(DEBUG,"Line dest is now %s",dmodes);
1857                         strlcpy(dest->modes,dmodes,MAXMODES);
1858
1859                 }
1860
1861                 return;
1862         }
1863         
1864         Ptr = FindChan(parameters[0]);
1865         if (Ptr)
1866         {
1867                 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
1868                 if (Ptr)
1869                 {
1870                         if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
1871                         {
1872                                 return;
1873                         }
1874                         process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);
1875                 }
1876         }
1877 }
1878
1879