2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
28 #include "core_channel.h"
30 const std::string::size_type ModeChannelKey::maxkeylen = 32;
32 ModeChannelKey::ModeChannelKey(Module* Creator)
33 : ParamMode<ModeChannelKey, LocalStringExt>(Creator, "key", 'k', PARAM_ALWAYS)
38 ModeAction ModeChannelKey::OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding)
40 const std::string* key = ext.get(channel);
41 bool exists = (key != NULL);
45 return MODEACTION_DENY;
46 if (exists && (parameter != *key))
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;
53 if (exists && adding && parameter == *key)
55 /* no-op, don't show */
56 return MODEACTION_DENY;
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.
65 while ((commapos = parameter.find(',')) != std::string::npos)
66 parameter.erase(commapos, 1);
68 // Truncate the parameter to the maximum key length.
69 if (parameter.length() > maxkeylen)
70 parameter.erase(maxkeylen);
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;
77 ext.set(channel, parameter);
82 channel->SetMode(this, adding);
83 return MODEACTION_ALLOW;
86 void ModeChannelKey::SerializeParam(Channel* chan, const std::string* key, std::string& out)
91 ModeAction ModeChannelKey::OnSet(User* source, Channel* chan, std::string& param)
93 // Dummy function, never called
94 return MODEACTION_DENY;
97 bool ModeChannelKey::IsParameterSecret()