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"
68 extern InspIRCd* ServerInstance;
72 ModeHandler::ModeHandler(char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly)
73 : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
77 ModeHandler::~ModeHandler()
81 bool ModeHandler::IsListMode()
86 ModeType ModeHandler::GetModeType()
91 bool ModeHandler::NeedsOper()
96 int ModeHandler::GetNumParams(bool adding)
98 return adding ? n_params_on : n_params_off;
101 char ModeHandler::GetModeChar()
106 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
108 return MODEACTION_DENY;
111 ModePair ModeHandler::ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
115 return std::make_pair(dest->IsModeSet(this->mode), "");
119 return std::make_pair(channel->IsModeSet(this->mode), "");
123 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
127 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
129 return (ours < theirs);
132 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
136 ModeWatcher::~ModeWatcher()
140 char ModeWatcher::GetModeChar()
145 ModeType ModeWatcher::GetModeType()
150 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type)
155 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
159 userrec* ModeParser::SanityChecks(userrec *user,const char *dest,chanrec *chan,int status)
162 if ((!user) || (!dest) || (!chan) || (!*dest))
166 d = ServerInstance->FindNick(dest);
169 user->WriteServ("401 %s %s :No such nick/channel",user->nick, dest);
175 const char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
180 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
182 ucrec* n = (ucrec*)(*i);
183 if (n->channel == chan)
185 if (n->uc_modes & MASK)
189 n->uc_modes = n->uc_modes | MASK;
193 n->channel->AddOppedUser(d);
196 n->channel->AddHalfoppedUser(d);
199 n->channel->AddVoicedUser(d);
202 log(DEBUG,"grant: %s %s",n->channel->name,d->nick);
209 const char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
214 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
216 ucrec* n = (ucrec*)(*i);
217 if (n->channel == chan)
219 if ((n->uc_modes & MASK) == 0)
227 n->channel->DelOppedUser(d);
230 n->channel->DelHalfoppedUser(d);
233 n->channel->DelVoicedUser(d);
236 log(DEBUG,"revoke: %s %s",n->channel->name,d->nick);
243 void ModeParser::DisplayCurrentModes(userrec *user, userrec* targetuser, chanrec* targetchannel, const char* text)
247 /* Display channel's current mode string */
248 user->WriteServ("324 %s %s +%s",user->nick, targetchannel->name, targetchannel->ChanModes(targetchannel->HasUser(user)));
249 user->WriteServ("329 %s %s %d", user->nick, targetchannel->name, targetchannel->created);
254 /* Display user's current mode string */
255 user->WriteServ("221 %s :+%s",targetuser->nick,targetuser->FormatModes());
256 user->WriteServ("008 %s :+%s", targetuser->nick, targetuser->FormatNoticeMasks());
260 /* No such nick/channel */
261 user->WriteServ("401 %s %s :No such nick/channel",user->nick, text);
265 void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool servermode)
267 std::string target = parameters[0];
268 ModeType type = MODETYPE_USER;
269 unsigned char mask = 0;
270 chanrec* targetchannel = ServerInstance->FindChan(parameters[0]);
271 userrec* targetuser = ServerInstance->FindNick(parameters[0]);
273 log(DEBUG,"ModeParser::Process start");
275 /* Special case for displaying the list for listmodes,
276 * e.g. MODE #chan b, or MODE #chan +b without a parameter
278 if ((targetchannel) && (pcnt == 2))
280 const char* mode = parameters[1];
283 unsigned char handler_id = ((*mode) - 65) | MASK_CHANNEL;
284 ModeHandler* mh = modehandlers[handler_id];
285 if ((mh) && (mh->IsListMode()))
287 mh->DisplayList(user, targetchannel);
294 this->DisplayCurrentModes(user, targetuser, targetchannel, parameters[0]);
300 type = MODETYPE_CHANNEL;
303 /* Extra security checks on channel modes
304 * (e.g. are they a (half)op?
307 if ((IS_LOCAL(user)) && (targetchannel->GetStatus(user) < STATUS_HOP))
309 /* We don't have halfop */
310 log(DEBUG,"The user is not a halfop or above, checking other reasons for being able to set the modes");
312 /* Are we a uline or is it a servermode? */
313 if ((!is_uline(user->server)) && (!servermode))
315 /* Not enough permission:
316 * NOT a uline and NOT a servermode,
317 * OR, NOT halfop or above.
319 user->WriteServ("482 %s %s :You're not a channel (half)operator",user->nick, targetchannel->name);
326 type = MODETYPE_USER;
331 /* No such nick/channel */
332 user->WriteServ("401 %s %s :No such nick/channel",user->nick, parameters[0]);
336 std::string mode_sequence = parameters[1];
337 std::string parameter = "";
338 std::ostringstream parameter_list;
339 std::string output_sequence = "";
340 bool adding = true, state_change = false;
341 unsigned char handler_id = 0;
342 int parameter_counter = 2; /* Index of first parameter */
344 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
346 unsigned char modechar = *letter;
351 * For + and - mode characters, we don't just stick the character into the output sequence.
352 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
353 * appearing in the output sequence, we store a flag which says there was a state change,
354 * which is set on any + or -, however, the + or - that we finish on is only appended to
355 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
358 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
359 * however, will allow the + if it is the first item in the sequence, regardless.
361 if ((!adding) || (!output_sequence.length()))
367 if ((adding) || (!output_sequence.length()))
375 * Watch carefully for the sleight of hand trick.
376 * 65 is the ascii value of 'A'. We take this from
377 * the char we're looking at to get a number between
378 * 1 and 127. We then logic-or it to get the hashed
379 * position, dependent on wether its a channel or
380 * a user mode. This is a little stranger, but a lot
381 * faster, than using a map of pairs.
383 handler_id = (modechar - 65) | mask;
385 if (modehandlers[handler_id])
389 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
391 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
394 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
396 if (modehandlers[handler_id]->GetNumParams(adding))
398 /* This mode expects a parameter, do we have any parameters left in our list to use? */
399 if (parameter_counter < pcnt)
401 parameter = parameters[parameter_counter++];
405 /* No parameter, continue to the next mode */
410 /* It's an oper only mode, check if theyre an oper. If they arent,
411 * eat any parameter that came with the mode, and continue to next
413 if ((IS_LOCAL(user)) && (modehandlers[handler_id]->NeedsOper()) && (!*user->oper))
416 /* Call the handler for the mode */
417 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
419 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter == ""))
421 /* The handler nuked the parameter and they are supposed to have one.
422 * We CANT continue now, even if they actually returned MODEACTION_ALLOW,
423 * so we bail to the next mode character.
428 if (ma == MODEACTION_ALLOW)
430 /* We're about to output a valid mode letter - was there previously a pending state-change? */
432 output_sequence.append(adding ? "+" : "-");
434 /* Add the mode letter */
435 output_sequence.push_back(modechar);
437 /* Is there a valid parameter for this mode? If so add it to the parameter list */
438 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
439 parameter_list << " " << parameter;
441 /* Call all the AfterMode events in the mode watchers for this mode */
442 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
443 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
445 /* Reset the state change flag */
446 state_change = false;
452 /* No mode handler? Unknown mode character then. */
453 user->WriteServ("472 %s %c :is unknown mode char to me",user->nick, modechar);
458 /* Was there at least one valid mode in the sequence? */
459 if (output_sequence != "")
463 if (type == MODETYPE_CHANNEL)
465 targetchannel->WriteChannelWithServ(ServerInstance->Config->ServerName, "MODE %s %s%s", targetchannel->name, output_sequence.c_str(), parameter_list.str().c_str());
469 targetuser->WriteServ("MODE %s %s",targetuser->nick,output_sequence.c_str());
474 if (type == MODETYPE_CHANNEL)
476 log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
477 targetchannel->WriteChannel(user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
478 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
482 user->WriteTo(targetuser,"MODE %s %s",targetuser->nick,output_sequence.c_str());
483 FOREACH_MOD(I_OnMode,OnMode(user, targetuser, TYPE_USER, output_sequence));
491 void cmd_mode::Handle (const char** parameters, int pcnt, userrec *user)
496 ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
501 void ModeParser::CleanMask(std::string &mask)
503 std::string::size_type pos_of_pling = mask.find_first_of('!');
504 std::string::size_type pos_of_at = mask.find_first_of('@');
505 std::string::size_type pos_of_dot = mask.find_first_of('.');
506 std::string::size_type pos_of_colon = mask.find_first_of(':'); /* Because ipv6 addresses are colon delimited */
508 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
510 /* Just a nick, or just a host */
511 if ((pos_of_dot == std::string::npos) && (pos_of_colon == std::string::npos))
513 /* It has no '.' in it, it must be a nick. */
518 /* Got a dot in it? Has to be a host */
519 mask = "*!*@" + mask;
522 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
524 /* Has an @ but no !, its a user@host */
527 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
529 /* Has a ! but no @, it must be a nick!ident */
534 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
536 unsigned char mask = 0;
537 unsigned char pos = 0;
539 /* Yes, i know, this might let people declare modes like '_' or '^'.
540 * If they do that, thats their problem, and if i ever EVER see an
541 * official InspIRCd developer do that, i'll beat them with a paddle!
543 if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z'))
546 mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
547 pos = (mh->GetModeChar()-65) | mask;
549 if (modehandlers[pos])
552 modehandlers[pos] = mh;
553 log(DEBUG,"ModeParser::AddMode: added mode %c",mh->GetModeChar());
557 ModeHandler* ModeParser::FindMode(unsigned const char modeletter, ModeType mt)
559 unsigned char mask = 0;
560 unsigned char pos = 0;
562 if ((modeletter < 'A') || (modeletter > 'z'))
565 mt == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
566 pos = (modeletter-65) | mask;
568 return modehandlers[pos];
571 std::string ModeParser::UserModeList()
576 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
578 unsigned char pos = (mode-65) | MASK_USER;
580 if (modehandlers[pos])
581 modestr[pointer++] = mode;
583 modestr[pointer++] = 0;
587 std::string ModeParser::ChannelModeList()
592 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
594 unsigned char pos = (mode-65) | MASK_CHANNEL;
596 if (modehandlers[pos])
597 modestr[pointer++] = mode;
599 modestr[pointer++] = 0;
603 std::string ModeParser::ParaModeList()
608 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
610 unsigned char pos = (mode-65) | MASK_CHANNEL;
612 if ((modehandlers[pos]) && (modehandlers[pos]->GetNumParams(true)))
613 modestr[pointer++] = mode;
615 modestr[pointer++] = 0;
619 bool ModeParser::AddModeWatcher(ModeWatcher* mw)
621 unsigned char mask = 0;
622 unsigned char pos = 0;
627 if ((mw->GetModeChar() < 'A') || (mw->GetModeChar() > 'z'))
630 mw->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
631 pos = (mw->GetModeChar()-65) | mask;
633 modewatchers[pos].push_back(mw);
634 log(DEBUG,"ModeParser::AddModeWatcher: watching mode %c",mw->GetModeChar());
639 bool ModeParser::DelModeWatcher(ModeWatcher* mw)
641 unsigned char mask = 0;
642 unsigned char pos = 0;
647 if ((mw->GetModeType() < 'A') || (mw->GetModeType() > 'z'))
650 mw->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
651 pos = (mw->GetModeChar()-65) | mask;
653 ModeWatchIter a = find(modewatchers[pos].begin(),modewatchers[pos].end(),mw);
655 if (a == modewatchers[pos].end())
658 modewatchers[pos].erase(a);
659 log(DEBUG,"ModeParser::DelModeWatcher: stopped watching mode %c",mw->GetModeChar());
664 ModeParser::ModeParser()
666 /* Clear mode list */
667 memset(modehandlers, 0, sizeof(modehandlers));
668 memset(modewatchers, 0, sizeof(modewatchers));
670 /* Initialise the RFC mode letters */
672 /* Start with channel simple modes, no params */
673 this->AddMode(new ModeChannelSecret, 's');
674 this->AddMode(new ModeChannelPrivate, 'p');
675 this->AddMode(new ModeChannelModerated, 'm');
676 this->AddMode(new ModeChannelTopicOps, 't');
677 this->AddMode(new ModeChannelNoExternal, 'n');
678 this->AddMode(new ModeChannelInviteOnly, 'i');
680 /* Cannel modes with params */
681 this->AddMode(new ModeChannelKey, 'k');
682 this->AddMode(new ModeChannelLimit, 'l');
684 /* Channel listmodes */
685 this->AddMode(new ModeChannelBan, 'b');
686 this->AddMode(new ModeChannelOp, 'o');
687 this->AddMode(new ModeChannelHalfOp, 'h');
688 this->AddMode(new ModeChannelVoice, 'v');
690 /* Now for usermodes */
691 this->AddMode(new ModeUserServerNotice, 's');
692 this->AddMode(new ModeUserWallops, 'w');
693 this->AddMode(new ModeUserInvisible, 'i');
694 this->AddMode(new ModeUserOperator, 'o');
695 this->AddMode(new ModeUserServerNoticeMask, 'n');
698 bool ModeParser::InsertMode(std::string &output, const char* mode, unsigned short section)
700 unsigned short currsection = 1;
701 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
703 if(section > 4 || section == 0)
705 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
709 for(; pos < output.size(); pos++)
711 if(section == currsection)
714 if(output[pos] == ',')
718 output.insert(pos, mode);