summaryrefslogtreecommitdiff
path: root/modules/chanserv/topic.cpp
blob: d103307ce5b7a8fd931f25158518c2918cad1abf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Anope IRC Services
 *
 * Copyright (C) 2003-2017 Anope Team <team@anope.org>
 *
 * This file is part of Anope. Anope is free software; you can
 * redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software
 * Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see see <http://www.gnu.org/licenses/>.
 */

#include "module.h"
#include "modules/chanserv/mode.h"
#include "modules/chanserv/info.h"
#include "modules/chanserv/set.h"

class CommandCSSetKeepTopic : public Command
{
 public:
	CommandCSSetKeepTopic(Module *creator, const Anope::string &cname = "chanserv/set/keeptopic") : Command(creator, cname, 2, 2)
	{
		this->SetDesc(_("Retain topic when channel is not in use"));
		this->SetSyntax(_("\037channel\037 {ON | OFF}"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		const Anope::string &chan = params[0];
		const Anope::string &param = params[1];

		if (Anope::ReadOnly)
		{
			source.Reply(_("Services are in read-only mode."));
			return;
		}

		ChanServ::Channel *ci = ChanServ::Find(chan);
		if (ci == NULL)
		{
			source.Reply(_("Channel \002{0}\002 isn't registered."), chan);
			return;
		}

		EventReturn MOD_RESULT;
		MOD_RESULT = EventManager::Get()->Dispatch(&Event::SetChannelOption::OnSetChannelOption, source, this, ci, param);
		if (MOD_RESULT == EVENT_STOP)
			return;

		if (MOD_RESULT != EVENT_ALLOW && !source.AccessFor(ci).HasPriv("SET") && source.GetPermission().empty() && !source.HasOverridePriv("chanserv/administration"))
		{
			source.Reply(_("Access denied. You do not have privilege \002{0}\002 on \002{1}\002."), "SET", ci->GetName());
			return;
		}

		if (param.equals_ci("ON"))
		{
			logger.Command(source, _("{source} used {command} on {channel} to enable keeptopic"));

			ci->SetKeepTopic(true);
			source.Reply(_("Topic retention option for \002{0}\002 is now \002on\002."), ci->GetName());
		}
		else if (param.equals_ci("OFF"))
		{
			logger.Command(source, _("{source} used {command} on {channel} to disable keeptopic"));

			ci->SetKeepTopic(false);
			source.Reply(_("Topic retention option for \002{0}\002 is now \002off\002."), ci->GetName());
		}
		else
		{
			this->OnSyntaxError(source);
		}
	}

	bool OnHelp(CommandSource &source, const Anope::string &) override
	{
		source.Reply(_("Enables or disables the \002topic retention\002 option for a \037channel\037."
		               " When \002topic retention\002 is set, the topic for the channel will be remembered by {0} even after the last user leaves the channel, and will be restored the next time the channel is created."),
		               source.service->nick);
		return true;
	}
};

class CommandCSTopic : public Command
{
	void Lock(CommandSource &source, ChanServ::Channel *ci, const std::vector<Anope::string> &params)
	{
		if (Anope::ReadOnly)
		{
			source.Reply(_("Services are in read-only mode."));
			return;
		}

		EventReturn MOD_RESULT;
		MOD_RESULT = EventManager::Get()->Dispatch(&Event::SetChannelOption::OnSetChannelOption, source, this, ci, "topiclock on");
		if (MOD_RESULT == EVENT_STOP)
			return;

		ci->SetTopicLock(true);
		source.Reply(_("Topic lock option for \002{0}\002 is now \002on\002."), ci->GetName());
	}

	void Unlock(CommandSource &source, ChanServ::Channel *ci, const std::vector<Anope::string> &params)
	{
		if (Anope::ReadOnly)
		{
			source.Reply(_("Services are in read-only mode."));
			return;
		}

		EventReturn MOD_RESULT;
		MOD_RESULT = EventManager::Get()->Dispatch(&Event::SetChannelOption::OnSetChannelOption, source, this, ci, "topiclock off");
		if (MOD_RESULT == EVENT_STOP)
			return;

		ci->SetTopicLock(false);
		source.Reply(_("Topic lock option for \002{0}\002 is now \002off\002."), ci->GetName());
	}

	void Set(CommandSource &source, ChanServ::Channel *ci, const Anope::string &topic)
	{
		bool has_topiclock = ci->IsTopicLock();
		ci->SetTopicLock(false);
		ci->GetChannel()->ChangeTopic(source.GetNick(), topic, Anope::CurTime);
		if (has_topiclock)
			ci->SetTopicLock(true);

		if (!topic.empty())
			logger.Command(source, _("{source} used {command} on {channel} to change the topic to: {0}"), topic);
		else
			logger.Command(source, _("{source} used {command} on {channel} to unset the topic"));
	}

	void Append(CommandSource &source, ChanServ::Channel *ci, const std::vector<Anope::string> &params)
	{
		const Anope::string &topic = params[2];

		Anope::string new_topic;
		if (!ci->GetChannel()->topic.empty())
		{
			new_topic = ci->GetChannel()->topic + " " + topic;
			ci->SetLastTopic("");
		}
		else
		{
			new_topic = topic;
		}

		this->Set(source, ci, new_topic);
	}

 public:
	CommandCSTopic(Module *creator) : Command(creator, "chanserv/topic", 2, 3)
	{
		this->SetDesc(_("Manipulate the topic of the specified channel"));
		this->SetSyntax(_("\037channel\037 [SET] [\037topic\037]"));
		this->SetSyntax(_("\037channel\037 APPEND \037topic\037"));
		this->SetSyntax(_("\037channel\037 [UNLOCK|LOCK]"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		const Anope::string &channel = params[0];
		const Anope::string &subcmd = params[1];

		ChanServ::Channel *ci = ChanServ::Find(channel);
		if (ci == NULL)
		{
			source.Reply(_("Channel \002{0}\002 isn't registered."), channel);
			return;
		}

		if (!source.AccessFor(ci).HasPriv("TOPIC") && !source.HasOverrideCommand("chanserv/topic"))
		{
			source.Reply(_("Access denied. You do not have privilege \002{0}\002 on \002{1}\002."), "TOPIC", ci->GetName());
			return;
		}

		if (subcmd.equals_ci("LOCK"))
		{
			this->Lock(source, ci, params);
		}
		else if (subcmd.equals_ci("UNLOCK"))
		{
			this->Unlock(source, ci, params);
		}
		else if (!ci->GetChannel())
		{
			source.Reply(_("Channel \002{0}\002 doesn't exist."), ci->GetName());
		}
		else if (subcmd.equals_ci("APPEND") && params.size() > 2)
		{
			this->Append(source, ci, params);
		}
		else
		{
			Anope::string topic;
			if (subcmd.equals_ci("SET"))
			{
				topic = params.size() > 2 ? params[2] : "";
			}
			else
			{
				topic = subcmd;
				if (params.size() > 2)
					topic += " " + params[2];
			}

			this->Set(source, ci, topic);
		}
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
	{
		source.Reply(_("Allows manipulating the topic of the specified channel."
		               " The \002SET\002 command changes the topic of the channel to the given topic or unsets the topic if no topic is given."
		               " The \002APPEND\002 command appends the given topic to the existing topic.\n"
		               "\n"
		               "\002LOCK\002 and \002UNLOCK\002 may be used to enable and disable topic lock."
		               " When topic lock is set, the channel topic will be unchangeable by users who do not have the \002TOPIC\002 privilege.\n"
		               "\n"
		               "Use of this command requires the \002{0}\002 privilege on \037channel\037."),
		               "TOPIC");
		return true;
	}
};

class CSTopic : public Module
	, public EventHook<Event::ChannelSync>
	, public EventHook<Event::TopicUpdated>
	, public EventHook<Event::ChanInfo>
{
	CommandCSTopic commandcstopic;
	CommandCSSetKeepTopic commandcssetkeeptopic;

	ServiceReference<ModeLocks> mlocks;

 public:
	CSTopic(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR)
		, EventHook<Event::ChannelSync>(this)
		, EventHook<Event::TopicUpdated>(this)
		, EventHook<Event::ChanInfo>(this)
		, commandcstopic(this)
		, commandcssetkeeptopic(this)
	{

	}

	void OnChannelSync(Channel *c) override
	{
		ChanServ::Channel *ci = c->GetChannel();
		if (!ci)
			return;

		/* Update channel topic */
		if ((ci->IsTopicLock() || ci->IsKeepTopic()) && ci->GetLastTopic() != c->topic)
		{
			ServiceBot *sender = ci->WhoSends();
			c->ChangeTopic(!ci->GetLastTopicSetter().empty() ? ci->GetLastTopicSetter() : (sender ? sender->nick : Me->GetName()),
					ci->GetLastTopic(), ci->GetLastTopicTime() ? ci->GetLastTopicTime() : Anope::CurTime);
		}
	}

	void OnTopicUpdated(User *source, Channel *c, const Anope::string &user, const Anope::string &topic) override
	{
		ChanServ::Channel *ci = c->GetChannel();
		if (!ci)
			return;

		/* We only compare the topics here, not the time or setter. This is because some (old) IRCds do not
		 * allow us to set the topic as someone else, meaning we have to bump the TS and change the setter to us.
		 * This desyncs what is really set with what we have stored, and we end up resetting the topic often when
		 * it is not required
		 */
		if (ci->IsTopicLock() && ci->GetLastTopic() != c->topic && (!source || !ci->AccessFor(source).HasPriv("TOPIC")))
		{
			c->ChangeTopic(ci->GetLastTopicSetter(), ci->GetLastTopic(), ci->GetLastTopicTime());
		}
		else
		{
			ci->SetLastTopic(c->topic);
			ci->SetLastTopicSetter(c->topic_setter);
			ci->SetLastTopicTime(c->topic_ts);
		}
	}

	void OnChanInfo(CommandSource &source, ChanServ::Channel *ci, InfoFormatter &info, bool show_all) override
	{
		if (ci->IsKeepTopic())
			info.AddOption(_("Topic retention"));
		if (ci->IsTopicLock())
			info.AddOption(_("Topic lock"));

		ModeLock *secret = mlocks ? mlocks->GetMLock(ci, "SECRET") : nullptr;
		Channel *c = ci->GetChannel();
		if (!ci->GetLastTopic().empty() && (show_all || ((!secret || secret->GetSet() == false) && (!c || !c->HasMode("SECRET")))))
		{
			info[_("Last topic")] = ci->GetLastTopic();
			info[_("Topic set by")] = ci->GetLastTopicSetter();
		}
	}
};

MODULE_INIT(CSTopic)