1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
18 #include "inspircd_io.h"
19 #include "inspircd_util.h"
20 #include "inspircd_config.h"
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
30 #include <ext/hash_map>
42 #include "connection.h"
53 #include "inspstring.h"
58 extern std::vector<Module*> modules;
59 extern std::vector<ircd_module*> factory;
60 extern std::vector<std::string> module_names;
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];
78 extern bool AllowHalfop;
79 extern bool AllowProtect;
80 extern bool AllowFounder;
84 char* give_ops(userrec *user,char *dest,chanrec *chan,int status)
89 if ((!user) || (!dest) || (!chan))
91 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
97 log(DEFAULT,"the target nickname given to give_ops was invalid");
98 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
104 log(DEFAULT,"the target nickname given to give_ops couldnt be found");
105 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
112 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
114 if (MOD_RESULT == ACR_DENY)
116 if (MOD_RESULT == ACR_DEFAULT)
118 if ((status < STATUS_OP) && (!is_uline(user->server)))
120 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
121 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
127 for (int i = 0; i != MAXCHANS; i++)
129 if ((d->chans[i].channel != NULL) && (chan != NULL))
130 if (!strcasecmp(d->chans[i].channel->name,chan->name))
132 if (d->chans[i].uc_modes & UCMODE_OP)
134 /* mode already set on user, dont allow multiple */
135 log(DEFAULT,"The target user given to give_ops was already opped on the channel");
138 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
139 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
143 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
148 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
153 if ((!user) || (!dest) || (!chan))
155 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
162 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
167 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
173 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
175 if (MOD_RESULT == ACR_DENY)
177 if (MOD_RESULT == ACR_DEFAULT)
179 if ((status < STATUS_OP) && (!is_uline(user->server)))
181 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
186 for (int i = 0; i != MAXCHANS; i++)
188 if ((d->chans[i].channel != NULL) && (chan != NULL))
189 if (!strcasecmp(d->chans[i].channel->name,chan->name))
191 if (d->chans[i].uc_modes & UCMODE_HOP)
193 /* mode already set on user, dont allow multiple */
196 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
197 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
205 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
210 if ((!user) || (!dest) || (!chan))
212 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
219 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
224 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
230 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
232 if (MOD_RESULT == ACR_DENY)
234 if (MOD_RESULT == ACR_DEFAULT)
236 if ((status < STATUS_HOP) && (!is_uline(user->server)))
238 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
243 for (int i = 0; i != MAXCHANS; i++)
245 if ((d->chans[i].channel != NULL) && (chan != NULL))
246 if (!strcasecmp(d->chans[i].channel->name,chan->name))
248 if (d->chans[i].uc_modes & UCMODE_VOICE)
250 /* mode already set on user, dont allow multiple */
253 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
254 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
262 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
267 if ((!user) || (!dest) || (!chan))
269 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
276 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
277 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
282 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
283 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
289 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
291 if (MOD_RESULT == ACR_DENY)
293 if (MOD_RESULT == ACR_DEFAULT)
295 if ((status < STATUS_OP) && (!is_uline(user->server)))
297 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
302 for (int i = 0; i != MAXCHANS; i++)
304 if ((d->chans[i].channel != NULL) && (chan != NULL))
305 if (!strcasecmp(d->chans[i].channel->name,chan->name))
307 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
309 /* mode already set on user, dont allow multiple */
312 d->chans[i].uc_modes ^= UCMODE_OP;
313 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
317 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
322 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
327 if ((!user) || (!dest) || (!chan))
329 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
336 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
341 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
347 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
349 if (MOD_RESULT == ACR_DENY)
351 if (MOD_RESULT == ACR_DEFAULT)
353 if ((status < STATUS_OP) && (!is_uline(user->server)))
355 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
360 for (int i = 0; i != MAXCHANS; i++)
362 if ((d->chans[i].channel != NULL) && (chan != NULL))
363 if (!strcasecmp(d->chans[i].channel->name,chan->name))
365 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
367 /* mode already set on user, dont allow multiple */
370 d->chans[i].uc_modes ^= UCMODE_HOP;
371 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
379 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
384 if ((!user) || (!dest) || (!chan))
386 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
393 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
398 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
404 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
406 if (MOD_RESULT == ACR_DENY)
408 if (MOD_RESULT == ACR_DEFAULT)
410 if ((status < STATUS_HOP) && (!is_uline(user->server)))
412 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
417 for (int i = 0; i != MAXCHANS; i++)
419 if ((d->chans[i].channel != NULL) && (chan != NULL))
420 if (!strcasecmp(d->chans[i].channel->name,chan->name))
422 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
424 /* mode already set on user, dont allow multiple */
427 d->chans[i].uc_modes ^= UCMODE_VOICE;
428 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
436 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
438 if ((!user) || (!dest) || (!chan)) {
439 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
444 if ((!user) || (!dest) || (!chan))
446 if (strchr(dest,'!')==0)
448 if (strchr(dest,'@')==0)
450 for (int i = 0; i < strlen(dest); i++)
453 for (int i = 0; i < strlen(dest); i++)
457 for (int i = 0; i < strlen(dest); i++)
463 for (int i = 0; i < strlen(dest); i++)
469 long maxbans = GetMaxBans(chan->name);
470 if (chan->bans.size() > maxbans)
472 WriteServ(user->fd,"478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)",user->nick, chan->name,chan->name,maxbans);
476 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
479 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
481 if (!strcasecmp(i->data,dest))
483 // dont allow a user to set the same ban twice
489 strncpy(b.data,dest,MAXBUF);
490 strncpy(b.set_by,user->nick,NICKMAX);
491 chan->bans.push_back(b);
495 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
497 if ((!user) || (!dest) || (!chan)) {
498 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
502 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
503 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
505 if (!strcasecmp(i->data,dest))
514 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
517 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
521 char modelist[MAXBUF];
522 char outlist[MAXBUF];
524 char outpars[32][MAXBUF];
530 bool k_set = false, l_set = false;
538 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
540 if (MOD_RESULT == ACR_DENY)
543 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
545 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
546 * parameters[2] onwards are parameters for
547 * modes that require them :) */
548 strlcpy(outlist,"+",MAXBUF);
551 log(DEBUG,"process_modes: modelist: %s",modelist);
553 for (ptr = 0; ptr < strlen(modelist); ptr++)
558 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
560 char modechar = modelist[ptr];
561 switch (modelist[ptr])
566 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
568 outlist[strlen(outlist)-1] = '-';
582 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
584 outlist[strlen(outlist)-1] = '+';
596 if ((param >= pcnt)) break;
597 log(DEBUG,"Enough parameters left");
601 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
604 log(DEBUG,"calling give_ops");
605 r = give_ops(user,parameters[param++],chan,status);
612 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
615 log(DEBUG,"calling take_ops");
616 r = take_ops(user,parameters[param++],chan,status);
622 strlcat(outlist,"o",MAXBUF);
623 strlcpy(outpars[pc++],r,MAXBUF);
628 if (((param >= pcnt)) || (!AllowHalfop)) break;
632 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
635 r = give_hops(user,parameters[param++],chan,status);
642 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
645 r = take_hops(user,parameters[param++],chan,status);
651 strlcat(outlist,"h",MAXBUF);
652 strlcpy(outpars[pc++],r,MAXBUF);
658 if ((param >= pcnt)) break;
662 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
665 r = give_voice(user,parameters[param++],chan,status);
672 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
675 r = take_voice(user,parameters[param++],chan,status);
681 strlcat(outlist,"v",MAXBUF);
682 strlcpy(outpars[pc++],r,MAXBUF);
687 if ((param >= pcnt)) break;
691 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
694 r = add_ban(user,parameters[param++],chan,status);
701 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
704 r = take_ban(user,parameters[param++],chan,status);
710 strlcat(outlist,"b",MAXBUF);
711 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
725 if (!strcmp(chan->key,""))
728 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
733 strlcpy(key,parameters[param++],MAXBUF);
734 if (strlen(key)>32) {
737 strlcpy(outpars[pc++],key,MAXBUF);
738 strlcpy(chan->key,key,MAXBUF);
746 /* checks on -k are case sensitive and only accurate to the
747 first 32 characters */
750 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
753 strlcpy(key,parameters[param++],MAXBUF);
758 /* only allow -k if correct key given */
759 if (!strcmp(chan->key,key))
761 strlcat(outlist,"k",MAXBUF);
762 strlcpy(chan->key,"",MAXBUF);
763 strlcpy(outpars[pc++],key,MAXBUF);
774 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
785 if ((param >= pcnt)) break;
791 bool invalid = false;
792 for (int i = 0; i < strlen(parameters[param]); i++)
794 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
799 if (atoi(parameters[param]) < 1)
808 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
812 chan->limit = atoi(parameters[param]);
814 // reported by mech: large values cause underflow
816 chan->limit = 0x7FFFFF;
821 strlcat(outlist,"l",MAXBUF);
822 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
830 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
833 if (chan->inviteonly != mdir)
835 strlcat(outlist,"i",MAXBUF);
837 chan->inviteonly = mdir;
843 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
846 if (chan->topiclock != mdir)
848 strlcat(outlist,"t",MAXBUF);
850 chan->topiclock = mdir;
856 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
859 if (chan->noexternal != mdir)
861 strlcat(outlist,"n",MAXBUF);
863 chan->noexternal = mdir;
869 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
872 if (chan->moderated != mdir)
874 strlcat(outlist,"m",MAXBUF);
876 chan->moderated = mdir;
882 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
885 if (chan->secret != mdir)
893 strlcat(outlist,"-p+",MAXBUF);
897 strlcat(outlist,"+p-",MAXBUF);
907 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
910 if (chan->c_private != mdir)
912 strlcat(outlist,"p",MAXBUF);
918 strlcat(outlist,"-s+",MAXBUF);
922 strlcat(outlist,"+s-",MAXBUF);
926 chan->c_private = mdir;
931 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
934 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
936 if (!ModeIsListMode(modechar,MT_CHANNEL))
938 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
942 if (ModeDefined(modechar,MT_CHANNEL))
944 log(DEBUG,"A module has claimed this mode");
947 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
949 p.push_back(parameters[param]);
951 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
953 p.push_back(parameters[param]);
956 bool handled = false;
959 // we're supposed to have a parameter, but none was given... so dont handle the mode.
960 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
962 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
968 // BIG ASS IDIOTIC CODER WARNING!
969 // Using OnRawMode on another modules mode's behavour
970 // will confuse the crap out of admins! just because you CAN
971 // do it, doesnt mean you SHOULD!
973 std::string para = "";
976 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
979 for (int i = 0; i <= MODCOUNT; i++)
983 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
986 log(DEBUG,"OnExtendedMode returned nonzero for a module");
987 char app[] = {modechar, 0};
988 if (ModeIsListMode(modechar,MT_CHANNEL))
998 strlcat(outlist, app,MAXBUF);
1000 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1007 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1009 strlcat(outlist, app,MAXBUF);
1011 else if (!strchr(outlist,modechar))
1013 strlcat(outlist, app,MAXBUF);
1016 chan->SetCustomMode(modechar,mdir);
1017 // include parameters in output if mode has them
1018 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1020 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1021 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1024 // break, because only one module can handle the mode.
1033 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1041 /* this ensures only the *valid* modes are sent out onto the network */
1042 while ((outlist[strlen(outlist)-1] == '-') || (outlist[strlen(outlist)-1] == '+'))
1044 outlist[strlen(outlist)-1] = '\0';
1046 if (strcmp(outlist,""))
1048 strlcpy(outstr,outlist,MAXBUF);
1049 for (ptr = 0; ptr < pc; ptr++)
1051 strlcat(outstr," ",MAXBUF);
1052 strlcat(outstr,outpars[ptr],MAXBUF);
1056 log(DEBUG,"Local mode change");
1057 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1065 WriteChannelWithServ(ServerName,chan,user,"MODE %s %s",chan->name,outstr);
1066 // M token for a usermode must go to all servers
1067 char buffer[MAXBUF];
1068 snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
1069 NetSendToAll(buffer);
1077 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1078 // M token for a usermode must go to all servers
1079 char buffer[MAXBUF];
1080 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
1081 NetSendToAll(buffer);
1088 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1090 bool allowed_umode(char umode, char* sourcemodes,bool adding)
1092 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1093 // RFC1459 specified modes
1094 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1096 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1100 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1101 if ((strchr(sourcemodes,'o')) && (!adding))
1103 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1106 else if (umode == 'o')
1108 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1112 // process any module-defined modes that need oper
1113 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1115 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1119 if (ModeDefined(umode,MT_CLIENT))
1121 // process any module-defined modes that don't need oper
1122 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1123 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1125 // no, this mode needs oper, and this user 'aint got what it takes!
1131 // anything else - return false.
1132 log(DEBUG,"umode %c not known by any ruleset",umode);
1136 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1143 strlcpy(s2->nick,ServerName,NICKMAX);
1144 strcpy(s2->modes,"o");
1151 if (ModeDefined(umode,MT_CLIENT))
1153 for (int i = 0; i <= MODCOUNT; i++)
1155 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1157 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1161 log(DEBUG,"No module claims umode %c",umode);
1180 void handle_mode(char **parameters, int pcnt, userrec *user)
1186 char outpars[MAXBUF];
1188 dest = Find(parameters[0]);
1195 if ((dest) && (pcnt == 1))
1197 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1201 if ((dest) && (pcnt > 1))
1203 char dmodes[MAXBUF];
1204 strlcpy(dmodes,dest->modes,MAXBUF);
1205 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1210 if (strchr(user->modes,'o'))
1221 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1225 strcpy(outpars,"+");
1228 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1231 for (int i = 0; i < strlen(parameters[1]); i++)
1233 if (parameters[1][i] == '+')
1237 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1239 outpars[strlen(outpars)-1] = '+';
1243 strcat(outpars,"+");
1249 if (parameters[1][i] == '-')
1253 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1255 outpars[strlen(outpars)-1] = '-';
1259 strcat(outpars,"-");
1267 if (strchr(user->modes,'o'))
1273 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1282 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1284 char umode = parameters[1][i];
1285 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1287 dmodes[strlen(dmodes)+1]='\0';
1288 dmodes[strlen(dmodes)] = parameters[1][i];
1289 outpars[strlen(outpars)+1]='\0';
1290 outpars[strlen(outpars)] = parameters[1][i];
1291 if (parameters[1][i] == 'o')
1293 FOREACH_MOD OnGlobalOper(dest);
1300 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1302 char umode = parameters[1][i];
1303 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1309 outpars[strlen(outpars)+1]='\0';
1310 outpars[strlen(outpars)] = parameters[1][i];
1313 for (q = 0; q < strlen(dmodes); q++)
1315 if (dmodes[q] != parameters[1][i])
1319 strlcat(temp,moo,MAXBUF);
1322 strlcpy(dmodes,temp,MAXBUF);
1329 if (strlen(outpars))
1332 strlcpy(b,"",MAXBUF);
1335 while (i < strlen (outpars))
1337 b[z++] = outpars[i++];
1339 if (i<strlen(outpars)-1)
1341 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1343 // someones playing silly buggers and trying
1344 // to put a +- or -+ into the line...
1348 if (i == strlen(outpars)-1)
1350 if ((outpars[i] == '-') || (outpars[i] == '+'))
1358 if ((b[z] == '-') || (b[z] == '+'))
1361 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1364 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1366 // M token for a usermode must go to all servers
1367 char buffer[MAXBUF];
1368 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1369 NetSendToAll(buffer);
1371 if (strlen(dmodes)>MAXMODES)
1373 dmodes[MAXMODES-1] = '\0';
1375 log(DEBUG,"Stripped mode line");
1376 log(DEBUG,"Line dest is now %s",dmodes);
1377 strlcpy(dest->modes,dmodes,MAXMODES);
1384 Ptr = FindChan(parameters[0]);
1389 /* just /modes #channel */
1390 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1391 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1397 char* mode = parameters[1];
1401 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1407 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1409 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1411 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1414 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1416 // list of items for an extmode
1417 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1418 FOREACH_MOD OnSendList(user,Ptr,*mode);
1424 if ((Ptr) && (!has_channel(user,Ptr)))
1426 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1433 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1435 if (MOD_RESULT == ACR_DENY)
1437 if (MOD_RESULT == ACR_DEFAULT)
1439 if (cstatus(user,Ptr) < STATUS_HOP)
1441 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1446 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1451 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
1458 void server_mode(char **parameters, int pcnt, userrec *user)
1464 char outpars[MAXBUF];
1466 dest = Find(parameters[0]);
1468 // fix: ChroNiCk found this - we cant use this as debug if its null!
1471 log(DEBUG,"server_mode on %s",dest->nick);
1474 if ((dest) && (pcnt > 1))
1476 log(DEBUG,"params > 1");
1478 char dmodes[MAXBUF];
1479 strlcpy(dmodes,dest->modes,MAXBUF);
1481 strcpy(outpars,"+");
1484 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1487 for (int i = 0; i < strlen(parameters[1]); i++)
1489 if (parameters[1][i] == '+')
1493 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1495 outpars[strlen(outpars)-1] = '+';
1499 strcat(outpars,"+");
1505 if (parameters[1][i] == '-')
1509 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1511 outpars[strlen(outpars)-1] = '-';
1515 strcat(outpars,"-");
1522 log(DEBUG,"begin mode processing entry");
1528 log(DEBUG,"umode %c being added",parameters[1][i]);
1529 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1531 char umode = parameters[1][i];
1532 log(DEBUG,"umode %c is an allowed umode",umode);
1533 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1535 dmodes[strlen(dmodes)+1]='\0';
1536 dmodes[strlen(dmodes)] = parameters[1][i];
1537 outpars[strlen(outpars)+1]='\0';
1538 outpars[strlen(outpars)] = parameters[1][i];
1544 // can only remove a mode they already have
1545 log(DEBUG,"umode %c being removed",parameters[1][i]);
1546 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1548 char umode = parameters[1][i];
1549 log(DEBUG,"umode %c is an allowed umode",umode);
1550 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1556 outpars[strlen(outpars)+1]='\0';
1557 outpars[strlen(outpars)] = parameters[1][i];
1560 for (q = 0; q < strlen(dmodes); q++)
1562 if (dmodes[q] != parameters[1][i])
1566 strlcat(temp,moo,MAXBUF);
1569 strlcpy(dmodes,temp,MAXBUF);
1576 if (strlen(outpars))
1579 strlcpy(b,"",MAXBUF);
1582 while (i < strlen (outpars))
1584 b[z++] = outpars[i++];
1586 if (i<strlen(outpars)-1)
1588 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1590 // someones playing silly buggers and trying
1591 // to put a +- or -+ into the line...
1595 if (i == strlen(outpars)-1)
1597 if ((outpars[i] == '-') || (outpars[i] == '+'))
1605 if ((b[z] == '-') || (b[z] == '+'))
1608 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1611 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1613 // M token for a usermode must go to all servers
1614 char buffer[MAXBUF];
1615 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1616 NetSendToAll(buffer);
1618 if (strlen(dmodes)>MAXMODES)
1620 dmodes[MAXMODES-1] = '\0';
1622 log(DEBUG,"Stripped mode line");
1623 log(DEBUG,"Line dest is now %s",dmodes);
1624 strlcpy(dest->modes,dmodes,MAXMODES);
1631 Ptr = FindChan(parameters[0]);
1634 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1638 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
1644 void merge_mode(char **parameters, int pcnt)
1650 char outpars[MAXBUF];
1652 dest = Find(parameters[0]);
1654 // fix: ChroNiCk found this - we cant use this as debug if its null!
1657 log(DEBUG,"merge_mode on %s",dest->nick);
1660 if ((dest) && (pcnt > 1))
1662 log(DEBUG,"params > 1");
1664 char dmodes[MAXBUF];
1665 strlcpy(dmodes,dest->modes,MAXBUF);
1667 strcpy(outpars,"+");
1670 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1673 for (int i = 0; i < strlen(parameters[1]); i++)
1675 if (parameters[1][i] == '+')
1679 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1681 outpars[strlen(outpars)-1] = '+';
1685 strcat(outpars,"+");
1691 if (parameters[1][i] == '-')
1695 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1697 outpars[strlen(outpars)-1] = '-';
1701 strcat(outpars,"-");
1708 log(DEBUG,"begin mode processing entry");
1714 log(DEBUG,"umode %c being added",parameters[1][i]);
1715 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1717 char umode = parameters[1][i];
1718 log(DEBUG,"umode %c is an allowed umode",umode);
1719 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1721 dmodes[strlen(dmodes)+1]='\0';
1722 dmodes[strlen(dmodes)] = parameters[1][i];
1723 outpars[strlen(outpars)+1]='\0';
1724 outpars[strlen(outpars)] = parameters[1][i];
1730 // can only remove a mode they already have
1731 log(DEBUG,"umode %c being removed",parameters[1][i]);
1732 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1734 char umode = parameters[1][i];
1735 log(DEBUG,"umode %c is an allowed umode",umode);
1736 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1742 outpars[strlen(outpars)+1]='\0';
1743 outpars[strlen(outpars)] = parameters[1][i];
1746 for (q = 0; q < strlen(dmodes); q++)
1748 if (dmodes[q] != parameters[1][i])
1752 strlcat(temp,moo,MAXBUF);
1755 strlcpy(dmodes,temp,MAXBUF);
1762 if (strlen(outpars))
1768 while (i < strlen (outpars))
1770 b[z++] = outpars[i++];
1772 if (i<strlen(outpars)-1)
1774 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1776 // someones playing silly buggers and trying
1777 // to put a +- or -+ into the line...
1781 if (i == strlen(outpars)-1)
1783 if ((outpars[i] == '-') || (outpars[i] == '+'))
1791 if ((b[z] == '-') || (b[z] == '+'))
1794 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1797 if (strlen(dmodes)>MAXMODES)
1799 dmodes[MAXMODES-1] = '\0';
1801 log(DEBUG,"Stripped mode line");
1802 log(DEBUG,"Line dest is now %s",dmodes);
1803 strlcpy(dest->modes,dmodes,MAXMODES);
1810 Ptr = FindChan(parameters[0]);
1814 strlcpy(s2.nick,ServerName,NICKMAX);
1815 strcpy(s2.modes,"o");
1817 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1822 void merge_mode2(char **parameters, int pcnt, userrec* user)
1828 char outpars[MAXBUF];
1830 dest = Find(parameters[0]);
1832 // fix: ChroNiCk found this - we cant use this as debug if its null!
1835 log(DEBUG,"merge_mode on %s",dest->nick);
1838 if ((dest) && (pcnt > 1))
1840 log(DEBUG,"params > 1");
1842 char dmodes[MAXBUF];
1843 strlcpy(dmodes,dest->modes,MAXBUF);
1845 strcpy(outpars,"+");
1848 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1850 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1851 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1853 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1856 for (int i = 0; i < strlen(parameters[1]); i++)
1858 if (parameters[1][i] == '+')
1862 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1864 outpars[strlen(outpars)-1] = '+';
1868 strcat(outpars,"+");
1874 if (parameters[1][i] == '-')
1878 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1880 outpars[strlen(outpars)-1] = '-';
1884 strcat(outpars,"-");
1891 log(DEBUG,"begin mode processing entry");
1897 log(DEBUG,"umode %c being added",parameters[1][i]);
1898 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1900 char umode = parameters[1][i];
1901 log(DEBUG,"umode %c is an allowed umode",umode);
1902 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1904 dmodes[strlen(dmodes)+1]='\0';
1905 dmodes[strlen(dmodes)] = parameters[1][i];
1906 outpars[strlen(outpars)+1]='\0';
1907 outpars[strlen(outpars)] = parameters[1][i];
1908 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1914 // can only remove a mode they already have
1915 log(DEBUG,"umode %c being removed",parameters[1][i]);
1916 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1918 char umode = parameters[1][i];
1919 log(DEBUG,"umode %c is an allowed umode",umode);
1920 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1926 outpars[strlen(outpars)+1]='\0';
1927 outpars[strlen(outpars)] = parameters[1][i];
1930 for (q = 0; q < strlen(dmodes); q++)
1932 if (dmodes[q] != parameters[1][i])
1936 strlcat(temp,moo,MAXBUF);
1939 strlcpy(dmodes,temp,MAXBUF);
1946 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1947 if (strlen(outpars))
1953 while (i < strlen (outpars))
1955 b[z++] = outpars[i++];
1957 if (i<strlen(outpars)-1)
1959 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1961 // someones playing silly buggers and trying
1962 // to put a +- or -+ into the line...
1966 if (i == strlen(outpars)-1)
1968 if ((outpars[i] == '-') || (outpars[i] == '+'))
1976 if ((b[z] == '-') || (b[z] == '+'))
1979 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1983 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
1984 log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
1986 if (strlen(dmodes)>MAXMODES)
1988 dmodes[MAXMODES-1] = '\0';
1990 log(DEBUG,"Stripped mode line");
1991 log(DEBUG,"Line dest is now %s",dmodes);
1992 strlcpy(dest->modes,dmodes,MAXMODES);
1999 Ptr = FindChan(parameters[0]);
2002 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2005 if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2009 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);