]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/cmode_k.cpp
ea035e7b888431473b3312e685961ae3cf93d549
[user/henk/code/inspircd.git] / src / modes / cmode_k.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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(NULL, 'k', PARAM_ALWAYS, MODETYPE_CHANNEL)
21 {
22 }
23
24 ModePair ModeChannelKey::ModeSet(User*, User*, Channel* channel, const std::string &parameter)
25 {
26     if (channel->modes[CM_KEY])
27     {
28                 std::string ckey = channel->GetModeParameter('k');
29                 return std::make_pair(true, ckey);
30     }
31     else
32     {
33                 return std::make_pair(false, parameter);
34     }
35 }
36
37 void ModeChannelKey::RemoveMode(Channel* channel, irc::modestacker* stack)
38 {
39         /** +k needs a parameter when being removed,
40          * so we have a special-case RemoveMode here for it
41          */
42
43         if (channel->IsModeSet('k'))
44         {
45                 if (stack)
46                 {
47                         stack->Push('k', channel->GetModeParameter('k'));
48                 }
49                 else
50                 {
51                         std::vector<std::string> parameters;
52                         parameters.push_back(channel->name);
53                         parameters.push_back("-k");
54                         parameters.push_back(channel->GetModeParameter('k'));
55                         ServerInstance->SendMode(parameters, ServerInstance->FakeClient);
56                 }
57         }
58 }
59
60 void ModeChannelKey::RemoveMode(User*, irc::modestacker* stack)
61 {
62 }
63
64 ModeAction ModeChannelKey::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
65 {
66         bool exists = channel->IsModeSet('k');
67         if (IS_LOCAL(source))
68         {
69                 if (exists == adding)
70                         return MODEACTION_DENY;
71                 if (exists && (parameter != channel->GetModeParameter('k')))
72                 {
73                         /* Key is currently set and the correct key wasnt given */
74                         return MODEACTION_DENY;
75                 }
76         } else {
77                 if (exists && adding && parameter == channel->GetModeParameter('k'))
78                 {
79                         /* no-op, don't show */
80                         return MODEACTION_DENY;
81                 }
82         }
83
84         /* invalid keys */
85         if (!parameter.length())
86                 return MODEACTION_DENY;
87
88         if (parameter.rfind(' ') != std::string::npos)
89                 return MODEACTION_DENY;
90
91         if (adding)
92         {
93                 std::string ckey;
94                 ckey.assign(parameter, 0, 32);
95                 parameter = ckey;
96                 channel->SetModeParam('k', parameter);
97         }
98         else
99         {
100                 channel->SetModeParam('k', "");
101         }
102         return MODEACTION_ALLOW;
103 }