]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_k.cpp
Dont allow an empty key (thanks stskeeps)
[user/henk/code/inspircd.git] / src / modes / cmode_k.cpp
1 #include "inspircd.h"
2 #include "mode.h"
3 #include "channels.h"
4 #include "users.h"
5 #include "modes/cmode_k.h"
6
7 ModeChannelKey::ModeChannelKey(InspIRCd* Instance) : ModeHandler(Instance, 'k', 1, 1, false, MODETYPE_CHANNEL, false)
8 {
9 }
10
11 ModePair ModeChannelKey::ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
12 {       
13         if (channel->modes[CM_KEY])
14         {
15                 return std::make_pair(true, channel->key);
16         }
17         else
18         {
19                 return std::make_pair(false, parameter);
20         }
21 }       
22
23 bool ModeChannelKey::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
24 {
25         /* When TS is equal, the alphabetically later channel key wins */
26         return (their_param < our_param);
27 }
28
29 ModeAction ModeChannelKey::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
30 {
31         if ((channel->modes[CM_KEY] != adding) || (!IS_LOCAL(source)))
32         {
33                 if (((channel->modes[CM_KEY]) && (strcasecmp(parameter.c_str(),channel->key))) && (IS_LOCAL(source)))
34                 {
35                         /* Key is currently set and the correct key wasnt given */
36                         ServerInstance->Log(DEBUG,"Key Cond 2");
37                         return MODEACTION_DENY;
38                 }
39                 else if ((!channel->modes[CM_KEY]) || ((adding) && (!IS_LOCAL(source))))
40                 {
41                         /* Key isnt currently set */
42                         if (parameter.length())
43                         {
44                                 strlcpy(channel->key,parameter.c_str(),32);
45                                 channel->modes[CM_KEY] = adding;
46                                 return MODEACTION_ALLOW;
47                         }
48                         else
49                                 return MODEACTION_DENY;
50                 }
51                 else if (((channel->modes[CM_KEY]) && (!strcasecmp(parameter.c_str(),channel->key))) || ((!adding) && (!IS_LOCAL(source))))
52                 {
53                         /* Key is currently set, and correct key was given */
54                         *channel->key = 0;
55                         channel->modes[CM_KEY] = adding;
56                         return MODEACTION_ALLOW;
57                 }
58                 ServerInstance->Log(DEBUG,"Key Cond three");
59                 return MODEACTION_DENY;
60         }
61         else
62         {
63                 ServerInstance->Log(DEBUG,"Key Condition one");
64                 return MODEACTION_DENY;
65         }
66 }