]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_k.cpp
18fb2165bbc340962ddc015f6328092976f461d3
[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 void ModeChannelKey::RemoveMode(chanrec* channel)
24 {
25         /** +k needs a parameter when being removed,
26          * so we have a special-case RemoveMode here for it
27          */
28         char moderemove[MAXBUF];
29         const char* parameters[] = { channel->name, moderemove, channel->key };
30
31         if (channel->IsModeSet(this->GetModeChar()))
32         {
33                 userrec* n = new userrec(ServerInstance);
34
35                 sprintf(moderemove,"-%c",this->GetModeChar());
36                 n->SetFd(FD_MAGIC_NUMBER);
37
38                 ServerInstance->SendMode(parameters, 3, n);
39
40                 delete n;
41         }
42 }
43
44 void ModeChannelKey::RemoveMode(userrec* user)
45 {
46 }
47
48 bool ModeChannelKey::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
49 {
50         /* When TS is equal, the alphabetically later channel key wins */
51         return (their_param < our_param);
52 }
53
54 ModeAction ModeChannelKey::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
55 {
56         if ((channel->modes[CM_KEY] != adding) || (!IS_LOCAL(source)))
57         {
58                 if (((channel->modes[CM_KEY]) && (strcasecmp(parameter.c_str(),channel->key))) && (IS_LOCAL(source)))
59                 {
60                         /* Key is currently set and the correct key wasnt given */
61                         ServerInstance->Log(DEBUG,"Key Cond 2");
62                         return MODEACTION_DENY;
63                 }
64                 else if ((!channel->modes[CM_KEY]) || ((adding) && (!IS_LOCAL(source))))
65                 {
66                         /* Key isnt currently set */
67                         if ((parameter.length()) && (parameter.rfind(' ') == std::string::npos))
68                         {
69                                 strlcpy(channel->key,parameter.c_str(),32);
70                                 channel->modes[CM_KEY] = adding;
71                                 return MODEACTION_ALLOW;
72                         }
73                         else
74                                 return MODEACTION_DENY;
75                 }
76                 else if (((channel->modes[CM_KEY]) && (!strcasecmp(parameter.c_str(),channel->key))) || ((!adding) && (!IS_LOCAL(source))))
77                 {
78                         /* Key is currently set, and correct key was given */
79                         *channel->key = 0;
80                         channel->modes[CM_KEY] = adding;
81                         return MODEACTION_ALLOW;
82                 }
83                 ServerInstance->Log(DEBUG,"Key Cond three");
84                 return MODEACTION_DENY;
85         }
86         else
87         {
88                 ServerInstance->Log(DEBUG,"Key Condition one");
89                 return MODEACTION_DENY;
90         }
91 }