1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 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 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
23 #include <sys/errno.h>
27 #include <ext/hash_map>
35 #include "connection.h"
45 #include "inspstring.h"
46 #include "helperfuncs.h"
50 extern std::vector<Module*> modules;
51 extern std::vector<ircd_module*> factory;
52 extern InspIRCd* ServerInstance;
53 extern ServerConfig* Config;
57 userrec* ModeParser::SanityChecks(userrec *user,char *dest,chanrec *chan,int status)
60 if ((!user) || (!dest) || (!chan) || (!*dest))
67 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
73 char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
75 for (unsigned int i = 0; i < d->chans.size(); i++)
77 if ((d->chans[i].channel != NULL) && (chan != NULL))
78 if (d->chans[i].channel == chan)
80 if (d->chans[i].uc_modes & MASK)
84 d->chans[i].uc_modes = d->chans[i].uc_modes | MASK;
88 d->chans[i].channel->AddOppedUser((char*)d);
91 d->chans[i].channel->AddHalfoppedUser((char*)d);
94 d->chans[i].channel->AddVoicedUser((char*)d);
97 log(DEBUG,"grant: %s %s",d->chans[i].channel->name,d->nick);
104 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
106 for (unsigned int i = 0; i < d->chans.size(); i++)
108 if ((d->chans[i].channel != NULL) && (chan != NULL))
109 if (d->chans[i].channel == chan)
111 if ((d->chans[i].uc_modes & MASK) == 0)
115 d->chans[i].uc_modes ^= MASK;
119 d->chans[i].channel->DelOppedUser((char*)d);
122 d->chans[i].channel->DelHalfoppedUser((char*)d);
125 d->chans[i].channel->DelVoicedUser((char*)d);
128 log(DEBUG,"revoke: %s %s",d->chans[i].channel->name,d->nick);
135 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
137 userrec *d = this->SanityChecks(user,dest,chan,status);
141 if (user->server == d->server)
144 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
146 if (MOD_RESULT == ACR_DENY)
148 if (MOD_RESULT == ACR_DEFAULT)
150 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
152 log(DEBUG,"%s cant give ops to %s because they have status %d and needs %d",user->nick,dest,status,STATUS_OP);
153 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
159 return this->Grant(d,chan,UCMODE_OP);
164 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
166 userrec *d = this->SanityChecks(user,dest,chan,status);
170 if (user->server == d->server)
173 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
175 if (MOD_RESULT == ACR_DENY)
177 if (MOD_RESULT == ACR_DEFAULT)
179 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
181 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
187 return this->Grant(d,chan,UCMODE_HOP);
192 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
194 userrec *d = this->SanityChecks(user,dest,chan,status);
198 if (user->server == d->server)
201 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
203 if (MOD_RESULT == ACR_DENY)
205 if (MOD_RESULT == ACR_DEFAULT)
207 if ((status < STATUS_HOP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
209 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
215 return this->Grant(d,chan,UCMODE_VOICE);
220 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
222 userrec *d = this->SanityChecks(user,dest,chan,status);
226 if (user->server == d->server)
229 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
231 if (MOD_RESULT == ACR_DENY)
233 if (MOD_RESULT == ACR_DEFAULT)
235 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)) && (IS_LOCAL(user)))
237 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
243 return this->Revoke(d,chan,UCMODE_OP);
248 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
250 userrec *d = this->SanityChecks(user,dest,chan,status);
254 if (user->server == d->server)
257 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
259 if (MOD_RESULT == ACR_DENY)
261 if (MOD_RESULT == ACR_DEFAULT)
263 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
264 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))) && (IS_LOCAL(user)))
266 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
272 return this->Revoke(d,chan,UCMODE_HOP);
277 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
279 userrec *d = this->SanityChecks(user,dest,chan,status);
283 if (user->server == d->server)
286 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
288 if (MOD_RESULT == ACR_DENY)
290 if (MOD_RESULT == ACR_DEFAULT)
292 if ((status < STATUS_HOP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
294 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
300 return this->Revoke(d,chan,UCMODE_VOICE);
305 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
308 int toomanyexclamation = 0;
311 if ((!user) || (!dest) || (!chan) || (!*dest))
313 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
317 for (char* i = dest; *i; i++)
319 if ((*i < 32) || (*i > 126))
325 toomanyexclamation++;
333 if (toomanyexclamation != 1 || toomanyat != 1)
335 * this stops sillyness like n!u!u!u@h, though note that most
336 * ircds don't actually verify banmask validity. --w00t
340 long maxbans = GetMaxBans(chan->name);
341 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
343 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);
347 log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
350 FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
355 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
357 if (!strcasecmp(i->data,dest))
359 // dont allow a user to set the same ban twice
365 strlcpy(b.data,dest,MAXBUF);
368 strlcpy(b.set_by,user->nick,NICKMAX-1);
372 strlcpy(b.set_by,Config->ServerName,NICKMAX-1);
374 chan->bans.push_back(b);
378 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
380 if ((!user) || (!dest) || (!chan) || (!*dest)) {
381 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
385 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
386 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
388 if (!strcasecmp(i->data,dest))
391 FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
401 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
402 // a section further down the chain tidies up the +-+- crap.
403 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
407 memset(counts,0,sizeof(counts));
408 memset(active,0,sizeof(active));
409 for (unsigned int i = 0; i < modes.length(); i++)
411 if ((modes[i] == '+') || (modes[i] == '-'))
415 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
417 log(DEBUG,"Tidy mode %c",modes[i]);
418 counts[(unsigned int)modes[i]]++;
419 active[(unsigned int)modes[i]] = true;
424 log(DEBUG,"Tidy mode %c",modes[i]);
425 counts[(unsigned int)modes[i]]++;
426 active[(unsigned int)modes[i]] = true;
429 for (int j = 65; j < 127; j++)
431 if ((counts[j] > 1) && (active[j] == true))
434 v[0] = (unsigned char)j;
436 std::string mode_str = v;
437 std::string::size_type pos = modes.find(mode_str);
438 if (pos != std::string::npos)
440 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
441 while (modes.find(mode_str) != std::string::npos)
442 modes.erase(modes.find(mode_str),1);
443 log(DEBUG,"New mode line: %s",modes.c_str());
450 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
453 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
457 char outlist[MAXBUF];
464 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
475 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
476 if (MOD_RESULT == ACR_DENY)
480 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
482 char* modelist = parameters[1]; /* mode list, e.g. +oo-o *
483 * parameters[2] onwards are parameters for
484 * modes that require them :) */
485 *outlist = *modelist;
486 char* outl = outlist+1;
488 mdir = (*modelist == '+');
490 log(DEBUG,"process_modes: modelist: %s",modelist);
492 std::string tidied = this->CompressModes(modelist,true);
493 strlcpy(modelist,tidied.c_str(),MAXBUF);
495 int len = tidied.length();
496 while (modelist[len-1] == ' ')
497 modelist[--len] = '\0';
501 for (modechar = (modelist + 1); *modechar; ptr++, modechar++)
505 /* If we have more than MAXMODES changes in one line,
506 * drop all after the MAXMODES
511 log(DEBUG,"Mode %c",*modechar);
528 if ((param >= pcnt)) break;
529 log(DEBUG,"Enough parameters left");
534 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
537 log(DEBUG,"calling GiveOps");
538 r = GiveOps(user,parameters[param++],chan,status);
545 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
548 log(DEBUG,"calling TakeOps");
549 r = TakeOps(user,parameters[param++],chan,status);
561 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
566 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
569 r = GiveHops(user,parameters[param++],chan,status);
576 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
579 r = TakeHops(user,parameters[param++],chan,status);
592 if ((param >= pcnt)) break;
597 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
600 r = GiveVoice(user,parameters[param++],chan,status);
607 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
610 r = TakeVoice(user,parameters[param++],chan,status);
622 if ((param >= pcnt)) break;
627 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
630 r = AddBan(user,parameters[param++],chan,status);
637 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
640 r = TakeBan(user,parameters[param++],chan,status);
647 outpars[pc++] = parameters[param-1];
661 if (previously_unset_k)
663 previously_set_k = true;
668 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
673 strlcpy(key,parameters[param++],32);
675 strlcpy(chan->key,key,MAXBUF);
683 /* checks on -k are case sensitive and only accurate to the
684 first 32 characters */
685 if (previously_set_k)
687 previously_unset_k = true;
691 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
694 strlcpy(key,parameters[param++],32);
695 /* only allow -k if correct key given */
696 if (!strcmp(chan->key,key))
710 if (previously_set_l)
712 previously_unset_l = true;
714 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
725 if ((param >= pcnt)) break;
730 if (previously_unset_l)
732 previously_set_l = true;
733 bool invalid = false;
734 for (char* f = parameters[param]; *f; f++)
736 if ((*f < '0') || (*f > '9'))
741 /* If the limit is < 1, or the new limit is the current limit, dont allow */
742 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
751 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
755 chan->limit = atoi(parameters[param]);
757 // reported by mech: large values cause underflow
759 chan->limit = 0x7FFF;
765 outpars[pc++] = parameters[param++];
773 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
778 if (!(chan->binarymodes & CM_INVITEONLY)) *outl++ = 'i';
779 chan->binarymodes |= CM_INVITEONLY;
783 if (chan->binarymodes & CM_INVITEONLY) *outl++ = 'i';
784 chan->binarymodes &= ~CM_INVITEONLY;
791 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
796 if (!(chan->binarymodes & CM_TOPICLOCK)) *outl++ = 't';
797 chan->binarymodes |= CM_TOPICLOCK;
801 if (chan->binarymodes & CM_TOPICLOCK) *outl++ = 't';
802 chan->binarymodes &= ~CM_TOPICLOCK;
809 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
814 if (!(chan->binarymodes & CM_NOEXTERNAL)) *outl++ = 'n';
815 chan->binarymodes |= CM_NOEXTERNAL;
819 if (chan->binarymodes & CM_NOEXTERNAL) *outl++ = 'n';
820 chan->binarymodes &= ~CM_NOEXTERNAL;
827 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
832 if (!(chan->binarymodes & CM_MODERATED)) *outl++ = 'm';
833 chan->binarymodes |= CM_MODERATED;
837 if (chan->binarymodes & CM_MODERATED) *outl++ = 'm';
838 chan->binarymodes &= ~CM_MODERATED;
845 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
850 if (!(chan->binarymodes & CM_SECRET)) *outl++ = 's';
851 chan->binarymodes |= CM_SECRET;
852 if (chan->binarymodes & CM_PRIVATE)
854 chan->binarymodes &= ~CM_PRIVATE;
857 *outl++ = '-'; *outl++ = 'p'; *outl++ = '+';
863 if (chan->binarymodes & CM_SECRET) *outl++ = 's';
864 chan->binarymodes &= ~CM_SECRET;
871 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
876 if (!(chan->binarymodes & CM_PRIVATE)) *outl++ = 'p';
877 chan->binarymodes |= CM_PRIVATE;
878 if (chan->binarymodes & CM_SECRET)
880 chan->binarymodes &= ~CM_SECRET;
883 *outl++ = '-'; *outl++ = 's'; *outl++ = '+';
889 if (chan->binarymodes & CM_PRIVATE) *outl++ = 'p';
890 chan->binarymodes &= ~CM_PRIVATE;
896 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",*modechar,chan->custom_modes);
899 if (((!strchr(chan->custom_modes,*modechar)) && (!mdir)) || ((strchr(chan->custom_modes,*modechar)) && (mdir)))
901 if (!ModeIsListMode(*modechar,MT_CHANNEL))
903 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
907 if (ModeDefined(*modechar,MT_CHANNEL))
909 log(DEBUG,"A module has claimed this mode");
912 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
914 p.push_back(parameters[param]);
916 if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
918 p.push_back(parameters[param]);
921 bool handled = false;
924 // we're supposed to have a parameter, but none was given... so dont handle the mode.
925 if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))
927 log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
933 // BIG ASS IDIOTIC CODER WARNING!
934 // Using OnRawMode on another modules mode's behavour
935 // will confuse the crap out of admins! just because you CAN
936 // do it, doesnt mean you SHOULD!
938 std::string para = "";
941 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
944 for (int i = 0; i <= MODCOUNT; i++)
948 int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
951 log(DEBUG,"OnExtendedMode returned nonzero for a module");
952 if (ModeIsListMode(*modechar,MT_CHANNEL))
965 outpars[pc++] = parameters[param++];
973 chan->SetCustomMode(*modechar,mdir);
974 // include parameters in output if mode has them
975 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
977 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
978 outpars[pc++] = parameters[param++];
982 // break, because only one module can handle the mode.
991 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
999 /* Null terminate it now we're done */
1003 /************ Fast, but confusing string tidying ************/
1005 while (*outl && (*outl < 'A'))
1007 /* outl now points to the first mode character after +'s and -'s */
1009 /* Now points at first mode-modifier + or - symbol */
1011 /* Now we tidy off any trailing -'s etc */
1014 while ((*--trim == '+') || (*trim == '-'))
1016 /************ Done wih the string tidy functions ************/
1019 /* The mode change must be at least two characters long (+ or - and at least one mode) */
1020 if (((*outl == '+') || (*outl == '-')) && *(outl+1))
1022 for (ptr = 0; ptr < pc; ptr++)
1024 charlcat(outl,' ',MAXBUF);
1025 strlcat(outl,outpars[ptr],MAXBUF-1);
1029 log(DEBUG,"Local mode change");
1030 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outl);
1031 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1039 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outl);
1047 WriteChannel(chan,user,"MODE %s %s",chan->name,outl);
1048 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1055 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1057 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1059 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1060 // Servers can +o and -o arbitrarily
1061 if ((serveroverride == true) && (umode == 'o'))
1065 // RFC1459 specified modes
1066 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1068 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1072 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1073 if ((strchr(sourcemodes,'o')) && (!adding))
1075 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1078 else if (umode == 'o')
1080 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1084 // process any module-defined modes that need oper
1085 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1087 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1091 if (ModeDefined(umode,MT_CLIENT))
1093 // process any module-defined modes that don't need oper
1094 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1095 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1097 // no, this mode needs oper, and this user 'aint got what it takes!
1103 // anything else - return false.
1104 log(DEBUG,"umode %c not known by any ruleset",umode);
1108 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1115 strlcpy(s2->nick,Config->ServerName,NICKMAX-1);
1124 if (ModeDefined(umode,MT_CLIENT))
1126 for (int i = 0; i <= MODCOUNT; i++)
1128 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1130 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1134 log(DEBUG,"No module claims umode %c",umode);
1153 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1159 char outpars[MAXBUF];
1160 bool next_ok = true;
1162 dest = Find(parameters[0]);
1169 if ((dest) && (pcnt == 1))
1171 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1175 if ((dest) && (pcnt > 1))
1177 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1178 parameters[1] = (char*)tidied.c_str();
1180 char dmodes[MAXBUF];
1181 strlcpy(dmodes,dest->modes,MAXMODES);
1182 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1187 if ((strchr(user->modes,'o')) || (is_uline(user->server)))
1198 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1202 outpars[0] = *parameters[1];
1204 direction = (*parameters[1] == '+');
1206 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1209 for (char* i = parameters[1]; *i; i++)
1214 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1219 if ((direction != 1) && (next_ok))
1221 charlcat(outpars,'+',MAXBUF);
1229 if ((direction != 0) && (next_ok))
1231 charlcat(outpars,'-',MAXBUF);
1239 if (strchr(user->modes,'o'))
1245 if ((*i == 'i') || (*i == 'w') || (*i == 's') || (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,direction,false)))
1254 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,false)))
1256 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1258 charlcat(dmodes,*i,MAXMODES);
1259 charlcat(outpars,*i,MAXMODES);
1262 FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1269 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,false)) && (strchr(dmodes,*i)))
1271 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1273 charlcat(outpars,*i,MAXMODES);
1274 charremove(dmodes,*i);
1291 for (char* i = outpars; *i;)
1294 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1296 // someones playing silly buggers and trying
1297 // to put a +- or -+ into the line...
1302 // Someone's trying to make the last character in
1303 // the line be a + or - symbol.
1304 if ((*i == '-') || (*i == '+'))
1312 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1314 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1315 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1318 log(DEBUG,"Stripped mode line");
1319 log(DEBUG,"Line dest is now %s",dmodes);
1320 strlcpy(dest->modes,dmodes,MAXMODES-1);
1327 Ptr = FindChan(parameters[0]);
1332 /* just /modes #channel */
1333 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr,has_channel(user,Ptr)));
1334 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1340 char* mode = parameters[1];
1344 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, Ptr, *mode, "", false, 0));
1350 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1352 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1354 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1357 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1359 // list of items for an extmode
1360 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1361 FOREACH_MOD(I_OnSendList,OnSendList(user,Ptr,*mode));
1367 if (((Ptr) && (!has_channel(user,Ptr))) && (!is_uline(user->server)) && (IS_LOCAL(user)))
1369 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1376 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1378 if (MOD_RESULT == ACR_DENY)
1380 if (MOD_RESULT == ACR_DEFAULT)
1382 if ((cstatus(user,Ptr) < STATUS_HOP) && (IS_LOCAL(user)))
1384 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1389 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1394 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1401 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1407 char outpars[MAXBUF];
1408 bool next_ok = true;
1410 dest = Find(parameters[0]);
1412 // fix: ChroNiCk found this - we cant use this as debug if its null!
1415 log(DEBUG,"server_mode on %s",dest->nick);
1418 if ((dest) && (pcnt > 1))
1420 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1421 parameters[1] = (char*)tidied.c_str();
1423 char dmodes[MAXBUF];
1424 strlcpy(dmodes,dest->modes,MAXBUF);
1426 outpars[0] = *parameters[1];
1428 direction = (*parameters[1] == '+');
1430 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1433 for (char* i = parameters[1]; *i; i++)
1438 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1443 if ((direction != 1) && (next_ok))
1446 charlcat(outpars,'+',MAXBUF);
1453 if ((direction != 0) && (next_ok))
1456 charlcat(outpars,'-',MAXBUF);
1462 log(DEBUG,"begin mode processing entry");
1468 log(DEBUG,"umode %c being added",*i);
1469 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,true)))
1471 log(DEBUG,"umode %c is an allowed umode",*i);
1472 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1474 charlcat(dmodes,*i,MAXMODES);
1475 charlcat(outpars,*i,MAXMODES);
1481 // can only remove a mode they already have
1482 log(DEBUG,"umode %c being removed",*i);
1483 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,true)) && (strchr(dmodes,*i)))
1485 log(DEBUG,"umode %c is an allowed umode",*i);
1486 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1488 charlcat(outpars,*i,MAXMODES);
1489 charremove(dmodes,*i);
1501 for (char* i = outpars; *i;)
1504 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1506 // someones playing silly buggers and trying
1507 // to put a +- or -+ into the line...
1512 // Someone's trying to make the last character in
1513 // the line be a + or - symbol.
1514 if ((*i == '-') || (*i == '+'))
1522 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1524 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1525 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1528 log(DEBUG,"Stripped mode line");
1529 log(DEBUG,"Line dest is now %s",dmodes);
1530 strlcpy(dest->modes,dmodes,MAXMODES-1);
1537 Ptr = FindChan(parameters[0]);
1540 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1544 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);