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; parameters[param][i] != 0; 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; parameters[1][i] != 0; i++)
1228 if (parameters[1][i] == '+')
1232 int t = strlen(outpars)-1;
1233 if ((outpars[t] == '+') || (outpars[t] == '-'))
1239 strcat(outpars,"+");
1245 if (parameters[1][i] == '-')
1249 int t = strlen(outpars)-1;
1250 if ((outpars[t] == '+') || (outpars[t] == '-'))
1256 strcat(outpars,"-");
1264 if (strchr(user->modes,'o'))
1270 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1279 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1281 char umode = parameters[1][i];
1282 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1284 int q = strlen(dmodes);
1285 int r = strlen(outpars);
1287 dmodes[q] = parameters[1][i];
1289 outpars[r] = parameters[1][i];
1290 if (parameters[1][i] == 'o')
1292 FOREACH_MOD OnGlobalOper(dest);
1299 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1301 char umode = parameters[1][i];
1302 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1308 int r = strlen(outpars);
1310 outpars[r] = parameters[1][i];
1313 for (q = 0; dmodes[q] != 0; q++)
1315 if (dmodes[q] != parameters[1][i])
1319 strlcat(temp,moo,MAXBUF);
1322 strlcpy(dmodes,temp,MAXBUF);
1335 strlcpy(b,"",MAXBUF);
1338 while (i < strlen (outpars))
1340 b[z++] = outpars[i++];
1342 if (i<strlen(outpars)-1)
1344 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1346 // someones playing silly buggers and trying
1347 // to put a +- or -+ into the line...
1351 if (i == strlen(outpars)-1)
1353 if ((outpars[i] == '-') || (outpars[i] == '+'))
1361 if ((b[z] == '-') || (b[z] == '+'))
1364 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1367 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1369 // M token for a usermode must go to all servers
1370 char buffer[MAXBUF];
1371 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1372 NetSendToAll(buffer);
1374 if (strlen(dmodes)>MAXMODES)
1376 dmodes[MAXMODES-1] = '\0';
1378 log(DEBUG,"Stripped mode line");
1379 log(DEBUG,"Line dest is now %s",dmodes);
1380 strlcpy(dest->modes,dmodes,MAXMODES);
1387 Ptr = FindChan(parameters[0]);
1392 /* just /modes #channel */
1393 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1394 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1400 char* mode = parameters[1];
1404 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1410 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1412 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1414 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1417 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1419 // list of items for an extmode
1420 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1421 FOREACH_MOD OnSendList(user,Ptr,*mode);
1427 if ((Ptr) && (!has_channel(user,Ptr)))
1429 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1436 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1438 if (MOD_RESULT == ACR_DENY)
1440 if (MOD_RESULT == ACR_DEFAULT)
1442 if (cstatus(user,Ptr) < STATUS_HOP)
1444 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1449 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1454 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1461 void server_mode(char **parameters, int pcnt, userrec *user)
1467 char outpars[MAXBUF];
1469 dest = Find(parameters[0]);
1471 // fix: ChroNiCk found this - we cant use this as debug if its null!
1474 log(DEBUG,"server_mode on %s",dest->nick);
1477 if ((dest) && (pcnt > 1))
1479 log(DEBUG,"params > 1");
1481 char dmodes[MAXBUF];
1482 strlcpy(dmodes,dest->modes,MAXBUF);
1484 strcpy(outpars,"+");
1487 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1490 for (int i = 0; parameters[1][i] != 0; i++)
1492 if (parameters[1][i] == '+')
1496 int t = strlen(outpars)-1;
1497 if ((outpars[t] == '+') || (outpars[t] == '-'))
1503 strcat(outpars,"+");
1509 if (parameters[1][i] == '-')
1513 int t = strlen(outpars)-1;
1514 if ((outpars[t] == '+') || (outpars[t] == '-'))
1520 strcat(outpars,"-");
1527 log(DEBUG,"begin mode processing entry");
1533 log(DEBUG,"umode %c being added",parameters[1][i]);
1534 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1536 char umode = parameters[1][i];
1537 log(DEBUG,"umode %c is an allowed umode",umode);
1538 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1540 int v1 = strlen(dmodes);
1541 int v2 = strlen(outpars);
1543 dmodes[v1] = parameters[1][i];
1545 outpars[v2] = parameters[1][i];
1551 // can only remove a mode they already have
1552 log(DEBUG,"umode %c being removed",parameters[1][i]);
1553 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1555 char umode = parameters[1][i];
1556 log(DEBUG,"umode %c is an allowed umode",umode);
1557 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1563 int v1 = strlen(outpars);
1565 outpars[v1] = parameters[1][i];
1568 for (q = 0; dmodes[q] != 0; q++)
1570 if (dmodes[q] != parameters[1][i])
1574 strlcat(temp,moo,MAXBUF);
1577 strlcpy(dmodes,temp,MAXBUF);
1587 strlcpy(b,"",MAXBUF);
1590 while (i < strlen (outpars))
1592 b[z++] = outpars[i++];
1594 if (i<strlen(outpars)-1)
1596 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1598 // someones playing silly buggers and trying
1599 // to put a +- or -+ into the line...
1603 if (i == strlen(outpars)-1)
1605 if ((outpars[i] == '-') || (outpars[i] == '+'))
1613 if ((b[z] == '-') || (b[z] == '+'))
1616 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1619 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1621 // M token for a usermode must go to all servers
1622 char buffer[MAXBUF];
1623 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1624 NetSendToAll(buffer);
1626 if (strlen(dmodes)>MAXMODES)
1628 dmodes[MAXMODES-1] = '\0';
1630 log(DEBUG,"Stripped mode line");
1631 log(DEBUG,"Line dest is now %s",dmodes);
1632 strlcpy(dest->modes,dmodes,MAXMODES);
1639 Ptr = FindChan(parameters[0]);
1642 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1646 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1652 void merge_mode(char **parameters, int pcnt)
1658 char outpars[MAXBUF];
1660 dest = Find(parameters[0]);
1662 // fix: ChroNiCk found this - we cant use this as debug if its null!
1665 log(DEBUG,"merge_mode on %s",dest->nick);
1668 if ((dest) && (pcnt > 1))
1670 log(DEBUG,"params > 1");
1672 char dmodes[MAXBUF];
1673 strlcpy(dmodes,dest->modes,MAXBUF);
1675 strcpy(outpars,"+");
1678 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1681 for (int i = 0; parameters[1][0] != 0; i++)
1683 if (parameters[1][i] == '+')
1687 int t = strlen(outpars)-1;
1688 if ((outpars[t] == '+') || (outpars[t] == '-'))
1694 strcat(outpars,"+");
1700 if (parameters[1][i] == '-')
1704 int t = strlen(outpars)-1;
1705 if ((outpars[t] == '+') || (outpars[t] == '-'))
1711 strcat(outpars,"-");
1718 log(DEBUG,"begin mode processing entry");
1724 log(DEBUG,"umode %c being added",parameters[1][i]);
1725 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1727 char umode = parameters[1][i];
1728 log(DEBUG,"umode %c is an allowed umode",umode);
1729 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1731 int v1 = strlen(dmodes);
1732 int v2 = strlen(outpars);
1734 dmodes[v1] = parameters[1][i];
1736 outpars[v2] = parameters[1][i];
1742 // can only remove a mode they already have
1743 log(DEBUG,"umode %c being removed",parameters[1][i]);
1744 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1746 char umode = parameters[1][i];
1747 log(DEBUG,"umode %c is an allowed umode",umode);
1748 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1754 int v1 = strlen(outpars);
1756 outpars[v1] = parameters[1][i];
1759 for (q = 0; dmodes[q] != 0; q++)
1761 if (dmodes[q] != parameters[1][i])
1765 strlcat(temp,moo,MAXBUF);
1768 strlcpy(dmodes,temp,MAXBUF);
1781 while (i < strlen (outpars))
1783 b[z++] = outpars[i++];
1785 if (i<strlen(outpars)-1)
1787 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1789 // someones playing silly buggers and trying
1790 // to put a +- or -+ into the line...
1794 if (i == strlen(outpars)-1)
1796 if ((outpars[i] == '-') || (outpars[i] == '+'))
1804 if ((b[z] == '-') || (b[z] == '+'))
1807 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1810 if (strlen(dmodes)>MAXMODES)
1812 dmodes[MAXMODES-1] = '\0';
1814 log(DEBUG,"Stripped mode line");
1815 log(DEBUG,"Line dest is now %s",dmodes);
1816 strlcpy(dest->modes,dmodes,MAXMODES);
1823 Ptr = FindChan(parameters[0]);
1827 strlcpy(s2.nick,ServerName,NICKMAX);
1828 strcpy(s2.modes,"o");
1830 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1835 void merge_mode2(char **parameters, int pcnt, userrec* user)
1841 char outpars[MAXBUF];
1843 dest = Find(parameters[0]);
1845 // fix: ChroNiCk found this - we cant use this as debug if its null!
1848 log(DEBUG,"merge_mode on %s",dest->nick);
1851 if ((dest) && (pcnt > 1))
1853 log(DEBUG,"params > 1");
1855 char dmodes[MAXBUF];
1856 strlcpy(dmodes,dest->modes,MAXBUF);
1858 strcpy(outpars,"+");
1861 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1863 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1864 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1866 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1869 for (int i = 0; parameters[1][i] != 0; i++)
1871 if (parameters[1][i] == '+')
1875 int t = strlen(outpars)-1;
1876 if ((outpars[t] == '+') || (outpars[t] == '-'))
1882 strcat(outpars,"+");
1888 if (parameters[1][i] == '-')
1892 int t = strlen(outpars)-1;
1893 if ((outpars[t] == '+') || (outpars[t] == '-'))
1899 strcat(outpars,"-");
1906 log(DEBUG,"begin mode processing entry");
1912 log(DEBUG,"umode %c being added",parameters[1][i]);
1913 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1915 char umode = parameters[1][i];
1916 log(DEBUG,"umode %c is an allowed umode",umode);
1917 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1919 int v1 = strlen(dmodes);
1920 int v2 = strlen(outpars);
1922 dmodes[v1] = parameters[1][i];
1924 outpars[v2] = parameters[1][i];
1925 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1931 // can only remove a mode they already have
1932 log(DEBUG,"umode %c being removed",parameters[1][i]);
1933 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1935 char umode = parameters[1][i];
1936 log(DEBUG,"umode %c is an allowed umode",umode);
1937 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1943 int v1 = strlen(outpars);
1945 outpars[v1] = parameters[1][i];
1948 for (q = 0; dmodes[q] != 0; q++)
1950 if (dmodes[q] != parameters[1][i])
1954 strlcat(temp,moo,MAXBUF);
1957 strlcpy(dmodes,temp,MAXBUF);
1964 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
1971 while (i < strlen (outpars))
1973 b[z++] = outpars[i++];
1975 if (i<strlen(outpars)-1)
1977 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1979 // someones playing silly buggers and trying
1980 // to put a +- or -+ into the line...
1984 if (i == strlen(outpars)-1)
1986 if ((outpars[i] == '-') || (outpars[i] == '+'))
1994 if ((b[z] == '-') || (b[z] == '+'))
1997 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2001 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2002 log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
2004 if (strlen(dmodes)>MAXMODES)
2006 dmodes[MAXMODES-1] = '\0';
2008 log(DEBUG,"Stripped mode line");
2009 log(DEBUG,"Line dest is now %s",dmodes);
2010 strlcpy(dest->modes,dmodes,MAXMODES);
2017 Ptr = FindChan(parameters[0]);
2020 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2023 if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2027 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);