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)
78 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
80 if (((ucrec*)(*i))->channel == chan)
82 if (((ucrec*)(*i))->uc_modes & MASK)
86 ((ucrec*)(*i))->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
90 ((ucrec*)(*i))->channel->AddOppedUser(d);
93 ((ucrec*)(*i))->channel->AddHalfoppedUser(d);
96 ((ucrec*)(*i))->channel->AddVoicedUser(d);
99 log(DEBUG,"grant: %s %s",((ucrec*)(*i))->channel->name,d->nick);
106 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
111 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
113 if (((ucrec*)(*i))->channel == chan)
115 if ((((ucrec*)(*i))->uc_modes & MASK) == 0)
119 ((ucrec*)(*i))->uc_modes ^= MASK;
123 ((ucrec*)(*i))->channel->DelOppedUser(d);
126 ((ucrec*)(*i))->channel->DelHalfoppedUser(d);
129 ((ucrec*)(*i))->channel->DelVoicedUser(d);
132 log(DEBUG,"revoke: %s %s",((ucrec*)(*i))->channel->name,d->nick);
139 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
141 userrec *d = this->SanityChecks(user,dest,chan,status);
148 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
150 if (MOD_RESULT == ACR_DENY)
152 if (MOD_RESULT == ACR_DEFAULT)
154 if ((status < STATUS_OP) && (!is_uline(user->server)))
156 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
162 return this->Grant(d,chan,UCMODE_OP);
167 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
169 userrec *d = this->SanityChecks(user,dest,chan,status);
176 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
178 if (MOD_RESULT == ACR_DENY)
180 if (MOD_RESULT == ACR_DEFAULT)
182 if ((status < STATUS_OP) && (!is_uline(user->server)))
184 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
190 return this->Grant(d,chan,UCMODE_HOP);
195 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
197 userrec *d = this->SanityChecks(user,dest,chan,status);
204 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
206 if (MOD_RESULT == ACR_DENY)
208 if (MOD_RESULT == ACR_DEFAULT)
210 if ((status < STATUS_HOP) && (!is_uline(user->server)))
212 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
218 return this->Grant(d,chan,UCMODE_VOICE);
223 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
225 userrec *d = this->SanityChecks(user,dest,chan,status);
232 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
234 if (MOD_RESULT == ACR_DENY)
236 if (MOD_RESULT == ACR_DEFAULT)
238 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
240 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
246 return this->Revoke(d,chan,UCMODE_OP);
251 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
253 userrec *d = this->SanityChecks(user,dest,chan,status);
260 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
262 if (MOD_RESULT == ACR_DENY)
264 if (MOD_RESULT == ACR_DEFAULT)
266 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
267 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
269 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
275 return this->Revoke(d,chan,UCMODE_HOP);
280 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
282 userrec *d = this->SanityChecks(user,dest,chan,status);
289 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
291 if (MOD_RESULT == ACR_DENY)
293 if (MOD_RESULT == ACR_DEFAULT)
295 if ((status < STATUS_HOP) && (!is_uline(user->server)))
297 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
303 return this->Revoke(d,chan,UCMODE_VOICE);
308 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
311 int toomanyexclamation = 0;
314 if ((!user) || (!dest) || (!chan) || (!*dest))
316 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
320 for (char* i = dest; *i; i++)
322 if ((*i < 32) || (*i > 126))
328 toomanyexclamation++;
336 if (toomanyexclamation != 1 || toomanyat != 1)
338 * this stops sillyness like n!u!u!u@h, though note that most
339 * ircds don't actually verify banmask validity. --w00t
343 long maxbans = GetMaxBans(chan->name);
344 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
346 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);
350 log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
353 FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
358 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
360 if (!strcasecmp(i->data,dest))
362 // dont allow a user to set the same ban twice
368 strlcpy(b.data,dest,MAXBUF);
371 strlcpy(b.set_by,user->nick,NICKMAX-1);
375 strlcpy(b.set_by,Config->ServerName,NICKMAX-1);
377 chan->bans.push_back(b);
381 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
383 if ((!user) || (!dest) || (!chan) || (!*dest)) {
384 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
388 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
389 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
391 if (!strcasecmp(i->data,dest))
394 FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
405 /** ModeParser::CompressModes()
406 * Tidies up redundant modes,
407 * e.g. +nt-nt+i becomes +-+i
408 * A section further down the chain tidies up the +-+- crap.
410 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
413 * OK, iterate over the mode string and count how many times a certain mode appears in it.
414 * Then, erase all instances of any character that appears more than once.
415 * This only operates on modes with no parameters, you can still +v-v+v-v+v-v to your heart's content.
418 /* Do we really need an int here? Can you fit enough modes in a line to overflow a short? */
421 memset(counts, 0, sizeof(counts));
422 memset(active, 0, sizeof(active));
424 for(unsigned char* i = (unsigned char*)modes.c_str(); *i; i++)
426 if((*i == '+') || (*i == '-'))
429 if(!channelmodes || (channelmodes && (strchr("itnmsp", *i) || (ModeDefined(*i, MT_CHANNEL) && !ModeDefinedOn(*i,MT_CHANNEL) && !ModeDefinedOff(*i,MT_CHANNEL)))))
431 log(DEBUG,"Tidy mode %c", *i);
437 for(unsigned char j = 65; j < 127; j++)
439 if ((counts[j] > 1) && (active[j] == true))
441 std::string::size_type pos;
443 while((pos = modes.find(j)) != std::string::npos)
445 log(DEBUG, "Deleting occurence of mode %c...", j);
447 log(DEBUG,"New mode line: %s", modes.c_str());
455 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
457 if ((!parameters) || (pcnt < 2)) {
461 char outlist[MAXBUF];
469 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 std::string tidied = this->CompressModes(parameters[1],true);
481 strlcpy(mlist,tidied.c_str(),MAXBUF);
482 char* modelist = mlist;
484 *outlist = *modelist;
485 char* outl = outlist+1;
487 mdir = (*modelist == '+');
489 log(DEBUG,"process_modes: modelist: %s",modelist);
491 int len = tidied.length();
492 while (modelist[len-1] == ' ')
493 modelist[--len] = '\0';
495 for (char* modechar = (modelist + 1); *modechar; ptr++, modechar++)
499 /* If we have more than MAXMODES changes in one line,
500 * drop all after the MAXMODES
521 if ((param >= pcnt)) break;
522 log(DEBUG,"Enough parameters left");
527 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
530 r = GiveOps(user,parameters[param++],chan,status);
537 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
540 r = TakeOps(user,parameters[param++],chan,status);
552 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
557 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
560 r = GiveHops(user,parameters[param++],chan,status);
567 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
570 r = TakeHops(user,parameters[param++],chan,status);
583 if ((param >= pcnt)) break;
588 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
591 r = GiveVoice(user,parameters[param++],chan,status);
598 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
601 r = TakeVoice(user,parameters[param++],chan,status);
613 if ((param >= pcnt)) break;
618 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
621 r = AddBan(user,parameters[param++],chan,status);
628 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
631 r = TakeBan(user,parameters[param++],chan,status);
638 outpars[pc++] = parameters[param-1];
652 if (previously_unset_k)
654 previously_set_k = true;
656 if (!chan->modes[CM_KEY])
659 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
664 strlcpy(key,parameters[param++],32);
666 strlcpy(chan->key,key,MAXBUF);
667 chan->modes[CM_KEY] = 1;
675 /* checks on -k are case sensitive and only accurate to the
676 first 32 characters */
677 if (previously_set_k)
679 previously_unset_k = true;
683 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
686 strlcpy(key,parameters[param++],32);
687 /* only allow -k if correct key given */
688 if (!strcmp(chan->key,key))
692 chan->modes[CM_KEY] = 0;
703 if (previously_set_l)
705 previously_unset_l = true;
707 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
710 if (chan->modes[CM_LIMIT])
714 chan->modes[CM_LIMIT] = 0;
719 if ((param >= pcnt)) break;
724 if (previously_unset_l)
726 previously_set_l = true;
727 bool invalid = false;
728 for (char* f = parameters[param]; *f; f++)
730 if ((*f < '0') || (*f > '9'))
735 /* If the limit is < 1, or the new limit is the current limit, dont allow */
736 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
745 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
749 chan->limit = atoi(parameters[param]);
751 // reported by mech: large values cause underflow
753 chan->limit = 0x7FFF;
759 chan->modes[CM_LIMIT] = 1;
760 outpars[pc++] = parameters[param++];
768 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
773 if (!(chan->modes[CM_INVITEONLY])) *outl++ = 'i';
774 chan->modes[CM_INVITEONLY] = 1;
778 if (chan->modes[CM_INVITEONLY]) *outl++ = 'i';
779 chan->modes[CM_INVITEONLY] = 0;
786 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
791 if (!(chan->modes[CM_TOPICLOCK])) *outl++ = 't';
792 chan->modes[CM_TOPICLOCK] = 1;
796 if (chan->modes[CM_TOPICLOCK]) *outl++ = 't';
797 chan->modes[CM_TOPICLOCK] = 0;
804 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
809 if (!(chan->modes[CM_NOEXTERNAL])) *outl++ = 'n';
810 chan->modes[CM_NOEXTERNAL] = 1;
814 if (chan->modes[CM_NOEXTERNAL]) *outl++ = 'n';
815 chan->modes[CM_NOEXTERNAL] = 0;
822 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
827 if (!(chan->modes[CM_MODERATED])) *outl++ = 'm';
828 chan->modes[CM_MODERATED] = 1;
832 if (chan->modes[CM_MODERATED]) *outl++ = 'm';
833 chan->modes[CM_MODERATED] = 0;
840 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
845 if (!(chan->modes[CM_SECRET])) *outl++ = 's';
846 chan->modes[CM_SECRET] = 1;
847 if (chan->modes[CM_PRIVATE])
849 chan->modes[CM_PRIVATE] = 0;
852 *outl++ = '-'; *outl++ = 'p'; *outl++ = '+';
858 if (chan->modes[CM_SECRET]) *outl++ = 's';
859 chan->modes[CM_SECRET] = 0;
866 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
871 if(!(chan->modes[CM_PRIVATE]))
874 chan->modes[CM_PRIVATE] = 1;
876 if(chan->modes[CM_SECRET])
878 chan->modes[CM_SECRET] = 0;
887 if(chan->modes[CM_PRIVATE])
890 chan->modes[CM_PRIVATE] = 0;
898 bool x = chan->modes[*modechar-65];
899 if ((!x && !mdir) || (x && 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 /* 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);
932 // BIG ASS IDIOTIC CODER WARNING!
933 // Using OnRawMode on another modules mode's behavour
934 // will confuse the crap out of admins! just because you CAN
935 // do it, doesnt mean you SHOULD!
937 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++];
971 chan->SetCustomMode(*modechar,mdir);
972 // include parameters in output if mode has them
973 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
977 chan->SetCustomModeParam(*modechar,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);
997 /* Null terminate it now we're done */
1001 /************ Fast, but confusing string tidying ************/
1003 while (*outl && (*outl < 'A'))
1005 /* outl now points to the first mode character after +'s and -'s */
1007 /* Now points at first mode-modifier + or - symbol */
1009 /* Now we tidy off any trailing -'s etc */
1012 while ((*--trim == '+') || (*trim == '-'))
1014 /************ Done wih the string tidy functions ************/
1017 /* The mode change must be at least two characters long (+ or - and at least one mode) */
1018 if (((*outl == '+') || (*outl == '-')) && *(outl+1))
1020 for (ptr = 0; ptr < pc; ptr++)
1022 charlcat(outl,' ',MAXBUF);
1023 strlcat(outl,outpars[ptr],MAXBUF-1);
1027 log(DEBUG,"Local mode change");
1028 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outl);
1029 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1037 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outl);
1045 WriteChannel(chan,user,"MODE %s %s",chan->name,outl);
1046 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1053 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1055 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1057 bool sourceoper = (strchr(sourcemodes,'o') != NULL);
1058 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1059 // Servers can +o and -o arbitrarily
1060 if ((serveroverride == true) && (umode == 'o'))
1064 // RFC1459 specified modes
1065 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1067 /* umode allowed by RFC1459 scemantics */
1071 /* user may not +o themselves or others, but an oper may de-oper other opers or themselves */
1072 if (sourceoper && !adding)
1076 else if (umode == 'o')
1078 /* Bad oper, bad bad! */
1082 /* process any module-defined modes that need oper */
1083 if ((ModeDefinedOper(umode,MT_CLIENT)) && (sourceoper))
1085 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1088 else if (ModeDefined(umode,MT_CLIENT))
1090 // process any module-defined modes that don't need oper
1091 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1092 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!sourceoper))
1094 // no, this mode needs oper, and this user 'aint got what it takes!
1100 // anything else - return false.
1101 log(DEBUG,"umode %c not known by any ruleset",umode);
1105 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1112 strlcpy(s2->nick,Config->ServerName,NICKMAX-1);
1121 if (ModeDefined(umode,MT_CLIENT))
1123 for (int i = 0; i <= MODCOUNT; i++)
1125 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1127 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1131 log(DEBUG,"No module claims umode %c",umode);
1150 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1153 * In theory what happens is...
1155 * Parse mode string here.
1157 * For each mode character, check against a list of mode 'watchers',
1158 * a watcher can explicitly deny or allow a mode, it can also modify
1159 * the mode parameter if there is one.
1161 * If the mode passes all the 'watchers's checks, call a mode 'handler'
1162 * for the mode, if one of the watchers said to explicitly allow it then
1163 * the mode handler will have a flag set, this *should* be honoured and
1164 * access checks *should* be overridden.
1169 * Mode handlers should be added with Server::AddHandler(), or some backend call that it uses.
1171 * Watchers should be added with Server::AddWatcher() or likewise a backend call for it,
1172 * there should be a way of adding a watcher for all modes, and whatever the method of
1173 * the watchers that gets called is should have a parameter giving the mode.
1176 /* All module callbacks for modes will be removed. */
1179 * How I *think* this works is that cmd_mode::Handle() has access checks for modes, but
1180 * ModeParser::ServerMode() doesn't...
1188 log(DEBUG, "cmd_mode::Handle() got a null user");
1192 if(dest = Find(parameters[0]))
1196 /* If there was only one parameter, they were just asking what modes someone has. */
1197 WriteServ(user->fd, "221 %s :+%s",dest->nick,dest->modes);
1202 ServerInstance->ModeGrok->HandleUserModes(parameters, pcnt, user, dest);
1206 else if(chan = FindChan(parameters[0]))
1210 /* One parameter, just tell them the channel's modes. */
1211 WriteServ(user->fd,"324 %s %s +%s",user->nick, chan->name, chanmodes(chan, chan->HasUser(user)));
1212 WriteServ(user->fd,"329 %s %s %d", user->nick, chan->name, chan->created);
1217 ServerInstance->ModeGrok->HandleChannelModes(parameters, pcnt, user, chan);
1223 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1227 void ModeParser::HandleUserModes(char** parameters, int pcnt, userrec* user, userrec* target)
1231 /* XXX - Neccessary? */
1232 char dmodes[MAXBUF];
1237 /* XXX - This is nasty, change it somehow so we don't create a std::string and then pretty much discard it */
1238 /* Maybe as the mode string will always get shorter then we should just pass a char* and modify that? */
1239 /* This should _not_ leave any '+-+-' in the string, it should be well-formed "+a-b+c" */
1240 tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1241 parameters[1] = (char*)tidied.c_str();
1243 strlcpy(dmodes,dest->modes,MAXMODES);
1244 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1246 if((user != dest) && (!*user->oper) && (!is_uline(user->server)))
1248 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1253 * Leaving this so a mode string with no + or - is taken as having a +.
1256 *outpos = (*parameters[1] == '-') ? '-' : '+';
1259 outend = out+MAXBUF;
1263 * outpos always points at where the next char should be added,
1264 * we have added the first character before we enter the loop
1265 * so it's safe to check --outpos
1268 for(char* i = parameters[1]; *i; i++)
1270 if(outpos == outend)
1272 /* We somehow hit the end of our buffer... :s */
1279 /* When does this happen? */
1283 if(!setting_on && (*--outpos != '-'))
1285 charlcat(outpars,'+',MAXBUF);
1292 if ((direction != 0) && (next_ok))
1294 charlcat(outpars,'-',MAXBUF);
1308 if ((*i == 'i') || (*i == 'w') || (*i == 's') || (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,direction,false)))
1317 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,false)))
1319 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1321 charlcat(dmodes,*i,53);
1322 charlcat(outpars,*i,MAXMODES);
1326 FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1329 dest->modebits |= UM_INVISIBLE;
1332 dest->modebits |= UM_SERVERNOTICE;
1335 dest->modebits |= UM_WALLOPS;
1345 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,false)) && (strchr(dmodes,*i)))
1347 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1349 charlcat(outpars,*i,MAXMODES);
1350 charremove(dmodes,*i);
1358 dest->modebits &= ~UM_INVISIBLE;
1361 dest->modebits &= ~UM_SERVERNOTICE;
1364 dest->modebits &= ~UM_WALLOPS;
1381 for (char* i = outpars; *i;)
1384 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1386 // someones playing silly buggers and trying
1387 // to put a +- or -+ into the line...
1392 // Someone's trying to make the last character in
1393 // the line be a + or - symbol.
1394 if ((*i == '-') || (*i == '+'))
1402 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1404 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1405 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1408 log(DEBUG,"Stripped mode line");
1409 log(DEBUG,"Line dest is now %s",dmodes);
1410 strlcpy(dest->modes,dmodes,MAXMODES-1);
1418 chan = FindChan(parameters[0]);
1423 /* just /modes #channel */
1424 WriteServ(user->fd,"324 %s %s +%s",user->nick, chan->name, chanmodes(chan, chan->HasUser(user)));
1425 WriteServ(user->fd,"329 %s %s %d", user->nick, chan->name, chan->created);
1430 char* mode = parameters[1];
1437 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *mode, "", false, 0));
1442 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1444 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, chan->name, i->data, i->set_by, i->set_time);
1446 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, chan->name);
1450 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1452 // list of items for an extmode
1453 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1454 FOREACH_MOD(I_OnSendList,OnSendList(user,chan,*mode));
1460 if ((IS_LOCAL(user)) && (!is_uline(user->server)) && (!chan->HasUser(user)))
1462 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, chan->name);
1467 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user, NULL, chan, AC_GENERAL_MODE));
1469 if(MOD_RESULT == ACR_DENY)
1472 if(MOD_RESULT == ACR_DEFAULT)
1474 if ((IS_LOCAL(user)) && (cstatus(user,chan) < STATUS_HOP))
1476 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
1481 ServerInstance->ModeGrok->ProcessModes(parameters,user,chan,cstatus(user,chan),pcnt,false,false,false);
1485 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1493 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1496 userrec* dest = Find(parameters[0]);
1499 char outpars[MAXBUF];
1500 bool next_ok = true;
1502 if ((dest) && (pcnt > 1))
1504 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1505 parameters[1] = (char*)tidied.c_str();
1507 char dmodes[MAXBUF];
1508 strlcpy(dmodes,dest->modes,MAXBUF);
1510 outpars[0] = *parameters[1];
1512 direction = (*parameters[1] == '+');
1514 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1517 for (char* i = parameters[1]; *i; i++)
1519 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1528 if ((direction != 1) && (next_ok))
1531 charlcat(outpars,'+',MAXBUF);
1537 if ((direction != 0) && (next_ok))
1540 charlcat(outpars,'-',MAXBUF);
1546 log(DEBUG,"begin mode processing entry");
1552 log(DEBUG,"umode %c being added",*i);
1553 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,true)))
1555 log(DEBUG,"umode %c is an allowed umode",*i);
1556 if ((*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o') || (ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)))
1558 charlcat(dmodes,*i,MAXBUF);
1559 charlcat(outpars,*i,53);
1563 dest->modebits |= UM_INVISIBLE;
1566 dest->modebits |= UM_SERVERNOTICE;
1569 dest->modebits |= UM_WALLOPS;
1579 // can only remove a mode they already have
1580 log(DEBUG,"umode %c being removed",*i);
1581 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,true)) && (strchr(dmodes,*i)))
1583 log(DEBUG,"umode %c is an allowed umode",*i);
1584 if ((*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o') || (ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)))
1586 charlcat(outpars,*i,MAXBUF);
1587 charremove(dmodes,*i);
1591 dest->modebits &= ~UM_INVISIBLE;
1594 dest->modebits &= ~UM_SERVERNOTICE;
1597 dest->modebits &= ~UM_WALLOPS;
1614 for (char* i = outpars; *i;)
1617 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1619 // someones playing silly buggers and trying
1620 // to put a +- or -+ into the line...
1625 // Someone's trying to make the last character in
1626 // the line be a + or - symbol.
1627 if ((*i == '-') || (*i == '+'))
1635 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1637 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1638 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1641 log(DEBUG,"Stripped mode line");
1642 log(DEBUG,"Line dest is now %s",dmodes);
1643 strlcpy(dest->modes,dmodes,MAXMODES-1);
1650 Ptr = FindChan(parameters[0]);
1653 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1657 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);