The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

cmode_k.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *            the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 #include "inspircd.h"
00015 #include "mode.h"
00016 #include "channels.h"
00017 #include "users.h"
00018 #include "modes/cmode_k.h"
00019 
00020 ModeChannelKey::ModeChannelKey(InspIRCd* Instance) : ModeHandler(Instance, 'k', 1, 1, false, MODETYPE_CHANNEL, false)
00021 {
00022 }
00023 
00024 ModePair ModeChannelKey::ModeSet(User*, User*, Channel* channel, const std::string &parameter)
00025 {       
00026     if (channel->modes[CM_KEY])
00027     {
00028                 std::string ckey = channel->GetModeParameter('k');
00029                 return std::make_pair(true, ckey);
00030     }
00031     else
00032     {
00033                 return std::make_pair(false, parameter);
00034     }
00035 }
00036 
00037 void ModeChannelKey::RemoveMode(Channel* channel, irc::modestacker* stack)
00038 {
00043         if (channel->IsModeSet(this->GetModeChar()))
00044         {
00045                 if (stack)
00046                 {
00047                         stack->Push(this->GetModeChar(), channel->GetModeParameter('k'));
00048                 }
00049                 else
00050                 {
00051                         std::vector<std::string> parameters; parameters.push_back(channel->name); parameters.push_back("-k"); parameters.push_back(channel->GetModeParameter('k'));
00052                         ServerInstance->SendMode(parameters, ServerInstance->FakeClient);
00053                 }
00054         }
00055 }
00056 
00057 void ModeChannelKey::RemoveMode(User*, irc::modestacker* stack)
00058 {
00059 }
00060 
00061 bool ModeChannelKey::CheckTimeStamp(time_t, time_t, const std::string &their_param, const std::string &our_param, Channel*)
00062 {
00063         /* When TS is equal, the alphabetically later channel key wins */
00064         return (their_param < our_param);
00065 }
00066 
00067 ModeAction ModeChannelKey::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding, bool servermode)
00068 {
00069         if ((channel->IsModeSet('k') != adding) || (!IS_LOCAL(source)))
00070         {
00071                 if (((channel->IsModeSet('k')) && (parameter != channel->GetModeParameter('k'))) && (IS_LOCAL(source)))
00072                 {
00073                         /* Key is currently set and the correct key wasnt given */
00074                         return MODEACTION_DENY;
00075                 }
00076                 else if ((!channel->IsModeSet('k')) || ((adding) && (!IS_LOCAL(source))))
00077                 {
00078                         /* Key isnt currently set */
00079                         if ((parameter.length()) && (parameter.rfind(' ') == std::string::npos))
00080                         {
00081                                 std::string ckey;
00082                                 ckey.assign(parameter, 0, 32);
00083                                 channel->SetModeParam('k', ckey.c_str(), adding);
00084                                 channel->SetMode('k', adding);
00085                                 parameter = ckey;
00086                                 return MODEACTION_ALLOW;
00087                         }
00088                         else
00089                                 return MODEACTION_DENY;
00090                 }
00091                 else if (((channel->IsModeSet('k')) && (parameter == channel->GetModeParameter('k'))) || ((!adding) && (!IS_LOCAL(source))))
00092                 {
00093                         /* Key is currently set, and correct key was given */
00094                         channel->SetMode('k', adding);
00095                         return MODEACTION_ALLOW;
00096                 }
00097                 return MODEACTION_DENY;
00098         }
00099         else
00100         {
00101                 return MODEACTION_DENY;
00102         }
00103 }
00104