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"
66 extern std::vector<Module*> modules;
67 extern std::vector<ircd_module*> factory;
68 extern InspIRCd* ServerInstance;
69 extern ServerConfig* Config;
73 ModeHandler::ModeHandler(char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly)
74 : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
78 ModeHandler::~ModeHandler()
82 bool ModeHandler::IsListMode()
87 ModeType ModeHandler::GetModeType()
92 bool ModeHandler::NeedsOper()
97 int ModeHandler::GetNumParams(bool adding)
99 return adding ? n_params_on : n_params_off;
102 char ModeHandler::GetModeChar()
107 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
109 return MODEACTION_DENY;
112 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
116 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
118 return (ours < theirs);
121 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
125 ModeWatcher::~ModeWatcher()
129 char ModeWatcher::GetModeChar()
134 ModeType ModeWatcher::GetModeType()
139 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type)
144 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
148 userrec* ModeParser::SanityChecks(userrec *user,const char *dest,chanrec *chan,int status)
151 if ((!user) || (!dest) || (!chan) || (!*dest))
158 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
164 const char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
169 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
171 ucrec* n = (ucrec*)(*i);
172 if (n->channel == chan)
174 if (n->uc_modes & MASK)
178 n->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
182 n->channel->AddOppedUser(d);
185 n->channel->AddHalfoppedUser(d);
188 n->channel->AddVoicedUser(d);
191 log(DEBUG,"grant: %s %s",n->channel->name,d->nick);
198 const char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
203 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
205 ucrec* n = (ucrec*)(*i);
206 if (n->channel == chan)
208 if ((n->uc_modes & MASK) == 0)
216 n->channel->DelOppedUser(d);
219 n->channel->DelHalfoppedUser(d);
222 n->channel->DelVoicedUser(d);
225 log(DEBUG,"revoke: %s %s",n->channel->name,d->nick);
232 void ModeParser::DisplayCurrentModes(userrec *user, userrec* targetuser, chanrec* targetchannel, const char* text)
236 /* Display channel's current mode string */
237 WriteServ(user->fd,"324 %s %s +%s",user->nick, targetchannel->name, chanmodes(targetchannel, targetchannel->HasUser(user)));
238 WriteServ(user->fd,"329 %s %s %d", user->nick, targetchannel->name, targetchannel->created);
242 /* Display user's current mode string */
243 WriteServ(user->fd,"221 %s :+%s",targetuser->nick,targetuser->FormatModes());
245 /* No such nick/channel */
246 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, text);
250 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
252 std::string target = parameters[0];
253 ModeType type = MODETYPE_USER;
254 unsigned char mask = 0;
255 chanrec* targetchannel = FindChan(parameters[0]);
256 userrec* targetuser = Find(parameters[0]);
258 log(DEBUG,"ModeParser::Process start");
262 this->DisplayCurrentModes(user, targetuser, targetchannel, parameters[0]);
268 type = MODETYPE_CHANNEL;
271 /* Extra security checks on channel modes
272 * (e.g. are they a (half)op?
275 if (cstatus(user, targetchannel) < STATUS_HOP)
277 /* We don't have halfop */
278 log(DEBUG,"The user is not a halfop or above, checking other reasons for being able to set the modes");
280 /* Are we a uline or is it a servermode? */
281 if ((!is_uline(user->server)) && (!servermode))
283 /* Not enough permission:
284 * NOT a uline and NOT a servermode,
285 * OR, NOT halfop or above.
287 WriteServ(user->fd,"482 %s %s :You're not a channel (half)operator",user->nick, targetchannel->name);
294 type = MODETYPE_USER;
299 /* No such nick/channel */
300 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
303 std::string mode_sequence = parameters[1];
304 std::string parameter = "";
305 std::ostringstream parameter_list;
306 std::string output_sequence = "";
307 bool adding = true, state_change = false;
309 int parameter_counter = 2; /* Index of first parameter */
311 for (std::string::const_iterator letter = mode_sequence.begin(); letter != mode_sequence.end(); letter++)
313 unsigned char modechar = *letter;
318 * For + and - mode characters, we don't just stick the character into the output sequence.
319 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
320 * appearing in the output sequence, we store a flag which says there was a state change,
321 * which is set on any + or -, however, the + or - that we finish on is only appended to
322 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
325 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
326 * however, will allow the + if it is the first item in the sequence, regardless.
328 if ((!adding) || (!output_sequence.length()))
334 if ((adding) || (!output_sequence.length()))
342 * Watch carefully for the sleight of hand trick.
343 * 65 is the ascii value of 'A'. We take this from
344 * the char we're looking at to get a number between
345 * 1 and 127. We then logic-or it to get the hashed
346 * position, dependent on wether its a channel or
347 * a user mode. This is a little stranger, but a lot
348 * faster, than using a map of pairs.
350 handler_id = (modechar - 65) | mask;
352 if (modehandlers[handler_id])
356 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
358 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
361 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
363 if (modehandlers[handler_id]->GetNumParams(adding))
365 /* This mode expects a parameter, do we have any parameters left in our list to use? */
366 if (parameter_counter < pcnt)
368 parameter = parameters[parameter_counter++];
372 /* No parameter, continue to the next mode */
377 /* Call the handler for the mode */
378 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
380 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter == ""))
382 /* The handler nuked the parameter and they are supposed to have one.
383 * We CANT continue now, even if they actually returned MODEACTION_ALLOW,
384 * so we bail to the next mode character.
389 if (ma == MODEACTION_ALLOW)
391 /* We're about to output a valid mode letter - was there previously a pending state-change? */
393 output_sequence.append(adding ? "+" : "-");
395 /* Add the mode letter */
396 output_sequence.push_back(modechar);
398 /* Is there a valid parameter for this mode? If so add it to the parameter list */
399 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
400 parameter_list << " " << parameter;
402 /* Call all the AfterMode events in the mode watchers for this mode */
403 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
404 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
406 /* Reset the state change flag */
407 state_change = false;
413 /* No mode handler? Unknown mode character then. */
414 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick, modechar);
419 /* Was there at least one valid mode in the sequence? */
420 if (output_sequence != "")
424 if (type == MODETYPE_CHANNEL)
426 WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
430 WriteServ(targetuser->fd,"MODE %s %s",targetuser->nick,output_sequence.c_str());
435 if (type == MODETYPE_CHANNEL)
437 log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
438 WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
439 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
443 WriteTo(user,targetuser,"MODE %s %s",targetuser->nick,output_sequence.c_str());
444 FOREACH_MOD(I_OnMode,OnMode(user, targetuser, TYPE_USER, output_sequence));
452 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
457 ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
462 void ModeParser::CleanMask(std::string &mask)
464 std::string::size_type pos_of_pling = mask.find_first_of('!');
465 std::string::size_type pos_of_at = mask.find_first_of('@');
466 std::string::size_type pos_of_dot = mask.find_first_of('.');
467 std::string::size_type pos_of_colon = mask.find_first_of(':'); /* Because ipv6 addresses are colon delimited */
469 if ((pos_of_pling == std::string::npos) && (pos_of_at == std::string::npos))
471 /* Just a nick, or just a host */
472 if ((pos_of_dot == std::string::npos) && (pos_of_colon == std::string::npos))
474 /* It has no '.' in it, it must be a nick. */
479 /* Got a dot in it? Has to be a host */
480 mask = "*!*@" + mask;
483 else if ((pos_of_pling == std::string::npos) && (pos_of_at != std::string::npos))
485 /* Has an @ but no !, its a user@host */
488 else if ((pos_of_pling != std::string::npos) && (pos_of_at == std::string::npos))
490 /* Has a ! but no @, it must be a nick!ident */
495 void ModeParser::BuildModeString(userrec* user)
499 bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
501 unsigned char mask = 0;
502 unsigned char pos = 0;
504 /* Yes, i know, this might let people declare modes like '_' or '^'.
505 * If they do that, thats their problem, and if i ever EVER see an
506 * official InspIRCd developer do that, i'll beat them with a paddle!
508 if ((modeletter < 'A') || (modeletter > 'z'))
511 mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
512 pos = (modeletter-65) | mask;
514 if (modehandlers[pos])
517 modehandlers[pos] = mh;
518 log(DEBUG,"ModeParser::AddMode: added mode %c",modeletter);
522 ModeParser::ModeParser()
524 /* Clear mode list */
525 memset(modehandlers, 0, sizeof(modehandlers));
526 memset(modewatchers, 0, sizeof(modewatchers));
528 /* Initialise the RFC mode letters */
530 /* Start with channel simple modes, no params */
531 this->AddMode(new ModeChannelSecret, 's');
532 this->AddMode(new ModeChannelPrivate, 'p');
533 this->AddMode(new ModeChannelModerated, 'm');
534 this->AddMode(new ModeChannelTopicOps, 't');
535 this->AddMode(new ModeChannelNoExternal, 'n');
536 this->AddMode(new ModeChannelInviteOnly, 'i');
538 /* Cannel modes with params */
539 this->AddMode(new ModeChannelKey, 'k');
540 this->AddMode(new ModeChannelLimit, 'l');
542 /* Channel listmodes */
543 this->AddMode(new ModeChannelBan, 'b');
544 this->AddMode(new ModeChannelOp, 'o');
545 this->AddMode(new ModeChannelHalfOp, 'h');
546 this->AddMode(new ModeChannelVoice, 'v');
548 /* Now for usermodes */
549 this->AddMode(new ModeUserServerNotice, 's');
550 this->AddMode(new ModeUserWallops, 'w');
551 this->AddMode(new ModeUserInvisible, 'i');
553 /* TODO: User modes +swio */