]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_k.cpp
Make User:: nick/ident/dhost/fullname and some other things std::string instead of...
[user/henk/code/inspircd.git] / src / modes / cmode_k.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "mode.h"
16 #include "channels.h"
17 #include "users.h"
18 #include "modes/cmode_k.h"
19
20 ModeChannelKey::ModeChannelKey(InspIRCd* Instance) : ModeHandler(Instance, 'k', 1, 1, false, MODETYPE_CHANNEL, false)
21 {
22 }
23
24 ModePair ModeChannelKey::ModeSet(User*, User*, Channel* channel, const std::string &parameter)
25 {       
26         if (channel->modes[CM_KEY])
27         {
28                 return std::make_pair(true, channel->key);
29         }
30         else
31         {
32                 return std::make_pair(false, parameter);
33         }
34 }
35
36 void ModeChannelKey::RemoveMode(Channel* channel, irc::modestacker* stack)
37 {
38         /** +k needs a parameter when being removed,
39          * so we have a special-case RemoveMode here for it
40          */
41
42         if (channel->IsModeSet(this->GetModeChar()))
43         {
44                 if (stack)
45                         stack->Push(this->GetModeChar(), channel->key);
46                 else
47                 {
48                         std::vector<std::string> parameters; parameters.push_back(channel->name); parameters.push_back("-k"); parameters.push_back(channel->key);
49                         ServerInstance->SendMode(parameters, ServerInstance->FakeClient);
50                 }
51         }
52 }
53
54 void ModeChannelKey::RemoveMode(User*, irc::modestacker* stack)
55 {
56 }
57
58 bool ModeChannelKey::CheckTimeStamp(time_t, time_t, const std::string &their_param, const std::string &our_param, Channel*)
59 {
60         /* When TS is equal, the alphabetically later channel key wins */
61         return (their_param < our_param);
62 }
63
64 ModeAction ModeChannelKey::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding, bool servermode)
65 {
66         if ((channel->modes[CM_KEY] != adding) || (!IS_LOCAL(source)))
67         {
68                 if (((channel->modes[CM_KEY]) && (strcasecmp(parameter.c_str(),channel->key))) && (IS_LOCAL(source)))
69                 {
70                         /* Key is currently set and the correct key wasnt given */
71                         return MODEACTION_DENY;
72                 }
73                 else if ((!channel->modes[CM_KEY]) || ((adding) && (!IS_LOCAL(source))))
74                 {
75                         /* Key isnt currently set */
76                         if ((parameter.length()) && (parameter.rfind(' ') == std::string::npos))
77                         {
78                                 strlcpy(channel->key,parameter.c_str(),32);
79                                 channel->modes[CM_KEY] = adding;
80                                 parameter = channel->key;
81                                 return MODEACTION_ALLOW;
82                         }
83                         else
84                                 return MODEACTION_DENY;
85                 }
86                 else if (((channel->modes[CM_KEY]) && (!strcasecmp(parameter.c_str(),channel->key))) || ((!adding) && (!IS_LOCAL(source))))
87                 {
88                         /* Key is currently set, and correct key was given */
89                         *channel->key = 0;
90                         channel->modes[CM_KEY] = adding;
91                         return MODEACTION_ALLOW;
92                 }
93                 return MODEACTION_DENY;
94         }
95         else
96         {
97                 return MODEACTION_DENY;
98         }
99 }
100