2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2012 Attila Molnar <attilamolnar@hush.com>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /* $ModDesc: Implements server-side topic locks and the server-to-server command SVSTOPIC */
23 class CommandSVSTOPIC : public Command
26 CommandSVSTOPIC(Module* Creator)
27 : Command(Creator, "SVSTOPIC", 1, 4)
29 flags_needed = FLAG_SERVERONLY;
32 CmdResult Handle(const std::vector<std::string> ¶meters, User *user)
34 if (!ServerInstance->ULine(user->server))
40 Channel* chan = ServerInstance->FindChan(parameters[0]);
44 if (parameters.size() == 4)
46 // 4 parameter version, set all topic data on the channel to the ones given in the parameters
47 time_t topicts = ConvToInt(parameters[1]);
50 ServerInstance->Logs->Log("m_topiclock", LOG_DEFAULT, "Received SVSTOPIC with a 0 topicts, dropped.");
55 newtopic.assign(parameters[3], 0, ServerInstance->Config->Limits.MaxTopic);
56 bool topics_differ = (chan->topic != newtopic);
57 if ((topics_differ) || (chan->topicset != topicts) || (chan->setby != parameters[2]))
59 // Update when any parameter differs
60 chan->topicset = topicts;
61 chan->setby.assign(parameters[2], 0, 127);
62 chan->topic = newtopic;
63 // Send TOPIC to clients only if the actual topic has changed, be silent otherwise
65 chan->WriteChannel(user, "TOPIC %s :%s", chan->name.c_str(), chan->topic.c_str());
70 // 1 parameter version, nuke the topic
71 bool topic_empty = chan->topic.empty();
72 if (!topic_empty || !chan->setby.empty())
78 chan->WriteChannel(user, "TOPIC %s :", chan->name.c_str());
85 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
87 return ROUTE_BROADCAST;
91 class FlagExtItem : public ExtensionItem
94 FlagExtItem(const std::string& key, Module* owner)
95 : ExtensionItem(key, owner)
103 bool get(const Extensible* container) const
105 return (get_raw(container) != NULL);
108 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
110 if (format == FORMAT_USER)
116 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
119 set_raw(container, this);
121 unset_raw(container);
124 void set(Extensible* container, bool value)
127 set_raw(container, this);
129 unset_raw(container);
132 void unset(Extensible* container)
134 unset_raw(container);
137 void free(void* item)
143 class ModuleTopicLock : public Module
146 FlagExtItem topiclock;
150 : cmd(this), topiclock("topiclock", this)
154 void init() CXX11_OVERRIDE
156 ServerInstance->Modules->AddService(cmd);
157 ServerInstance->Modules->AddService(topiclock);
158 ServerInstance->Modules->Attach(I_OnPreTopicChange, this);
161 ModResult OnPreTopicChange(User* user, Channel* chan, const std::string &topic) CXX11_OVERRIDE
163 // Only fired for local users currently, but added a check anyway
164 if ((IS_LOCAL(user)) && (topiclock.get(chan)))
166 user->WriteNumeric(744, "%s :TOPIC cannot be changed due to topic lock being active on the channel", chan->name.c_str());
170 return MOD_RES_PASSTHRU;
173 Version GetVersion() CXX11_OVERRIDE
175 return Version("Implements server-side topic locks and the server-to-server command SVSTOPIC", VF_COMMON | VF_VENDOR);
179 MODULE_INIT(ModuleTopicLock)