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"
53 /* +h (channel halfop) */
54 #include "modes/cmode_h.h"
55 /* +v (channel voice) */
56 #include "modes/cmode_v.h"
59 extern std::vector<Module*> modules;
60 extern std::vector<ircd_module*> factory;
61 extern InspIRCd* ServerInstance;
62 extern ServerConfig* Config;
66 ModeHandler::ModeHandler(char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly)
67 : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
71 ModeHandler::~ModeHandler()
75 bool ModeHandler::IsListMode()
80 ModeType ModeHandler::GetModeType()
85 bool ModeHandler::NeedsOper()
90 int ModeHandler::GetNumParams(bool adding)
92 return adding ? n_params_on : n_params_off;
95 char ModeHandler::GetModeChar()
100 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
102 return MODEACTION_DENY;
105 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
109 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
111 return (ours < theirs);
114 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
118 ModeWatcher::~ModeWatcher()
122 char ModeWatcher::GetModeChar()
127 ModeType ModeWatcher::GetModeType()
132 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type)
137 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
141 userrec* ModeParser::SanityChecks(userrec *user,const char *dest,chanrec *chan,int status)
144 if ((!user) || (!dest) || (!chan) || (!*dest))
151 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
157 const char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
162 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
164 ucrec* n = (ucrec*)(*i);
165 if (n->channel == chan)
167 if (n->uc_modes & MASK)
171 n->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
175 n->channel->AddOppedUser(d);
178 n->channel->AddHalfoppedUser(d);
181 n->channel->AddVoicedUser(d);
184 log(DEBUG,"grant: %s %s",n->channel->name,d->nick);
191 const char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
196 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
198 ucrec* n = (ucrec*)(*i);
199 if (n->channel == chan)
201 if ((n->uc_modes & MASK) == 0)
209 n->channel->DelOppedUser(d);
212 n->channel->DelHalfoppedUser(d);
215 n->channel->DelVoicedUser(d);
218 log(DEBUG,"revoke: %s %s",n->channel->name,d->nick);
226 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
228 userrec *d = this->SanityChecks(user,dest,chan,status);
235 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
237 if (MOD_RESULT == ACR_DENY)
239 if (MOD_RESULT == ACR_DEFAULT)
241 if ((status < STATUS_HOP) && (!is_uline(user->server)))
243 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
249 return this->Grant(d,chan,UCMODE_VOICE);
254 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
256 userrec *d = this->SanityChecks(user,dest,chan,status);
263 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
265 if (MOD_RESULT == ACR_DENY)
267 if (MOD_RESULT == ACR_DEFAULT)
269 if ((status < STATUS_HOP) && (!is_uline(user->server)))
271 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
277 return this->Revoke(d,chan,UCMODE_VOICE);
282 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
284 std::string target = parameters[0];
285 ModeType type = MODETYPE_USER;
286 unsigned char mask = 0;
287 chanrec* targetchannel = FindChan(parameters[0]);
288 userrec* targetuser = Find(parameters[0]);
290 log(DEBUG,"ModeParser::Process start");
296 log(DEBUG,"Target type is CHANNEL");
297 type = MODETYPE_CHANNEL;
300 /* Extra security checks on channel modes
301 * (e.g. are they a (half)op?
304 if (cstatus(user, targetchannel) < STATUS_HOP)
306 /* We don't have halfop */
307 log(DEBUG,"The user is not a halfop or above, checking other reasons for being able to set the modes");
309 /* Are we a uline or is it a servermode? */
310 if ((!is_uline(user->server)) && (!servermode))
312 /* Not enough permission:
313 * NOT a uline and NOT a servermode,
314 * OR, NOT halfop or above.
316 WriteServ(user->fd,"482 %s %s :You're not a channel (half)operator",user->nick, targetchannel->name);
323 log(DEBUG,"Target type is USER");
324 type = MODETYPE_USER;
329 /* No such nick/channel */
330 log(DEBUG,"Target type is UNKNOWN, bailing");
333 std::string mode_sequence = parameters[1];
334 std::string parameter = "";
335 std::ostringstream parameter_list;
336 std::string output_sequence = "";
337 bool adding = true, state_change = false;
339 int parameter_counter = 2; /* Index of first parameter */
341 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
343 unsigned char modechar = *letter;
348 * For + and - mode characters, we don't just stick the character into the output sequence.
349 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
350 * appearing in the output sequence, we store a flag which says there was a state change,
351 * which is set on any + or -, however, the + or - that we finish on is only appended to
352 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
355 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
356 * however, will allow the + if it is the first item in the sequence, regardless.
358 if ((!adding) || (!output_sequence.length()))
364 if ((adding) || (!output_sequence.length()))
372 * Watch carefully for the sleight of hand trick.
373 * 65 is the ascii value of 'A'. We take this from
374 * the char we're looking at to get a number between
375 * 1 and 127. We then logic-or it to get the hashed
376 * position, dependent on wether its a channel or
377 * a user mode. This is a little stranger, but a lot
378 * faster, than using a map of pairs.
380 handler_id = (modechar - 65) | mask;
382 if (modehandlers[handler_id])
386 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
388 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
391 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
393 if (modehandlers[handler_id]->GetNumParams(adding))
395 /* This mode expects a parameter, do we have any parameters left in our list to use? */
396 if (parameter_counter < pcnt)
398 parameter = parameters[parameter_counter++];
402 /* No parameter, continue to the next mode */
407 /* Call the handler for the mode */
408 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
410 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter == ""))
412 /* The handler nuked the parameter and they are supposed to have one.
413 * We CANT continue now, even if they actually returned MODEACTION_ALLOW,
414 * so we bail to the next mode character.
419 if (ma == MODEACTION_ALLOW)
421 /* We're about to output a valid mode letter - was there previously a pending state-change? */
423 output_sequence.append(adding ? "+" : "-");
425 /* Add the mode letter */
426 output_sequence.push_back(modechar);
428 /* Is there a valid parameter for this mode? If so add it to the parameter list */
429 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
430 parameter_list << " " << parameter;
432 /* Call all the AfterMode events in the mode watchers for this mode */
433 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
434 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
436 /* Reset the state change flag */
437 state_change = false;
443 /* No mode handler? Unknown mode character then. */
444 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick, modechar);
449 /* Was there at least one valid mode in the sequence? */
450 if (output_sequence != "")
454 if (type == MODETYPE_CHANNEL)
456 WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
461 if (type == MODETYPE_CHANNEL)
463 log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
464 WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
465 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
473 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
478 ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
483 void ModeParser::CleanMask(std::string &mask)
485 std::string::size_type pos_of_pling = mask.find_first_of('!');
486 std::string::size_type pos_of_at = mask.find_first_of('@');
487 std::string::size_type pos_of_dot = mask.find_first_of('.');
488 std::string::size_type pos_of_colon = mask.find_first_of(':'); /* Because ipv6 addresses are colon delimited */
490 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
492 /* Just a nick, or just a host */
493 if ((pos_of_dot == std::string::npos) && (pos_of_colon == std::string::npos))
495 /* It has no '.' in it, it must be a nick. */
500 /* Got a dot in it? Has to be a host */
501 mask = "*!*@" + mask;
504 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
506 /* Has an @ but no !, its a user@host */
509 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
511 /* Has a ! but no @, it must be a nick!ident */
516 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
518 unsigned char mask = 0;
519 unsigned char pos = 0;
521 /* Yes, i know, this might let people declare modes like '_' or '^'.
522 * If they do that, thats their problem, and if i ever EVER see an
523 * official InspIRCd developer do that, i'll beat them with a paddle!
525 if ((modeletter < 'A') || (modeletter > 'z'))
528 mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
529 pos = (modeletter-65) | mask;
531 if (modehandlers[pos])
534 modehandlers[pos] = mh;
535 log(DEBUG,"ModeParser::AddMode: added mode %c",modeletter);
539 ModeParser::ModeParser()
541 /* Clear mode list */
542 memset(modehandlers, 0, sizeof(modehandlers));
543 memset(modewatchers, 0, sizeof(modewatchers));
545 /* Initialise the RFC mode letters */
547 /* Start with channel simple modes, no params */
548 this->AddMode(new ModeChannelSecret, 's');
549 this->AddMode(new ModeChannelPrivate, 'p');
550 this->AddMode(new ModeChannelModerated, 'm');
551 this->AddMode(new ModeChannelTopicOps, 't');
552 this->AddMode(new ModeChannelNoExternal, 'n');
553 this->AddMode(new ModeChannelInviteOnly, 'i');
555 /* Cannel modes with params */
556 this->AddMode(new ModeChannelKey, 'k');
557 this->AddMode(new ModeChannelLimit, 'l');
559 /* Channel listmodes */
560 this->AddMode(new ModeChannelBan, 'b');
561 this->AddMode(new ModeChannelOp, 'o');
562 this->AddMode(new ModeChannelHalfOp, 'h');
563 this->AddMode(new ModeChannelVoice, 'v');
565 /* TODO: User modes +swio */