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"
53 /* +t (only (half) ops can change topic) */
54 #include "modes/cmode_t.h"
55 /* +n (no external messages) */
56 #include "modes/cmode_n.h"
57 /* +i (invite only) */
58 #include "modes/cmode_i.h"
61 extern std::vector<Module*> modules;
62 extern std::vector<ircd_module*> factory;
63 extern InspIRCd* ServerInstance;
64 extern ServerConfig* Config;
68 ModeHandler::ModeHandler(char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly)
69 : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
73 ModeHandler::~ModeHandler()
77 bool ModeHandler::IsListMode()
82 ModeType ModeHandler::GetModeType()
87 bool ModeHandler::NeedsOper()
92 int ModeHandler::GetNumParams(bool adding)
94 return adding ? n_params_on : n_params_off;
97 char ModeHandler::GetModeChar()
102 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
104 return MODEACTION_DENY;
107 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
111 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
113 return (ours < theirs);
116 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
120 ModeWatcher::~ModeWatcher()
124 char ModeWatcher::GetModeChar()
129 ModeType ModeWatcher::GetModeType()
134 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type)
139 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
143 userrec* ModeParser::SanityChecks(userrec *user,char *dest,chanrec *chan,int status)
146 if ((!user) || (!dest) || (!chan) || (!*dest))
153 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
159 char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
164 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
166 if (((ucrec*)(*i))->channel == chan)
168 if (((ucrec*)(*i))->uc_modes & MASK)
172 ((ucrec*)(*i))->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
176 ((ucrec*)(*i))->channel->AddOppedUser(d);
179 ((ucrec*)(*i))->channel->AddHalfoppedUser(d);
182 ((ucrec*)(*i))->channel->AddVoicedUser(d);
185 log(DEBUG,"grant: %s %s",((ucrec*)(*i))->channel->name,d->nick);
192 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
197 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
199 if (((ucrec*)(*i))->channel == chan)
201 if ((((ucrec*)(*i))->uc_modes & MASK) == 0)
205 ((ucrec*)(*i))->uc_modes ^= MASK;
209 ((ucrec*)(*i))->channel->DelOppedUser(d);
212 ((ucrec*)(*i))->channel->DelHalfoppedUser(d);
215 ((ucrec*)(*i))->channel->DelVoicedUser(d);
218 log(DEBUG,"revoke: %s %s",((ucrec*)(*i))->channel->name,d->nick);
225 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
227 userrec *d = this->SanityChecks(user,dest,chan,status);
234 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
236 if (MOD_RESULT == ACR_DENY)
238 if (MOD_RESULT == ACR_DEFAULT)
240 if ((status < STATUS_OP) && (!is_uline(user->server)))
242 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
248 return this->Grant(d,chan,UCMODE_OP);
253 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
255 userrec *d = this->SanityChecks(user,dest,chan,status);
262 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
264 if (MOD_RESULT == ACR_DENY)
266 if (MOD_RESULT == ACR_DEFAULT)
268 if ((status < STATUS_OP) && (!is_uline(user->server)))
270 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
276 return this->Grant(d,chan,UCMODE_HOP);
281 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
283 userrec *d = this->SanityChecks(user,dest,chan,status);
290 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
292 if (MOD_RESULT == ACR_DENY)
294 if (MOD_RESULT == ACR_DEFAULT)
296 if ((status < STATUS_HOP) && (!is_uline(user->server)))
298 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
304 return this->Grant(d,chan,UCMODE_VOICE);
309 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
311 userrec *d = this->SanityChecks(user,dest,chan,status);
318 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
320 if (MOD_RESULT == ACR_DENY)
322 if (MOD_RESULT == ACR_DEFAULT)
324 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
326 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
332 return this->Revoke(d,chan,UCMODE_OP);
337 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
339 userrec *d = this->SanityChecks(user,dest,chan,status);
346 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
348 if (MOD_RESULT == ACR_DENY)
350 if (MOD_RESULT == ACR_DEFAULT)
352 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
353 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
355 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
361 return this->Revoke(d,chan,UCMODE_HOP);
366 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
368 userrec *d = this->SanityChecks(user,dest,chan,status);
375 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
377 if (MOD_RESULT == ACR_DENY)
379 if (MOD_RESULT == ACR_DEFAULT)
381 if ((status < STATUS_HOP) && (!is_uline(user->server)))
383 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
389 return this->Revoke(d,chan,UCMODE_VOICE);
394 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
396 std::string target = parameters[0];
397 ModeType type = MODETYPE_USER;
398 unsigned char mask = 0;
399 chanrec* targetchannel = FindChan(parameters[0]);
400 userrec* targetuser = Find(parameters[0]);
402 log(DEBUG,"ModeParser::Process start");
408 log(DEBUG,"Target type is CHANNEL");
409 type = MODETYPE_CHANNEL;
414 log(DEBUG,"Target type is USER");
415 type = MODETYPE_USER;
420 /* No such nick/channel */
421 log(DEBUG,"Target type is UNKNOWN, bailing");
424 std::string mode_sequence = parameters[1];
425 std::string parameter = "";
426 std::ostringstream parameter_list;
427 std::string output_sequence = "";
428 bool adding = true, state_change = false;
430 int parameter_counter = 2; /* Index of first parameter */
432 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
434 unsigned char modechar = *letter;
439 log(DEBUG,"Iterate mode letter %c",modechar);
442 * For + and - mode characters, we don't just stick the character into the output sequence.
443 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
444 * appearing in the output sequence, we store a flag which says there was a state change,
445 * which is set on any + or -, however, the + or - that we finish on is only appended to
446 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
449 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
450 * however, will allow the + if it is the first item in the sequence, regardless.
452 if ((!adding) || (!output_sequence.length()))
458 if ((adding) || (!output_sequence.length()))
466 * Watch carefully for the sleight of hand trick.
467 * 65 is the ascii value of 'A'. We take this from
468 * the char we're looking at to get a number between
469 * 1 and 127. We then logic-or it to get the hashed
470 * position, dependent on wether its a channel or
471 * a user mode. This is a little stranger, but a lot
472 * faster, than using a map of pairs.
474 handler_id = (modechar - 65) | mask;
476 if (modehandlers[handler_id])
480 log(DEBUG,"Found a ModeHandler* for mode %c",modechar);
482 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
484 log(DEBUG,"Call a ModeWatcher*");
485 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
488 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
490 log(DEBUG,"Modetype match, calling handler");
492 if (modehandlers[handler_id]->GetNumParams(adding))
494 log(DEBUG,"ModeHandler* for this mode says it has parameters. pcnt=%d parameter_counter=%d",pcnt,parameter_counter);
496 if (parameter_counter < pcnt)
498 parameter = parameters[parameter_counter++];
502 /* No parameter, continue to the next mode */
506 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
508 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter == ""))
510 /* The handler nuked the parameter and they are supposed to have one.
511 * We CANT continue now, even if they actually returned MODEACTION_ALLOW,
512 * so we bail to the next mode character.
517 if (ma == MODEACTION_ALLOW)
519 log(DEBUG,"ModeAction was allow");
521 /* We're about to output a valid mode letter - was there previously a pending state-change? */
524 log(DEBUG,"Appending state change");
525 output_sequence.append(adding ? "+" : "-");
528 /* Add the mode letter */
529 output_sequence.push_back(modechar);
530 log(DEBUG,"Added mode letter to output sequence, sequence now: '%s'",output_sequence.c_str());
532 /* Is there a valid parameter for this mode? If so add it to the parameter list */
533 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
535 log(DEBUG,"Added parameter to parameter_list, list now: '%s'",parameter_list.str().c_str());
536 parameter_list << " " << parameter;
539 /* Call all the AfterMode events in the mode watchers for this mode */
540 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
542 log(DEBUG,"Called a ModeWatcher* after event");
543 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
546 /* Reset the state change flag */
547 state_change = false;
554 /* Was there at least one valid mode in the sequence? */
555 if (output_sequence != "")
559 if (type == MODETYPE_CHANNEL)
561 WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
566 if (type == MODETYPE_CHANNEL)
568 log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
569 WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
570 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
578 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
583 ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
588 void ModeParser::CleanMask(std::string &mask)
590 std::string::size_type pos_of_pling = mask.find_first_of('!');
591 std::string::size_type pos_of_at = mask.find_first_of('@');
592 std::string::size_type pos_of_dot = mask.find_first_of('.');
593 std::string::size_type pos_of_colon = mask.find_first_of(':'); /* Because ipv6 addresses are colon delimited */
595 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
597 /* Just a nick, or just a host */
598 if ((pos_of_dot == std::string::npos) && (pos_of_colon == std::string::npos))
600 /* It has no '.' in it, it must be a nick. */
605 /* Got a dot in it? Has to be a host */
606 mask = "*!*@" + mask;
609 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
611 /* Has an @ but no !, its a user@host */
614 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
616 /* Has a ! but no @, it must be a nick!ident */
621 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
623 unsigned char mask = 0;
624 unsigned char pos = 0;
626 /* Yes, i know, this might let people declare modes like '_' or '^'.
627 * If they do that, thats their problem, and if i ever EVER see an
628 * official InspIRCd developer do that, i'll beat them with a paddle!
630 if ((modeletter < 'A') || (modeletter > 'z'))
633 mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
634 pos = (modeletter-65) | mask;
636 if (modehandlers[pos])
639 modehandlers[pos] = mh;
640 log(DEBUG,"ModeParser::AddMode: added mode %c",modeletter);
644 ModeParser::ModeParser()
646 /* Clear mode list */
647 memset(modehandlers, 0, sizeof(modehandlers));
648 memset(modewatchers, 0, sizeof(modewatchers));
650 /* Initialise the RFC mode letters */
651 this->AddMode(new ModeChannelSecret, 's');
652 this->AddMode(new ModeChannelPrivate, 'p');
653 this->AddMode(new ModeChannelBan, 'b');
654 this->AddMode(new ModeChannelModerated, 'm');
655 this->AddMode(new ModeChannelTopicOps, 't');
656 this->AddMode(new ModeChannelNoExternal, 'n');
657 this->AddMode(new ModeChannelInviteOnly, 'i');
658 /* TODO: Modes +l, +k, +o, +v, +h */