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>
31 #include "connection.h"
41 #include "inspstring.h"
42 #include "helperfuncs.h"
46 #include "modes/cmode_s.h"
48 #include "modes/cmode_p.h"
50 #include "modes/cmode_b.h"
52 #include "modes/cmode_m.h"
55 extern std::vector<Module*> modules;
56 extern std::vector<ircd_module*> factory;
57 extern InspIRCd* ServerInstance;
58 extern ServerConfig* Config;
62 ModeHandler::ModeHandler(char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly)
63 : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
67 ModeHandler::~ModeHandler()
71 bool ModeHandler::IsListMode()
76 ModeType ModeHandler::GetModeType()
81 bool ModeHandler::NeedsOper()
86 int ModeHandler::GetNumParams(bool adding)
88 return adding ? n_params_on : n_params_off;
91 char ModeHandler::GetModeChar()
96 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
98 return MODEACTION_DENY;
101 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
105 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
107 return (ours < theirs);
110 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
114 ModeWatcher::~ModeWatcher()
118 char ModeWatcher::GetModeChar()
123 ModeType ModeWatcher::GetModeType()
128 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type)
133 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
137 userrec* ModeParser::SanityChecks(userrec *user,char *dest,chanrec *chan,int status)
140 if ((!user) || (!dest) || (!chan) || (!*dest))
147 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
153 char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
158 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
160 if (((ucrec*)(*i))->channel == chan)
162 if (((ucrec*)(*i))->uc_modes & MASK)
166 ((ucrec*)(*i))->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
170 ((ucrec*)(*i))->channel->AddOppedUser(d);
173 ((ucrec*)(*i))->channel->AddHalfoppedUser(d);
176 ((ucrec*)(*i))->channel->AddVoicedUser(d);
179 log(DEBUG,"grant: %s %s",((ucrec*)(*i))->channel->name,d->nick);
186 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
191 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
193 if (((ucrec*)(*i))->channel == chan)
195 if ((((ucrec*)(*i))->uc_modes & MASK) == 0)
199 ((ucrec*)(*i))->uc_modes ^= MASK;
203 ((ucrec*)(*i))->channel->DelOppedUser(d);
206 ((ucrec*)(*i))->channel->DelHalfoppedUser(d);
209 ((ucrec*)(*i))->channel->DelVoicedUser(d);
212 log(DEBUG,"revoke: %s %s",((ucrec*)(*i))->channel->name,d->nick);
219 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
221 userrec *d = this->SanityChecks(user,dest,chan,status);
228 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
230 if (MOD_RESULT == ACR_DENY)
232 if (MOD_RESULT == ACR_DEFAULT)
234 if ((status < STATUS_OP) && (!is_uline(user->server)))
236 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
242 return this->Grant(d,chan,UCMODE_OP);
247 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
249 userrec *d = this->SanityChecks(user,dest,chan,status);
256 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
258 if (MOD_RESULT == ACR_DENY)
260 if (MOD_RESULT == ACR_DEFAULT)
262 if ((status < STATUS_OP) && (!is_uline(user->server)))
264 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
270 return this->Grant(d,chan,UCMODE_HOP);
275 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
277 userrec *d = this->SanityChecks(user,dest,chan,status);
284 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
286 if (MOD_RESULT == ACR_DENY)
288 if (MOD_RESULT == ACR_DEFAULT)
290 if ((status < STATUS_HOP) && (!is_uline(user->server)))
292 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
298 return this->Grant(d,chan,UCMODE_VOICE);
303 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
305 userrec *d = this->SanityChecks(user,dest,chan,status);
312 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
314 if (MOD_RESULT == ACR_DENY)
316 if (MOD_RESULT == ACR_DEFAULT)
318 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
320 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
326 return this->Revoke(d,chan,UCMODE_OP);
331 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
333 userrec *d = this->SanityChecks(user,dest,chan,status);
340 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
342 if (MOD_RESULT == ACR_DENY)
344 if (MOD_RESULT == ACR_DEFAULT)
346 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
347 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
349 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
355 return this->Revoke(d,chan,UCMODE_HOP);
360 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
362 userrec *d = this->SanityChecks(user,dest,chan,status);
369 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
371 if (MOD_RESULT == ACR_DENY)
373 if (MOD_RESULT == ACR_DEFAULT)
375 if ((status < STATUS_HOP) && (!is_uline(user->server)))
377 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
383 return this->Revoke(d,chan,UCMODE_VOICE);
388 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
390 std::string target = parameters[0];
391 ModeType type = MODETYPE_USER;
392 unsigned char mask = 0;
393 chanrec* targetchannel = FindChan(parameters[0]);
394 userrec* targetuser = Find(parameters[0]);
396 log(DEBUG,"ModeParser::Process start");
402 log(DEBUG,"Target type is CHANNEL");
403 type = MODETYPE_CHANNEL;
408 log(DEBUG,"Target type is USER");
409 type = MODETYPE_USER;
414 /* No such nick/channel */
415 log(DEBUG,"Target type is UNKNOWN, bailing");
418 std::string mode_sequence = parameters[1];
419 std::string parameter = "";
420 std::ostringstream parameter_list;
421 std::string output_sequence = "";
422 bool adding = true, state_change = false;
424 int parameter_counter = 2; /* Index of first parameter */
426 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
428 unsigned char modechar = *letter;
433 log(DEBUG,"Iterate mode letter %c",modechar);
436 * For + and - mode characters, we don't just stick the character into the output sequence.
437 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
438 * appearing in the output sequence, we store a flag which says there was a state change,
439 * which is set on any + or -, however, the + or - that we finish on is only appended to
440 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
443 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
444 * however, will allow the + if it is the first item in the sequence, regardless.
446 if ((!adding) || (!output_sequence.length()))
452 if ((adding) || (!output_sequence.length()))
460 * Watch carefully for the sleight of hand trick.
461 * 65 is the ascii value of 'A'. We take this from
462 * the char we're looking at to get a number between
463 * 1 and 127. We then logic-or it to get the hashed
464 * position, dependent on wether its a channel or
465 * a user mode. This is a little stranger, but a lot
466 * faster, than using a map of pairs.
468 handler_id = (modechar - 65) | mask;
470 if (modehandlers[handler_id])
474 log(DEBUG,"Found a ModeHandler* for mode %c",modechar);
476 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
478 log(DEBUG,"Call a ModeWatcher*");
479 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
482 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
484 log(DEBUG,"Modetype match, calling handler");
486 if (modehandlers[handler_id]->GetNumParams(adding))
488 log(DEBUG,"ModeHandler* for this mode says it has parameters. pcnt=%d parameter_counter=%d",pcnt,parameter_counter);
490 if (parameter_counter < pcnt)
492 parameter = parameters[parameter_counter++];
496 /* No parameter, continue to the next mode */
500 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
502 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter == ""))
504 /* The handler nuked the parameter and they are supposed to have one.
505 * We CANT continue now, even if they actually returned MODEACTION_ALLOW,
506 * so we bail to the next mode character.
511 if (ma == MODEACTION_ALLOW)
513 log(DEBUG,"ModeAction was allow");
515 /* We're about to output a valid mode letter - was there previously a pending state-change? */
518 log(DEBUG,"Appending state change");
519 output_sequence.append(adding ? "+" : "-");
522 /* Add the mode letter */
523 output_sequence.push_back(modechar);
524 log(DEBUG,"Added mode letter to output sequence, sequence now: '%s'",output_sequence.c_str());
526 /* Is there a valid parameter for this mode? If so add it to the parameter list */
527 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
529 log(DEBUG,"Added parameter to parameter_list, list now: '%s'",parameter_list.str().c_str());
530 parameter_list << " " << parameter;
533 /* Call all the AfterMode events in the mode watchers for this mode */
534 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
536 log(DEBUG,"Called a ModeWatcher* after event");
537 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
540 /* Reset the state change flag */
541 state_change = false;
548 /* Was there at least one valid mode in the sequence? */
549 if (output_sequence != "")
553 if (type == MODETYPE_CHANNEL)
555 WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
560 if (type == MODETYPE_CHANNEL)
562 log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
563 WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
564 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
572 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
577 ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
582 void ModeParser::CleanMask(std::string &mask)
584 std::string::size_type pos_of_pling = mask.find_first_of('!');
585 std::string::size_type pos_of_at = mask.find_first_of('@');
586 std::string::size_type pos_of_dot = mask.find_first_of('.');
587 std::string::size_type pos_of_colon = mask.find_first_of(':'); /* Because ipv6 addresses are colon delimited */
589 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
591 /* Just a nick, or just a host */
592 if ((pos_of_dot == std::string::npos) && (pos_of_colon == std::string::npos))
594 /* It has no '.' in it, it must be a nick. */
599 /* Got a dot in it? Has to be a host */
600 mask = "*!*@" + mask;
603 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
605 /* Has an @ but no !, its a user@host */
608 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
610 /* Has a ! but no @, it must be a nick!ident */
615 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
617 unsigned char mask = 0;
618 unsigned char pos = 0;
620 /* Yes, i know, this might let people declare modes like '_' or '^'.
621 * If they do that, thats their problem, and if i ever EVER see an
622 * official InspIRCd developer do that, i'll beat them with a paddle!
624 if ((modeletter < 'A') || (modeletter > 'z'))
627 mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
628 pos = (modeletter-65) | mask;
630 if (modehandlers[pos])
633 modehandlers[pos] = mh;
634 log(DEBUG,"ModeParser::AddMode: added mode %c",modeletter);
638 ModeParser::ModeParser()
640 /* Clear mode list */
641 memset(modehandlers, 0, sizeof(modehandlers));
642 memset(modewatchers, 0, sizeof(modewatchers));
644 /* Initialise the RFC mode letters */
645 this->AddMode(new ModeChannelSecret, 's');
646 this->AddMode(new ModeChannelPrivate, 'p');
647 this->AddMode(new ModeChannelBan, 'b');
648 this->AddMode(new ModeChannelModerated, 'm');