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