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 ModeOutput::ModeOutput(std::string parameter, ModeAction action) : par(parameter), act(action)
61 ModeAction ModeOutput::GetAction()
66 std::string& ModeOutput::GetParameter()
71 ModeHandler::ModeHandler(char modeletter, int parameters, bool listmode, ModeType type, bool operonly) : mode(modeletter), n_params(parameters), list(listmode), m_type(type), oper(operonly)
75 ModeHandler::~ModeHandler()
79 bool ModeHandler::IsListMode()
84 ModeType ModeHandler::GetModeType()
89 bool ModeHandler::NeedsOper()
94 int ModeHandler::GetNumParams()
99 char ModeHandler::GetModeChar()
104 ModeOutput ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding)
106 return ModeOutput("", MODEACTION_DENY);
109 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
113 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
115 return (ours < theirs);
118 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
122 ModeWatcher::~ModeWatcher()
126 char ModeWatcher::GetModeChar()
131 ModeType ModeWatcher::GetModeType()
136 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding)
141 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding)
145 userrec* ModeParser::SanityChecks(userrec *user,char *dest,chanrec *chan,int status)
148 if ((!user) || (!dest) || (!chan) || (!*dest))
155 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
161 char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
166 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
168 if (((ucrec*)(*i))->channel == chan)
170 if (((ucrec*)(*i))->uc_modes & MASK)
174 ((ucrec*)(*i))->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
178 ((ucrec*)(*i))->channel->AddOppedUser(d);
181 ((ucrec*)(*i))->channel->AddHalfoppedUser(d);
184 ((ucrec*)(*i))->channel->AddVoicedUser(d);
187 log(DEBUG,"grant: %s %s",((ucrec*)(*i))->channel->name,d->nick);
194 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
199 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
201 if (((ucrec*)(*i))->channel == chan)
203 if ((((ucrec*)(*i))->uc_modes & MASK) == 0)
207 ((ucrec*)(*i))->uc_modes ^= MASK;
211 ((ucrec*)(*i))->channel->DelOppedUser(d);
214 ((ucrec*)(*i))->channel->DelHalfoppedUser(d);
217 ((ucrec*)(*i))->channel->DelVoicedUser(d);
220 log(DEBUG,"revoke: %s %s",((ucrec*)(*i))->channel->name,d->nick);
227 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
229 userrec *d = this->SanityChecks(user,dest,chan,status);
236 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
238 if (MOD_RESULT == ACR_DENY)
240 if (MOD_RESULT == ACR_DEFAULT)
242 if ((status < STATUS_OP) && (!is_uline(user->server)))
244 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
250 return this->Grant(d,chan,UCMODE_OP);
255 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
257 userrec *d = this->SanityChecks(user,dest,chan,status);
264 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
266 if (MOD_RESULT == ACR_DENY)
268 if (MOD_RESULT == ACR_DEFAULT)
270 if ((status < STATUS_OP) && (!is_uline(user->server)))
272 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
278 return this->Grant(d,chan,UCMODE_HOP);
283 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
285 userrec *d = this->SanityChecks(user,dest,chan,status);
292 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
294 if (MOD_RESULT == ACR_DENY)
296 if (MOD_RESULT == ACR_DEFAULT)
298 if ((status < STATUS_HOP) && (!is_uline(user->server)))
300 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
306 return this->Grant(d,chan,UCMODE_VOICE);
311 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
313 userrec *d = this->SanityChecks(user,dest,chan,status);
320 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
322 if (MOD_RESULT == ACR_DENY)
324 if (MOD_RESULT == ACR_DEFAULT)
326 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
328 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
334 return this->Revoke(d,chan,UCMODE_OP);
339 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
341 userrec *d = this->SanityChecks(user,dest,chan,status);
348 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
350 if (MOD_RESULT == ACR_DENY)
352 if (MOD_RESULT == ACR_DEFAULT)
354 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
355 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
357 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
363 return this->Revoke(d,chan,UCMODE_HOP);
368 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
370 userrec *d = this->SanityChecks(user,dest,chan,status);
377 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
379 if (MOD_RESULT == ACR_DENY)
381 if (MOD_RESULT == ACR_DEFAULT)
383 if ((status < STATUS_HOP) && (!is_uline(user->server)))
385 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
391 return this->Revoke(d,chan,UCMODE_VOICE);
396 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
399 int toomanyexclamation = 0;
402 if ((!user) || (!dest) || (!chan) || (!*dest))
404 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
408 for (char* i = dest; *i; i++)
410 if ((*i < 32) || (*i > 126))
416 toomanyexclamation++;
424 if (toomanyexclamation != 1 || toomanyat != 1)
426 * this stops sillyness like n!u!u!u@h, though note that most
427 * ircds don't actually verify banmask validity. --w00t
431 long maxbans = GetMaxBans(chan->name);
432 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
434 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);
438 log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
441 FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
446 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
448 if (!strcasecmp(i->data,dest))
450 // dont allow a user to set the same ban twice
456 strlcpy(b.data,dest,MAXBUF);
459 strlcpy(b.set_by,user->nick,NICKMAX-1);
463 strlcpy(b.set_by,Config->ServerName,NICKMAX-1);
465 chan->bans.push_back(b);
469 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
471 if ((!user) || (!dest) || (!chan) || (!*dest)) {
472 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
476 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
477 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
479 if (!strcasecmp(i->data,dest))
482 FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
493 /** ModeParser::CompressModes()
494 * Tidies up redundant modes,
495 * e.g. +nt-nt+i becomes +-+i
496 * A section further down the chain tidies up the +-+- crap.
498 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
501 * OK, iterate over the mode string and count how many times a certain mode appears in it.
502 * Then, erase all instances of any character that appears more than once.
503 * This only operates on modes with no parameters, you can still +v-v+v-v+v-v to your heart's content.
506 /* Do we really need an int here? Can you fit enough modes in a line to overflow a short? */
509 memset(counts, 0, sizeof(counts));
510 memset(active, 0, sizeof(active));
512 for(unsigned char* i = (unsigned char*)modes.c_str(); *i; i++)
514 if((*i == '+') || (*i == '-'))
517 if(!channelmodes || (channelmodes && (strchr("itnmsp", *i) || (ModeDefined(*i, MT_CHANNEL) && !ModeDefinedOn(*i,MT_CHANNEL) && !ModeDefinedOff(*i,MT_CHANNEL)))))
519 log(DEBUG,"Tidy mode %c", *i);
525 for(unsigned char j = 65; j < 127; j++)
527 if ((counts[j] > 1) && (active[j] == true))
529 std::string::size_type pos;
531 while((pos = modes.find(j)) != std::string::npos)
533 log(DEBUG, "Deleting occurence of mode %c...", j);
535 log(DEBUG,"New mode line: %s", modes.c_str());
543 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
545 if ((!parameters) || (pcnt < 2)) {
549 char outlist[MAXBUF];
557 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
563 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
564 if (MOD_RESULT == ACR_DENY)
568 std::string tidied = this->CompressModes(parameters[1],true);
569 strlcpy(mlist,tidied.c_str(),MAXBUF);
570 char* modelist = mlist;
572 *outlist = *modelist;
573 char* outl = outlist+1;
575 mdir = (*modelist == '+');
577 log(DEBUG,"process_modes: modelist: %s",modelist);
579 int len = tidied.length();
580 while (modelist[len-1] == ' ')
581 modelist[--len] = '\0';
583 for (char* modechar = (modelist + 1); *modechar; ptr++, modechar++)
587 /* If we have more than MAXMODES changes in one line,
588 * drop all after the MAXMODES
609 if ((param >= pcnt)) break;
610 log(DEBUG,"Enough parameters left");
615 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
618 r = GiveOps(user,parameters[param++],chan,status);
625 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
628 r = TakeOps(user,parameters[param++],chan,status);
640 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
645 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
648 r = GiveHops(user,parameters[param++],chan,status);
655 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
658 r = TakeHops(user,parameters[param++],chan,status);
671 if ((param >= pcnt)) break;
676 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
679 r = GiveVoice(user,parameters[param++],chan,status);
686 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
689 r = TakeVoice(user,parameters[param++],chan,status);
701 if ((param >= pcnt)) break;
706 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
709 r = AddBan(user,parameters[param++],chan,status);
716 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
719 r = TakeBan(user,parameters[param++],chan,status);
726 outpars[pc++] = parameters[param-1];
740 if (previously_unset_k)
742 previously_set_k = true;
744 if (!chan->modes[CM_KEY])
747 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
752 strlcpy(key,parameters[param++],32);
754 strlcpy(chan->key,key,MAXBUF);
755 chan->modes[CM_KEY] = 1;
763 /* checks on -k are case sensitive and only accurate to the
764 first 32 characters */
765 if (previously_set_k)
767 previously_unset_k = true;
771 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
774 strlcpy(key,parameters[param++],32);
775 /* only allow -k if correct key given */
776 if (!strcmp(chan->key,key))
780 chan->modes[CM_KEY] = 0;
791 if (previously_set_l)
793 previously_unset_l = true;
795 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
798 if (chan->modes[CM_LIMIT])
802 chan->modes[CM_LIMIT] = 0;
807 if ((param >= pcnt)) break;
812 if (previously_unset_l)
814 previously_set_l = true;
815 bool invalid = false;
816 for (char* f = parameters[param]; *f; f++)
818 if ((*f < '0') || (*f > '9'))
823 /* If the limit is < 1, or the new limit is the current limit, dont allow */
824 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
833 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
837 chan->limit = atoi(parameters[param]);
839 // reported by mech: large values cause underflow
841 chan->limit = 0x7FFF;
847 chan->modes[CM_LIMIT] = 1;
848 outpars[pc++] = parameters[param++];
856 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
861 if (!(chan->modes[CM_INVITEONLY])) *outl++ = 'i';
862 chan->modes[CM_INVITEONLY] = 1;
866 if (chan->modes[CM_INVITEONLY]) *outl++ = 'i';
867 chan->modes[CM_INVITEONLY] = 0;
874 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
879 if (!(chan->modes[CM_TOPICLOCK])) *outl++ = 't';
880 chan->modes[CM_TOPICLOCK] = 1;
884 if (chan->modes[CM_TOPICLOCK]) *outl++ = 't';
885 chan->modes[CM_TOPICLOCK] = 0;
892 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
897 if (!(chan->modes[CM_NOEXTERNAL])) *outl++ = 'n';
898 chan->modes[CM_NOEXTERNAL] = 1;
902 if (chan->modes[CM_NOEXTERNAL]) *outl++ = 'n';
903 chan->modes[CM_NOEXTERNAL] = 0;
910 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
915 if (!(chan->modes[CM_MODERATED])) *outl++ = 'm';
916 chan->modes[CM_MODERATED] = 1;
920 if (chan->modes[CM_MODERATED]) *outl++ = 'm';
921 chan->modes[CM_MODERATED] = 0;
928 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
933 if (!(chan->modes[CM_SECRET])) *outl++ = 's';
934 chan->modes[CM_SECRET] = 1;
935 if (chan->modes[CM_PRIVATE])
937 chan->modes[CM_PRIVATE] = 0;
940 *outl++ = '-'; *outl++ = 'p'; *outl++ = '+';
946 if (chan->modes[CM_SECRET]) *outl++ = 's';
947 chan->modes[CM_SECRET] = 0;
954 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
959 if(!(chan->modes[CM_PRIVATE]))
962 chan->modes[CM_PRIVATE] = 1;
964 if(chan->modes[CM_SECRET])
966 chan->modes[CM_SECRET] = 0;
975 if(chan->modes[CM_PRIVATE])
978 chan->modes[CM_PRIVATE] = 0;
986 bool x = chan->modes[*modechar-65];
987 if ((!x && !mdir) || (x && mdir))
989 if (!ModeIsListMode(*modechar,MT_CHANNEL))
991 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
995 if (ModeDefined(*modechar,MT_CHANNEL))
997 /* A module has claimed this mode */
1000 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1002 p.push_back(parameters[param]);
1004 if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
1006 p.push_back(parameters[param]);
1009 bool handled = false;
1012 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1013 if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))
1015 log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
1020 // BIG ASS IDIOTIC CODER WARNING!
1021 // Using OnRawMode on another modules mode's behavour
1022 // will confuse the crap out of admins! just because you CAN
1023 // do it, doesnt mean you SHOULD!
1025 std::string para = "";
1029 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
1032 for (int i = 0; i <= MODCOUNT; i++)
1036 int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
1039 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1040 if (ModeIsListMode(*modechar,MT_CHANNEL))
1051 *outl++ = *modechar;
1053 outpars[pc++] = parameters[param++];
1058 *outl++ = *modechar;
1059 chan->SetCustomMode(*modechar,mdir);
1060 // include parameters in output if mode has them
1061 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1065 chan->SetCustomModeParam(*modechar,parameters[param],mdir);
1066 outpars[pc++] = parameters[param++];
1070 // break, because only one module can handle the mode.
1079 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
1085 /* Null terminate it now we're done */
1089 /************ Fast, but confusing string tidying ************/
1091 while (*outl && (*outl < 'A'))
1093 /* outl now points to the first mode character after +'s and -'s */
1095 /* Now points at first mode-modifier + or - symbol */
1097 /* Now we tidy off any trailing -'s etc */
1100 while ((*--trim == '+') || (*trim == '-'))
1102 /************ Done wih the string tidy functions ************/
1105 /* The mode change must be at least two characters long (+ or - and at least one mode) */
1106 if (((*outl == '+') || (*outl == '-')) && *(outl+1))
1108 for (ptr = 0; ptr < pc; ptr++)
1110 charlcat(outl,' ',MAXBUF);
1111 strlcat(outl,outpars[ptr],MAXBUF-1);
1115 log(DEBUG,"Local mode change");
1116 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outl);
1117 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1125 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outl);
1133 WriteChannel(chan,user,"MODE %s %s",chan->name,outl);
1134 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1141 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1143 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1145 bool sourceoper = (strchr(sourcemodes,'o') != NULL);
1146 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1147 // Servers can +o and -o arbitrarily
1148 if ((serveroverride == true) && (umode == 'o'))
1152 // RFC1459 specified modes
1153 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1155 /* umode allowed by RFC1459 scemantics */
1159 /* user may not +o themselves or others, but an oper may de-oper other opers or themselves */
1160 if (sourceoper && !adding)
1164 else if (umode == 'o')
1166 /* Bad oper, bad bad! */
1170 /* process any module-defined modes that need oper */
1171 if ((ModeDefinedOper(umode,MT_CLIENT)) && (sourceoper))
1173 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1176 else if (ModeDefined(umode,MT_CLIENT))
1178 // process any module-defined modes that don't need oper
1179 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1180 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!sourceoper))
1182 // no, this mode needs oper, and this user 'aint got what it takes!
1188 // anything else - return false.
1189 log(DEBUG,"umode %c not known by any ruleset",umode);
1193 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1200 strlcpy(s2->nick,Config->ServerName,NICKMAX-1);
1209 if (ModeDefined(umode,MT_CLIENT))
1211 for (int i = 0; i <= MODCOUNT; i++)
1213 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1215 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1219 log(DEBUG,"No module claims umode %c",umode);
1238 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1241 userrec* dest = Find(parameters[0]);
1245 char outpars[MAXBUF];
1246 bool next_ok = true;
1251 if ((dest) && (pcnt == 1))
1253 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1256 else if ((dest) && (pcnt > 1))
1258 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1259 parameters[1] = (char*)tidied.c_str();
1261 char dmodes[MAXBUF];
1262 strlcpy(dmodes,dest->modes,MAXMODES);
1263 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1268 if ((*user->oper) || (is_uline(user->server)))
1279 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1283 outpars[0] = *parameters[1];
1285 direction = (*parameters[1] == '+');
1287 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1290 for (char* i = parameters[1]; *i; i++)
1292 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1301 if ((direction != 1) && (next_ok))
1303 charlcat(outpars,'+',MAXBUF);
1310 if ((direction != 0) && (next_ok))
1312 charlcat(outpars,'-',MAXBUF);
1326 if ((*i == 'i') || (*i == 'w') || (*i == 's') || (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,direction,false)))
1335 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,false)))
1337 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1339 charlcat(dmodes,*i,53);
1340 charlcat(outpars,*i,MAXMODES);
1344 FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1347 dest->modebits |= UM_INVISIBLE;
1350 dest->modebits |= UM_SERVERNOTICE;
1353 dest->modebits |= UM_WALLOPS;
1363 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,false)) && (strchr(dmodes,*i)))
1365 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1367 charlcat(outpars,*i,MAXMODES);
1368 charremove(dmodes,*i);
1376 dest->modebits &= ~UM_INVISIBLE;
1379 dest->modebits &= ~UM_SERVERNOTICE;
1382 dest->modebits &= ~UM_WALLOPS;
1399 for (char* i = outpars; *i;)
1402 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1404 // someones playing silly buggers and trying
1405 // to put a +- or -+ into the line...
1410 // Someone's trying to make the last character in
1411 // the line be a + or - symbol.
1412 if ((*i == '-') || (*i == '+'))
1420 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1422 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1423 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1426 log(DEBUG,"Stripped mode line");
1427 log(DEBUG,"Line dest is now %s",dmodes);
1428 strlcpy(dest->modes,dmodes,MAXMODES-1);
1436 chan = FindChan(parameters[0]);
1441 /* just /modes #channel */
1442 WriteServ(user->fd,"324 %s %s +%s",user->nick, chan->name, chanmodes(chan, chan->HasUser(user)));
1443 WriteServ(user->fd,"329 %s %s %d", user->nick, chan->name, chan->created);
1448 char* mode = parameters[1];
1455 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *mode, "", false, 0));
1460 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1462 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, chan->name, i->data, i->set_by, i->set_time);
1464 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, chan->name);
1468 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1470 // list of items for an extmode
1471 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1472 FOREACH_MOD(I_OnSendList,OnSendList(user,chan,*mode));
1478 if ((IS_LOCAL(user)) && (!is_uline(user->server)) && (!chan->HasUser(user)))
1480 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, chan->name);
1485 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user, NULL, chan, AC_GENERAL_MODE));
1487 if(MOD_RESULT == ACR_DENY)
1490 if(MOD_RESULT == ACR_DEFAULT)
1492 if ((IS_LOCAL(user)) && (cstatus(user,chan) < STATUS_HOP))
1494 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
1499 ServerInstance->ModeGrok->ProcessModes(parameters,user,chan,cstatus(user,chan),pcnt,false,false,false);
1503 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1511 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1514 userrec* dest = Find(parameters[0]);
1517 char outpars[MAXBUF];
1518 bool next_ok = true;
1520 if ((dest) && (pcnt > 1))
1522 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1523 parameters[1] = (char*)tidied.c_str();
1525 char dmodes[MAXBUF];
1526 strlcpy(dmodes,dest->modes,MAXBUF);
1528 outpars[0] = *parameters[1];
1530 direction = (*parameters[1] == '+');
1532 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1535 for (char* i = parameters[1]; *i; i++)
1537 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1546 if ((direction != 1) && (next_ok))
1549 charlcat(outpars,'+',MAXBUF);
1555 if ((direction != 0) && (next_ok))
1558 charlcat(outpars,'-',MAXBUF);
1564 log(DEBUG,"begin mode processing entry");
1570 log(DEBUG,"umode %c being added",*i);
1571 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,true)))
1573 log(DEBUG,"umode %c is an allowed umode",*i);
1574 if ((*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o') || (ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)))
1576 charlcat(dmodes,*i,MAXBUF);
1577 charlcat(outpars,*i,53);
1581 dest->modebits |= UM_INVISIBLE;
1584 dest->modebits |= UM_SERVERNOTICE;
1587 dest->modebits |= UM_WALLOPS;
1597 // can only remove a mode they already have
1598 log(DEBUG,"umode %c being removed",*i);
1599 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,true)) && (strchr(dmodes,*i)))
1601 log(DEBUG,"umode %c is an allowed umode",*i);
1602 if ((*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o') || (ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)))
1604 charlcat(outpars,*i,MAXBUF);
1605 charremove(dmodes,*i);
1609 dest->modebits &= ~UM_INVISIBLE;
1612 dest->modebits &= ~UM_SERVERNOTICE;
1615 dest->modebits &= ~UM_WALLOPS;
1632 for (char* i = outpars; *i;)
1635 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1637 // someones playing silly buggers and trying
1638 // to put a +- or -+ into the line...
1643 // Someone's trying to make the last character in
1644 // the line be a + or - symbol.
1645 if ((*i == '-') || (*i == '+'))
1653 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1655 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1656 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1659 log(DEBUG,"Stripped mode line");
1660 log(DEBUG,"Line dest is now %s",dmodes);
1661 strlcpy(dest->modes,dmodes,MAXMODES-1);
1668 Ptr = FindChan(parameters[0]);
1671 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1675 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);