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"
58 /* +s (server notices) */
59 #include "modes/umode_s.h"
60 /* +w (see wallops) */
61 #include "modes/umode_w.h"
63 #include "modes/umode_i.h"
65 #include "modes/umode_o.h"
66 /* +n (notice mask - our implementation of snomasks) */
67 #include "modes/umode_n.h"
70 extern std::vector<Module*> modules;
71 extern std::vector<ircd_module*> factory;
72 extern InspIRCd* ServerInstance;
73 extern ServerConfig* Config;
77 ModeHandler::ModeHandler(char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly)
78 : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
82 ModeHandler::~ModeHandler()
86 bool ModeHandler::IsListMode()
91 ModeType ModeHandler::GetModeType()
96 bool ModeHandler::NeedsOper()
101 int ModeHandler::GetNumParams(bool adding)
103 return adding ? n_params_on : n_params_off;
106 char ModeHandler::GetModeChar()
111 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
113 return MODEACTION_DENY;
116 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
120 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
122 return (ours < theirs);
125 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
129 ModeWatcher::~ModeWatcher()
133 char ModeWatcher::GetModeChar()
138 ModeType ModeWatcher::GetModeType()
143 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type)
148 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
152 userrec* ModeParser::SanityChecks(userrec *user,const char *dest,chanrec *chan,int status)
155 if ((!user) || (!dest) || (!chan) || (!*dest))
162 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
168 const char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
173 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
175 ucrec* n = (ucrec*)(*i);
176 if (n->channel == chan)
178 if (n->uc_modes & MASK)
182 n->uc_modes = n->uc_modes | MASK;
186 n->channel->AddOppedUser(d);
189 n->channel->AddHalfoppedUser(d);
192 n->channel->AddVoicedUser(d);
195 log(DEBUG,"grant: %s %s",n->channel->name,d->nick);
202 const char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
207 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
209 ucrec* n = (ucrec*)(*i);
210 if (n->channel == chan)
212 if ((n->uc_modes & MASK) == 0)
220 n->channel->DelOppedUser(d);
223 n->channel->DelHalfoppedUser(d);
226 n->channel->DelVoicedUser(d);
229 log(DEBUG,"revoke: %s %s",n->channel->name,d->nick);
236 void ModeParser::DisplayCurrentModes(userrec *user, userrec* targetuser, chanrec* targetchannel, const char* text)
240 /* Display channel's current mode string */
241 WriteServ(user->fd,"324 %s %s +%s",user->nick, targetchannel->name, chanmodes(targetchannel, targetchannel->HasUser(user)));
242 WriteServ(user->fd,"329 %s %s %d", user->nick, targetchannel->name, targetchannel->created);
247 /* Display user's current mode string */
248 WriteServ(user->fd,"221 %s :+%s",targetuser->nick,targetuser->FormatModes());
251 /* No such nick/channel */
252 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, text);
256 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
258 std::string target = parameters[0];
259 ModeType type = MODETYPE_USER;
260 unsigned char mask = 0;
261 chanrec* targetchannel = FindChan(parameters[0]);
262 userrec* targetuser = Find(parameters[0]);
264 log(DEBUG,"ModeParser::Process start");
268 this->DisplayCurrentModes(user, targetuser, targetchannel, parameters[0]);
274 type = MODETYPE_CHANNEL;
277 /* Extra security checks on channel modes
278 * (e.g. are they a (half)op?
281 if (cstatus(user, targetchannel) < STATUS_HOP)
283 /* We don't have halfop */
284 log(DEBUG,"The user is not a halfop or above, checking other reasons for being able to set the modes");
286 /* Are we a uline or is it a servermode? */
287 if ((!is_uline(user->server)) && (!servermode))
289 /* Not enough permission:
290 * NOT a uline and NOT a servermode,
291 * OR, NOT halfop or above.
293 WriteServ(user->fd,"482 %s %s :You're not a channel (half)operator",user->nick, targetchannel->name);
300 type = MODETYPE_USER;
305 /* No such nick/channel */
306 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
310 /* Special case for displaying the list for listmodes,
311 * e.g. MODE #chan b, or MODE #chan +b without a parameter
313 if ((type== MODETYPE_CHANNEL) && (pcnt == 2))
315 char* mode = parameters[1];
319 unsigned char handler_id = ((*mode) - 65) | mask;
320 ModeHandler* mh = modehandlers[handler_id];
322 if ((mh) && (mh->IsListMode()))
324 mh->DisplayList(user, targetchannel);
328 std::string mode_sequence = parameters[1];
329 std::string parameter = "";
330 std::ostringstream parameter_list;
331 std::string output_sequence = "";
332 bool adding = true, state_change = false;
333 unsigned char handler_id = 0;
334 int parameter_counter = 2; /* Index of first parameter */
336 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
338 unsigned char modechar = *letter;
343 * For + and - mode characters, we don't just stick the character into the output sequence.
344 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
345 * appearing in the output sequence, we store a flag which says there was a state change,
346 * which is set on any + or -, however, the + or - that we finish on is only appended to
347 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
350 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
351 * however, will allow the + if it is the first item in the sequence, regardless.
353 if ((!adding) || (!output_sequence.length()))
359 if ((adding) || (!output_sequence.length()))
367 * Watch carefully for the sleight of hand trick.
368 * 65 is the ascii value of 'A'. We take this from
369 * the char we're looking at to get a number between
370 * 1 and 127. We then logic-or it to get the hashed
371 * position, dependent on wether its a channel or
372 * a user mode. This is a little stranger, but a lot
373 * faster, than using a map of pairs.
375 handler_id = (modechar - 65) | mask;
377 if (modehandlers[handler_id])
381 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
383 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
386 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
388 if (modehandlers[handler_id]->GetNumParams(adding))
390 /* This mode expects a parameter, do we have any parameters left in our list to use? */
391 if (parameter_counter < pcnt)
393 parameter = parameters[parameter_counter++];
397 /* No parameter, continue to the next mode */
402 /* It's an oper only mode, check if theyre an oper. If they arent,
403 * eat any parameter that came with the mode, and continue to next
405 if ((IS_LOCAL(user)) && (modehandlers[handler_id]->NeedsOper()) && (!*user->oper))
408 /* Call the handler for the mode */
409 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
411 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter == ""))
413 /* The handler nuked the parameter and they are supposed to have one.
414 * We CANT continue now, even if they actually returned MODEACTION_ALLOW,
415 * so we bail to the next mode character.
420 if (ma == MODEACTION_ALLOW)
422 /* We're about to output a valid mode letter - was there previously a pending state-change? */
424 output_sequence.append(adding ? "+" : "-");
426 /* Add the mode letter */
427 output_sequence.push_back(modechar);
429 /* Is there a valid parameter for this mode? If so add it to the parameter list */
430 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
431 parameter_list << " " << parameter;
433 /* Call all the AfterMode events in the mode watchers for this mode */
434 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
435 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
437 /* Reset the state change flag */
438 state_change = false;
444 /* No mode handler? Unknown mode character then. */
445 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick, modechar);
450 /* Was there at least one valid mode in the sequence? */
451 if (output_sequence != "")
455 if (type == MODETYPE_CHANNEL)
457 WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
461 WriteServ(targetuser->fd,"MODE %s %s",targetuser->nick,output_sequence.c_str());
466 if (type == MODETYPE_CHANNEL)
468 log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
469 WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
470 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
474 WriteTo(user,targetuser,"MODE %s %s",targetuser->nick,output_sequence.c_str());
475 FOREACH_MOD(I_OnMode,OnMode(user, targetuser, TYPE_USER, output_sequence));
483 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
488 ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
493 void ModeParser::CleanMask(std::string &mask)
495 std::string::size_type pos_of_pling = mask.find_first_of('!');
496 std::string::size_type pos_of_at = mask.find_first_of('@');
497 std::string::size_type pos_of_dot = mask.find_first_of('.');
498 std::string::size_type pos_of_colon = mask.find_first_of(':'); /* Because ipv6 addresses are colon delimited */
500 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
502 /* Just a nick, or just a host */
503 if ((pos_of_dot == std::string::npos) && (pos_of_colon == std::string::npos))
505 /* It has no '.' in it, it must be a nick. */
510 /* Got a dot in it? Has to be a host */
511 mask = "*!*@" + mask;
514 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
516 /* Has an @ but no !, its a user@host */
519 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
521 /* Has a ! but no @, it must be a nick!ident */
526 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
528 unsigned char mask = 0;
529 unsigned char pos = 0;
531 /* Yes, i know, this might let people declare modes like '_' or '^'.
532 * If they do that, thats their problem, and if i ever EVER see an
533 * official InspIRCd developer do that, i'll beat them with a paddle!
535 if ((mh->GetModeChar() < 'A') || (mh->GetModeChar() > 'z'))
538 mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
539 pos = (mh->GetModeChar()-65) | mask;
541 if (modehandlers[pos])
544 modehandlers[pos] = mh;
545 log(DEBUG,"ModeParser::AddMode: added mode %c",mh->GetModeChar());
549 bool ModeParser::AddModeWatcher(ModeWatcher* mw)
551 unsigned char mask = 0;
552 unsigned char pos = 0;
557 if ((mw->GetModeChar() < 'A') || (mw->GetModeChar() > 'z'))
560 mw->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
561 pos = (mw->GetModeChar()-65) | mask;
563 modewatchers[pos].push_back(mw);
564 log(DEBUG,"ModeParser::AddModeWatcher: watching mode %c",mw->GetModeChar());
569 bool ModeParser::DelModeWatcher(ModeWatcher* mw)
571 unsigned char mask = 0;
572 unsigned char pos = 0;
577 if ((mw->GetModeType() < 'A') || (mw->GetModeType() > 'z'))
580 mw->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
581 pos = (mw->GetModeChar()-65) | mask;
583 ModeWatchIter a = find(modewatchers[pos].begin(),modewatchers[pos].end(),mw);
585 if (a == modewatchers[pos].end())
588 modewatchers[pos].erase(a);
589 log(DEBUG,"ModeParser::DelModeWatcher: stopped watching mode %c",mw->GetModeChar());
594 ModeParser::ModeParser()
596 /* Clear mode list */
597 memset(modehandlers, 0, sizeof(modehandlers));
598 memset(modewatchers, 0, sizeof(modewatchers));
600 /* Initialise the RFC mode letters */
602 /* Start with channel simple modes, no params */
603 this->AddMode(new ModeChannelSecret, 's');
604 this->AddMode(new ModeChannelPrivate, 'p');
605 this->AddMode(new ModeChannelModerated, 'm');
606 this->AddMode(new ModeChannelTopicOps, 't');
607 this->AddMode(new ModeChannelNoExternal, 'n');
608 this->AddMode(new ModeChannelInviteOnly, 'i');
610 /* Cannel modes with params */
611 this->AddMode(new ModeChannelKey, 'k');
612 this->AddMode(new ModeChannelLimit, 'l');
614 /* Channel listmodes */
615 this->AddMode(new ModeChannelBan, 'b');
616 this->AddMode(new ModeChannelOp, 'o');
617 this->AddMode(new ModeChannelHalfOp, 'h');
618 this->AddMode(new ModeChannelVoice, 'v');
620 /* Now for usermodes */
621 this->AddMode(new ModeUserServerNotice, 's');
622 this->AddMode(new ModeUserWallops, 'w');
623 this->AddMode(new ModeUserInvisible, 'i');
624 this->AddMode(new ModeUserOperator, 'o');
625 this->AddMode(new ModeUserServerNoticeMask, 'n');