]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_k.cpp
194c0efcbc0a14c679cdd9421dd1d2c9f3e5e680
[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         char moderemove[MAXBUF];
42         const char* parameters[] = { channel->name, moderemove, channel->key };
43
44         if (channel->IsModeSet(this->GetModeChar()))
45         {
46                 if (stack)
47                         stack->Push(this->GetModeChar(), channel->key);
48                 else
49                 {
50                         sprintf(moderemove,"-%c",this->GetModeChar());
51                         ServerInstance->SendMode(parameters, 3, ServerInstance->FakeClient);
52                 }
53         }
54 }
55
56 void ModeChannelKey::RemoveMode(User*, irc::modestacker* stack)
57 {
58 }
59
60 bool ModeChannelKey::CheckTimeStamp(time_t, time_t, const std::string &their_param, const std::string &our_param, Channel*)
61 {
62         /* When TS is equal, the alphabetically later channel key wins */
63         return (their_param < our_param);
64 }
65
66 ModeAction ModeChannelKey::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding, bool servermode)
67 {
68         if ((channel->modes[CM_KEY] != adding) || (!IS_LOCAL(source)))
69         {
70                 if (((channel->modes[CM_KEY]) && (strcasecmp(parameter.c_str(),channel->key))) && (IS_LOCAL(source)))
71                 {
72                         /* Key is currently set and the correct key wasnt given */
73                         return MODEACTION_DENY;
74                 }
75                 else if ((!channel->modes[CM_KEY]) || ((adding) && (!IS_LOCAL(source))))
76                 {
77                         /* Key isnt currently set */
78                         if ((parameter.length()) && (parameter.rfind(' ') == std::string::npos))
79                         {
80                                 strlcpy(channel->key,parameter.c_str(),32);
81                                 channel->modes[CM_KEY] = adding;
82                                 parameter = channel->key;
83                                 return MODEACTION_ALLOW;
84                         }
85                         else
86                                 return MODEACTION_DENY;
87                 }
88                 else if (((channel->modes[CM_KEY]) && (!strcasecmp(parameter.c_str(),channel->key))) || ((!adding) && (!IS_LOCAL(source))))
89                 {
90                         /* Key is currently set, and correct key was given */
91                         *channel->key = 0;
92                         channel->modes[CM_KEY] = adding;
93                         return MODEACTION_ALLOW;
94                 }
95                 return MODEACTION_DENY;
96         }
97         else
98         {
99                 return MODEACTION_DENY;
100         }
101 }
102