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 FOREACH_RESULT(OnAddBan(user,chan,dest));
479 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
481 if (!strcasecmp(i->data,dest))
483 // dont allow a user to set the same ban twice
489 strncpy(b.data,dest,MAXBUF);
490 strncpy(b.set_by,user->nick,NICKMAX);
491 chan->bans.push_back(b);
495 char* take_ban(userrec *user,char *dest,chanrec *chan,int status)
497 if ((!user) || (!dest) || (!chan)) {
498 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
502 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
503 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
505 if (!strcasecmp(i->data,dest))
508 FOREACH_RESULT(OnDelBan(user,chan,dest));
518 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
519 // a section further down the chain tidies up the +-+- crap.
520 std::string compress_modes(std::string modes,bool channelmodes)
525 memset(counts,0,sizeof(counts));
526 memset(active,0,sizeof(active));
527 log(DEBUG,"compress_modes: %s",modes.c_str());
528 for (int i = 0; i < modes.length(); i++)
530 if ((modes[i] == '+') || (modes[i] == '-'))
534 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
536 log(DEBUG,"Tidy mode %c",modes[i]);
537 counts[(unsigned int)modes[i]]++;
538 active[(unsigned int)modes[i]] = true;
543 log(DEBUG,"Tidy mode %c",modes[i]);
544 counts[(unsigned int)modes[i]]++;
545 active[(unsigned int)modes[i]] = true;
548 for (int j = 65; j < 127; j++)
550 if ((counts[j] > 1) && (active[j] == true))
553 v[0] = (unsigned char)j;
555 std::string mode_str = v;
556 std::string::size_type pos = modes.find(mode_str);
557 if (pos != std::string::npos)
559 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
560 while (modes.find(mode_str) != std::string::npos)
561 modes.erase(modes.find(mode_str),1);
562 log(DEBUG,"New mode line: %s",modes.c_str());
569 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
572 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
576 char modelist[MAXBUF];
577 char outlist[MAXBUF];
579 char outpars[32][MAXBUF];
585 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
593 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
595 if (MOD_RESULT == ACR_DENY)
598 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
600 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
601 * parameters[2] onwards are parameters for
602 * modes that require them :) */
603 strlcpy(outlist,"+",MAXBUF);
606 log(DEBUG,"process_modes: modelist: %s",modelist);
608 std::string tidied = compress_modes(modelist,true);
609 strlcpy(modelist,tidied.c_str(),MAXBUF);
611 int len = strlen(modelist);
612 while (modelist[len-1] == ' ')
613 modelist[--len] = '\0';
614 for (ptr = 0; ptr < len; ptr++)
619 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
621 char modechar = modelist[ptr];
622 switch (modelist[ptr])
627 int t = strlen(outlist)-1;
628 if ((outlist[t] == '+') || (outlist[t] == '-'))
644 int t = strlen(outlist)-1;
645 if ((outlist[t] == '+') || (outlist[t] == '-'))
659 if ((param >= pcnt)) break;
660 log(DEBUG,"Enough parameters left");
664 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
667 log(DEBUG,"calling give_ops");
668 r = give_ops(user,parameters[param++],chan,status);
675 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
678 log(DEBUG,"calling take_ops");
679 r = take_ops(user,parameters[param++],chan,status);
685 strlcat(outlist,"o",MAXBUF);
686 strlcpy(outpars[pc++],r,MAXBUF);
691 if (((param >= pcnt)) || (!AllowHalfop)) break;
695 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
698 r = give_hops(user,parameters[param++],chan,status);
705 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
708 r = take_hops(user,parameters[param++],chan,status);
714 strlcat(outlist,"h",MAXBUF);
715 strlcpy(outpars[pc++],r,MAXBUF);
721 if ((param >= pcnt)) break;
725 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
728 r = give_voice(user,parameters[param++],chan,status);
735 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
738 r = take_voice(user,parameters[param++],chan,status);
744 strlcat(outlist,"v",MAXBUF);
745 strlcpy(outpars[pc++],r,MAXBUF);
750 if ((param >= pcnt)) break;
754 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
757 r = add_ban(user,parameters[param++],chan,status);
764 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
767 r = take_ban(user,parameters[param++],chan,status);
773 strlcat(outlist,"b",MAXBUF);
774 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
788 if (previously_unset_k)
790 previously_set_k = true;
792 if (!strcmp(chan->key,""))
795 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
800 strlcpy(key,parameters[param++],32);
801 strlcpy(outpars[pc++],key,MAXBUF);
802 strlcpy(chan->key,key,MAXBUF);
810 /* checks on -k are case sensitive and only accurate to the
811 first 32 characters */
812 if (previously_set_k)
814 previously_unset_k = true;
818 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
821 strlcpy(key,parameters[param++],32);
822 /* only allow -k if correct key given */
823 if (!strcmp(chan->key,key))
825 strlcat(outlist,"k",MAXBUF);
826 strlcpy(chan->key,"",MAXBUF);
827 strlcpy(outpars[pc++],key,MAXBUF);
837 if (previously_set_l)
839 previously_unset_l = true;
841 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
852 if ((param >= pcnt)) break;
857 if (previously_unset_l)
859 previously_set_l = true;
860 bool invalid = false;
861 for (int i = 0; i < strlen(parameters[param]); i++)
863 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
868 if (atoi(parameters[param]) < 1)
877 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
881 chan->limit = atoi(parameters[param]);
883 // reported by mech: large values cause underflow
885 chan->limit = 0x7FFFFF;
890 strlcat(outlist,"l",MAXBUF);
891 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
899 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
902 if (chan->inviteonly != mdir)
904 strlcat(outlist,"i",MAXBUF);
906 chan->inviteonly = mdir;
912 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
915 if (chan->topiclock != mdir)
917 strlcat(outlist,"t",MAXBUF);
919 chan->topiclock = mdir;
925 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
928 if (chan->noexternal != mdir)
930 strlcat(outlist,"n",MAXBUF);
932 chan->noexternal = mdir;
938 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
941 if (chan->moderated != mdir)
943 strlcat(outlist,"m",MAXBUF);
945 chan->moderated = mdir;
951 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
954 if (chan->secret != mdir)
962 strlcat(outlist,"-p+",MAXBUF);
966 strlcat(outlist,"+p-",MAXBUF);
976 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
979 if (chan->c_private != mdir)
981 strlcat(outlist,"p",MAXBUF);
987 strlcat(outlist,"-s+",MAXBUF);
991 strlcat(outlist,"+s-",MAXBUF);
995 chan->c_private = mdir;
1000 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
1003 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
1005 if (!ModeIsListMode(modechar,MT_CHANNEL))
1007 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
1011 if (ModeDefined(modechar,MT_CHANNEL))
1013 log(DEBUG,"A module has claimed this mode");
1016 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1018 p.push_back(parameters[param]);
1020 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
1022 p.push_back(parameters[param]);
1025 bool handled = false;
1028 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1029 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
1031 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
1037 // BIG ASS IDIOTIC CODER WARNING!
1038 // Using OnRawMode on another modules mode's behavour
1039 // will confuse the crap out of admins! just because you CAN
1040 // do it, doesnt mean you SHOULD!
1042 std::string para = "";
1045 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
1048 for (int i = 0; i <= MODCOUNT; i++)
1052 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
1055 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1056 char app[] = {modechar, 0};
1057 if (ModeIsListMode(modechar,MT_CHANNEL))
1068 strlcat(outlist, app,MAXBUF);
1070 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1077 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1079 strlcat(outlist, app,MAXBUF);
1081 else if (!strchr(outlist,modechar))
1083 strlcat(outlist, app,MAXBUF);
1086 chan->SetCustomMode(modechar,mdir);
1087 // include parameters in output if mode has them
1088 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1090 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1091 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1094 // break, because only one module can handle the mode.
1103 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1111 /* this ensures only the *valid* modes are sent out onto the network */
1112 int xt = strlen(outlist)-1;
1113 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1116 xt = strlen(outlist)-1;
1120 strlcpy(outstr,outlist,MAXBUF);
1121 for (ptr = 0; ptr < pc; ptr++)
1123 strlcat(outstr," ",MAXBUF);
1124 strlcat(outstr,outpars[ptr],MAXBUF);
1128 log(DEBUG,"Local mode change");
1129 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1137 WriteChannelWithServ(ServerName,chan,"MODE %s %s",chan->name,outstr);
1138 // M token for a usermode must go to all servers
1139 char buffer[MAXBUF];
1140 snprintf(buffer,MAXBUF,"M %s %s",chan->name, outstr);
1141 NetSendToAll(buffer);
1149 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1150 // M token for a usermode must go to all servers
1151 char buffer[MAXBUF];
1152 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
1153 NetSendToAll(buffer);
1160 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1162 bool allowed_umode(char umode, char* sourcemodes,bool adding)
1164 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1165 // RFC1459 specified modes
1166 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1168 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1172 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1173 if ((strchr(sourcemodes,'o')) && (!adding))
1175 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1178 else if (umode == 'o')
1180 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1184 // process any module-defined modes that need oper
1185 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1187 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1191 if (ModeDefined(umode,MT_CLIENT))
1193 // process any module-defined modes that don't need oper
1194 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1195 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1197 // no, this mode needs oper, and this user 'aint got what it takes!
1203 // anything else - return false.
1204 log(DEBUG,"umode %c not known by any ruleset",umode);
1208 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
1215 strlcpy(s2->nick,ServerName,NICKMAX);
1216 strcpy(s2->modes,"o");
1223 if (ModeDefined(umode,MT_CLIENT))
1225 for (int i = 0; i <= MODCOUNT; i++)
1227 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1229 log(DEBUG,"Module %s claims umode %c",module_names[i].c_str(),umode);
1233 log(DEBUG,"No module claims umode %c",umode);
1252 void handle_mode(char **parameters, int pcnt, userrec *user)
1258 char outpars[MAXBUF];
1260 dest = Find(parameters[0]);
1267 if ((dest) && (pcnt == 1))
1269 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1273 if ((dest) && (pcnt > 1))
1275 std::string tidied = compress_modes(parameters[1],false);
1276 parameters[1] = (char*)tidied.c_str();
1278 char dmodes[MAXBUF];
1279 strlcpy(dmodes,dest->modes,MAXBUF);
1280 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1285 if (strchr(user->modes,'o'))
1296 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1300 strcpy(outpars,"+");
1303 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1306 for (int i = 0; i < strlen(parameters[1]); i++)
1308 if (parameters[1][i] == ' ')
1310 if (parameters[1][i] == '+')
1314 int t = strlen(outpars)-1;
1315 if ((outpars[t] == '+') || (outpars[t] == '-'))
1321 strcat(outpars,"+");
1327 if (parameters[1][i] == '-')
1331 int t = strlen(outpars)-1;
1332 if ((outpars[t] == '+') || (outpars[t] == '-'))
1338 strcat(outpars,"-");
1346 if (strchr(user->modes,'o'))
1352 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
1361 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1363 char umode = parameters[1][i];
1364 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1366 int q = strlen(dmodes);
1367 int r = strlen(outpars);
1369 dmodes[q] = parameters[1][i];
1371 outpars[r] = parameters[1][i];
1372 if (parameters[1][i] == 'o')
1374 FOREACH_MOD OnGlobalOper(dest);
1381 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1383 char umode = parameters[1][i];
1384 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1390 int r = strlen(outpars);
1392 outpars[r] = parameters[1][i];
1395 for (q = 0; q < strlen(dmodes); q++)
1397 if (dmodes[q] != parameters[1][i])
1401 strlcat(temp,moo,MAXBUF);
1404 strlcpy(dmodes,temp,MAXBUF);
1417 strlcpy(b,"",MAXBUF);
1420 while (i < strlen (outpars))
1422 b[z++] = outpars[i++];
1424 if (i<strlen(outpars)-1)
1426 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1428 // someones playing silly buggers and trying
1429 // to put a +- or -+ into the line...
1433 if (i == strlen(outpars)-1)
1435 if ((outpars[i] == '-') || (outpars[i] == '+'))
1443 if ((b[z] == '-') || (b[z] == '+'))
1446 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1451 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1452 // M token for a usermode must go to all servers
1453 char buffer[MAXBUF];
1454 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1455 NetSendToAll(buffer);
1458 if (strlen(dmodes)>MAXMODES)
1460 dmodes[MAXMODES-1] = '\0';
1462 log(DEBUG,"Stripped mode line");
1463 log(DEBUG,"Line dest is now %s",dmodes);
1464 strlcpy(dest->modes,dmodes,MAXMODES);
1471 Ptr = FindChan(parameters[0]);
1476 /* just /modes #channel */
1477 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1478 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1484 char* mode = parameters[1];
1488 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1494 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1496 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1498 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1501 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1503 // list of items for an extmode
1504 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1505 FOREACH_MOD OnSendList(user,Ptr,*mode);
1511 if ((Ptr) && (!has_channel(user,Ptr)))
1513 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1520 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1522 if (MOD_RESULT == ACR_DENY)
1524 if (MOD_RESULT == ACR_DEFAULT)
1526 if (cstatus(user,Ptr) < STATUS_HOP)
1528 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1533 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1538 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1545 void server_mode(char **parameters, int pcnt, userrec *user)
1551 char outpars[MAXBUF];
1553 dest = Find(parameters[0]);
1555 // fix: ChroNiCk found this - we cant use this as debug if its null!
1558 log(DEBUG,"server_mode on %s",dest->nick);
1561 if ((dest) && (pcnt > 1))
1563 std::string tidied = compress_modes(parameters[1],false);
1564 parameters[1] = (char*)tidied.c_str();
1566 char dmodes[MAXBUF];
1567 strlcpy(dmodes,dest->modes,MAXBUF);
1569 strcpy(outpars,"+");
1572 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1575 for (int i = 0; i < strlen(parameters[1]); i++)
1577 if (parameters[1][i] == ' ')
1579 if (parameters[1][i] == '+')
1583 int t = strlen(outpars)-1;
1584 if ((outpars[t] == '+') || (outpars[t] == '-'))
1590 strcat(outpars,"+");
1596 if (parameters[1][i] == '-')
1600 int t = strlen(outpars)-1;
1601 if ((outpars[t] == '+') || (outpars[t] == '-'))
1607 strcat(outpars,"-");
1614 log(DEBUG,"begin mode processing entry");
1620 log(DEBUG,"umode %c being added",parameters[1][i]);
1621 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
1623 char umode = parameters[1][i];
1624 log(DEBUG,"umode %c is an allowed umode",umode);
1625 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1627 int v1 = strlen(dmodes);
1628 int v2 = strlen(outpars);
1630 dmodes[v1] = parameters[1][i];
1632 outpars[v2] = parameters[1][i];
1638 // can only remove a mode they already have
1639 log(DEBUG,"umode %c being removed",parameters[1][i]);
1640 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
1642 char umode = parameters[1][i];
1643 log(DEBUG,"umode %c is an allowed umode",umode);
1644 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1650 int v1 = strlen(outpars);
1652 outpars[v1] = parameters[1][i];
1655 for (q = 0; q < strlen(dmodes); q++)
1657 if (dmodes[q] != parameters[1][i])
1661 strlcat(temp,moo,MAXBUF);
1664 strlcpy(dmodes,temp,MAXBUF);
1674 strlcpy(b,"",MAXBUF);
1677 while (i < strlen (outpars))
1679 b[z++] = outpars[i++];
1681 if (i<strlen(outpars)-1)
1683 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1685 // someones playing silly buggers and trying
1686 // to put a +- or -+ into the line...
1690 if (i == strlen(outpars)-1)
1692 if ((outpars[i] == '-') || (outpars[i] == '+'))
1700 if ((b[z] == '-') || (b[z] == '+'))
1703 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1708 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1709 // M token for a usermode must go to all servers
1710 char buffer[MAXBUF];
1711 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
1712 NetSendToAll(buffer);
1715 if (strlen(dmodes)>MAXMODES)
1717 dmodes[MAXMODES-1] = '\0';
1719 log(DEBUG,"Stripped mode line");
1720 log(DEBUG,"Line dest is now %s",dmodes);
1721 strlcpy(dest->modes,dmodes,MAXMODES);
1728 Ptr = FindChan(parameters[0]);
1731 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1735 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1741 void merge_mode(char **parameters, int pcnt)
1747 char outpars[MAXBUF];
1749 dest = Find(parameters[0]);
1751 // fix: ChroNiCk found this - we cant use this as debug if its null!
1754 log(DEBUG,"merge_mode on %s",dest->nick);
1757 if ((dest) && (pcnt > 1))
1759 std::string tidied = compress_modes(parameters[1],false);
1760 parameters[1] = (char*)tidied.c_str();
1762 char dmodes[MAXBUF];
1763 strlcpy(dmodes,dest->modes,MAXBUF);
1765 strcpy(outpars,"+");
1768 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1771 for (int i = 0; i < strlen(parameters[1]); i++)
1773 if (parameters[1][i] == ' ')
1775 if (parameters[1][i] == '+')
1779 int t = strlen(outpars)-1;
1780 if ((outpars[t] == '+') || (outpars[t] == '-'))
1786 strcat(outpars,"+");
1792 if (parameters[1][i] == '-')
1796 int t = strlen(outpars)-1;
1797 if ((outpars[t] == '+') || (outpars[t] == '-'))
1803 strcat(outpars,"-");
1810 log(DEBUG,"begin mode processing entry");
1816 log(DEBUG,"umode %c being added",parameters[1][i]);
1817 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
1819 char umode = parameters[1][i];
1820 log(DEBUG,"umode %c is an allowed umode",umode);
1821 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1823 int v1 = strlen(dmodes);
1824 int v2 = strlen(outpars);
1826 dmodes[v1] = parameters[1][i];
1828 outpars[v2] = parameters[1][i];
1834 // can only remove a mode they already have
1835 log(DEBUG,"umode %c being removed",parameters[1][i]);
1836 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
1838 char umode = parameters[1][i];
1839 log(DEBUG,"umode %c is an allowed umode",umode);
1840 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1846 int v1 = strlen(outpars);
1848 outpars[v1] = parameters[1][i];
1851 for (q = 0; q < strlen(dmodes); q++)
1853 if (dmodes[q] != parameters[1][i])
1857 strlcat(temp,moo,MAXBUF);
1860 strlcpy(dmodes,temp,MAXBUF);
1873 while (i < strlen (outpars))
1875 b[z++] = outpars[i++];
1877 if (i<strlen(outpars)-1)
1879 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1881 // someones playing silly buggers and trying
1882 // to put a +- or -+ into the line...
1886 if (i == strlen(outpars)-1)
1888 if ((outpars[i] == '-') || (outpars[i] == '+'))
1896 if ((b[z] == '-') || (b[z] == '+'))
1899 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
1902 if (strlen(dmodes)>MAXMODES)
1904 dmodes[MAXMODES-1] = '\0';
1906 log(DEBUG,"Stripped mode line");
1907 log(DEBUG,"Line dest is now %s",dmodes);
1908 strlcpy(dest->modes,dmodes,MAXMODES);
1915 Ptr = FindChan(parameters[0]);
1919 strlcpy(s2.nick,ServerName,NICKMAX);
1920 strcpy(s2.modes,"o");
1922 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
1927 void merge_mode2(char **parameters, int pcnt, userrec* user)
1933 char outpars[MAXBUF];
1935 dest = Find(parameters[0]);
1937 // fix: ChroNiCk found this - we cant use this as debug if its null!
1940 log(DEBUG,"merge_mode2 on %s",dest->nick);
1943 if ((dest) && (pcnt > 1))
1945 std::string tidied = compress_modes(parameters[1],false);
1946 parameters[1] = (char*)tidied.c_str();
1948 char dmodes[MAXBUF];
1949 strlcpy(dmodes,dest->modes,MAXBUF);
1951 strcpy(outpars,"+");
1954 if ((parameters[1][0] == ':') && (strlen(parameters[1])>1))
1956 // some stupid 3rd party things (such as services packages) put a colon on the mode list...
1957 log(DEBUG,"Some muppet put a colon on the modelist! changed to '%s'",++parameters[1]);
1959 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1962 for (int i = 0; i < strlen(parameters[1]); i++)
1964 if (parameters[1][i] == ' ')
1966 if (parameters[1][i] == '+')
1970 int t = strlen(outpars)-1;
1971 if ((outpars[t] == '+') || (outpars[t] == '-'))
1977 strcat(outpars,"+");
1983 if (parameters[1][i] == '-')
1987 int t = strlen(outpars)-1;
1988 if ((outpars[t] == '+') || (outpars[t] == '-'))
1994 strcat(outpars,"-");
2001 log(DEBUG,"begin mode processing entry");
2007 log(DEBUG,"umode %c being added",parameters[1][i]);
2008 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
2010 char umode = parameters[1][i];
2011 log(DEBUG,"umode %c is an allowed umode",umode);
2012 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2014 int v1 = strlen(dmodes);
2015 int v2 = strlen(outpars);
2017 dmodes[v1] = parameters[1][i];
2019 outpars[v2] = parameters[1][i];
2020 log(DEBUG,"OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2026 // can only remove a mode they already have
2027 log(DEBUG,"umode %c being removed",parameters[1][i]);
2028 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
2030 char umode = parameters[1][i];
2031 log(DEBUG,"umode %c is an allowed umode",umode);
2032 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2038 int v1 = strlen(outpars);
2040 outpars[v1] = parameters[1][i];
2043 for (q = 0; q < strlen(dmodes); q++)
2045 if (dmodes[q] != parameters[1][i])
2049 strlcat(temp,moo,MAXBUF);
2052 strlcpy(dmodes,temp,MAXBUF);
2059 log(DEBUG,"DONE! OUTPARS='%s', DMODES='%s'",outpars,dmodes);
2066 while (i < strlen (outpars))
2068 b[z++] = outpars[i++];
2070 if (i<strlen(outpars)-1)
2072 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2074 // someones playing silly buggers and trying
2075 // to put a +- or -+ into the line...
2079 if (i == strlen(outpars)-1)
2081 if ((outpars[i] == '-') || (outpars[i] == '+'))
2089 if ((b[z] == '-') || (b[z] == '+'))
2093 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
2098 WriteTo(user,dest,"MODE %s :%s",dest->nick,b);
2099 log(DEBUG,"Sent: :%s MODE %s",user->nick,b);
2102 if (strlen(dmodes)>MAXMODES)
2104 dmodes[MAXMODES-1] = '\0';
2106 log(DEBUG,"Stripped mode line");
2107 log(DEBUG,"Line dest is now %s",dmodes);
2108 strlcpy(dest->modes,dmodes,MAXMODES);
2114 Ptr = FindChan(parameters[0]);
2117 log(DEBUG,"merge_mode2: found channel %s",Ptr->name);
2120 if ((cstatus(user,Ptr) < STATUS_HOP) && (!is_uline(user->server)))
2124 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,true);