]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_channel/cmode_k.cpp
ea4b5241db311b39d172e3e258ccc37ee11f84a8
[user/henk/code/inspircd.git] / src / coremods / core_channel / cmode_k.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013-2015 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
8  *   Copyright (C) 2009 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
10  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11  *   Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28 #include "core_channel.h"
29
30 const std::string::size_type ModeChannelKey::maxkeylen = 32;
31
32 ModeChannelKey::ModeChannelKey(Module* Creator)
33         : ParamMode<ModeChannelKey, LocalStringExt>(Creator, "key", 'k', PARAM_ALWAYS)
34 {
35         syntax = "<key>";
36 }
37
38 ModeAction ModeChannelKey::OnModeChange(User* source, User*, Channel* channel, std::string &parameter, bool adding)
39 {
40         const std::string* key = ext.get(channel);
41         bool exists = (key != NULL);
42         if (IS_LOCAL(source))
43         {
44                 if (exists == adding)
45                         return MODEACTION_DENY;
46                 if (exists && (parameter != *key))
47                 {
48                         /* Key is currently set and the correct key wasn't given */
49                         return MODEACTION_DENY;
50                 }
51         } else {
52                 if (exists && adding && parameter == *key)
53                 {
54                         /* no-op, don't show */
55                         return MODEACTION_DENY;
56                 }
57         }
58
59         if (adding)
60         {
61                 // When joining a channel multiple keys are delimited with a comma so we strip
62                 // them out here to avoid creating channels that are unjoinable.
63                 size_t commapos;
64                 while ((commapos = parameter.find(',')) != std::string::npos)
65                         parameter.erase(commapos, 1);
66
67                 // Truncate the parameter to the maximum key length.
68                 if (parameter.length() > maxkeylen)
69                         parameter.erase(maxkeylen);
70
71                 // If the password is empty here then it only consisted of commas. This is not
72                 // acceptable so we reject the mode change.
73                 if (parameter.empty())
74                         return MODEACTION_DENY;
75
76                 ext.set(channel, parameter);
77         }
78         else
79                 ext.unset(channel);
80
81         channel->SetMode(this, adding);
82         return MODEACTION_ALLOW;
83 }
84
85 void ModeChannelKey::SerializeParam(Channel* chan, const std::string* key, std::string& out)
86 {
87         out += *key;
88 }
89
90 ModeAction ModeChannelKey::OnSet(User* source, Channel* chan, std::string& param)
91 {
92         // Dummy function, never called
93         return MODEACTION_DENY;
94 }
95
96 bool ModeChannelKey::IsParameterSecret()
97 {
98         return true;
99 }