-
- switch (*modechar)
- {
- case '-':
- *outl++ = '-';
- mdir = 0;
- break;
-
- case '+':
- *outl++ = '+';
- mdir = 1;
- break;
-
- case 'o':
- log(DEBUG,"Ops");
- if ((param >= pcnt)) break;
- log(DEBUG,"Enough parameters left");
- r = NULL;
- if (mdir == 1)
- {
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
- if (!MOD_RESULT)
- {
- r = GiveOps(user,parameters[param++],chan,status);
- }
- else param++;
- }
- else
- {
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
- if (!MOD_RESULT)
- {
- r = TakeOps(user,parameters[param++],chan,status);
- }
- else param++;
- }
- if (r)
- {
- *outl++ = 'o';
- outpars[pc++] = r;
- }
- break;
-
- case 'h':
- if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
- r = NULL;
- if (mdir == 1)
- {
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
- if (!MOD_RESULT)
- {
- r = GiveHops(user,parameters[param++],chan,status);
- }
- else param++;
- }
- else
- {
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
- if (!MOD_RESULT)
- {
- r = TakeHops(user,parameters[param++],chan,status);
- }
- else param++;
- }
- if (r)
- {
- *outl++ = 'h';
- outpars[pc++] = r;
- }
- break;
-
-
- case 'v':
- if ((param >= pcnt)) break;
- r = NULL;
- if (mdir == 1)
- {
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
- if (!MOD_RESULT)
- {
- r = GiveVoice(user,parameters[param++],chan,status);
- }
- else param++;
- }
- else
- {
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
- if (!MOD_RESULT)
- {
- r = TakeVoice(user,parameters[param++],chan,status);
- }
- else param++;
- }
- if (r)
- {
- *outl++ = 'v';
- outpars[pc++] = r;
- }
- break;
-
- case 'b':
- if ((param >= pcnt)) break;
- r = NULL;
- if (mdir == 1)
- {
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
- if (!MOD_RESULT)
- {
- r = AddBan(user,parameters[param++],chan,status);
- }
- else param++;
- }
- else
- {
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
- if (!MOD_RESULT)
- {
- r = TakeBan(user,parameters[param++],chan,status);
- }
- else param++;
- }
- if (r)
- {
- *outl++ = 'b';
- outpars[pc++] = parameters[param-1];
- }
- break;
-
-
- case 'k':
- if ((param >= pcnt))
- break;
-
- if (mdir == 1)
- {
- if (k_set)
- break;
-
- if (previously_unset_k)
- break;
- previously_set_k = true;
-
- if (!chan->modes[CM_KEY])
- {
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
- if (!MOD_RESULT)
- {
- *outl++ = 'k';
- char key[MAXBUF];
- strlcpy(key,parameters[param++],32);
- outpars[pc++] = key;
- strlcpy(chan->key,key,MAXBUF);
- chan->modes[CM_KEY] = 1;
- k_set = true;
- }
- else param++;
- }
- }
- else
- {
- /* checks on -k are case sensitive and only accurate to the
- first 32 characters */
- if (previously_set_k)
- break;
- previously_unset_k = true;
-
- char key[MAXBUF];
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
- if (!MOD_RESULT)
- {
- strlcpy(key,parameters[param++],32);
- /* only allow -k if correct key given */
- if (!strcmp(chan->key,key))
- {
- *outl++ = 'k';
- *chan->key = 0;
- chan->modes[CM_KEY] = 0;
- outpars[pc++] = key;
- }
- }
- else param++;
- }
- break;
-
- case 'l':
- if (mdir == 0)
- {
- if (previously_set_l)
- break;
- previously_unset_l = true;
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
- if (!MOD_RESULT)
- {
- if (chan->modes[CM_LIMIT])
- {
- *outl++ = 'l';
- chan->limit = 0;
- chan->modes[CM_LIMIT] = 0;
- }
- }
- }
-
- if ((param >= pcnt)) break;
- if (mdir == 1)
- {
- if (l_set)
- break;
- if (previously_unset_l)
- break;
- previously_set_l = true;
- bool invalid = false;
- for (char* f = parameters[param]; *f; f++)
- {
- if ((*f < '0') || (*f > '9'))
- {
- invalid = true;
- }
- }
- /* If the limit is < 1, or the new limit is the current limit, dont allow */
- if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
- {
- invalid = true;
- }
-
- if (invalid)
- break;
-
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
- if (!MOD_RESULT)
- {
-
- chan->limit = atoi(parameters[param]);
-
- // reported by mech: large values cause underflow
- if (chan->limit < 0)
- chan->limit = 0x7FFF;
- }
-
- if (chan->limit)
- {
- *outl++ = 'l';
- chan->modes[CM_LIMIT] = 1;
- outpars[pc++] = parameters[param++];
- l_set = true;
- }
- }
- break;
-
- case 'i':
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
- if (!MOD_RESULT)
- {
- if (mdir)
- {
- if (!(chan->modes[CM_INVITEONLY])) *outl++ = 'i';
- chan->modes[CM_INVITEONLY] = 1;
- }
- else
- {
- if (chan->modes[CM_INVITEONLY]) *outl++ = 'i';
- chan->modes[CM_INVITEONLY] = 0;
- }
- }
- break;
-
- case 't':
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
- if (!MOD_RESULT)
- {
- if (mdir)
- {
- if (!(chan->modes[CM_TOPICLOCK])) *outl++ = 't';
- chan->modes[CM_TOPICLOCK] = 1;
- }
- else
- {
- if (chan->modes[CM_TOPICLOCK]) *outl++ = 't';
- chan->modes[CM_TOPICLOCK] = 0;
- }
- }
- break;
-
- case 'n':
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
- if (!MOD_RESULT)
- {
- if (mdir)
- {
- if (!(chan->modes[CM_NOEXTERNAL])) *outl++ = 'n';
- chan->modes[CM_NOEXTERNAL] = 1;
- }
- else
- {
- if (chan->modes[CM_NOEXTERNAL]) *outl++ = 'n';
- chan->modes[CM_NOEXTERNAL] = 0;
- }
- }
- break;
-
- case 'm':
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
- if (!MOD_RESULT)
- {
- if (mdir)
- {
- if (!(chan->modes[CM_MODERATED])) *outl++ = 'm';
- chan->modes[CM_MODERATED] = 1;
- }
- else
- {
- if (chan->modes[CM_MODERATED]) *outl++ = 'm';
- chan->modes[CM_MODERATED] = 0;
- }
- }
- break;
-
- case 's':
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
- if (!MOD_RESULT)
- {
- if (mdir)
- {
- if (!(chan->modes[CM_SECRET])) *outl++ = 's';
- chan->modes[CM_SECRET] = 1;
- if (chan->modes[CM_PRIVATE])
- {
- chan->modes[CM_PRIVATE] = 0;
- if (mdir)
- {
- *outl++ = '-'; *outl++ = 'p'; *outl++ = '+';
- }
- }
- }
- else
- {
- if (chan->modes[CM_SECRET]) *outl++ = 's';
- chan->modes[CM_SECRET] = 0;
- }
- }
- break;
-
- case 'p':
- MOD_RESULT = 0;
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
- if(!MOD_RESULT)
- {
- if(mdir)
- {
- if(!(chan->modes[CM_PRIVATE]))
- *outl++ = 'p';
-
- chan->modes[CM_PRIVATE] = 1;
-
- if(chan->modes[CM_SECRET])
- {
- chan->modes[CM_SECRET] = 0;
-
- *outl++ = '-';
- *outl++ = 's';
- *outl++ = '+';
- }
- }
- else
- {
- if(chan->modes[CM_PRIVATE])
- *outl++ = 'p';
-
- chan->modes[CM_PRIVATE] = 0;
- }
- }
- break;
-
- default:
- string_list p;
- p.clear();
- bool x = chan->modes[*modechar-65];
- if ((!x && !mdir) || (x && mdir))
- {
- if (!ModeIsListMode(*modechar,MT_CHANNEL))
- {
- log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
- break;
- }
- }
- if (ModeDefined(*modechar,MT_CHANNEL))
- {
- /* A module has claimed this mode */
- if (param<pcnt)
- {
- if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
- {
- p.push_back(parameters[param]);
- }
- if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
- {
- p.push_back(parameters[param]);
- }
- }
- bool handled = false;
- if (param>=pcnt)
- {
- // we're supposed to have a parameter, but none was given... so dont handle the mode.
- if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))
- {
- log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
- handled = true;
- param++;
- }
- }
- // BIG ASS IDIOTIC CODER WARNING!
- // Using OnRawMode on another modules mode's behavour
- // will confuse the crap out of admins! just because you CAN
- // do it, doesnt mean you SHOULD!
- MOD_RESULT = 0;
- std::string para = "";
- if (p.size())
- para = p[0];
-
- FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
- if(!MOD_RESULT)
- {
- for (int i = 0; i <= MODCOUNT; i++)
- {
- if (!handled)
- {
- int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
- if (t != 0)
- {
- log(DEBUG,"OnExtendedMode returned nonzero for a module");
- if (ModeIsListMode(*modechar,MT_CHANNEL))
- {
- if (t == -1)
- {
- //pc++;
- param++;
- }
- else
- {
- if (param < pcnt)
- {
- *outl++ = *modechar;
- }
- outpars[pc++] = parameters[param++];
- }
- }
- else
- {
- *outl++ = *modechar;
- chan->SetCustomMode(*modechar,mdir);
- // include parameters in output if mode has them
- if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
- {
- if (param < pcnt)
- {
- chan->SetCustomModeParam(*modechar,parameters[param],mdir);
- outpars[pc++] = parameters[param++];
- }
- }
- }
- // break, because only one module can handle the mode.
- handled = true;
- }
- }
- }
- }
- }
- else
- {
- WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
- }
- break;
- }
- }
-
- /* Null terminate it now we're done */
- *outl = 0;
-
-
- /************ Fast, but confusing string tidying ************/
- outl = outlist;
- while (*outl && (*outl < 'A'))
- outl++;
- /* outl now points to the first mode character after +'s and -'s */
- outl--;
- /* Now points at first mode-modifier + or - symbol */
- char* trim = outl;
- /* Now we tidy off any trailing -'s etc */
- while (*trim++);
- trim--;
- while ((*--trim == '+') || (*trim == '-'))
- *trim = 0;
- /************ Done wih the string tidy functions ************/
-
-
- /* The mode change must be at least two characters long (+ or - and at least one mode) */
- if (((*outl == '+') || (*outl == '-')) && *(outl+1))
- {
- for (ptr = 0; ptr < pc; ptr++)
- {
- charlcat(outl,' ',MAXBUF);
- strlcat(outl,outpars[ptr],MAXBUF-1);
- }
- if (local)
- {
- log(DEBUG,"Local mode change");
- WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outl);
- FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
- }
- else
- {
- if (servermode)
- {
- if (!silent)
- {
- WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outl);
- }
-
- }
- else
- {
- if (!silent)
- {
- WriteChannel(chan,user,"MODE %s %s",chan->name,outl);
- FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
- }
- }
- }
- }