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 "configreader.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 ModeHandler::ModeHandler(char modeletter, int parameters, bool listmode, ModeType type, bool operonly) : mode(modeletter), n_params(parameters), list(listmode), m_type(type), oper(operonly)
61 ModeHandler::~ModeHandler()
65 bool ModeHandler::IsListMode()
70 ModeType ModeHandler::GetModeType()
75 bool ModeHandler::NeedsOper()
80 int ModeHandler::GetNumParams()
85 char ModeHandler::GetModeChar()
90 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
92 return MODEACTION_DENY;
95 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
99 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
101 return (ours < theirs);
104 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
108 ModeWatcher::~ModeWatcher()
112 char ModeWatcher::GetModeChar()
117 ModeType ModeWatcher::GetModeType()
122 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
127 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding)
131 userrec* ModeParser::SanityChecks(userrec *user,char *dest,chanrec *chan,int status)
134 if ((!user) || (!dest) || (!chan) || (!*dest))
141 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
147 char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
152 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
154 if (((ucrec*)(*i))->channel == chan)
156 if (((ucrec*)(*i))->uc_modes & MASK)
160 ((ucrec*)(*i))->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
164 ((ucrec*)(*i))->channel->AddOppedUser(d);
167 ((ucrec*)(*i))->channel->AddHalfoppedUser(d);
170 ((ucrec*)(*i))->channel->AddVoicedUser(d);
173 log(DEBUG,"grant: %s %s",((ucrec*)(*i))->channel->name,d->nick);
180 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
185 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
187 if (((ucrec*)(*i))->channel == chan)
189 if ((((ucrec*)(*i))->uc_modes & MASK) == 0)
193 ((ucrec*)(*i))->uc_modes ^= MASK;
197 ((ucrec*)(*i))->channel->DelOppedUser(d);
200 ((ucrec*)(*i))->channel->DelHalfoppedUser(d);
203 ((ucrec*)(*i))->channel->DelVoicedUser(d);
206 log(DEBUG,"revoke: %s %s",((ucrec*)(*i))->channel->name,d->nick);
213 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
215 userrec *d = this->SanityChecks(user,dest,chan,status);
222 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
224 if (MOD_RESULT == ACR_DENY)
226 if (MOD_RESULT == ACR_DEFAULT)
228 if ((status < STATUS_OP) && (!is_uline(user->server)))
230 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
236 return this->Grant(d,chan,UCMODE_OP);
241 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
243 userrec *d = this->SanityChecks(user,dest,chan,status);
250 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
252 if (MOD_RESULT == ACR_DENY)
254 if (MOD_RESULT == ACR_DEFAULT)
256 if ((status < STATUS_OP) && (!is_uline(user->server)))
258 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
264 return this->Grant(d,chan,UCMODE_HOP);
269 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
271 userrec *d = this->SanityChecks(user,dest,chan,status);
278 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
280 if (MOD_RESULT == ACR_DENY)
282 if (MOD_RESULT == ACR_DEFAULT)
284 if ((status < STATUS_HOP) && (!is_uline(user->server)))
286 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
292 return this->Grant(d,chan,UCMODE_VOICE);
297 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
299 userrec *d = this->SanityChecks(user,dest,chan,status);
306 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
308 if (MOD_RESULT == ACR_DENY)
310 if (MOD_RESULT == ACR_DEFAULT)
312 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
314 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
320 return this->Revoke(d,chan,UCMODE_OP);
325 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
327 userrec *d = this->SanityChecks(user,dest,chan,status);
334 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
336 if (MOD_RESULT == ACR_DENY)
338 if (MOD_RESULT == ACR_DEFAULT)
340 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
341 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
343 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
349 return this->Revoke(d,chan,UCMODE_HOP);
354 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
356 userrec *d = this->SanityChecks(user,dest,chan,status);
363 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
365 if (MOD_RESULT == ACR_DENY)
367 if (MOD_RESULT == ACR_DEFAULT)
369 if ((status < STATUS_HOP) && (!is_uline(user->server)))
371 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
377 return this->Revoke(d,chan,UCMODE_VOICE);
382 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
385 int toomanyexclamation = 0;
388 if ((!user) || (!dest) || (!chan) || (!*dest))
390 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
394 for (char* i = dest; *i; i++)
396 if ((*i < 32) || (*i > 126))
402 toomanyexclamation++;
410 if (toomanyexclamation != 1 || toomanyat != 1)
412 * this stops sillyness like n!u!u!u@h, though note that most
413 * ircds don't actually verify banmask validity. --w00t
417 long maxbans = GetMaxBans(chan->name);
418 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
420 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);
424 log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
427 FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
432 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
434 if (!strcasecmp(i->data,dest))
436 // dont allow a user to set the same ban twice
442 strlcpy(b.data,dest,MAXBUF);
445 strlcpy(b.set_by,user->nick,NICKMAX-1);
449 strlcpy(b.set_by,Config->ServerName,NICKMAX-1);
451 chan->bans.push_back(b);
455 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
457 if ((!user) || (!dest) || (!chan) || (!*dest)) {
458 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
462 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
463 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
465 if (!strcasecmp(i->data,dest))
468 FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
479 /** ModeParser::CompressModes()
480 * Tidies up redundant modes,
481 * e.g. +nt-nt+i becomes +-+i
482 * A section further down the chain tidies up the +-+- crap.
484 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
487 * OK, iterate over the mode string and count how many times a certain mode appears in it.
488 * Then, erase all instances of any character that appears more than once.
489 * This only operates on modes with no parameters, you can still +v-v+v-v+v-v to your heart's content.
492 /* Do we really need an int here? Can you fit enough modes in a line to overflow a short? */
495 memset(counts, 0, sizeof(counts));
496 memset(active, 0, sizeof(active));
498 for(unsigned char* i = (unsigned char*)modes.c_str(); *i; i++)
500 if((*i == '+') || (*i == '-'))
503 if(!channelmodes || (channelmodes && (strchr("itnmsp", *i) || (ModeDefined(*i, MT_CHANNEL) && !ModeDefinedOn(*i,MT_CHANNEL) && !ModeDefinedOff(*i,MT_CHANNEL)))))
505 log(DEBUG,"Tidy mode %c", *i);
511 for(unsigned char j = 65; j < 127; j++)
513 if ((counts[j] > 1) && (active[j] == true))
515 std::string::size_type pos;
517 while((pos = modes.find(j)) != std::string::npos)
519 log(DEBUG, "Deleting occurence of mode %c...", j);
521 log(DEBUG,"New mode line: %s", modes.c_str());
529 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
531 if ((!parameters) || (pcnt < 2)) {
535 char outlist[MAXBUF];
543 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
549 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
550 if (MOD_RESULT == ACR_DENY)
554 std::string tidied = this->CompressModes(parameters[1],true);
555 strlcpy(mlist,tidied.c_str(),MAXBUF);
556 char* modelist = mlist;
558 *outlist = *modelist;
559 char* outl = outlist+1;
561 mdir = (*modelist == '+');
563 log(DEBUG,"process_modes: modelist: %s",modelist);
565 int len = tidied.length();
566 while (modelist[len-1] == ' ')
567 modelist[--len] = '\0';
569 for (char* modechar = (modelist + 1); *modechar; ptr++, modechar++)
573 /* If we have more than MAXMODES changes in one line,
574 * drop all after the MAXMODES
595 if ((param >= pcnt)) break;
596 log(DEBUG,"Enough parameters left");
601 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
604 r = GiveOps(user,parameters[param++],chan,status);
611 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
614 r = TakeOps(user,parameters[param++],chan,status);
626 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
631 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
634 r = GiveHops(user,parameters[param++],chan,status);
641 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
644 r = TakeHops(user,parameters[param++],chan,status);
657 if ((param >= pcnt)) break;
662 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
665 r = GiveVoice(user,parameters[param++],chan,status);
672 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
675 r = TakeVoice(user,parameters[param++],chan,status);
687 if ((param >= pcnt)) break;
692 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
695 r = AddBan(user,parameters[param++],chan,status);
702 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
705 r = TakeBan(user,parameters[param++],chan,status);
712 outpars[pc++] = parameters[param-1];
726 if (previously_unset_k)
728 previously_set_k = true;
730 if (!chan->modes[CM_KEY])
733 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
738 strlcpy(key,parameters[param++],32);
740 strlcpy(chan->key,key,MAXBUF);
741 chan->modes[CM_KEY] = 1;
749 /* checks on -k are case sensitive and only accurate to the
750 first 32 characters */
751 if (previously_set_k)
753 previously_unset_k = true;
757 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
760 strlcpy(key,parameters[param++],32);
761 /* only allow -k if correct key given */
762 if (!strcmp(chan->key,key))
766 chan->modes[CM_KEY] = 0;
777 if (previously_set_l)
779 previously_unset_l = true;
781 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
784 if (chan->modes[CM_LIMIT])
788 chan->modes[CM_LIMIT] = 0;
793 if ((param >= pcnt)) break;
798 if (previously_unset_l)
800 previously_set_l = true;
801 bool invalid = false;
802 for (char* f = parameters[param]; *f; f++)
804 if ((*f < '0') || (*f > '9'))
809 /* If the limit is < 1, or the new limit is the current limit, dont allow */
810 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
819 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
823 chan->limit = atoi(parameters[param]);
825 // reported by mech: large values cause underflow
827 chan->limit = 0x7FFF;
833 chan->modes[CM_LIMIT] = 1;
834 outpars[pc++] = parameters[param++];
842 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
847 if (!(chan->modes[CM_INVITEONLY])) *outl++ = 'i';
848 chan->modes[CM_INVITEONLY] = 1;
852 if (chan->modes[CM_INVITEONLY]) *outl++ = 'i';
853 chan->modes[CM_INVITEONLY] = 0;
860 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
865 if (!(chan->modes[CM_TOPICLOCK])) *outl++ = 't';
866 chan->modes[CM_TOPICLOCK] = 1;
870 if (chan->modes[CM_TOPICLOCK]) *outl++ = 't';
871 chan->modes[CM_TOPICLOCK] = 0;
878 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
883 if (!(chan->modes[CM_NOEXTERNAL])) *outl++ = 'n';
884 chan->modes[CM_NOEXTERNAL] = 1;
888 if (chan->modes[CM_NOEXTERNAL]) *outl++ = 'n';
889 chan->modes[CM_NOEXTERNAL] = 0;
896 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
901 if (!(chan->modes[CM_MODERATED])) *outl++ = 'm';
902 chan->modes[CM_MODERATED] = 1;
906 if (chan->modes[CM_MODERATED]) *outl++ = 'm';
907 chan->modes[CM_MODERATED] = 0;
914 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
919 if (!(chan->modes[CM_SECRET])) *outl++ = 's';
920 chan->modes[CM_SECRET] = 1;
921 if (chan->modes[CM_PRIVATE])
923 chan->modes[CM_PRIVATE] = 0;
926 *outl++ = '-'; *outl++ = 'p'; *outl++ = '+';
932 if (chan->modes[CM_SECRET]) *outl++ = 's';
933 chan->modes[CM_SECRET] = 0;
940 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
945 if(!(chan->modes[CM_PRIVATE]))
948 chan->modes[CM_PRIVATE] = 1;
950 if(chan->modes[CM_SECRET])
952 chan->modes[CM_SECRET] = 0;
961 if(chan->modes[CM_PRIVATE])
964 chan->modes[CM_PRIVATE] = 0;
972 bool x = chan->modes[*modechar-65];
973 if ((!x && !mdir) || (x && mdir))
975 if (!ModeIsListMode(*modechar,MT_CHANNEL))
977 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
981 if (ModeDefined(*modechar,MT_CHANNEL))
983 /* A module has claimed this mode */
986 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
988 p.push_back(parameters[param]);
990 if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
992 p.push_back(parameters[param]);
995 bool handled = false;
998 // we're supposed to have a parameter, but none was given... so dont handle the mode.
999 if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))
1001 log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
1006 // BIG ASS IDIOTIC CODER WARNING!
1007 // Using OnRawMode on another modules mode's behavour
1008 // will confuse the crap out of admins! just because you CAN
1009 // do it, doesnt mean you SHOULD!
1011 std::string para = "";
1015 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
1018 for (int i = 0; i <= MODCOUNT; i++)
1022 int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
1025 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1026 if (ModeIsListMode(*modechar,MT_CHANNEL))
1037 *outl++ = *modechar;
1039 outpars[pc++] = parameters[param++];
1044 *outl++ = *modechar;
1045 chan->SetCustomMode(*modechar,mdir);
1046 // include parameters in output if mode has them
1047 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1051 chan->SetCustomModeParam(*modechar,parameters[param],mdir);
1052 outpars[pc++] = parameters[param++];
1056 // break, because only one module can handle the mode.
1065 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
1071 /* Null terminate it now we're done */
1075 /************ Fast, but confusing string tidying ************/
1077 while (*outl && (*outl < 'A'))
1079 /* outl now points to the first mode character after +'s and -'s */
1081 /* Now points at first mode-modifier + or - symbol */
1083 /* Now we tidy off any trailing -'s etc */
1086 while ((*--trim == '+') || (*trim == '-'))
1088 /************ Done wih the string tidy functions ************/
1091 /* The mode change must be at least two characters long (+ or - and at least one mode) */
1092 if (((*outl == '+') || (*outl == '-')) && *(outl+1))
1094 for (ptr = 0; ptr < pc; ptr++)
1096 charlcat(outl,' ',MAXBUF);
1097 strlcat(outl,outpars[ptr],MAXBUF-1);
1101 log(DEBUG,"Local mode change");
1102 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outl);
1103 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1111 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outl);
1119 WriteChannel(chan,user,"MODE %s %s",chan->name,outl);
1120 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1127 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1129 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1131 bool sourceoper = (strchr(sourcemodes,'o') != NULL);
1132 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1133 // Servers can +o and -o arbitrarily
1134 if ((serveroverride == true) && (umode == 'o'))
1138 // RFC1459 specified modes
1139 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1141 /* umode allowed by RFC1459 scemantics */
1145 /* user may not +o themselves or others, but an oper may de-oper other opers or themselves */
1146 if (sourceoper && !adding)
1150 else if (umode == 'o')
1152 /* Bad oper, bad bad! */
1156 /* process any module-defined modes that need oper */
1157 if ((ModeDefinedOper(umode,MT_CLIENT)) && (sourceoper))
1159 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1162 else if (ModeDefined(umode,MT_CLIENT))
1164 // process any module-defined modes that don't need oper
1165 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1166 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!sourceoper))
1168 // no, this mode needs oper, and this user 'aint got what it takes!
1174 // anything else - return false.
1175 log(DEBUG,"umode %c not known by any ruleset",umode);
1179 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1186 strlcpy(s2->nick,Config->ServerName,NICKMAX-1);
1195 if (ModeDefined(umode,MT_CLIENT))
1197 for (int i = 0; i <= MODCOUNT; i++)
1199 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1201 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1205 log(DEBUG,"No module claims umode %c",umode);
1224 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1227 userrec* dest = Find(parameters[0]);
1231 char outpars[MAXBUF];
1232 bool next_ok = true;
1237 if ((dest) && (pcnt == 1))
1239 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1242 else if ((dest) && (pcnt > 1))
1244 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1245 parameters[1] = (char*)tidied.c_str();
1247 char dmodes[MAXBUF];
1248 strlcpy(dmodes,dest->modes,MAXMODES);
1249 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1254 if ((*user->oper) || (is_uline(user->server)))
1265 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1269 outpars[0] = *parameters[1];
1271 direction = (*parameters[1] == '+');
1273 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1276 for (char* i = parameters[1]; *i; i++)
1278 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1287 if ((direction != 1) && (next_ok))
1289 charlcat(outpars,'+',MAXBUF);
1296 if ((direction != 0) && (next_ok))
1298 charlcat(outpars,'-',MAXBUF);
1312 if ((*i == 'i') || (*i == 'w') || (*i == 's') || (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,direction,false)))
1321 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,false)))
1323 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1325 charlcat(dmodes,*i,53);
1326 charlcat(outpars,*i,MAXMODES);
1330 FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1333 dest->modebits |= UM_INVISIBLE;
1336 dest->modebits |= UM_SERVERNOTICE;
1339 dest->modebits |= UM_WALLOPS;
1349 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,false)) && (strchr(dmodes,*i)))
1351 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1353 charlcat(outpars,*i,MAXMODES);
1354 charremove(dmodes,*i);
1362 dest->modebits &= ~UM_INVISIBLE;
1365 dest->modebits &= ~UM_SERVERNOTICE;
1368 dest->modebits &= ~UM_WALLOPS;
1385 for (char* i = outpars; *i;)
1388 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1390 // someones playing silly buggers and trying
1391 // to put a +- or -+ into the line...
1396 // Someone's trying to make the last character in
1397 // the line be a + or - symbol.
1398 if ((*i == '-') || (*i == '+'))
1406 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1408 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1409 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1412 log(DEBUG,"Stripped mode line");
1413 log(DEBUG,"Line dest is now %s",dmodes);
1414 strlcpy(dest->modes,dmodes,MAXMODES-1);
1422 chan = FindChan(parameters[0]);
1427 /* just /modes #channel */
1428 WriteServ(user->fd,"324 %s %s +%s",user->nick, chan->name, chanmodes(chan, chan->HasUser(user)));
1429 WriteServ(user->fd,"329 %s %s %d", user->nick, chan->name, chan->created);
1434 char* mode = parameters[1];
1441 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *mode, "", false, 0));
1446 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1448 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, chan->name, i->data, i->set_by, i->set_time);
1450 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, chan->name);
1454 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1456 // list of items for an extmode
1457 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1458 FOREACH_MOD(I_OnSendList,OnSendList(user,chan,*mode));
1464 if ((IS_LOCAL(user)) && (!is_uline(user->server)) && (!chan->HasUser(user)))
1466 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, chan->name);
1471 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user, NULL, chan, AC_GENERAL_MODE));
1473 if(MOD_RESULT == ACR_DENY)
1476 if(MOD_RESULT == ACR_DEFAULT)
1478 if ((IS_LOCAL(user)) && (cstatus(user,chan) < STATUS_HOP))
1480 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
1485 ServerInstance->ModeGrok->ProcessModes(parameters,user,chan,cstatus(user,chan),pcnt,false,false,false);
1489 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1497 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1500 userrec* dest = Find(parameters[0]);
1503 char outpars[MAXBUF];
1504 bool next_ok = true;
1506 if ((dest) && (pcnt > 1))
1508 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1509 parameters[1] = (char*)tidied.c_str();
1511 char dmodes[MAXBUF];
1512 strlcpy(dmodes,dest->modes,MAXBUF);
1514 outpars[0] = *parameters[1];
1516 direction = (*parameters[1] == '+');
1518 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1521 for (char* i = parameters[1]; *i; i++)
1523 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1532 if ((direction != 1) && (next_ok))
1535 charlcat(outpars,'+',MAXBUF);
1541 if ((direction != 0) && (next_ok))
1544 charlcat(outpars,'-',MAXBUF);
1550 log(DEBUG,"begin mode processing entry");
1556 log(DEBUG,"umode %c being added",*i);
1557 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,true)))
1559 log(DEBUG,"umode %c is an allowed umode",*i);
1560 if ((*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o') || (ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)))
1562 charlcat(dmodes,*i,MAXBUF);
1563 charlcat(outpars,*i,53);
1567 dest->modebits |= UM_INVISIBLE;
1570 dest->modebits |= UM_SERVERNOTICE;
1573 dest->modebits |= UM_WALLOPS;
1583 // can only remove a mode they already have
1584 log(DEBUG,"umode %c being removed",*i);
1585 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,true)) && (strchr(dmodes,*i)))
1587 log(DEBUG,"umode %c is an allowed umode",*i);
1588 if ((*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o') || (ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)))
1590 charlcat(outpars,*i,MAXBUF);
1591 charremove(dmodes,*i);
1595 dest->modebits &= ~UM_INVISIBLE;
1598 dest->modebits &= ~UM_SERVERNOTICE;
1601 dest->modebits &= ~UM_WALLOPS;
1618 for (char* i = outpars; *i;)
1621 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1623 // someones playing silly buggers and trying
1624 // to put a +- or -+ into the line...
1629 // Someone's trying to make the last character in
1630 // the line be a + or - symbol.
1631 if ((*i == '-') || (*i == '+'))
1639 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1641 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1642 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1645 log(DEBUG,"Stripped mode line");
1646 log(DEBUG,"Line dest is now %s",dmodes);
1647 strlcpy(dest->modes,dmodes,MAXMODES-1);
1654 Ptr = FindChan(parameters[0]);
1657 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1661 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);