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"
23 #include <sys/errno.h>
27 #include <ext/hash_map>
35 #include "connection.h"
45 #include "inspstring.h"
46 #include "helperfuncs.h"
49 #include "modes/cmode_s.h"
52 extern std::vector<Module*> modules;
53 extern std::vector<ircd_module*> factory;
54 extern InspIRCd* ServerInstance;
55 extern ServerConfig* Config;
59 ModeHandler::ModeHandler(char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly) : mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly)
63 ModeHandler::~ModeHandler()
67 bool ModeHandler::IsListMode()
72 ModeType ModeHandler::GetModeType()
77 bool ModeHandler::NeedsOper()
82 int ModeHandler::GetNumParams(bool adding)
84 return adding ? n_params_on : n_params_off;
87 char ModeHandler::GetModeChar()
92 ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
94 return MODEACTION_DENY;
97 void ModeHandler::DisplayList(userrec* user, chanrec* channel)
101 bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
103 return (ours < theirs);
106 ModeWatcher::ModeWatcher(char modeletter, ModeType type) : mode(modeletter), m_type(type)
110 ModeWatcher::~ModeWatcher()
114 char ModeWatcher::GetModeChar()
119 ModeType ModeWatcher::GetModeType()
124 bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding, ModeType type)
129 void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter, bool adding, ModeType type)
133 userrec* ModeParser::SanityChecks(userrec *user,char *dest,chanrec *chan,int status)
136 if ((!user) || (!dest) || (!chan) || (!*dest))
143 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
149 char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
154 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
156 if (((ucrec*)(*i))->channel == chan)
158 if (((ucrec*)(*i))->uc_modes & MASK)
162 ((ucrec*)(*i))->uc_modes = ((ucrec*)(*i))->uc_modes | MASK;
166 ((ucrec*)(*i))->channel->AddOppedUser(d);
169 ((ucrec*)(*i))->channel->AddHalfoppedUser(d);
172 ((ucrec*)(*i))->channel->AddVoicedUser(d);
175 log(DEBUG,"grant: %s %s",((ucrec*)(*i))->channel->name,d->nick);
182 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
187 for (std::vector<ucrec*>::const_iterator i = d->chans.begin(); i != d->chans.end(); i++)
189 if (((ucrec*)(*i))->channel == chan)
191 if ((((ucrec*)(*i))->uc_modes & MASK) == 0)
195 ((ucrec*)(*i))->uc_modes ^= MASK;
199 ((ucrec*)(*i))->channel->DelOppedUser(d);
202 ((ucrec*)(*i))->channel->DelHalfoppedUser(d);
205 ((ucrec*)(*i))->channel->DelVoicedUser(d);
208 log(DEBUG,"revoke: %s %s",((ucrec*)(*i))->channel->name,d->nick);
215 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
217 userrec *d = this->SanityChecks(user,dest,chan,status);
224 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
226 if (MOD_RESULT == ACR_DENY)
228 if (MOD_RESULT == ACR_DEFAULT)
230 if ((status < STATUS_OP) && (!is_uline(user->server)))
232 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
238 return this->Grant(d,chan,UCMODE_OP);
243 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
245 userrec *d = this->SanityChecks(user,dest,chan,status);
252 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
254 if (MOD_RESULT == ACR_DENY)
256 if (MOD_RESULT == ACR_DEFAULT)
258 if ((status < STATUS_OP) && (!is_uline(user->server)))
260 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
266 return this->Grant(d,chan,UCMODE_HOP);
271 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
273 userrec *d = this->SanityChecks(user,dest,chan,status);
280 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
282 if (MOD_RESULT == ACR_DENY)
284 if (MOD_RESULT == ACR_DEFAULT)
286 if ((status < STATUS_HOP) && (!is_uline(user->server)))
288 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
294 return this->Grant(d,chan,UCMODE_VOICE);
299 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
301 userrec *d = this->SanityChecks(user,dest,chan,status);
308 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
310 if (MOD_RESULT == ACR_DENY)
312 if (MOD_RESULT == ACR_DEFAULT)
314 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
316 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
322 return this->Revoke(d,chan,UCMODE_OP);
327 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
329 userrec *d = this->SanityChecks(user,dest,chan,status);
336 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
338 if (MOD_RESULT == ACR_DENY)
340 if (MOD_RESULT == ACR_DEFAULT)
342 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
343 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
345 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
351 return this->Revoke(d,chan,UCMODE_HOP);
356 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
358 userrec *d = this->SanityChecks(user,dest,chan,status);
365 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
367 if (MOD_RESULT == ACR_DENY)
369 if (MOD_RESULT == ACR_DEFAULT)
371 if ((status < STATUS_HOP) && (!is_uline(user->server)))
373 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
379 return this->Revoke(d,chan,UCMODE_VOICE);
384 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
387 int toomanyexclamation = 0;
390 if ((!user) || (!dest) || (!chan) || (!*dest))
392 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
396 for (char* i = dest; *i; i++)
398 if ((*i < 32) || (*i > 126))
404 toomanyexclamation++;
412 if (toomanyexclamation != 1 || toomanyat != 1)
414 * this stops sillyness like n!u!u!u@h, though note that most
415 * ircds don't actually verify banmask validity. --w00t
419 long maxbans = GetMaxBans(chan->name);
420 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
422 WriteServ(user->fd,"478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)",user->nick, chan->name,chan->name,maxbans);
426 log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
429 FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
434 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
436 if (!strcasecmp(i->data,dest))
438 // dont allow a user to set the same ban twice
444 strlcpy(b.data,dest,MAXBUF);
447 strlcpy(b.set_by,user->nick,NICKMAX-1);
451 strlcpy(b.set_by,Config->ServerName,NICKMAX-1);
453 chan->bans.push_back(b);
457 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
459 if ((!user) || (!dest) || (!chan) || (!*dest)) {
460 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
464 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
465 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
467 if (!strcasecmp(i->data,dest))
470 FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
480 void ModeParser::Process(char **parameters, int pcnt, userrec *user, bool servermode)
482 std::string target = parameters[0];
483 ModeType type = MODETYPE_USER;
484 chanrec* targetchannel = FindChan(parameters[0]);
485 userrec* targetuser = Find(parameters[0]);
487 log(DEBUG,"ModeParser::Process start");
493 log(DEBUG,"Target type is CHANNEL");
494 type = MODETYPE_CHANNEL;
498 log(DEBUG,"Target type is USER");
499 type = MODETYPE_USER;
503 /* No such nick/channel */
504 log(DEBUG,"Target type is UNKNOWN, bailing");
507 std::string mode_sequence = parameters[1];
508 std::string parameter = "";
509 std::ostringstream parameter_list;
510 std::string output_sequence = "";
511 bool adding = true, state_change = false;
513 int parameter_counter = 2; /* Index of first parameter */
515 for (std::string::const_iterator modeletter = mode_sequence.begin(); modeletter != mode_sequence.end(); modeletter++)
520 log(DEBUG,"Iterate mode letter %c",*modeletter);
523 * For + and - mode characters, we don't just stick the character into the output sequence.
524 * This is because the user may do something dumb, like: +-+ooo or +oo-+. To prevent this
525 * appearing in the output sequence, we store a flag which says there was a state change,
526 * which is set on any + or -, however, the + or - that we finish on is only appended to
527 * the output stream in the event it is followed by a non "+ or -" character, such as o or v.
530 /* The following expression prevents: +o+o nick nick, compressing it to +oo nick nick,
531 * however, will allow the + if it is the first item in the sequence, regardless.
533 if ((!adding) || (!output_sequence.length()))
539 if ((adding) || (!output_sequence.length()))
546 /* 65 is the ascii value of 'A' */
547 handler_id = *modeletter - 65;
549 if (modehandlers[handler_id])
553 log(DEBUG,"Found a ModeHandler* for mode %c",*modeletter);
555 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
557 log(DEBUG,"Call a ModeWatcher*");
558 if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == MODEACTION_DENY)
561 if ((modehandlers[handler_id]->GetModeType() == type) && (!abort))
563 log(DEBUG,"Modetype match, calling handler");
565 if (modehandlers[handler_id]->GetNumParams(adding))
567 log(DEBUG,"ModeHandler* for this mode says it has parameters");
569 if (pcnt < parameter_counter)
571 parameter = parameters[parameter_counter++];
578 ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
579 if (ma == MODEACTION_ALLOW)
581 log(DEBUG,"ModeAction was allow");
583 /* We're about to output a valid mode letter - was there previously a pending state-change? */
586 log(DEBUG,"Appending state change");
587 output_sequence.append(adding ? "+" : "-");
590 /* Add the mode letter */
591 output_sequence = output_sequence + *modeletter;
592 log(DEBUG,"Added mode letter to output sequence, sequence now: '%s'",output_sequence.c_str());
594 /* Is there a valid parameter for this mode? If so add it to the parameter list */
595 if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter != ""))
597 log(DEBUG,"Added parameter to parameter_list, list now: '%s'",parameter_list.str().c_str());
598 parameter_list << " " << parameter;
601 /* Call all the AfterMode events in the mode watchers for this mode */
602 for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
604 log(DEBUG,"Called a ModeWatcher* after event");
605 (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
608 /* Reset the state change flag */
609 state_change = false;
616 /* Was there at least one valid mode in the sequence? */
617 if (output_sequence != "")
621 if (type == MODETYPE_CHANNEL)
623 WriteChannelWithServ(Config->ServerName,targetchannel,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
628 if (type == MODETYPE_CHANNEL)
630 log(DEBUG,"Write output sequence and parameters to channel: %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
631 WriteChannel(targetchannel,user,"MODE %s %s%s",targetchannel->name,output_sequence.c_str(),parameter_list.str().c_str());
632 FOREACH_MOD(I_OnMode,OnMode(user, targetchannel, TYPE_CHANNEL, output_sequence + parameter_list.str()));
640 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
646 char outpars[MAXBUF];
652 ServerInstance->ModeGrok->Process(parameters, pcnt, user, false);
658 ModeParser::ModeParser()
660 /* Dummy framework, XXX tidyme */
661 ModeChannelSecret* cmode_s = new ModeChannelSecret();
662 modehandlers[(unsigned char)'s'-65] = cmode_s;