]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_k.cpp
11f13c3231cc258cc93d14fc8e9b2c4c3a5ed6d7
[user/henk/code/inspircd.git] / src / modes / cmode_k.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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)
37 {
38         /** +k needs a parameter when being removed,
39          * so we have a special-case RemoveMode here for it
40          */
41         char moderemove[MAXBUF];
42         const char* parameters[] = { channel->name, moderemove, channel->key };
43
44         if (channel->IsModeSet(this->GetModeChar()))
45         {
46                 sprintf(moderemove,"-%c",this->GetModeChar());
47                 ServerInstance->SendMode(parameters, 3, ServerInstance->FakeClient);
48         }
49 }
50
51 void ModeChannelKey::RemoveMode(User*)
52 {
53 }
54
55 bool ModeChannelKey::CheckTimeStamp(time_t, time_t, const std::string &their_param, const std::string &our_param, Channel*)
56 {
57         /* When TS is equal, the alphabetically later channel key wins */
58         return (their_param < our_param);
59 }
60
61 ModeAction ModeChannelKey::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
62 {
63         if ((channel->modes[CM_KEY] != adding) || (!IS_LOCAL(source)))
64         {
65                 if (((channel->modes[CM_KEY]) && (strcasecmp(parameter.c_str(),channel->key))) && (IS_LOCAL(source)))
66                 {
67                         /* Key is currently set and the correct key wasnt given */
68                         return MODEACTION_DENY;
69                 }
70                 else if ((!channel->modes[CM_KEY]) || ((adding) && (!IS_LOCAL(source))))
71                 {
72                         /* Key isnt currently set */
73                         if ((parameter.length()) && (parameter.rfind(' ') == std::string::npos))
74                         {
75                                 strlcpy(channel->key,parameter.c_str(),32);
76                                 channel->modes[CM_KEY] = adding;
77                                 parameter = channel->key;
78                                 return MODEACTION_ALLOW;
79                         }
80                         else
81                                 return MODEACTION_DENY;
82                 }
83                 else if (((channel->modes[CM_KEY]) && (!strcasecmp(parameter.c_str(),channel->key))) || ((!adding) && (!IS_LOCAL(source))))
84                 {
85                         /* Key is currently set, and correct key was given */
86                         *channel->key = 0;
87                         channel->modes[CM_KEY] = adding;
88                         return MODEACTION_ALLOW;
89                 }
90                 return MODEACTION_DENY;
91         }
92         else
93         {
94                 return MODEACTION_DENY;
95         }
96 }
97