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