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"
49 #include "modes/cmode_s.h"
50 #include "modes/cmode_p.h"
53 extern std::vector<Module*> modules;
54 extern std::vector<ircd_module*> factory;
55 extern InspIRCd* ServerInstance;
56 extern ServerConfig* Config;
60 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)
64 ModeHandler::~ModeHandler()
68 bool ModeHandler::IsListMode()
73 ModeType ModeHandler::GetModeType()
78 bool ModeHandler::NeedsOper()
83 int ModeHandler::GetNumParams(bool adding)
85 return adding ? n_params_on : n_params_off;
88 char ModeHandler::GetModeChar()
93 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
95 return MODEACTION_DENY;
98 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
102 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
104 return (ours < theirs);
107 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
111 ModeWatcher::~ModeWatcher()
115 char ModeWatcher::GetModeChar()
120 ModeType ModeWatcher::GetModeType()
125 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type)
130 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
134 userrec* ModeParser::SanityChecks(userrec *user,char *dest,chanrec *chan,int status)
137 if ((!user) || (!dest) || (!chan) || (!*dest))
144 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
150 char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
155 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
157 if (((ucrec*)(*i))->channel == chan)
159 if (((ucrec*)(*i))->uc_modes & MASK)
163 ((ucrec*)(*i))->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
167 ((ucrec*)(*i))->channel->AddOppedUser(d);
170 ((ucrec*)(*i))->channel->AddHalfoppedUser(d);
173 ((ucrec*)(*i))->channel->AddVoicedUser(d);
176 log(DEBUG,"grant: %s %s",((ucrec*)(*i))->channel->name,d->nick);
183 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
188 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
190 if (((ucrec*)(*i))->channel == chan)
192 if ((((ucrec*)(*i))->uc_modes & MASK) == 0)
196 ((ucrec*)(*i))->uc_modes ^= MASK;
200 ((ucrec*)(*i))->channel->DelOppedUser(d);
203 ((ucrec*)(*i))->channel->DelHalfoppedUser(d);
206 ((ucrec*)(*i))->channel->DelVoicedUser(d);
209 log(DEBUG,"revoke: %s %s",((ucrec*)(*i))->channel->name,d->nick);
216 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
218 userrec *d = this->SanityChecks(user,dest,chan,status);
225 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
227 if (MOD_RESULT == ACR_DENY)
229 if (MOD_RESULT == ACR_DEFAULT)
231 if ((status < STATUS_OP) && (!is_uline(user->server)))
233 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
239 return this->Grant(d,chan,UCMODE_OP);
244 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
246 userrec *d = this->SanityChecks(user,dest,chan,status);
253 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
255 if (MOD_RESULT == ACR_DENY)
257 if (MOD_RESULT == ACR_DEFAULT)
259 if ((status < STATUS_OP) && (!is_uline(user->server)))
261 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
267 return this->Grant(d,chan,UCMODE_HOP);
272 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
274 userrec *d = this->SanityChecks(user,dest,chan,status);
281 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
283 if (MOD_RESULT == ACR_DENY)
285 if (MOD_RESULT == ACR_DEFAULT)
287 if ((status < STATUS_HOP) && (!is_uline(user->server)))
289 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
295 return this->Grant(d,chan,UCMODE_VOICE);
300 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
302 userrec *d = this->SanityChecks(user,dest,chan,status);
309 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
311 if (MOD_RESULT == ACR_DENY)
313 if (MOD_RESULT == ACR_DEFAULT)
315 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
317 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
323 return this->Revoke(d,chan,UCMODE_OP);
328 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
330 userrec *d = this->SanityChecks(user,dest,chan,status);
337 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
339 if (MOD_RESULT == ACR_DENY)
341 if (MOD_RESULT == ACR_DEFAULT)
343 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
344 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
346 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
352 return this->Revoke(d,chan,UCMODE_HOP);
357 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
359 userrec *d = this->SanityChecks(user,dest,chan,status);
366 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
368 if (MOD_RESULT == ACR_DENY)
370 if (MOD_RESULT == ACR_DEFAULT)
372 if ((status < STATUS_HOP) && (!is_uline(user->server)))
374 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
380 return this->Revoke(d,chan,UCMODE_VOICE);
385 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
387 std::string target = parameters[0];
388 ModeType type = MODETYPE_USER;
389 unsigned char mask = 0;
390 chanrec* targetchannel = FindChan(parameters[0]);
391 userrec* targetuser = Find(parameters[0]);
393 log(DEBUG,"ModeParser::Process start");
399 log(DEBUG,"Target type is CHANNEL");
400 type = MODETYPE_CHANNEL;
405 log(DEBUG,"Target type is USER");
406 type = MODETYPE_USER;
411 /* No such nick/channel */
412 log(DEBUG,"Target type is UNKNOWN, bailing");
415 std::string mode_sequence = parameters[1];
416 std::string parameter = "";
417 std::ostringstream parameter_list;
418 std::string output_sequence = "";
419 bool adding = true, state_change = false;
421 int parameter_counter = 2; /* Index of first parameter */
423 for (std::string::const_iterator modeletter = mode_sequence.begin(); modeletter != mode_sequence.end(); modeletter++)
428 log(DEBUG,"Iterate mode letter %c",*modeletter);
431 * For + and - mode characters, we don't just stick the character into the output sequence.
432 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
433 * appearing in the output sequence, we store a flag which says there was a state change,
434 * which is set on any + or -, however, the + or - that we finish on is only appended to
435 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
438 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
439 * however, will allow the + if it is the first item in the sequence, regardless.
441 if ((!adding) || (!output_sequence.length()))
447 if ((adding) || (!output_sequence.length()))
455 * Watch carefully for the sleight of hand trick.
456 * 65 is the ascii value of 'A'. We take this from
457 * the char we're looking at to get a number between
458 * 1 and 127. We then logic-or it to get the hashed
459 * position, dependent on wether its a channel or
460 * a user mode. This is a little stranger, but a lot
461 * faster, than using a map of pairs.
463 handler_id = (*modeletter - 65) | mask;
465 if (modehandlers[handler_id])
469 log(DEBUG,"Found a ModeHandler* for mode %c",*modeletter);
471 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
473 log(DEBUG,"Call a ModeWatcher*");
474 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
477 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
479 log(DEBUG,"Modetype match, calling handler");
481 if (modehandlers[handler_id]->GetNumParams(adding))
483 log(DEBUG,"ModeHandler* for this mode says it has parameters");
485 if (pcnt < parameter_counter)
487 parameter = parameters[parameter_counter++];
494 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
495 if (ma == MODEACTION_ALLOW)
497 log(DEBUG,"ModeAction was allow");
499 /* We're about to output a valid mode letter - was there previously a pending state-change? */
502 log(DEBUG,"Appending state change");
503 output_sequence.append(adding ? "+" : "-");
506 /* Add the mode letter */
507 output_sequence = output_sequence + *modeletter;
508 log(DEBUG,"Added mode letter to output sequence, sequence now: '%s'",output_sequence.c_str());
510 /* Is there a valid parameter for this mode? If so add it to the parameter list */
511 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
513 log(DEBUG,"Added parameter to parameter_list, list now: '%s'",parameter_list.str().c_str());
514 parameter_list << " " << parameter;
517 /* Call all the AfterMode events in the mode watchers for this mode */
518 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
520 log(DEBUG,"Called a ModeWatcher* after event");
521 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
524 /* Reset the state change flag */
525 state_change = false;
532 /* Was there at least one valid mode in the sequence? */
533 if (output_sequence != "")
537 if (type == MODETYPE_CHANNEL)
539 WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
544 if (type == MODETYPE_CHANNEL)
546 log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
547 WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
548 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
556 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
562 char outpars[MAXBUF];
568 ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
573 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
575 unsigned char mask = 0;
576 unsigned char pos = 0;
578 /* Yes, i know, this might let people declare modes like '_' or '^'.
579 * If they do that, thats their problem, and if i ever EVER see an
580 * official InspIRCd developer do that, i'll beat them with a paddle!
582 if ((modeletter < 'A') || (modeletter > 'z'))
585 mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
586 pos = (modeletter-65) | mask;
588 if (modehandlers[pos])
591 modehandlers[pos] = mh;
595 ModeParser::ModeParser()
597 /* Clear mode list */
598 memset(modehandlers, 0, sizeof(modehandlers));
599 memset(modewatchers, 0, sizeof(modewatchers));
601 /* Initialise the RFC mode letters */
602 this->AddMode(new ModeChannelSecret, 's');
603 this->AddMode(new ModeChannelPrivate, 'p');