]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_topiclock.cpp
Automatically attach modules to events
[user/henk/code/inspircd.git] / src / modules / m_topiclock.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2012 Attila Molnar <attilamolnar@hush.com>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19 #include "inspircd.h"
20
21 class CommandSVSTOPIC : public Command
22 {
23  public:
24         CommandSVSTOPIC(Module* Creator)
25                 : Command(Creator, "SVSTOPIC", 1, 4)
26         {
27                 flags_needed = FLAG_SERVERONLY;
28         }
29
30         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
31         {
32                 if (!ServerInstance->ULine(user->server))
33                 {
34                         // Ulines only
35                         return CMD_FAILURE;
36                 }
37
38                 Channel* chan = ServerInstance->FindChan(parameters[0]);
39                 if (!chan)
40                         return CMD_FAILURE;
41
42                 if (parameters.size() == 4)
43                 {
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]);
46                         if (!topicts)
47                         {
48                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Received SVSTOPIC with a 0 topicts, dropped.");
49                                 return CMD_INVALID;
50                         }
51
52                         std::string newtopic;
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]))
56                         {
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
62                                 if (topics_differ)
63                                         chan->WriteChannel(user, "TOPIC %s :%s", chan->name.c_str(), chan->topic.c_str());
64                         }
65                 }
66                 else
67                 {
68                         // 1 parameter version, nuke the topic
69                         bool topic_empty = chan->topic.empty();
70                         if (!topic_empty || !chan->setby.empty())
71                         {
72                                 chan->topicset = 0;
73                                 chan->setby.clear();
74                                 chan->topic.clear();
75                                 if (!topic_empty)
76                                         chan->WriteChannel(user, "TOPIC %s :", chan->name.c_str());
77                         }
78                 }
79
80                 return CMD_SUCCESS;
81         }
82
83         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
84         {
85                 return ROUTE_BROADCAST;
86         }
87 };
88
89 class FlagExtItem : public ExtensionItem
90 {
91  public:
92         FlagExtItem(const std::string& key, Module* owner)
93                 : ExtensionItem(key, owner)
94         {
95         }
96
97         ~FlagExtItem()
98         {
99         }
100
101         bool get(const Extensible* container) const
102         {
103                 return (get_raw(container) != NULL);
104         }
105
106         std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
107         {
108                 if (format == FORMAT_USER)
109                         return "true";
110
111                 return "1";
112         }
113
114         void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
115         {
116                 if (value == "1")
117                         set_raw(container, this);
118                 else
119                         unset_raw(container);
120         }
121
122         void set(Extensible* container, bool value)
123         {
124                 if (value)
125                         set_raw(container, this);
126                 else
127                         unset_raw(container);
128         }
129
130         void unset(Extensible* container)
131         {
132                 unset_raw(container);
133         }
134
135         void free(void* item)
136         {
137                 // nothing to free
138         }
139 };
140
141 class ModuleTopicLock : public Module
142 {
143         CommandSVSTOPIC cmd;
144         FlagExtItem topiclock;
145
146  public:
147         ModuleTopicLock()
148                 : cmd(this), topiclock("topiclock", this)
149         {
150         }
151
152         void init() CXX11_OVERRIDE
153         {
154                 ServerInstance->Modules->AddService(cmd);
155                 ServerInstance->Modules->AddService(topiclock);
156         }
157
158         ModResult OnPreTopicChange(User* user, Channel* chan, const std::string &topic) CXX11_OVERRIDE
159         {
160                 // Only fired for local users currently, but added a check anyway
161                 if ((IS_LOCAL(user)) && (topiclock.get(chan)))
162                 {
163                         user->WriteNumeric(744, "%s :TOPIC cannot be changed due to topic lock being active on the channel", chan->name.c_str());
164                         return MOD_RES_DENY;
165                 }
166
167                 return MOD_RES_PASSTHRU;
168         }
169
170         Version GetVersion() CXX11_OVERRIDE
171         {
172                 return Version("Implements server-side topic locks and the server-to-server command SVSTOPIC", VF_COMMON | VF_VENDOR);
173         }
174 };
175
176 MODULE_INIT(ModuleTopicLock)