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"
24 #include "connection.h"
28 #include "inspstring.h"
29 #include "helperfuncs.h"
34 #include "modes/cmode_s.h"
36 #include "modes/cmode_p.h"
38 #include "modes/cmode_b.h"
40 #include "modes/cmode_m.h"
41 /* +t (only (half) ops can change topic) */
42 #include "modes/cmode_t.h"
43 /* +n (no external messages) */
44 #include "modes/cmode_n.h"
45 /* +i (invite only) */
46 #include "modes/cmode_i.h"
47 /* +k (keyed channel) */
48 #include "modes/cmode_k.h"
49 /* +l (channel user limit) */
50 #include "modes/cmode_l.h"
52 #include "modes/cmode_o.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,const 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 const 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 ucrec* n = (ucrec*)(*i);
161 if (n->channel == chan)
163 if (n->uc_modes & MASK)
167 n->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
171 n->channel->AddOppedUser(d);
174 n->channel->AddHalfoppedUser(d);
177 n->channel->AddVoicedUser(d);
180 log(DEBUG,"grant: %s %s",n->channel->name,d->nick);
187 const char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
192 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
194 ucrec* n = (ucrec*)(*i);
195 if (n->channel == chan)
197 if ((n->uc_modes & MASK) == 0)
205 n->channel->DelOppedUser(d);
208 n->channel->DelHalfoppedUser(d);
211 n->channel->DelVoicedUser(d);
214 log(DEBUG,"revoke: %s %s",n->channel->name,d->nick);
221 /*char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
223 userrec *d = this->SanityChecks(user,dest,chan,status);
230 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
232 if (MOD_RESULT == ACR_DENY)
234 if (MOD_RESULT == ACR_DEFAULT)
236 if ((status < STATUS_OP) && (!is_uline(user->server)))
238 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
244 return this->Grant(d,chan,UCMODE_HOP);
249 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
251 userrec *d = this->SanityChecks(user,dest,chan,status);
258 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
260 if (MOD_RESULT == ACR_DENY)
262 if (MOD_RESULT == ACR_DEFAULT)
264 if ((status < STATUS_HOP) && (!is_uline(user->server)))
266 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
272 return this->Grant(d,chan,UCMODE_VOICE);
277 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
279 userrec *d = this->SanityChecks(user,dest,chan,status);
286 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
288 if (MOD_RESULT == ACR_DENY)
290 if (MOD_RESULT == ACR_DEFAULT)
292 // Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves
293 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
295 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
301 return this->Revoke(d,chan,UCMODE_HOP);
306 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
308 userrec *d = this->SanityChecks(user,dest,chan,status);
315 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
317 if (MOD_RESULT == ACR_DENY)
319 if (MOD_RESULT == ACR_DEFAULT)
321 if ((status < STATUS_HOP) && (!is_uline(user->server)))
323 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
329 return this->Revoke(d,chan,UCMODE_VOICE);
334 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
336 std::string target = parameters[0];
337 ModeType type = MODETYPE_USER;
338 unsigned char mask = 0;
339 chanrec* targetchannel = FindChan(parameters[0]);
340 userrec* targetuser = Find(parameters[0]);
342 log(DEBUG,"ModeParser::Process start");
348 log(DEBUG,"Target type is CHANNEL");
349 type = MODETYPE_CHANNEL;
352 /* Extra security checks on channel modes
353 * (e.g. are they a (half)op?
356 if (((!is_uline(user->server)) && (!servermode)) || (cstatus(user, targetchannel) < STATUS_HOP))
358 /* Not enough permission:
359 * NOT a uline and NOT a servermode,
360 * OR, NOT halfop or above.
362 WriteServ(user->fd,"482 %s %s :You're not a channel (half)operator",user->nick, targetchannel->name);
368 log(DEBUG,"Target type is USER");
369 type = MODETYPE_USER;
374 /* No such nick/channel */
375 log(DEBUG,"Target type is UNKNOWN, bailing");
378 std::string mode_sequence = parameters[1];
379 std::string parameter = "";
380 std::ostringstream parameter_list;
381 std::string output_sequence = "";
382 bool adding = true, state_change = false;
384 int parameter_counter = 2; /* Index of first parameter */
386 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
388 unsigned char modechar = *letter;
393 log(DEBUG,"Iterate mode letter %c",modechar);
396 * For + and - mode characters, we don't just stick the character into the output sequence.
397 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
398 * appearing in the output sequence, we store a flag which says there was a state change,
399 * which is set on any + or -, however, the + or - that we finish on is only appended to
400 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
403 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
404 * however, will allow the + if it is the first item in the sequence, regardless.
406 if ((!adding) || (!output_sequence.length()))
412 if ((adding) || (!output_sequence.length()))
420 * Watch carefully for the sleight of hand trick.
421 * 65 is the ascii value of 'A'. We take this from
422 * the char we're looking at to get a number between
423 * 1 and 127. We then logic-or it to get the hashed
424 * position, dependent on wether its a channel or
425 * a user mode. This is a little stranger, but a lot
426 * faster, than using a map of pairs.
428 handler_id = (modechar - 65) | mask;
430 if (modehandlers[handler_id])
434 log(DEBUG,"Found a ModeHandler* for mode %c",modechar);
436 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
438 log(DEBUG,"Call a ModeWatcher*");
439 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
442 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
444 log(DEBUG,"Modetype match, calling handler");
446 if (modehandlers[handler_id]->GetNumParams(adding))
448 log(DEBUG,"ModeHandler* for this mode says it has parameters. pcnt=%d parameter_counter=%d",pcnt,parameter_counter);
450 if (parameter_counter < pcnt)
452 parameter = parameters[parameter_counter++];
456 /* No parameter, continue to the next mode */
460 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
462 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter == ""))
464 /* The handler nuked the parameter and they are supposed to have one.
465 * We CANT continue now, even if they actually returned MODEACTION_ALLOW,
466 * so we bail to the next mode character.
471 if (ma == MODEACTION_ALLOW)
473 log(DEBUG,"ModeAction was allow");
475 /* We're about to output a valid mode letter - was there previously a pending state-change? */
478 log(DEBUG,"Appending state change");
479 output_sequence.append(adding ? "+" : "-");
482 /* Add the mode letter */
483 output_sequence.push_back(modechar);
484 log(DEBUG,"Added mode letter to output sequence, sequence now: '%s'",output_sequence.c_str());
486 /* Is there a valid parameter for this mode? If so add it to the parameter list */
487 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
489 log(DEBUG,"Added parameter to parameter_list, list now: '%s'",parameter_list.str().c_str());
490 parameter_list << " " << parameter;
493 /* Call all the AfterMode events in the mode watchers for this mode */
494 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
496 log(DEBUG,"Called a ModeWatcher* after event");
497 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
500 /* Reset the state change flag */
501 state_change = false;
508 /* Was there at least one valid mode in the sequence? */
509 if (output_sequence != "")
513 if (type == MODETYPE_CHANNEL)
515 WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
520 if (type == MODETYPE_CHANNEL)
522 log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
523 WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
524 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
532 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
537 ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
542 void ModeParser::CleanMask(std::string &mask)
544 std::string::size_type pos_of_pling = mask.find_first_of('!');
545 std::string::size_type pos_of_at = mask.find_first_of('@');
546 std::string::size_type pos_of_dot = mask.find_first_of('.');
547 std::string::size_type pos_of_colon = mask.find_first_of(':'); /* Because ipv6 addresses are colon delimited */
549 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
551 /* Just a nick, or just a host */
552 if ((pos_of_dot == std::string::npos) && (pos_of_colon == std::string::npos))
554 /* It has no '.' in it, it must be a nick. */
559 /* Got a dot in it? Has to be a host */
560 mask = "*!*@" + mask;
563 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
565 /* Has an @ but no !, its a user@host */
568 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
570 /* Has a ! but no @, it must be a nick!ident */
575 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
577 unsigned char mask = 0;
578 unsigned char pos = 0;
580 /* Yes, i know, this might let people declare modes like '_' or '^'.
581 * If they do that, thats their problem, and if i ever EVER see an
582 * official InspIRCd developer do that, i'll beat them with a paddle!
584 if ((modeletter < 'A') || (modeletter > 'z'))
587 mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
588 pos = (modeletter-65) | mask;
590 if (modehandlers[pos])
593 modehandlers[pos] = mh;
594 log(DEBUG,"ModeParser::AddMode: added mode %c",modeletter);
598 ModeParser::ModeParser()
600 /* Clear mode list */
601 memset(modehandlers, 0, sizeof(modehandlers));
602 memset(modewatchers, 0, sizeof(modewatchers));
604 /* Initialise the RFC mode letters */
606 /* Start with simple modes, no params */
607 this->AddMode(new ModeChannelSecret, 's');
608 this->AddMode(new ModeChannelPrivate, 'p');
609 this->AddMode(new ModeChannelModerated, 'm');
610 this->AddMode(new ModeChannelTopicOps, 't');
611 this->AddMode(new ModeChannelNoExternal, 'n');
612 this->AddMode(new ModeChannelInviteOnly, 'i');
614 /* Now modes with params */
615 this->AddMode(new ModeChannelKey, 'k');
616 this->AddMode(new ModeChannelLimit, 'l');
619 this->AddMode(new ModeChannelBan, 'b');
620 this->AddMode(new ModeChannelOp, 'o');
622 /* TODO: Modes +v, +h */