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"
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 ModeHandler::ModeHandler(InspIRCd* Instance, char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly)
69 : ServerInstance(Instance), mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
73 ModeHandler::~ModeHandler()
77 bool ModeHandler::IsListMode()
82 ModeType ModeHandler::GetModeType()
87 bool ModeHandler::NeedsOper()
92 int ModeHandler::GetNumParams(bool adding)
94 return adding ? n_params_on : n_params_off;
97 char ModeHandler::GetModeChar()
102 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
104 return MODEACTION_DENY;
107 ModePair ModeHandler::ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
111 return std::make_pair(dest->IsModeSet(this->mode), "");
115 return std::make_pair(channel->IsModeSet(this->mode), "");
119 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
123 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
125 return (ours < theirs);
128 ModeWatcher::ModeWatcher(InspIRCd* Instance, char modeletter, ModeType type) : ServerInstance(Instance), mode(modeletter), m_type(type)
132 ModeWatcher::~ModeWatcher()
136 char ModeWatcher::GetModeChar()
141 ModeType ModeWatcher::GetModeType()
146 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type)
151 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
155 userrec* ModeParser::SanityChecks(userrec *user,const char *dest,chanrec *chan,int status)
158 if ((!user) || (!dest) || (!chan) || (!*dest))
162 d = ServerInstance->FindNick(dest);
165 user->WriteServ("401 %s %s :No such nick/channel",user->nick, dest);
171 const char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
176 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
178 ucrec* n = (ucrec*)(*i);
179 if (n->channel == chan)
181 if (n->uc_modes & MASK)
185 n->uc_modes = n->uc_modes | MASK;
189 n->channel->AddOppedUser(d);
192 n->channel->AddHalfoppedUser(d);
195 n->channel->AddVoicedUser(d);
198 ServerInstance->Log(DEBUG,"grant: %s %s",n->channel->name,d->nick);
205 const char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
210 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
212 ucrec* n = (ucrec*)(*i);
213 if (n->channel == chan)
215 if ((n->uc_modes & MASK) == 0)
223 n->channel->DelOppedUser(d);
226 n->channel->DelHalfoppedUser(d);
229 n->channel->DelVoicedUser(d);
232 ServerInstance->Log(DEBUG,"revoke: %s %s",n->channel->name,d->nick);
239 void ModeParser::DisplayCurrentModes(userrec *user, userrec* targetuser, chanrec* targetchannel, const char* text)
243 /* Display channel's current mode string */
244 user->WriteServ("324 %s %s +%s",user->nick, targetchannel->name, targetchannel->ChanModes(targetchannel->HasUser(user)));
245 user->WriteServ("329 %s %s %d", user->nick, targetchannel->name, targetchannel->created);
250 /* Display user's current mode string */
251 user->WriteServ("221 %s :+%s",targetuser->nick,targetuser->FormatModes());
252 user->WriteServ("008 %s :+%s", targetuser->nick, targetuser->FormatNoticeMasks());
256 /* No such nick/channel */
257 user->WriteServ("401 %s %s :No such nick/channel",user->nick, text);
261 void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool servermode)
263 std::string target = parameters[0];
264 ModeType type = MODETYPE_USER;
265 unsigned char mask = 0;
266 chanrec* targetchannel = ServerInstance->FindChan(parameters[0]);
267 userrec* targetuser = ServerInstance->FindNick(parameters[0]);
269 ServerInstance->Log(DEBUG,"ModeParser::Process start");
271 /* Special case for displaying the list for listmodes,
272 * e.g. MODE #chan b, or MODE #chan +b without a parameter
274 if ((targetchannel) && (pcnt == 2))
276 const char* mode = parameters[1];
279 unsigned char handler_id = ((*mode) - 65) | MASK_CHANNEL;
280 ModeHandler* mh = modehandlers[handler_id];
281 if ((mh) && (mh->IsListMode()))
283 mh->DisplayList(user, targetchannel);
290 this->DisplayCurrentModes(user, targetuser, targetchannel, parameters[0]);
296 type = MODETYPE_CHANNEL;
299 /* Extra security checks on channel modes
300 * (e.g. are they a (half)op?
303 if ((IS_LOCAL(user)) && (targetchannel->GetStatus(user) < STATUS_HOP))
305 /* We don't have halfop */
306 ServerInstance->Log(DEBUG,"The user is not a halfop or above, checking other reasons for being able to set the modes");
308 /* Are we a uline or is it a servermode? */
309 if ((!ServerInstance->ULine(user->server)) && (!servermode))
311 /* Not enough permission:
312 * NOT a uline and NOT a servermode,
313 * OR, NOT halfop or above.
315 user->WriteServ("482 %s %s :You're not a channel (half)operator",user->nick, targetchannel->name);
322 type = MODETYPE_USER;
327 /* No such nick/channel */
328 user->WriteServ("401 %s %s :No such nick/channel",user->nick, parameters[0]);
332 std::string mode_sequence = parameters[1];
333 std::string parameter = "";
334 std::ostringstream parameter_list;
335 std::string output_sequence = "";
336 bool adding = true, state_change = false;
337 unsigned char handler_id = 0;
338 int parameter_counter = 2; /* Index of first parameter */
340 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
342 unsigned char modechar = *letter;
347 * For + and - mode characters, we don't just stick the character into the output sequence.
348 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
349 * appearing in the output sequence, we store a flag which says there was a state change,
350 * which is set on any + or -, however, the + or - that we finish on is only appended to
351 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
354 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
355 * however, will allow the + if it is the first item in the sequence, regardless.
357 if ((!adding) || (!output_sequence.length()))
363 if ((adding) || (!output_sequence.length()))
371 * Watch carefully for the sleight of hand trick.
372 * 65 is the ascii value of 'A'. We take this from
373 * the char we're looking at to get a number between
374 * 1 and 127. We then logic-or it to get the hashed
375 * position, dependent on wether its a channel or
376 * a user mode. This is a little stranger, but a lot
377 * faster, than using a map of pairs.
379 handler_id = (modechar - 65) | mask;
381 if (modehandlers[handler_id])
385 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
387 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
390 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
392 if (modehandlers[handler_id]->GetNumParams(adding))
394 /* This mode expects a parameter, do we have any parameters left in our list to use? */
395 if (parameter_counter < pcnt)
397 parameter = parameters[parameter_counter++];
400 if ((parameter.rfind(':') || (parameter.rfind(' '))))
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 ServerInstance->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->Modes->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 ServerInstance->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 ServerInstance->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 ServerInstance->Log(DEBUG,"ModeParser::DelModeWatcher: stopped watching mode %c",mw->GetModeChar());
664 ModeParser::ModeParser(InspIRCd* Instance) : ServerInstance(Instance)
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(Instance), 's');
674 this->AddMode(new ModeChannelPrivate(Instance), 'p');
675 this->AddMode(new ModeChannelModerated(Instance), 'm');
676 this->AddMode(new ModeChannelTopicOps(Instance), 't');
677 this->AddMode(new ModeChannelNoExternal(Instance), 'n');
678 this->AddMode(new ModeChannelInviteOnly(Instance), 'i');
680 /* Cannel modes with params */
681 this->AddMode(new ModeChannelKey(Instance), 'k');
682 this->AddMode(new ModeChannelLimit(Instance), 'l');
684 /* Channel listmodes */
685 this->AddMode(new ModeChannelBan(Instance), 'b');
686 this->AddMode(new ModeChannelOp(Instance), 'o');
687 this->AddMode(new ModeChannelHalfOp(Instance), 'h');
688 this->AddMode(new ModeChannelVoice(Instance), 'v');
690 /* Now for usermodes */
691 this->AddMode(new ModeUserServerNotice(Instance), 's');
692 this->AddMode(new ModeUserWallops(Instance), 'w');
693 this->AddMode(new ModeUserInvisible(Instance), 'i');
694 this->AddMode(new ModeUserOperator(Instance), 'o');
695 this->AddMode(new ModeUserServerNoticeMask(Instance), '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 ServerInstance->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);