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 modeletter = mode_sequence.begin(); modeletter != mode_sequence.end(); modeletter++)
431 log(DEBUG,"Iterate mode letter %c",*modeletter);
434 * For + and - mode characters, we don't just stick the character into the output sequence.
435 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
436 * appearing in the output sequence, we store a flag which says there was a state change,
437 * which is set on any + or -, however, the + or - that we finish on is only appended to
438 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
441 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
442 * however, will allow the + if it is the first item in the sequence, regardless.
444 if ((!adding) || (!output_sequence.length()))
450 if ((adding) || (!output_sequence.length()))
458 * Watch carefully for the sleight of hand trick.
459 * 65 is the ascii value of 'A'. We take this from
460 * the char we're looking at to get a number between
461 * 1 and 127. We then logic-or it to get the hashed
462 * position, dependent on wether its a channel or
463 * a user mode. This is a little stranger, but a lot
464 * faster, than using a map of pairs.
466 handler_id = (*modeletter - 65) | mask;
468 if (modehandlers[handler_id])
472 log(DEBUG,"Found a ModeHandler* for mode %c",*modeletter);
474 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
476 log(DEBUG,"Call a ModeWatcher*");
477 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
480 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
482 log(DEBUG,"Modetype match, calling handler");
484 if (modehandlers[handler_id]->GetNumParams(adding))
486 log(DEBUG,"ModeHandler* for this mode says it has parameters. pcnt=%d parameter_counter=%d",pcnt,parameter_counter);
488 if (parameter_counter < pcnt)
490 parameter = parameters[parameter_counter++];
494 /* No parameter, continue to the next mode */
498 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
499 if (ma == MODEACTION_ALLOW)
501 log(DEBUG,"ModeAction was allow");
503 /* We're about to output a valid mode letter - was there previously a pending state-change? */
506 log(DEBUG,"Appending state change");
507 output_sequence.append(adding ? "+" : "-");
510 /* Add the mode letter */
511 output_sequence = output_sequence + *modeletter;
512 log(DEBUG,"Added mode letter to output sequence, sequence now: '%s'",output_sequence.c_str());
514 /* Is there a valid parameter for this mode? If so add it to the parameter list */
515 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
517 log(DEBUG,"Added parameter to parameter_list, list now: '%s'",parameter_list.str().c_str());
518 parameter_list << " " << parameter;
521 /* Call all the AfterMode events in the mode watchers for this mode */
522 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
524 log(DEBUG,"Called a ModeWatcher* after event");
525 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
528 /* Reset the state change flag */
529 state_change = false;
536 /* Was there at least one valid mode in the sequence? */
537 if (output_sequence != "")
541 if (type == MODETYPE_CHANNEL)
543 WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
548 if (type == MODETYPE_CHANNEL)
550 log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
551 WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
552 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
560 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
565 ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
570 void ModeParser::CleanMask(std::string &mask)
572 std::string::size_type pos_of_pling = mask.find_first_of('!');
573 std::string::size_type pos_of_at = mask.find_first_of('@');
576 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
578 unsigned char mask = 0;
579 unsigned char pos = 0;
581 /* Yes, i know, this might let people declare modes like '_' or '^'.
582 * If they do that, thats their problem, and if i ever EVER see an
583 * official InspIRCd developer do that, i'll beat them with a paddle!
585 if ((modeletter < 'A') || (modeletter > 'z'))
588 mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
589 pos = (modeletter-65) | mask;
591 if (modehandlers[pos])
594 modehandlers[pos] = mh;
595 log(DEBUG,"ModeParser::AddMode: added mode %c",modeletter);
599 ModeParser::ModeParser()
601 /* Clear mode list */
602 memset(modehandlers, 0, sizeof(modehandlers));
603 memset(modewatchers, 0, sizeof(modewatchers));
605 /* Initialise the RFC mode letters */
606 this->AddMode(new ModeChannelSecret, 's');
607 this->AddMode(new ModeChannelPrivate, 'p');
608 this->AddMode(new ModeChannelBan, 'b');
609 this->AddMode(new ModeChannelModerated, 'm');