1 /*
2 * InspIRCd -- Internet Relay Chat Daemon
3 *
4 * Copyright (C) 2017-2020 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
ModeChannelKey(Module * Creator)32 ModeChannelKey::ModeChannelKey(Module* Creator)
33 : ParamMode<ModeChannelKey, LocalStringExt>(Creator, "key", 'k', PARAM_ALWAYS)
34 {
35 syntax = "<key>";
36 }
37
OnModeChange(User * source,User *,Channel * channel,std::string & parameter,bool adding)38 ModeAction ModeChannelKey::OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, 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 source->WriteNumeric(ERR_KEYSET, channel->name, "Channel key already set");
50 return MODEACTION_DENY;
51 }
52 } else {
53 if (exists && adding && parameter == *key)
54 {
55 /* no-op, don't show */
56 return MODEACTION_DENY;
57 }
58 }
59
60 if (adding)
61 {
62 // When joining a channel multiple keys are delimited with a comma so we strip
63 // them out here to avoid creating channels that are unjoinable.
64 size_t commapos;
65 while ((commapos = parameter.find(',')) != std::string::npos)
66 parameter.erase(commapos, 1);
67
68 // Truncate the parameter to the maximum key length.
69 if (parameter.length() > maxkeylen)
70 parameter.erase(maxkeylen);
71
72 // If the password is empty here then it only consisted of commas. This is not
73 // acceptable so we reject the mode change.
74 if (parameter.empty())
75 return MODEACTION_DENY;
76
77 ext.set(channel, parameter);
78 }
79 else
80 ext.unset(channel);
81
82 channel->SetMode(this, adding);
83 return MODEACTION_ALLOW;
84 }
85
SerializeParam(Channel * chan,const std::string * key,std::string & out)86 void ModeChannelKey::SerializeParam(Channel* chan, const std::string* key, std::string& out)
87 {
88 out += *key;
89 }
90
OnSet(User * source,Channel * chan,std::string & param)91 ModeAction ModeChannelKey::OnSet(User* source, Channel* chan, std::string& param)
92 {
93 // Dummy function, never called
94 return MODEACTION_DENY;
95 }
96
IsParameterSecret()97 bool ModeChannelKey::IsParameterSecret()
98 {
99 return true;
100 }
101