]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_topiclock.cpp
Convert WriteNumeric() calls to pass the parameters of the numeric as method parameters
[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 (!user->server->IsULine())
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, ExtensionItem::EXT_CHANNEL, owner)
94         {
95         }
96
97         bool get(const Extensible* container) const
98         {
99                 return (get_raw(container) != NULL);
100         }
101
102         std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
103         {
104                 if (format == FORMAT_USER)
105                         return "true";
106
107                 return "1";
108         }
109
110         void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
111         {
112                 if (value == "1")
113                         set_raw(container, this);
114                 else
115                         unset_raw(container);
116         }
117
118         void set(Extensible* container, bool value)
119         {
120                 if (value)
121                         set_raw(container, this);
122                 else
123                         unset_raw(container);
124         }
125
126         void unset(Extensible* container)
127         {
128                 unset_raw(container);
129         }
130
131         void free(void* item)
132         {
133                 // nothing to free
134         }
135 };
136
137 class ModuleTopicLock : public Module
138 {
139         CommandSVSTOPIC cmd;
140         FlagExtItem topiclock;
141
142  public:
143         ModuleTopicLock()
144                 : cmd(this), topiclock("topiclock", this)
145         {
146         }
147
148         ModResult OnPreTopicChange(User* user, Channel* chan, const std::string &topic) CXX11_OVERRIDE
149         {
150                 // Only fired for local users currently, but added a check anyway
151                 if ((IS_LOCAL(user)) && (topiclock.get(chan)))
152                 {
153                         user->WriteNumeric(744, chan->name, "TOPIC cannot be changed due to topic lock being active on the channel");
154                         return MOD_RES_DENY;
155                 }
156
157                 return MOD_RES_PASSTHRU;
158         }
159
160         Version GetVersion() CXX11_OVERRIDE
161         {
162                 return Version("Implements server-side topic locks and the server-to-server command SVSTOPIC", VF_COMMON | VF_VENDOR);
163         }
164 };
165
166 MODULE_INIT(ModuleTopicLock)