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"
27 #include "inspstring.h"
28 #include "helperfuncs.h"
33 #include "modes/cmode_s.h"
35 #include "modes/cmode_p.h"
37 #include "modes/cmode_b.h"
39 #include "modes/cmode_m.h"
40 /* +t (only (half) ops can change topic) */
41 #include "modes/cmode_t.h"
42 /* +n (no external messages) */
43 #include "modes/cmode_n.h"
44 /* +i (invite only) */
45 #include "modes/cmode_i.h"
46 /* +k (keyed channel) */
47 #include "modes/cmode_k.h"
48 /* +l (channel user limit) */
49 #include "modes/cmode_l.h"
51 #include "modes/cmode_o.h"
52 /* +h (channel halfop) */
53 #include "modes/cmode_h.h"
54 /* +v (channel voice) */
55 #include "modes/cmode_v.h"
57 /* +s (server notices) */
58 #include "modes/umode_s.h"
59 /* +w (see wallops) */
60 #include "modes/umode_w.h"
62 #include "modes/umode_i.h"
64 #include "modes/umode_o.h"
65 /* +n (notice mask - our implementation of snomasks) */
66 #include "modes/umode_n.h"
69 extern std::vector<Module*> modules;
70 extern std::vector<ircd_module*> factory;
72 extern InspIRCd* ServerInstance;
76 ModeHandler::ModeHandler(char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly)
77 : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
81 ModeHandler::~ModeHandler()
85 bool ModeHandler::IsListMode()
90 ModeType ModeHandler::GetModeType()
95 bool ModeHandler::NeedsOper()
100 int ModeHandler::GetNumParams(bool adding)
102 return adding ? n_params_on : n_params_off;
105 char ModeHandler::GetModeChar()
110 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
112 return MODEACTION_DENY;
115 ModePair ModeHandler::ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
119 return std::make_pair(dest->IsModeSet(this->mode), "");
123 return std::make_pair(channel->IsModeSet(this->mode), "");
127 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
131 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
133 return (ours < theirs);
136 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
140 ModeWatcher::~ModeWatcher()
144 char ModeWatcher::GetModeChar()
149 ModeType ModeWatcher::GetModeType()
154 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type)
159 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
163 userrec* ModeParser::SanityChecks(userrec *user,const char *dest,chanrec *chan,int status)
166 if ((!user) || (!dest) || (!chan) || (!*dest))
170 d = ServerInstance->FindNick(dest);
173 user->WriteServ("401 %s %s :No such nick/channel",user->nick, dest);
179 const char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
184 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
186 ucrec* n = (ucrec*)(*i);
187 if (n->channel == chan)
189 if (n->uc_modes & MASK)
193 n->uc_modes = n->uc_modes | MASK;
197 n->channel->AddOppedUser(d);
200 n->channel->AddHalfoppedUser(d);
203 n->channel->AddVoicedUser(d);
206 log(DEBUG,"grant: %s %s",n->channel->name,d->nick);
213 const char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
218 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
220 ucrec* n = (ucrec*)(*i);
221 if (n->channel == chan)
223 if ((n->uc_modes & MASK) == 0)
231 n->channel->DelOppedUser(d);
234 n->channel->DelHalfoppedUser(d);
237 n->channel->DelVoicedUser(d);
240 log(DEBUG,"revoke: %s %s",n->channel->name,d->nick);
247 void ModeParser::DisplayCurrentModes(userrec *user, userrec* targetuser, chanrec* targetchannel, const char* text)
251 /* Display channel's current mode string */
252 user->WriteServ("324 %s %s +%s",user->nick, targetchannel->name, targetchannel->ChanModes(targetchannel->HasUser(user)));
253 user->WriteServ("329 %s %s %d", user->nick, targetchannel->name, targetchannel->created);
258 /* Display user's current mode string */
259 user->WriteServ("221 %s :+%s",targetuser->nick,targetuser->FormatModes());
260 user->WriteServ("008 %s :+%s", targetuser->nick, targetuser->FormatNoticeMasks());
264 /* No such nick/channel */
265 user->WriteServ("401 %s %s :No such nick/channel",user->nick, text);
269 void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool servermode)
271 std::string target = parameters[0];
272 ModeType type = MODETYPE_USER;
273 unsigned char mask = 0;
274 chanrec* targetchannel = ServerInstance->FindChan(parameters[0]);
275 userrec* targetuser = ServerInstance->FindNick(parameters[0]);
277 log(DEBUG,"ModeParser::Process start");
279 /* Special case for displaying the list for listmodes,
280 * e.g. MODE #chan b, or MODE #chan +b without a parameter
282 if ((targetchannel) && (pcnt == 2))
284 const char* mode = parameters[1];
287 unsigned char handler_id = ((*mode) - 65) | MASK_CHANNEL;
288 ModeHandler* mh = modehandlers[handler_id];
289 if ((mh) && (mh->IsListMode()))
291 mh->DisplayList(user, targetchannel);
298 this->DisplayCurrentModes(user, targetuser, targetchannel, parameters[0]);
304 type = MODETYPE_CHANNEL;
307 /* Extra security checks on channel modes
308 * (e.g. are they a (half)op?
311 if ((IS_LOCAL(user)) && (targetchannel->GetStatus(user) < STATUS_HOP))
313 /* We don't have halfop */
314 log(DEBUG,"The user is not a halfop or above, checking other reasons for being able to set the modes");
316 /* Are we a uline or is it a servermode? */
317 if ((!is_uline(user->server)) && (!servermode))
319 /* Not enough permission:
320 * NOT a uline and NOT a servermode,
321 * OR, NOT halfop or above.
323 user->WriteServ("482 %s %s :You're not a channel (half)operator",user->nick, targetchannel->name);
330 type = MODETYPE_USER;
335 /* No such nick/channel */
336 user->WriteServ("401 %s %s :No such nick/channel",user->nick, parameters[0]);
340 std::string mode_sequence = parameters[1];
341 std::string parameter = "";
342 std::ostringstream parameter_list;
343 std::string output_sequence = "";
344 bool adding = true, state_change = false;
345 unsigned char handler_id = 0;
346 int parameter_counter = 2; /* Index of first parameter */
348 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
350 unsigned char modechar = *letter;
355 * For + and - mode characters, we don't just stick the character into the output sequence.
356 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
357 * appearing in the output sequence, we store a flag which says there was a state change,
358 * which is set on any + or -, however, the + or - that we finish on is only appended to
359 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
362 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
363 * however, will allow the + if it is the first item in the sequence, regardless.
365 if ((!adding) || (!output_sequence.length()))
371 if ((adding) || (!output_sequence.length()))
379 * Watch carefully for the sleight of hand trick.
380 * 65 is the ascii value of 'A'. We take this from
381 * the char we're looking at to get a number between
382 * 1 and 127. We then logic-or it to get the hashed
383 * position, dependent on wether its a channel or
384 * a user mode. This is a little stranger, but a lot
385 * faster, than using a map of pairs.
387 handler_id = (modechar - 65) | mask;
389 if (modehandlers[handler_id])
393 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
395 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
398 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
400 if (modehandlers[handler_id]->GetNumParams(adding))
402 /* This mode expects a parameter, do we have any parameters left in our list to use? */
403 if (parameter_counter < pcnt)
405 parameter = parameters[parameter_counter++];
409 /* No parameter, continue to the next mode */
414 /* It's an oper only mode, check if theyre an oper. If they arent,
415 * eat any parameter that came with the mode, and continue to next
417 if ((IS_LOCAL(user)) && (modehandlers[handler_id]->NeedsOper()) && (!*user->oper))
420 /* Call the handler for the mode */
421 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
423 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter == ""))
425 /* The handler nuked the parameter and they are supposed to have one.
426 * We CANT continue now, even if they actually returned MODEACTION_ALLOW,
427 * so we bail to the next mode character.
432 if (ma == MODEACTION_ALLOW)
434 /* We're about to output a valid mode letter - was there previously a pending state-change? */
436 output_sequence.append(adding ? "+" : "-");
438 /* Add the mode letter */
439 output_sequence.push_back(modechar);
441 /* Is there a valid parameter for this mode? If so add it to the parameter list */
442 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
443 parameter_list << " " << parameter;
445 /* Call all the AfterMode events in the mode watchers for this mode */
446 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
447 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
449 /* Reset the state change flag */
450 state_change = false;
456 /* No mode handler? Unknown mode character then. */
457 user->WriteServ("472 %s %c :is unknown mode char to me",user->nick, modechar);
462 /* Was there at least one valid mode in the sequence? */
463 if (output_sequence != "")
467 if (type == MODETYPE_CHANNEL)
469 targetchannel->WriteChannelWithServ(ServerInstance->Config->ServerName, "MODE %s %s%s", targetchannel->name, output_sequence.c_str(), parameter_list.str().c_str());
473 targetuser->WriteServ("MODE %s %s",targetuser->nick,output_sequence.c_str());
478 if (type == MODETYPE_CHANNEL)
480 log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
481 targetchannel->WriteChannel(user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
482 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
486 user->WriteTo(targetuser,"MODE %s %s",targetuser->nick,output_sequence.c_str());
487 FOREACH_MOD(I_OnMode,OnMode(user, targetuser, TYPE_USER, output_sequence));
495 void cmd_mode::Handle (const char** parameters, int pcnt, userrec *user)
500 ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
505 void ModeParser::CleanMask(std::string &mask)
507 std::string::size_type pos_of_pling = mask.find_first_of('!');
508 std::string::size_type pos_of_at = mask.find_first_of('@');
509 std::string::size_type pos_of_dot = mask.find_first_of('.');
510 std::string::size_type pos_of_colon = mask.find_first_of(':'); /* Because ipv6 addresses are colon delimited */
512 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
514 /* Just a nick, or just a host */
515 if ((pos_of_dot == std::string::npos) && (pos_of_colon == std::string::npos))
517 /* It has no '.' in it, it must be a nick. */
522 /* Got a dot in it? Has to be a host */
523 mask = "*!*@" + mask;
526 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
528 /* Has an @ but no !, its a user@host */
531 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
533 /* Has a ! but no @, it must be a nick!ident */
538 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
540 unsigned char mask = 0;
541 unsigned char pos = 0;
543 /* Yes, i know, this might let people declare modes like '_' or '^'.
544 * If they do that, thats their problem, and if i ever EVER see an
545 * official InspIRCd developer do that, i'll beat them with a paddle!
547 if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z'))
550 mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
551 pos = (mh->GetModeChar()-65) | mask;
553 if (modehandlers[pos])
556 modehandlers[pos] = mh;
557 log(DEBUG,"ModeParser::AddMode: added mode %c",mh->GetModeChar());
561 ModeHandler* ModeParser::FindMode(unsigned const char modeletter, ModeType mt)
563 unsigned char mask = 0;
564 unsigned char pos = 0;
566 if ((modeletter < 'A') || (modeletter > 'z'))
569 mt == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
570 pos = (modeletter-65) | mask;
572 return modehandlers[pos];
575 std::string ModeParser::UserModeList()
580 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
582 unsigned char pos = (mode-65) | MASK_USER;
584 if (modehandlers[pos])
585 modestr[pointer++] = mode;
587 modestr[pointer++] = 0;
591 std::string ModeParser::ChannelModeList()
596 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
598 unsigned char pos = (mode-65) | MASK_CHANNEL;
600 if (modehandlers[pos])
601 modestr[pointer++] = mode;
603 modestr[pointer++] = 0;
607 std::string ModeParser::ParaModeList()
612 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
614 unsigned char pos = (mode-65) | MASK_CHANNEL;
616 if ((modehandlers[pos]) && (modehandlers[pos]->GetNumParams(true)))
617 modestr[pointer++] = mode;
619 modestr[pointer++] = 0;
623 bool ModeParser::AddModeWatcher(ModeWatcher* mw)
625 unsigned char mask = 0;
626 unsigned char pos = 0;
631 if ((mw->GetModeChar() < 'A') || (mw->GetModeChar() > 'z'))
634 mw->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
635 pos = (mw->GetModeChar()-65) | mask;
637 modewatchers[pos].push_back(mw);
638 log(DEBUG,"ModeParser::AddModeWatcher: watching mode %c",mw->GetModeChar());
643 bool ModeParser::DelModeWatcher(ModeWatcher* mw)
645 unsigned char mask = 0;
646 unsigned char pos = 0;
651 if ((mw->GetModeType() < 'A') || (mw->GetModeType() > 'z'))
654 mw->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
655 pos = (mw->GetModeChar()-65) | mask;
657 ModeWatchIter a = find(modewatchers[pos].begin(),modewatchers[pos].end(),mw);
659 if (a == modewatchers[pos].end())
662 modewatchers[pos].erase(a);
663 log(DEBUG,"ModeParser::DelModeWatcher: stopped watching mode %c",mw->GetModeChar());
668 ModeParser::ModeParser()
670 /* Clear mode list */
671 memset(modehandlers, 0, sizeof(modehandlers));
672 memset(modewatchers, 0, sizeof(modewatchers));
674 /* Initialise the RFC mode letters */
676 /* Start with channel simple modes, no params */
677 this->AddMode(new ModeChannelSecret, 's');
678 this->AddMode(new ModeChannelPrivate, 'p');
679 this->AddMode(new ModeChannelModerated, 'm');
680 this->AddMode(new ModeChannelTopicOps, 't');
681 this->AddMode(new ModeChannelNoExternal, 'n');
682 this->AddMode(new ModeChannelInviteOnly, 'i');
684 /* Cannel modes with params */
685 this->AddMode(new ModeChannelKey, 'k');
686 this->AddMode(new ModeChannelLimit, 'l');
688 /* Channel listmodes */
689 this->AddMode(new ModeChannelBan, 'b');
690 this->AddMode(new ModeChannelOp, 'o');
691 this->AddMode(new ModeChannelHalfOp, 'h');
692 this->AddMode(new ModeChannelVoice, 'v');
694 /* Now for usermodes */
695 this->AddMode(new ModeUserServerNotice, 's');
696 this->AddMode(new ModeUserWallops, 'w');
697 this->AddMode(new ModeUserInvisible, 'i');
698 this->AddMode(new ModeUserOperator, 'o');
699 this->AddMode(new ModeUserServerNoticeMask, 'n');