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/>.
21 class CommandSVSTOPIC : public Command
24 CommandSVSTOPIC(Module* Creator)
25 : Command(Creator, "SVSTOPIC", 1, 4)
27 flags_needed = FLAG_SERVERONLY;
30 CmdResult Handle(const std::vector<std::string> ¶meters, User *user)
32 if (!ServerInstance->ULine(user->server))
38 Channel* chan = ServerInstance->FindChan(parameters[0]);
42 if (parameters.size() == 4)
44 // 4 parameter version, set all topic data on the channel to the ones given in the parameters
45 time_t topicts = ConvToInt(parameters[1]);
48 ServerInstance->Logs->Log("m_topiclock", LOG_DEFAULT, "Received SVSTOPIC with a 0 topicts, dropped.");
53 newtopic.assign(parameters[3], 0, ServerInstance->Config->Limits.MaxTopic);
54 bool topics_differ = (chan->topic != newtopic);
55 if ((topics_differ) || (chan->topicset != topicts) || (chan->setby != parameters[2]))
57 // Update when any parameter differs
58 chan->topicset = topicts;
59 chan->setby.assign(parameters[2], 0, 127);
60 chan->topic = newtopic;
61 // Send TOPIC to clients only if the actual topic has changed, be silent otherwise
63 chan->WriteChannel(user, "TOPIC %s :%s", chan->name.c_str(), chan->topic.c_str());
68 // 1 parameter version, nuke the topic
69 bool topic_empty = chan->topic.empty();
70 if (!topic_empty || !chan->setby.empty())
76 chan->WriteChannel(user, "TOPIC %s :", chan->name.c_str());
83 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
85 return ROUTE_BROADCAST;
89 class FlagExtItem : public ExtensionItem
92 FlagExtItem(const std::string& key, Module* owner)
93 : ExtensionItem(key, owner)
101 bool get(const Extensible* container) const
103 return (get_raw(container) != NULL);
106 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
108 if (format == FORMAT_USER)
114 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
117 set_raw(container, this);
119 unset_raw(container);
122 void set(Extensible* container, bool value)
125 set_raw(container, this);
127 unset_raw(container);
130 void unset(Extensible* container)
132 unset_raw(container);
135 void free(void* item)
141 class ModuleTopicLock : public Module
144 FlagExtItem topiclock;
148 : cmd(this), topiclock("topiclock", this)
152 void init() CXX11_OVERRIDE
154 ServerInstance->Modules->AddService(cmd);
155 ServerInstance->Modules->AddService(topiclock);
156 ServerInstance->Modules->Attach(I_OnPreTopicChange, this);
159 ModResult OnPreTopicChange(User* user, Channel* chan, const std::string &topic) CXX11_OVERRIDE
161 // Only fired for local users currently, but added a check anyway
162 if ((IS_LOCAL(user)) && (topiclock.get(chan)))
164 user->WriteNumeric(744, "%s :TOPIC cannot be changed due to topic lock being active on the channel", chan->name.c_str());
168 return MOD_RES_PASSTHRU;
171 Version GetVersion() CXX11_OVERRIDE
173 return Version("Implements server-side topic locks and the server-to-server command SVSTOPIC", VF_COMMON | VF_VENDOR);
177 MODULE_INIT(ModuleTopicLock)