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 such 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 such 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 such nick/channel",user->nick, dest);
165 WriteServ(user->fd,"401 %s %s :No such 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 such nick/channel",user->nick, dest);
221 WriteServ(user->fd,"401 %s %s :No such 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 such 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 such 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 such nick/channel",user->nick, dest);
336 WriteServ(user->fd,"401 %s %s :No such 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 such nick/channel",user->nick, dest);
392 WriteServ(user->fd,"401 %s %s :No such 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 int l = strlen(dest);
441 if (strchr(dest,'!')==0)
443 if (strchr(dest,'@')==0)
445 for (int i = 0; i < l; i++)
448 for (int i = 0; i < l; i++)
452 for (int i = 0; i < l; i++)
458 for (int i = 0; i < l; i++)
464 long maxbans = GetMaxBans(chan->name);
465 if (chan->bans.size() > maxbans)
467 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);
471 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
474 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
476 if (!strcasecmp(i->data,dest))
478 // dont allow a user to set the same ban twice
484 strncpy(b.data,dest,MAXBUF);
485 strncpy(b.set_by,user->nick,NICKMAX);
486 chan->bans.push_back(b);
490 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
492 if ((!user) || (!dest) || (!chan)) {
493 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
497 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
498 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
500 if (!strcasecmp(i->data,dest))
509 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
512 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
516 char modelist[MAXBUF];
517 char outlist[MAXBUF];
519 char outpars[32][MAXBUF];
525 bool k_set = false, l_set = false;
533 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
535 if (MOD_RESULT == ACR_DENY)
538 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
540 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
541 * parameters[2] onwards are parameters for
542 * modes that require them :) */
543 strlcpy(outlist,"+",MAXBUF);
546 log(DEBUG,"process_modes: modelist: %s",modelist);
548 int len = strlen(modelist);
549 while (modelist[len-1] == ' ')
550 modelist[--len] = '\0';
551 for (ptr = 0; ptr < len; ptr++)
556 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
558 char modechar = modelist[ptr];
559 switch (modelist[ptr])
564 int t = strlen(outlist)-1;
565 if ((outlist[t] == '+') || (outlist[t] == '-'))
581 int t = strlen(outlist)-1;
582 if ((outlist[t] == '+') || (outlist[t] == '-'))
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++],32);
734 strlcpy(outpars[pc++],key,MAXBUF);
735 strlcpy(chan->key,key,MAXBUF);
743 /* checks on -k are case sensitive and only accurate to the
744 first 32 characters */
747 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
750 strlcpy(key,parameters[param++],32);
751 /* only allow -k if correct key given */
752 if (!strcmp(chan->key,key))
754 strlcat(outlist,"k",MAXBUF);
755 strlcpy(chan->key,"",MAXBUF);
756 strlcpy(outpars[pc++],key,MAXBUF);
767 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
778 if ((param >= pcnt)) break;
784 bool invalid = false;
785 for (int i = 0; i < strlen(parameters[param]); i++)
787 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
792 if (atoi(parameters[param]) < 1)
801 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
805 chan->limit = atoi(parameters[param]);
807 // reported by mech: large values cause underflow
809 chan->limit = 0x7FFFFF;
814 strlcat(outlist,"l",MAXBUF);
815 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
823 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
826 if (chan->inviteonly != mdir)
828 strlcat(outlist,"i",MAXBUF);
830 chan->inviteonly = mdir;
836 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
839 if (chan->topiclock != mdir)
841 strlcat(outlist,"t",MAXBUF);
843 chan->topiclock = mdir;
849 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
852 if (chan->noexternal != mdir)
854 strlcat(outlist,"n",MAXBUF);
856 chan->noexternal = mdir;
862 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
865 if (chan->moderated != mdir)
867 strlcat(outlist,"m",MAXBUF);
869 chan->moderated = mdir;
875 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
878 if (chan->secret != mdir)
886 strlcat(outlist,"-p+",MAXBUF);
890 strlcat(outlist,"+p-",MAXBUF);
900 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
903 if (chan->c_private != mdir)
905 strlcat(outlist,"p",MAXBUF);
911 strlcat(outlist,"-s+",MAXBUF);
915 strlcat(outlist,"+s-",MAXBUF);
919 chan->c_private = mdir;
924 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
927 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
929 if (!ModeIsListMode(modechar,MT_CHANNEL))
931 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
935 if (ModeDefined(modechar,MT_CHANNEL))
937 log(DEBUG,"A module has claimed this mode");
940 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
942 p.push_back(parameters[param]);
944 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
946 p.push_back(parameters[param]);
949 bool handled = false;
952 // we're supposed to have a parameter, but none was given... so dont handle the mode.
953 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
955 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
961 // BIG ASS IDIOTIC CODER WARNING!
962 // Using OnRawMode on another modules mode's behavour
963 // will confuse the crap out of admins! just because you CAN
964 // do it, doesnt mean you SHOULD!
966 std::string para = "";
969 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
972 for (int i = 0; i <= MODCOUNT; i++)
976 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
979 log(DEBUG,"OnExtendedMode returned nonzero for a module");
980 char app[] = {modechar, 0};
981 if (ModeIsListMode(modechar,MT_CHANNEL))
991 strlcat(outlist, app,MAXBUF);
993 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1000 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1002 strlcat(outlist, app,MAXBUF);
1004 else if (!strchr(outlist,modechar))
1006 strlcat(outlist, app,MAXBUF);
1009 chan->SetCustomMode(modechar,mdir);
1010 // include parameters in output if mode has them
1011 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1013 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1014 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1017 // break, because only one module can handle the mode.
1026 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1034 /* this ensures only the *valid* modes are sent out onto the network */
1035 int xt = strlen(outlist)-1;
1036 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1039 xt = strlen(outlist)-1;
1043 strlcpy(outstr,outlist,MAXBUF);
1044 for (ptr = 0; ptr < pc; ptr++)
1046 strlcat(outstr," ",MAXBUF);
1047 strlcat(outstr,outpars[ptr],MAXBUF);
1051 log(DEBUG,"Local mode change");
1052 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1060 WriteChannelWithServ(ServerName,chan,"MODE %s %s",chan->name,outstr);
1061 // M token for a usermode must go to all servers
1062 char buffer[MAXBUF];
1063 snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
1064 NetSendToAll(buffer);
1072 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1073 // M token for a usermode must go to all servers
1074 char buffer[MAXBUF];
1075 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
1076 NetSendToAll(buffer);
1083 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1085 bool allowed_umode(char umode, char* sourcemodes,bool adding)
1087 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1088 // RFC1459 specified modes
1089 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1091 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1095 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1096 if ((strchr(sourcemodes,'o')) && (!adding))
1098 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1101 else if (umode == 'o')
1103 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1107 // process any module-defined modes that need oper
1108 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1110 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1114 if (ModeDefined(umode,MT_CLIENT))
1116 // process any module-defined modes that don't need oper
1117 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1118 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1120 // no, this mode needs oper, and this user 'aint got what it takes!
1126 // anything else - return false.
1127 log(DEBUG,"umode %c not known by any ruleset",umode);
1131 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1138 strlcpy(s2->nick,ServerName,NICKMAX);
1139 strcpy(s2->modes,"o");
1146 if (ModeDefined(umode,MT_CLIENT))
1148 for (int i = 0; i <= MODCOUNT; i++)
1150 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1152 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1156 log(DEBUG,"No module claims umode %c",umode);
1175 void handle_mode(char **parameters, int pcnt, userrec *user)
1181 char outpars[MAXBUF];
1183 dest = Find(parameters[0]);
1190 if ((dest) && (pcnt == 1))
1192 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1196 if ((dest) && (pcnt > 1))
1198 char dmodes[MAXBUF];
1199 strlcpy(dmodes,dest->modes,MAXBUF);
1200 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1205 if (strchr(user->modes,'o'))
1216 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1220 strcpy(outpars,"+");
1223 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1226 for (int i = 0; i < strlen(parameters[1]); i++)
1228 if (parameters[1][i] == ' ')
1230 if (parameters[1][i] == '+')
1234 int t = strlen(outpars)-1;
1235 if ((outpars[t] == '+') || (outpars[t] == '-'))
1241 strcat(outpars,"+");
1247 if (parameters[1][i] == '-')
1251 int t = strlen(outpars)-1;
1252 if ((outpars[t] == '+') || (outpars[t] == '-'))
1258 strcat(outpars,"-");
1266 if (strchr(user->modes,'o'))
1272 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1281 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1283 char umode = parameters[1][i];
1284 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1286 int q = strlen(dmodes);
1287 int r = strlen(outpars);
1289 dmodes[q] = parameters[1][i];
1291 outpars[r] = parameters[1][i];
1292 if (parameters[1][i] == 'o')
1294 FOREACH_MOD OnGlobalOper(dest);
1301 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1303 char umode = parameters[1][i];
1304 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1310 int r = strlen(outpars);
1312 outpars[r] = parameters[1][i];
1315 for (q = 0; q < strlen(dmodes); q++)
1317 if (dmodes[q] != parameters[1][i])
1321 strlcat(temp,moo,MAXBUF);
1324 strlcpy(dmodes,temp,MAXBUF);
1337 strlcpy(b,"",MAXBUF);
1340 while (i < strlen (outpars))
1342 b[z++] = outpars[i++];
1344 if (i<strlen(outpars)-1)
1346 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1348 // someones playing silly buggers and trying
1349 // to put a +- or -+ into the line...
1353 if (i == strlen(outpars)-1)
1355 if ((outpars[i] == '-') || (outpars[i] == '+'))
1363 if ((b[z] == '-') || (b[z] == '+'))
1366 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1369 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1371 // M token for a usermode must go to all servers
1372 char buffer[MAXBUF];
1373 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1374 NetSendToAll(buffer);
1376 if (strlen(dmodes)>MAXMODES)
1378 dmodes[MAXMODES-1] = '\0';
1380 log(DEBUG,"Stripped mode line");
1381 log(DEBUG,"Line dest is now %s",dmodes);
1382 strlcpy(dest->modes,dmodes,MAXMODES);
1389 Ptr = FindChan(parameters[0]);
1394 /* just /modes #channel */
1395 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1396 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1402 char* mode = parameters[1];
1406 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1412 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1414 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1416 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1419 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1421 // list of items for an extmode
1422 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1423 FOREACH_MOD OnSendList(user,Ptr,*mode);
1429 if ((Ptr) && (!has_channel(user,Ptr)))
1431 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1438 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1440 if (MOD_RESULT == ACR_DENY)
1442 if (MOD_RESULT == ACR_DEFAULT)
1444 if (cstatus(user,Ptr) < STATUS_HOP)
1446 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1451 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1456 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1463 void server_mode(char **parameters, int pcnt, userrec *user)
1469 char outpars[MAXBUF];
1471 dest = Find(parameters[0]);
1473 // fix: ChroNiCk found this - we cant use this as debug if its null!
1476 log(DEBUG,"server_mode on %s",dest->nick);
1479 if ((dest) && (pcnt > 1))
1481 log(DEBUG,"params > 1");
1483 char dmodes[MAXBUF];
1484 strlcpy(dmodes,dest->modes,MAXBUF);
1486 strcpy(outpars,"+");
1489 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1492 for (int i = 0; i < strlen(parameters[1]); i++)
1494 if (parameters[1][i] == ' ')
1496 if (parameters[1][i] == '+')
1500 int t = strlen(outpars)-1;
1501 if ((outpars[t] == '+') || (outpars[t] == '-'))
1507 strcat(outpars,"+");
1513 if (parameters[1][i] == '-')
1517 int t = strlen(outpars)-1;
1518 if ((outpars[t] == '+') || (outpars[t] == '-'))
1524 strcat(outpars,"-");
1531 log(DEBUG,"begin mode processing entry");
1537 log(DEBUG,"umode %c being added",parameters[1][i]);
1538 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1540 char umode = parameters[1][i];
1541 log(DEBUG,"umode %c is an allowed umode",umode);
1542 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1544 int v1 = strlen(dmodes);
1545 int v2 = strlen(outpars);
1547 dmodes[v1] = parameters[1][i];
1549 outpars[v2] = parameters[1][i];
1555 // can only remove a mode they already have
1556 log(DEBUG,"umode %c being removed",parameters[1][i]);
1557 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1559 char umode = parameters[1][i];
1560 log(DEBUG,"umode %c is an allowed umode",umode);
1561 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1567 int v1 = strlen(outpars);
1569 outpars[v1] = parameters[1][i];
1572 for (q = 0; q < strlen(dmodes); q++)
1574 if (dmodes[q] != parameters[1][i])
1578 strlcat(temp,moo,MAXBUF);
1581 strlcpy(dmodes,temp,MAXBUF);
1591 strlcpy(b,"",MAXBUF);
1594 while (i < strlen (outpars))
1596 b[z++] = outpars[i++];
1598 if (i<strlen(outpars)-1)
1600 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1602 // someones playing silly buggers and trying
1603 // to put a +- or -+ into the line...
1607 if (i == strlen(outpars)-1)
1609 if ((outpars[i] == '-') || (outpars[i] == '+'))
1617 if ((b[z] == '-') || (b[z] == '+'))
1620 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1623 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1625 // M token for a usermode must go to all servers
1626 char buffer[MAXBUF];
1627 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1628 NetSendToAll(buffer);
1630 if (strlen(dmodes)>MAXMODES)
1632 dmodes[MAXMODES-1] = '\0';
1634 log(DEBUG,"Stripped mode line");
1635 log(DEBUG,"Line dest is now %s",dmodes);
1636 strlcpy(dest->modes,dmodes,MAXMODES);
1643 Ptr = FindChan(parameters[0]);
1646 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1650 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1656 void merge_mode(char **parameters, int pcnt)
1662 char outpars[MAXBUF];
1664 dest = Find(parameters[0]);
1666 // fix: ChroNiCk found this - we cant use this as debug if its null!
1669 log(DEBUG,"merge_mode on %s",dest->nick);
1672 if ((dest) && (pcnt > 1))
1674 log(DEBUG,"params > 1");
1676 char dmodes[MAXBUF];
1677 strlcpy(dmodes,dest->modes,MAXBUF);
1679 strcpy(outpars,"+");
1682 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1685 for (int i = 0; i < strlen(parameters[1]); i++)
1687 if (parameters[1][i] == ' ')
1689 if (parameters[1][i] == '+')
1693 int t = strlen(outpars)-1;
1694 if ((outpars[t] == '+') || (outpars[t] == '-'))
1700 strcat(outpars,"+");
1706 if (parameters[1][i] == '-')
1710 int t = strlen(outpars)-1;
1711 if ((outpars[t] == '+') || (outpars[t] == '-'))
1717 strcat(outpars,"-");
1724 log(DEBUG,"begin mode processing entry");
1730 log(DEBUG,"umode %c being added",parameters[1][i]);
1731 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1733 char umode = parameters[1][i];
1734 log(DEBUG,"umode %c is an allowed umode",umode);
1735 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1737 int v1 = strlen(dmodes);
1738 int v2 = strlen(outpars);
1740 dmodes[v1] = parameters[1][i];
1742 outpars[v2] = parameters[1][i];
1748 // can only remove a mode they already have
1749 log(DEBUG,"umode %c being removed",parameters[1][i]);
1750 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1752 char umode = parameters[1][i];
1753 log(DEBUG,"umode %c is an allowed umode",umode);
1754 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1760 int v1 = strlen(outpars);
1762 outpars[v1] = parameters[1][i];
1765 for (q = 0; q < strlen(dmodes); q++)
1767 if (dmodes[q] != parameters[1][i])
1771 strlcat(temp,moo,MAXBUF);
1774 strlcpy(dmodes,temp,MAXBUF);
1787 while (i < strlen (outpars))
1789 b[z++] = outpars[i++];
1791 if (i<strlen(outpars)-1)
1793 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1795 // someones playing silly buggers and trying
1796 // to put a +- or -+ into the line...
1800 if (i == strlen(outpars)-1)
1802 if ((outpars[i] == '-') || (outpars[i] == '+'))
1810 if ((b[z] == '-') || (b[z] == '+'))
1813 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1816 if (strlen(dmodes)>MAXMODES)
1818 dmodes[MAXMODES-1] = '\0';
1820 log(DEBUG,"Stripped mode line");
1821 log(DEBUG,"Line dest is now %s",dmodes);
1822 strlcpy(dest->modes,dmodes,MAXMODES);
1829 Ptr = FindChan(parameters[0]);
1833 strlcpy(s2.nick,ServerName,NICKMAX);
1834 strcpy(s2.modes,"o");
1836 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1841 void merge_mode2(char **parameters, int pcnt, userrec* user)
1847 char outpars[MAXBUF];
1849 dest = Find(parameters[0]);
1851 // fix: ChroNiCk found this - we cant use this as debug if its null!
1854 log(DEBUG,"merge_mode2 on %s",dest->nick);
1857 if ((dest) && (pcnt > 1))
1859 log(DEBUG,"params > 1");
1861 char dmodes[MAXBUF];
1862 strlcpy(dmodes,dest->modes,MAXBUF);
1864 strcpy(outpars,"+");
1867 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1869 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1870 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1872 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1875 for (int i = 0; i < strlen(parameters[1]); i++)
1877 if (parameters[1][i] == ' ')
1879 if (parameters[1][i] == '+')
1883 int t = strlen(outpars)-1;
1884 if ((outpars[t] == '+') || (outpars[t] == '-'))
1890 strcat(outpars,"+");
1896 if (parameters[1][i] == '-')
1900 int t = strlen(outpars)-1;
1901 if ((outpars[t] == '+') || (outpars[t] == '-'))
1907 strcat(outpars,"-");
1914 log(DEBUG,"begin mode processing entry");
1920 log(DEBUG,"umode %c being added",parameters[1][i]);
1921 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1923 char umode = parameters[1][i];
1924 log(DEBUG,"umode %c is an allowed umode",umode);
1925 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1927 int v1 = strlen(dmodes);
1928 int v2 = strlen(outpars);
1930 dmodes[v1] = parameters[1][i];
1932 outpars[v2] = parameters[1][i];
1933 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1939 // can only remove a mode they already have
1940 log(DEBUG,"umode %c being removed",parameters[1][i]);
1941 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1943 char umode = parameters[1][i];
1944 log(DEBUG,"umode %c is an allowed umode",umode);
1945 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1951 int v1 = strlen(outpars);
1953 outpars[v1] = parameters[1][i];
1956 for (q = 0; q < strlen(dmodes); q++)
1958 if (dmodes[q] != parameters[1][i])
1962 strlcat(temp,moo,MAXBUF);
1965 strlcpy(dmodes,temp,MAXBUF);
1972 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1979 while (i < strlen (outpars))
1981 b[z++] = outpars[i++];
1983 if (i<strlen(outpars)-1)
1985 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1987 // someones playing silly buggers and trying
1988 // to put a +- or -+ into the line...
1992 if (i == strlen(outpars)-1)
1994 if ((outpars[i] == '-') || (outpars[i] == '+'))
2002 if ((b[z] == '-') || (b[z] == '+'))
2005 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2009 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2010 log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
2012 if (strlen(dmodes)>MAXMODES)
2014 dmodes[MAXMODES-1] = '\0';
2016 log(DEBUG,"Stripped mode line");
2017 log(DEBUG,"Line dest is now %s",dmodes);
2018 strlcpy(dest->modes,dmodes,MAXMODES);
2025 Ptr = FindChan(parameters[0]);
2028 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2031 if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2035 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);