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)
88 if ((!user) || (!dest) || (!chan))
90 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
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);
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);
111 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
113 if (MOD_RESULT == ACR_DENY)
115 if (MOD_RESULT == ACR_DEFAULT)
117 if ((status < STATUS_OP) && (!is_uline(user->server)))
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);
126 for (int i = 0; i != MAXCHANS; i++)
128 if ((d->chans[i].channel != NULL) && (chan != NULL))
129 if (!strcasecmp(d->chans[i].channel->name,chan->name))
131 if (d->chans[i].uc_modes & UCMODE_OP)
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");
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);
142 log(DEFAULT,"The target channel given to give_ops was not in the users mode list");
147 char* give_hops(userrec *user,char *dest,chanrec *chan,int status)
151 if ((!user) || (!dest) || (!chan))
153 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
160 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
165 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
171 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
173 if (MOD_RESULT == ACR_DENY)
175 if (MOD_RESULT == ACR_DEFAULT)
177 if ((status < STATUS_OP) && (!is_uline(user->server)))
179 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
184 for (int i = 0; i != MAXCHANS; i++)
186 if ((d->chans[i].channel != NULL) && (chan != NULL))
187 if (!strcasecmp(d->chans[i].channel->name,chan->name))
189 if (d->chans[i].uc_modes & UCMODE_HOP)
191 /* mode already set on user, dont allow multiple */
194 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
195 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
203 char* give_voice(userrec *user,char *dest,chanrec *chan,int status)
207 if ((!user) || (!dest) || (!chan))
209 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
216 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
221 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
227 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
229 if (MOD_RESULT == ACR_DENY)
231 if (MOD_RESULT == ACR_DEFAULT)
233 if ((status < STATUS_HOP) && (!is_uline(user->server)))
235 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
240 for (int i = 0; i != MAXCHANS; i++)
242 if ((d->chans[i].channel != NULL) && (chan != NULL))
243 if (!strcasecmp(d->chans[i].channel->name,chan->name))
245 if (d->chans[i].uc_modes & UCMODE_VOICE)
247 /* mode already set on user, dont allow multiple */
250 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
251 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
259 char* take_ops(userrec *user,char *dest,chanrec *chan,int status)
263 if ((!user) || (!dest) || (!chan))
265 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
272 log(DEBUG,"take_ops was given an invalid target nickname of %s",dest);
273 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
278 log(DEBUG,"take_ops couldnt resolve the target nickname: %s",dest);
279 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
285 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
287 if (MOD_RESULT == ACR_DENY)
289 if (MOD_RESULT == ACR_DEFAULT)
291 if ((status < STATUS_OP) && (!is_uline(user->server)))
293 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
298 for (int i = 0; i != MAXCHANS; i++)
300 if ((d->chans[i].channel != NULL) && (chan != NULL))
301 if (!strcasecmp(d->chans[i].channel->name,chan->name))
303 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
305 /* mode already set on user, dont allow multiple */
308 d->chans[i].uc_modes ^= UCMODE_OP;
309 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
313 log(DEBUG,"take_ops couldnt locate the target channel in the target users list");
318 char* take_hops(userrec *user,char *dest,chanrec *chan,int status)
322 if ((!user) || (!dest) || (!chan))
324 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
331 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
336 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
342 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
344 if (MOD_RESULT == ACR_DENY)
346 if (MOD_RESULT == ACR_DEFAULT)
348 if ((status < STATUS_OP) && (!is_uline(user->server)))
350 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
355 for (int i = 0; i != MAXCHANS; i++)
357 if ((d->chans[i].channel != NULL) && (chan != NULL))
358 if (!strcasecmp(d->chans[i].channel->name,chan->name))
360 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
362 /* mode already set on user, dont allow multiple */
365 d->chans[i].uc_modes ^= UCMODE_HOP;
366 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
374 char* take_voice(userrec *user,char *dest,chanrec *chan,int status)
378 if ((!user) || (!dest) || (!chan))
380 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
387 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
392 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
398 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
400 if (MOD_RESULT == ACR_DENY)
402 if (MOD_RESULT == ACR_DEFAULT)
404 if ((status < STATUS_HOP) && (!is_uline(user->server)))
406 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
411 for (int i = 0; i != MAXCHANS; i++)
413 if ((d->chans[i].channel != NULL) && (chan != NULL))
414 if (!strcasecmp(d->chans[i].channel->name,chan->name))
416 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
418 /* mode already set on user, dont allow multiple */
421 d->chans[i].uc_modes ^= UCMODE_VOICE;
422 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
430 char* add_ban(userrec *user,char *dest,chanrec *chan,int status)
432 if ((!user) || (!dest) || (!chan)) {
433 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
438 if ((!user) || (!dest) || (!chan))
440 if (strchr(dest,'!')==0)
442 if (strchr(dest,'@')==0)
444 for (int i = 0; i < strlen(dest); i++)
447 for (int i = 0; i < strlen(dest); i++)
451 for (int i = 0; i < strlen(dest); i++)
457 for (int i = 0; i < strlen(dest); i++)
463 long maxbans = GetMaxBans(chan->name);
464 if (chan->bans.size() > maxbans)
466 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);
470 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
473 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
475 if (!strcasecmp(i->data,dest))
477 // dont allow a user to set the same ban twice
483 strncpy(b.data,dest,MAXBUF);
484 strncpy(b.set_by,user->nick,NICKMAX);
485 chan->bans.push_back(b);
489 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
491 if ((!user) || (!dest) || (!chan)) {
492 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
496 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
497 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
499 if (!strcasecmp(i->data,dest))
508 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
511 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
515 char modelist[MAXBUF];
516 char outlist[MAXBUF];
518 char outpars[32][MAXBUF];
524 bool k_set = false, l_set = false;
532 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
534 if (MOD_RESULT == ACR_DENY)
537 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
539 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
540 * parameters[2] onwards are parameters for
541 * modes that require them :) */
542 strlcpy(outlist,"+",MAXBUF);
545 log(DEBUG,"process_modes: modelist: %s",modelist);
547 for (ptr = 0; ptr < strlen(modelist); ptr++)
552 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
554 char modechar = modelist[ptr];
555 switch (modelist[ptr])
560 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
562 outlist[strlen(outlist)-1] = '-';
576 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
578 outlist[strlen(outlist)-1] = '+';
590 if ((param >= pcnt)) break;
591 log(DEBUG,"Enough parameters left");
595 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
598 log(DEBUG,"calling give_ops");
599 r = give_ops(user,parameters[param++],chan,status);
606 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
609 log(DEBUG,"calling take_ops");
610 r = take_ops(user,parameters[param++],chan,status);
616 strlcat(outlist,"o",MAXBUF);
617 strlcpy(outpars[pc++],r,MAXBUF);
622 if (((param >= pcnt)) || (!AllowHalfop)) break;
626 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
629 r = give_hops(user,parameters[param++],chan,status);
636 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
639 r = take_hops(user,parameters[param++],chan,status);
645 strlcat(outlist,"h",MAXBUF);
646 strlcpy(outpars[pc++],r,MAXBUF);
652 if ((param >= pcnt)) break;
656 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
659 r = give_voice(user,parameters[param++],chan,status);
666 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
669 r = take_voice(user,parameters[param++],chan,status);
675 strlcat(outlist,"v",MAXBUF);
676 strlcpy(outpars[pc++],r,MAXBUF);
681 if ((param >= pcnt)) break;
685 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
688 r = add_ban(user,parameters[param++],chan,status);
695 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
698 r = take_ban(user,parameters[param++],chan,status);
704 strlcat(outlist,"b",MAXBUF);
705 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
719 if (!strcmp(chan->key,""))
722 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
727 strlcpy(key,parameters[param++],MAXBUF);
728 if (strlen(key)>32) {
731 strlcpy(outpars[pc++],key,MAXBUF);
732 strlcpy(chan->key,key,MAXBUF);
740 /* checks on -k are case sensitive and only accurate to the
741 first 32 characters */
744 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
747 strlcpy(key,parameters[param++],MAXBUF);
752 /* only allow -k if correct key given */
753 if (!strcmp(chan->key,key))
755 strlcat(outlist,"k",MAXBUF);
756 strlcpy(chan->key,"",MAXBUF);
757 strlcpy(outpars[pc++],key,MAXBUF);
768 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
779 if ((param >= pcnt)) break;
785 bool invalid = false;
786 for (int i = 0; i < strlen(parameters[param]); i++)
788 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
793 if (atoi(parameters[param]) < 1)
802 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
806 chan->limit = atoi(parameters[param]);
808 // reported by mech: large values cause underflow
810 chan->limit = 0x7FFFFF;
815 strlcat(outlist,"l",MAXBUF);
816 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
824 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
827 if (chan->inviteonly != mdir)
829 strlcat(outlist,"i",MAXBUF);
831 chan->inviteonly = mdir;
837 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
840 if (chan->topiclock != mdir)
842 strlcat(outlist,"t",MAXBUF);
844 chan->topiclock = mdir;
850 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
853 if (chan->noexternal != mdir)
855 strlcat(outlist,"n",MAXBUF);
857 chan->noexternal = mdir;
863 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
866 if (chan->moderated != mdir)
868 strlcat(outlist,"m",MAXBUF);
870 chan->moderated = mdir;
876 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
879 if (chan->secret != mdir)
887 strlcat(outlist,"-p+",MAXBUF);
891 strlcat(outlist,"+p-",MAXBUF);
901 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
904 if (chan->c_private != mdir)
906 strlcat(outlist,"p",MAXBUF);
912 strlcat(outlist,"-s+",MAXBUF);
916 strlcat(outlist,"+s-",MAXBUF);
920 chan->c_private = mdir;
925 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
928 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
930 if (!ModeIsListMode(modechar,MT_CHANNEL))
932 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
936 if (ModeDefined(modechar,MT_CHANNEL))
938 log(DEBUG,"A module has claimed this mode");
941 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
943 p.push_back(parameters[param]);
945 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
947 p.push_back(parameters[param]);
950 bool handled = false;
953 // we're supposed to have a parameter, but none was given... so dont handle the mode.
954 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
956 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
962 // BIG ASS IDIOTIC CODER WARNING!
963 // Using OnRawMode on another modules mode's behavour
964 // will confuse the crap out of admins! just because you CAN
965 // do it, doesnt mean you SHOULD!
967 std::string para = "";
970 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
973 for (int i = 0; i <= MODCOUNT; i++)
977 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
980 log(DEBUG,"OnExtendedMode returned nonzero for a module");
981 char app[] = {modechar, 0};
982 if (ModeIsListMode(modechar,MT_CHANNEL))
992 strlcat(outlist, app,MAXBUF);
994 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1001 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1003 strlcat(outlist, app,MAXBUF);
1005 else if (!strchr(outlist,modechar))
1007 strlcat(outlist, app,MAXBUF);
1010 chan->SetCustomMode(modechar,mdir);
1011 // include parameters in output if mode has them
1012 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1014 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1015 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1018 // break, because only one module can handle the mode.
1027 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1035 /* this ensures only the *valid* modes are sent out onto the network */
1036 while ((outlist[strlen(outlist)-1] == '-') || (outlist[strlen(outlist)-1] == '+'))
1038 outlist[strlen(outlist)-1] = '\0';
1040 if (strcmp(outlist,""))
1042 strlcpy(outstr,outlist,MAXBUF);
1043 for (ptr = 0; ptr < pc; ptr++)
1045 strlcat(outstr," ",MAXBUF);
1046 strlcat(outstr,outpars[ptr],MAXBUF);
1050 log(DEBUG,"Local mode change");
1051 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1059 WriteChannelWithServ(ServerName,chan,user,"MODE %s %s",chan->name,outstr);
1060 // M token for a usermode must go to all servers
1061 char buffer[MAXBUF];
1062 snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
1063 NetSendToAll(buffer);
1071 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1072 // M token for a usermode must go to all servers
1073 char buffer[MAXBUF];
1074 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
1075 NetSendToAll(buffer);
1082 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1084 bool allowed_umode(char umode, char* sourcemodes,bool adding)
1086 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1087 // RFC1459 specified modes
1088 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1090 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1094 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1095 if ((strchr(sourcemodes,'o')) && (!adding))
1097 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1100 else if (umode == 'o')
1102 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1106 // process any module-defined modes that need oper
1107 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1109 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1113 if (ModeDefined(umode,MT_CLIENT))
1115 // process any module-defined modes that don't need oper
1116 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1117 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1119 // no, this mode needs oper, and this user 'aint got what it takes!
1125 // anything else - return false.
1126 log(DEBUG,"umode %c not known by any ruleset",umode);
1130 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1137 strlcpy(s2->nick,ServerName,NICKMAX);
1138 strcpy(s2->modes,"o");
1145 if (ModeDefined(umode,MT_CLIENT))
1147 for (int i = 0; i <= MODCOUNT; i++)
1149 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1151 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1155 log(DEBUG,"No module claims umode %c",umode);
1174 void handle_mode(char **parameters, int pcnt, userrec *user)
1180 char outpars[MAXBUF];
1182 dest = Find(parameters[0]);
1189 if ((dest) && (pcnt == 1))
1191 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1195 if ((dest) && (pcnt > 1))
1197 char dmodes[MAXBUF];
1198 strlcpy(dmodes,dest->modes,MAXBUF);
1199 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1204 if (strchr(user->modes,'o'))
1215 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1219 strcpy(outpars,"+");
1222 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1225 for (int i = 0; i < strlen(parameters[1]); i++)
1227 if (parameters[1][i] == '+')
1231 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1233 outpars[strlen(outpars)-1] = '+';
1237 strcat(outpars,"+");
1243 if (parameters[1][i] == '-')
1247 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1249 outpars[strlen(outpars)-1] = '-';
1253 strcat(outpars,"-");
1261 if (strchr(user->modes,'o'))
1267 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1276 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1278 char umode = parameters[1][i];
1279 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1281 dmodes[strlen(dmodes)+1]='\0';
1282 dmodes[strlen(dmodes)] = parameters[1][i];
1283 outpars[strlen(outpars)+1]='\0';
1284 outpars[strlen(outpars)] = parameters[1][i];
1285 if (parameters[1][i] == 'o')
1287 FOREACH_MOD OnGlobalOper(dest);
1294 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1296 char umode = parameters[1][i];
1297 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1303 outpars[strlen(outpars)+1]='\0';
1304 outpars[strlen(outpars)] = parameters[1][i];
1307 for (q = 0; q < strlen(dmodes); q++)
1309 if (dmodes[q] != parameters[1][i])
1313 strlcat(temp,moo,MAXBUF);
1316 strlcpy(dmodes,temp,MAXBUF);
1323 if (strlen(outpars))
1326 strlcpy(b,"",MAXBUF);
1329 while (i < strlen (outpars))
1331 b[z++] = outpars[i++];
1333 if (i<strlen(outpars)-1)
1335 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1337 // someones playing silly buggers and trying
1338 // to put a +- or -+ into the line...
1342 if (i == strlen(outpars)-1)
1344 if ((outpars[i] == '-') || (outpars[i] == '+'))
1352 if ((b[z] == '-') || (b[z] == '+'))
1355 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1358 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1360 // M token for a usermode must go to all servers
1361 char buffer[MAXBUF];
1362 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1363 NetSendToAll(buffer);
1365 if (strlen(dmodes)>MAXMODES)
1367 dmodes[MAXMODES-1] = '\0';
1369 log(DEBUG,"Stripped mode line");
1370 log(DEBUG,"Line dest is now %s",dmodes);
1371 strlcpy(dest->modes,dmodes,MAXMODES);
1378 Ptr = FindChan(parameters[0]);
1383 /* just /modes #channel */
1384 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1385 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1391 char* mode = parameters[1];
1395 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1401 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1403 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1405 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1408 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1410 // list of items for an extmode
1411 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1412 FOREACH_MOD OnSendList(user,Ptr,*mode);
1418 if ((Ptr) && (!has_channel(user,Ptr)))
1420 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1427 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1429 if (MOD_RESULT == ACR_DENY)
1431 if (MOD_RESULT == ACR_DEFAULT)
1433 if (cstatus(user,Ptr) < STATUS_HOP)
1435 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1440 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1445 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
1452 void server_mode(char **parameters, int pcnt, userrec *user)
1458 char outpars[MAXBUF];
1460 dest = Find(parameters[0]);
1462 // fix: ChroNiCk found this - we cant use this as debug if its null!
1465 log(DEBUG,"server_mode on %s",dest->nick);
1468 if ((dest) && (pcnt > 1))
1470 log(DEBUG,"params > 1");
1472 char dmodes[MAXBUF];
1473 strlcpy(dmodes,dest->modes,MAXBUF);
1475 strcpy(outpars,"+");
1478 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1481 for (int i = 0; i < strlen(parameters[1]); i++)
1483 if (parameters[1][i] == '+')
1487 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1489 outpars[strlen(outpars)-1] = '+';
1493 strcat(outpars,"+");
1499 if (parameters[1][i] == '-')
1503 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1505 outpars[strlen(outpars)-1] = '-';
1509 strcat(outpars,"-");
1516 log(DEBUG,"begin mode processing entry");
1522 log(DEBUG,"umode %c being added",parameters[1][i]);
1523 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1525 char umode = parameters[1][i];
1526 log(DEBUG,"umode %c is an allowed umode",umode);
1527 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1529 dmodes[strlen(dmodes)+1]='\0';
1530 dmodes[strlen(dmodes)] = parameters[1][i];
1531 outpars[strlen(outpars)+1]='\0';
1532 outpars[strlen(outpars)] = parameters[1][i];
1538 // can only remove a mode they already have
1539 log(DEBUG,"umode %c being removed",parameters[1][i]);
1540 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1542 char umode = parameters[1][i];
1543 log(DEBUG,"umode %c is an allowed umode",umode);
1544 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1550 outpars[strlen(outpars)+1]='\0';
1551 outpars[strlen(outpars)] = parameters[1][i];
1554 for (q = 0; q < strlen(dmodes); q++)
1556 if (dmodes[q] != parameters[1][i])
1560 strlcat(temp,moo,MAXBUF);
1563 strlcpy(dmodes,temp,MAXBUF);
1570 if (strlen(outpars))
1573 strlcpy(b,"",MAXBUF);
1576 while (i < strlen (outpars))
1578 b[z++] = outpars[i++];
1580 if (i<strlen(outpars)-1)
1582 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1584 // someones playing silly buggers and trying
1585 // to put a +- or -+ into the line...
1589 if (i == strlen(outpars)-1)
1591 if ((outpars[i] == '-') || (outpars[i] == '+'))
1599 if ((b[z] == '-') || (b[z] == '+'))
1602 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1605 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1607 // M token for a usermode must go to all servers
1608 char buffer[MAXBUF];
1609 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1610 NetSendToAll(buffer);
1612 if (strlen(dmodes)>MAXMODES)
1614 dmodes[MAXMODES-1] = '\0';
1616 log(DEBUG,"Stripped mode line");
1617 log(DEBUG,"Line dest is now %s",dmodes);
1618 strlcpy(dest->modes,dmodes,MAXMODES);
1625 Ptr = FindChan(parameters[0]);
1628 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1632 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
1638 void merge_mode(char **parameters, int pcnt)
1644 char outpars[MAXBUF];
1646 dest = Find(parameters[0]);
1648 // fix: ChroNiCk found this - we cant use this as debug if its null!
1651 log(DEBUG,"merge_mode on %s",dest->nick);
1654 if ((dest) && (pcnt > 1))
1656 log(DEBUG,"params > 1");
1658 char dmodes[MAXBUF];
1659 strlcpy(dmodes,dest->modes,MAXBUF);
1661 strcpy(outpars,"+");
1664 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1667 for (int i = 0; i < strlen(parameters[1]); i++)
1669 if (parameters[1][i] == '+')
1673 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1675 outpars[strlen(outpars)-1] = '+';
1679 strcat(outpars,"+");
1685 if (parameters[1][i] == '-')
1689 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1691 outpars[strlen(outpars)-1] = '-';
1695 strcat(outpars,"-");
1702 log(DEBUG,"begin mode processing entry");
1708 log(DEBUG,"umode %c being added",parameters[1][i]);
1709 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1711 char umode = parameters[1][i];
1712 log(DEBUG,"umode %c is an allowed umode",umode);
1713 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1715 dmodes[strlen(dmodes)+1]='\0';
1716 dmodes[strlen(dmodes)] = parameters[1][i];
1717 outpars[strlen(outpars)+1]='\0';
1718 outpars[strlen(outpars)] = parameters[1][i];
1724 // can only remove a mode they already have
1725 log(DEBUG,"umode %c being removed",parameters[1][i]);
1726 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1728 char umode = parameters[1][i];
1729 log(DEBUG,"umode %c is an allowed umode",umode);
1730 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1736 outpars[strlen(outpars)+1]='\0';
1737 outpars[strlen(outpars)] = parameters[1][i];
1740 for (q = 0; q < strlen(dmodes); q++)
1742 if (dmodes[q] != parameters[1][i])
1746 strlcat(temp,moo,MAXBUF);
1749 strlcpy(dmodes,temp,MAXBUF);
1756 if (strlen(outpars))
1762 while (i < strlen (outpars))
1764 b[z++] = outpars[i++];
1766 if (i<strlen(outpars)-1)
1768 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1770 // someones playing silly buggers and trying
1771 // to put a +- or -+ into the line...
1775 if (i == strlen(outpars)-1)
1777 if ((outpars[i] == '-') || (outpars[i] == '+'))
1785 if ((b[z] == '-') || (b[z] == '+'))
1788 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1791 if (strlen(dmodes)>MAXMODES)
1793 dmodes[MAXMODES-1] = '\0';
1795 log(DEBUG,"Stripped mode line");
1796 log(DEBUG,"Line dest is now %s",dmodes);
1797 strlcpy(dest->modes,dmodes,MAXMODES);
1804 Ptr = FindChan(parameters[0]);
1808 strlcpy(s2.nick,ServerName,NICKMAX);
1809 strcpy(s2.modes,"o");
1811 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1816 void merge_mode2(char **parameters, int pcnt, userrec* user)
1822 char outpars[MAXBUF];
1824 dest = Find(parameters[0]);
1826 // fix: ChroNiCk found this - we cant use this as debug if its null!
1829 log(DEBUG,"merge_mode on %s",dest->nick);
1832 if ((dest) && (pcnt > 1))
1834 log(DEBUG,"params > 1");
1836 char dmodes[MAXBUF];
1837 strlcpy(dmodes,dest->modes,MAXBUF);
1839 strcpy(outpars,"+");
1842 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1844 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1845 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1847 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1850 for (int i = 0; i < strlen(parameters[1]); i++)
1852 if (parameters[1][i] == '+')
1856 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1858 outpars[strlen(outpars)-1] = '+';
1862 strcat(outpars,"+");
1868 if (parameters[1][i] == '-')
1872 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1874 outpars[strlen(outpars)-1] = '-';
1878 strcat(outpars,"-");
1885 log(DEBUG,"begin mode processing entry");
1891 log(DEBUG,"umode %c being added",parameters[1][i]);
1892 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1894 char umode = parameters[1][i];
1895 log(DEBUG,"umode %c is an allowed umode",umode);
1896 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1898 dmodes[strlen(dmodes)+1]='\0';
1899 dmodes[strlen(dmodes)] = parameters[1][i];
1900 outpars[strlen(outpars)+1]='\0';
1901 outpars[strlen(outpars)] = parameters[1][i];
1902 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1908 // can only remove a mode they already have
1909 log(DEBUG,"umode %c being removed",parameters[1][i]);
1910 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1912 char umode = parameters[1][i];
1913 log(DEBUG,"umode %c is an allowed umode",umode);
1914 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1920 outpars[strlen(outpars)+1]='\0';
1921 outpars[strlen(outpars)] = parameters[1][i];
1924 for (q = 0; q < strlen(dmodes); q++)
1926 if (dmodes[q] != parameters[1][i])
1930 strlcat(temp,moo,MAXBUF);
1933 strlcpy(dmodes,temp,MAXBUF);
1940 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1941 if (strlen(outpars))
1947 while (i < strlen (outpars))
1949 b[z++] = outpars[i++];
1951 if (i<strlen(outpars)-1)
1953 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1955 // someones playing silly buggers and trying
1956 // to put a +- or -+ into the line...
1960 if (i == strlen(outpars)-1)
1962 if ((outpars[i] == '-') || (outpars[i] == '+'))
1970 if ((b[z] == '-') || (b[z] == '+'))
1973 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1977 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
1978 log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
1980 if (strlen(dmodes)>MAXMODES)
1982 dmodes[MAXMODES-1] = '\0';
1984 log(DEBUG,"Stripped mode line");
1985 log(DEBUG,"Line dest is now %s",dmodes);
1986 strlcpy(dest->modes,dmodes,MAXMODES);
1993 Ptr = FindChan(parameters[0]);
1996 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
1999 if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2003 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);