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_on, int parameters_off, bool listmode, ModeType type, bool operonly) : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), 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(bool adding)
82 return adding ? n_params_on : n_params_off;
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, ModeType type)
127 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
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));
478 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
480 std::string target = parameters[0];
481 ModeType type = MODETYPE_USER;
482 chanrec* targetchannel = FindChan(parameters[0]);
483 userrec* targetuser = Find(parameters[0]);
489 type = MODETYPE_CHANNEL;
493 type = MODETYPE_USER;
497 /* No such nick/channel */
500 std::string mode_sequence = parameters[1];
501 std::string parameter = "";
502 std::ostringstream parameter_list;
503 std::string output_sequence = "";
504 bool adding = true, state_change = false;
506 int parameter_counter = 2; /* Index of first parameter */
508 for (std::string::const_iterator modeletter = mode_sequence.begin(); modeletter != mode_sequence.end(); modeletter++)
513 * For + and - mode characters, we don't just stick the character into the output sequence.
514 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
515 * appearing in the output sequence, we store a flag which says there was a state change,
516 * which is set on any + or -, however, the + or - that we finish on is only appended to
517 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
520 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
521 * however, will allow the + if it is the first item in the sequence, regardless.
523 if ((!adding) || (!output_sequence.length()))
529 if ((adding) || (!output_sequence.length()))
536 /* 65 is the ascii value of 'A' */
537 handler_id = *modeletter - 65;
539 if (modehandlers[handler_id])
542 for (std::vector<ModeWatcher*>::iterator watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
544 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
547 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
549 if (modehandlers[handler_id]->GetNumParams(adding))
551 if (pcnt < parameter_counter)
553 parameter = parameters[parameter_counter++];
560 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
561 if (ma == MODEACTION_ALLOW)
563 /* We're about to output a valid mode letter - was there previously a pending state-change? */
565 output_sequence.append(adding ? "+" : "-");
567 /* Add the mode letter */
568 output_sequence = output_sequence + *modeletter;
570 /* Is there a valid parameter for this mode? If so add it to the parameter list */
571 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
573 parameter_list << " " << parameter;
576 /* Call all the AfterMode events in the mode watchers for this mode */
577 for (std::vector<ModeWatcher*>::iterator watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
579 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
582 /* Reset the state change flag */
583 state_change = false;
592 if (type == MODETYPE_CHANNEL)
594 WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
599 if (type == MODETYPE_CHANNEL)
601 WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
602 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
608 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
613 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
615 if ((!parameters) || (pcnt < 2)) {
619 char outlist[MAXBUF];
627 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
633 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
634 if (MOD_RESULT == ACR_DENY)
638 std::string tidied = this->CompressModes(parameters[1],true);
639 strlcpy(mlist,tidied.c_str(),MAXBUF);
640 char* modelist = mlist;
642 *outlist = *modelist;
643 char* outl = outlist+1;
645 mdir = (*modelist == '+');
647 log(DEBUG,"process_modes: modelist: %s",modelist);
649 int len = tidied.length();
650 while (modelist[len-1] == ' ')
651 modelist[--len] = '\0';
653 for (char* modechar = (modelist + 1); *modechar; ptr++, modechar++)
657 /* If we have more than MAXMODES changes in one line,
658 * drop all after the MAXMODES
679 if ((param >= pcnt)) break;
680 log(DEBUG,"Enough parameters left");
685 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
688 r = GiveOps(user,parameters[param++],chan,status);
695 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
698 r = TakeOps(user,parameters[param++],chan,status);
710 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
715 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
718 r = GiveHops(user,parameters[param++],chan,status);
725 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
728 r = TakeHops(user,parameters[param++],chan,status);
741 if ((param >= pcnt)) break;
746 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
749 r = GiveVoice(user,parameters[param++],chan,status);
756 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
759 r = TakeVoice(user,parameters[param++],chan,status);
771 if ((param >= pcnt)) break;
776 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
779 r = AddBan(user,parameters[param++],chan,status);
786 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
789 r = TakeBan(user,parameters[param++],chan,status);
796 outpars[pc++] = parameters[param-1];
810 if (previously_unset_k)
812 previously_set_k = true;
814 if (!chan->modes[CM_KEY])
817 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
822 strlcpy(key,parameters[param++],32);
824 strlcpy(chan->key,key,MAXBUF);
825 chan->modes[CM_KEY] = 1;
833 /* checks on -k are case sensitive and only accurate to the
834 first 32 characters */
835 if (previously_set_k)
837 previously_unset_k = true;
841 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
844 strlcpy(key,parameters[param++],32);
845 /* only allow -k if correct key given */
846 if (!strcmp(chan->key,key))
850 chan->modes[CM_KEY] = 0;
861 if (previously_set_l)
863 previously_unset_l = true;
865 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
868 if (chan->modes[CM_LIMIT])
872 chan->modes[CM_LIMIT] = 0;
877 if ((param >= pcnt)) break;
882 if (previously_unset_l)
884 previously_set_l = true;
885 bool invalid = false;
886 for (char* f = parameters[param]; *f; f++)
888 if ((*f < '0') || (*f > '9'))
893 /* If the limit is < 1, or the new limit is the current limit, dont allow */
894 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
903 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
907 chan->limit = atoi(parameters[param]);
909 // reported by mech: large values cause underflow
911 chan->limit = 0x7FFF;
917 chan->modes[CM_LIMIT] = 1;
918 outpars[pc++] = parameters[param++];
926 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
931 if (!(chan->modes[CM_INVITEONLY])) *outl++ = 'i';
932 chan->modes[CM_INVITEONLY] = 1;
936 if (chan->modes[CM_INVITEONLY]) *outl++ = 'i';
937 chan->modes[CM_INVITEONLY] = 0;
944 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
949 if (!(chan->modes[CM_TOPICLOCK])) *outl++ = 't';
950 chan->modes[CM_TOPICLOCK] = 1;
954 if (chan->modes[CM_TOPICLOCK]) *outl++ = 't';
955 chan->modes[CM_TOPICLOCK] = 0;
962 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
967 if (!(chan->modes[CM_NOEXTERNAL])) *outl++ = 'n';
968 chan->modes[CM_NOEXTERNAL] = 1;
972 if (chan->modes[CM_NOEXTERNAL]) *outl++ = 'n';
973 chan->modes[CM_NOEXTERNAL] = 0;
980 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
985 if (!(chan->modes[CM_MODERATED])) *outl++ = 'm';
986 chan->modes[CM_MODERATED] = 1;
990 if (chan->modes[CM_MODERATED]) *outl++ = 'm';
991 chan->modes[CM_MODERATED] = 0;
998 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
1003 if (!(chan->modes[CM_SECRET])) *outl++ = 's';
1004 chan->modes[CM_SECRET] = 1;
1005 if (chan->modes[CM_PRIVATE])
1007 chan->modes[CM_PRIVATE] = 0;
1010 *outl++ = '-'; *outl++ = 'p'; *outl++ = '+';
1016 if (chan->modes[CM_SECRET]) *outl++ = 's';
1017 chan->modes[CM_SECRET] = 0;
1024 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
1029 if(!(chan->modes[CM_PRIVATE]))
1032 chan->modes[CM_PRIVATE] = 1;
1034 if(chan->modes[CM_SECRET])
1036 chan->modes[CM_SECRET] = 0;
1045 if(chan->modes[CM_PRIVATE])
1048 chan->modes[CM_PRIVATE] = 0;
1056 bool x = chan->modes[*modechar-65];
1057 if ((!x && !mdir) || (x && mdir))
1059 if (!ModeIsListMode(*modechar,MT_CHANNEL))
1061 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
1065 if (ModeDefined(*modechar,MT_CHANNEL))
1067 /* A module has claimed this mode */
1070 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1072 p.push_back(parameters[param]);
1074 if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
1076 p.push_back(parameters[param]);
1079 bool handled = false;
1082 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1083 if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))
1085 log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
1090 // BIG ASS IDIOTIC CODER WARNING!
1091 // Using OnRawMode on another modules mode's behavour
1092 // will confuse the crap out of admins! just because you CAN
1093 // do it, doesnt mean you SHOULD!
1095 std::string para = "";
1099 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
1102 for (int i = 0; i <= MODCOUNT; i++)
1106 int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
1109 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1110 if (ModeIsListMode(*modechar,MT_CHANNEL))
1121 *outl++ = *modechar;
1123 outpars[pc++] = parameters[param++];
1128 *outl++ = *modechar;
1129 chan->SetCustomMode(*modechar,mdir);
1130 // include parameters in output if mode has them
1131 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
1135 chan->SetCustomModeParam(*modechar,parameters[param],mdir);
1136 outpars[pc++] = parameters[param++];
1140 // break, because only one module can handle the mode.
1149 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
1155 /* Null terminate it now we're done */
1159 /************ Fast, but confusing string tidying ************/
1161 while (*outl && (*outl < 'A'))
1163 /* outl now points to the first mode character after +'s and -'s */
1165 /* Now points at first mode-modifier + or - symbol */
1167 /* Now we tidy off any trailing -'s etc */
1170 while ((*--trim == '+') || (*trim == '-'))
1172 /************ Done wih the string tidy functions ************/
1175 /* The mode change must be at least two characters long (+ or - and at least one mode) */
1176 if (((*outl == '+') || (*outl == '-')) && *(outl+1))
1178 for (ptr = 0; ptr < pc; ptr++)
1180 charlcat(outl,' ',MAXBUF);
1181 strlcat(outl,outpars[ptr],MAXBUF-1);
1185 log(DEBUG,"Local mode change");
1186 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outl);
1187 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1195 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outl);
1203 WriteChannel(chan,user,"MODE %s %s",chan->name,outl);
1204 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1211 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1213 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1215 bool sourceoper = (strchr(sourcemodes,'o') != NULL);
1216 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1217 // Servers can +o and -o arbitrarily
1218 if ((serveroverride == true) && (umode == 'o'))
1222 // RFC1459 specified modes
1223 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1225 /* umode allowed by RFC1459 scemantics */
1229 /* user may not +o themselves or others, but an oper may de-oper other opers or themselves */
1230 if (sourceoper && !adding)
1234 else if (umode == 'o')
1236 /* Bad oper, bad bad! */
1240 /* process any module-defined modes that need oper */
1241 if ((ModeDefinedOper(umode,MT_CLIENT)) && (sourceoper))
1243 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1246 else if (ModeDefined(umode,MT_CLIENT))
1248 // process any module-defined modes that don't need oper
1249 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1250 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!sourceoper))
1252 // no, this mode needs oper, and this user 'aint got what it takes!
1258 // anything else - return false.
1259 log(DEBUG,"umode %c not known by any ruleset",umode);
1263 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1270 strlcpy(s2->nick,Config->ServerName,NICKMAX-1);
1279 if (ModeDefined(umode,MT_CLIENT))
1281 for (int i = 0; i <= MODCOUNT; i++)
1283 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1285 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1289 log(DEBUG,"No module claims umode %c",umode);
1308 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1311 userrec* dest = Find(parameters[0]);
1315 char outpars[MAXBUF];
1316 bool next_ok = true;
1321 if ((dest) && (pcnt == 1))
1323 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1326 else if ((dest) && (pcnt > 1))
1328 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1329 parameters[1] = (char*)tidied.c_str();
1331 char dmodes[MAXBUF];
1332 strlcpy(dmodes,dest->modes,MAXMODES);
1333 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1338 if ((*user->oper) || (is_uline(user->server)))
1349 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1353 outpars[0] = *parameters[1];
1355 direction = (*parameters[1] == '+');
1357 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1360 for (char* i = parameters[1]; *i; i++)
1362 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1371 if ((direction != 1) && (next_ok))
1373 charlcat(outpars,'+',MAXBUF);
1380 if ((direction != 0) && (next_ok))
1382 charlcat(outpars,'-',MAXBUF);
1396 if ((*i == 'i') || (*i == 'w') || (*i == 's') || (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,direction,false)))
1405 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,false)))
1407 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1409 charlcat(dmodes,*i,53);
1410 charlcat(outpars,*i,MAXMODES);
1414 FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1417 dest->modebits |= UM_INVISIBLE;
1420 dest->modebits |= UM_SERVERNOTICE;
1423 dest->modebits |= UM_WALLOPS;
1433 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,false)) && (strchr(dmodes,*i)))
1435 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1437 charlcat(outpars,*i,MAXMODES);
1438 charremove(dmodes,*i);
1446 dest->modebits &= ~UM_INVISIBLE;
1449 dest->modebits &= ~UM_SERVERNOTICE;
1452 dest->modebits &= ~UM_WALLOPS;
1469 for (char* i = outpars; *i;)
1472 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1474 // someones playing silly buggers and trying
1475 // to put a +- or -+ into the line...
1480 // Someone's trying to make the last character in
1481 // the line be a + or - symbol.
1482 if ((*i == '-') || (*i == '+'))
1490 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1492 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1493 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1496 log(DEBUG,"Stripped mode line");
1497 log(DEBUG,"Line dest is now %s",dmodes);
1498 strlcpy(dest->modes,dmodes,MAXMODES-1);
1506 chan = FindChan(parameters[0]);
1511 /* just /modes #channel */
1512 WriteServ(user->fd,"324 %s %s +%s",user->nick, chan->name, chanmodes(chan, chan->HasUser(user)));
1513 WriteServ(user->fd,"329 %s %s %d", user->nick, chan->name, chan->created);
1518 char* mode = parameters[1];
1525 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *mode, "", false, 0));
1530 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1532 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, chan->name, i->data, i->set_by, i->set_time);
1534 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, chan->name);
1538 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1540 // list of items for an extmode
1541 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1542 FOREACH_MOD(I_OnSendList,OnSendList(user,chan,*mode));
1548 if ((IS_LOCAL(user)) && (!is_uline(user->server)) && (!chan->HasUser(user)))
1550 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, chan->name);
1555 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user, NULL, chan, AC_GENERAL_MODE));
1557 if(MOD_RESULT == ACR_DENY)
1560 if(MOD_RESULT == ACR_DEFAULT)
1562 if ((IS_LOCAL(user)) && (cstatus(user,chan) < STATUS_HOP))
1564 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
1569 ServerInstance->ModeGrok->ProcessModes(parameters,user,chan,cstatus(user,chan),pcnt,false,false,false);
1573 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1581 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1584 userrec* dest = Find(parameters[0]);
1587 char outpars[MAXBUF];
1588 bool next_ok = true;
1590 if ((dest) && (pcnt > 1))
1592 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1593 parameters[1] = (char*)tidied.c_str();
1595 char dmodes[MAXBUF];
1596 strlcpy(dmodes,dest->modes,MAXBUF);
1598 outpars[0] = *parameters[1];
1600 direction = (*parameters[1] == '+');
1602 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1605 for (char* i = parameters[1]; *i; i++)
1607 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1616 if ((direction != 1) && (next_ok))
1619 charlcat(outpars,'+',MAXBUF);
1625 if ((direction != 0) && (next_ok))
1628 charlcat(outpars,'-',MAXBUF);
1634 log(DEBUG,"begin mode processing entry");
1640 log(DEBUG,"umode %c being added",*i);
1641 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,true)))
1643 log(DEBUG,"umode %c is an allowed umode",*i);
1644 if ((*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o') || (ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)))
1646 charlcat(dmodes,*i,MAXBUF);
1647 charlcat(outpars,*i,53);
1651 dest->modebits |= UM_INVISIBLE;
1654 dest->modebits |= UM_SERVERNOTICE;
1657 dest->modebits |= UM_WALLOPS;
1667 // can only remove a mode they already have
1668 log(DEBUG,"umode %c being removed",*i);
1669 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,true)) && (strchr(dmodes,*i)))
1671 log(DEBUG,"umode %c is an allowed umode",*i);
1672 if ((*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o') || (ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)))
1674 charlcat(outpars,*i,MAXBUF);
1675 charremove(dmodes,*i);
1679 dest->modebits &= ~UM_INVISIBLE;
1682 dest->modebits &= ~UM_SERVERNOTICE;
1685 dest->modebits &= ~UM_WALLOPS;
1702 for (char* i = outpars; *i;)
1705 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1707 // someones playing silly buggers and trying
1708 // to put a +- or -+ into the line...
1713 // Someone's trying to make the last character in
1714 // the line be a + or - symbol.
1715 if ((*i == '-') || (*i == '+'))
1723 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1725 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1726 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1729 log(DEBUG,"Stripped mode line");
1730 log(DEBUG,"Line dest is now %s",dmodes);
1731 strlcpy(dest->modes,dmodes,MAXMODES-1);
1738 Ptr = FindChan(parameters[0]);
1741 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1745 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);