summaryrefslogtreecommitdiff
path: root/modules/commands/cs_entrymsg.cpp
blob: 3be4d53c7e240ab50dbc7843b5b0f4eeaad1fe7b (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
/* ChanServ core functions
 *
 * (C) 2003-2012 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church.
 */

/*************************************************************************/

#include "module.h"

struct EntryMsg : Serializable
{
	serialize_obj<ChannelInfo> ci;
	Anope::string creator;
	Anope::string message;
	time_t when;

	EntryMsg(ChannelInfo *c, const Anope::string &cname, const Anope::string &cmessage, time_t ct = Anope::CurTime)
	{
	
		this->ci = c;
		this->creator = cname;
		this->message = cmessage;
		this->when = ct;
	}

	const Anope::string serialize_name() const anope_override
	{
		return "EntryMsg";
	}

	Serialize::Data serialize() const anope_override
	{
		Serialize::Data data;

		data["ci"] << this->ci->name;
		data["creator"] << this->creator;
		data["message"] << this->message;
		data["when"].setType(Serialize::DT_INT) << this->when;

		return data;
	}

	static Serializable* unserialize(Serializable *obj, Serialize::Data &data);
};

static unsigned MaxEntries = 0;

struct EntryMessageList : std::vector<EntryMsg>, ExtensibleItem
{
};

Serializable* EntryMsg::unserialize(Serializable *obj, Serialize::Data &data)
{
	ChannelInfo *ci = cs_findchan(data["ci"].astr());
	if (!ci)
		return NULL;

	if (obj)
	{
		EntryMsg *msg = anope_dynamic_static_cast<EntryMsg *>(obj);
		msg->ci = ci;
		data["creator"] >> msg->creator;
		data["message"] >> msg->message;
		data["when"] >> msg->when;
		return msg;
	}

	EntryMessageList *messages = ci->GetExt<EntryMessageList *>("cs_entrymsg");
	if (messages == NULL)
	{
		messages = new EntryMessageList();
		ci->Extend("cs_entrymsg", messages);
	}

	messages->push_back(EntryMsg(ci, data["creator"].astr(), data["message"].astr()));
	return &messages->back();
}

class CommandEntryMessage : public Command
{
 private:
	void DoList(CommandSource &source, ChannelInfo *ci)
	{
		EntryMessageList *messages = ci->GetExt<EntryMessageList *>("cs_entrymsg");
		if (messages == NULL)
		{
			source.Reply(_("Entry message list for \002%s\002 is empty."), ci->name.c_str());
			return;
		}

		source.Reply(_("Entry message list for \002%s\002:"), ci->name.c_str());

		ListFormatter list;
		list.addColumn("Number").addColumn("Creator").addColumn("Created").addColumn("Message");
		for (unsigned i = 0; i < messages->size(); ++i)
		{
			EntryMsg &msg = messages->at(i);

			ListFormatter::ListEntry entry;
			entry["Number"] = stringify(i + 1);
			entry["Creator"] = msg.creator;
			entry["Created"] = do_strftime(msg.when);
			entry["Message"] = msg.message;
			list.addEntry(entry);
		}

		std::vector<Anope::string> replies;
		list.Process(replies);
		for (unsigned i = 0; i < replies.size(); ++i)
			source.Reply(replies[i]);

		source.Reply(_("End of entry message list."));
	}
		
	void DoAdd(CommandSource &source, ChannelInfo *ci, const Anope::string &message)
	{
		EntryMessageList *messages = ci->GetExt<EntryMessageList *>("cs_entrymsg");
		if (messages == NULL)
		{
			messages = new EntryMessageList();
			ci->Extend("cs_entrymsg", messages);
		}

		if (MaxEntries && messages->size() >= MaxEntries)
			source.Reply(_("The entry message list for \002%s\002 is full."), ci->name.c_str());
		else
		{
			messages->push_back(EntryMsg(ci, source.u->nick, message));
			source.Reply(_("Entry message added to \002%s\002"), ci->name.c_str());
		}
	}

	void DoDel(CommandSource &source, ChannelInfo *ci, const Anope::string &message)
	{
		EntryMessageList *messages = ci->GetExt<EntryMessageList *>("cs_entrymsg");
		if (!message.is_pos_number_only())
			source.Reply(("Entry message \002%s\002 not found on channel \002%s\002."), message.c_str(), ci->name.c_str());
		else if (messages != NULL)
		{
			try
			{
				unsigned i = convertTo<unsigned>(message);
				if (i > 0 && i <= messages->size())
				{
					messages->erase(messages->begin() + i - 1);
					if (messages->empty())
						ci->Shrink("cs_entrymsg");
					source.Reply(_("Entry message \002%i\002 for \002%s\002 deleted."), i, ci->name.c_str());
				}
				else
					throw ConvertException();
			}
			catch (const ConvertException &)
			{
				source.Reply(_("Entry message \002%s\002 not found on channel \002%s\002."), message.c_str(), ci->name.c_str());
			}
		}
		else
			source.Reply(_("Entry message list for \002%s\002 is empty."), ci->name.c_str());
	}

	void DoClear(CommandSource &source, ChannelInfo *ci)
	{
		ci->Shrink("cs_entrymsg");
		source.Reply(_("Entry messages for \002%s\002 have been cleared."), ci->name.c_str());
	}

 public:
	CommandEntryMessage(Module *creator) : Command(creator, "chanserv/entrymsg", 2, 3)
	{
		this->SetDesc(_("Manage the channel's entry messages"));
		this->SetSyntax(_("\037channel\037 {ADD|DEL|LIST|CLEAR} [\037message\037|\037num\037]"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
	{
		User *u = source.u;

		ChannelInfo *ci = cs_findchan(params[0]);
		if (ci == NULL)
		{
			source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str());
			return;
		}

		if (IsFounder(u, ci) || u->HasCommand("chanserv/set"))
		{
			bool success = true;
			if (params[1].equals_ci("LIST"))
				this->DoList(source, ci);
			else if (params[1].equals_ci("CLEAR"))
				this->DoClear(source, ci);
			else if (params.size() < 3)
			{
				success = false;
				this->OnSyntaxError(source, "");
			}
			else if (params[1].equals_ci("ADD"))
				this->DoAdd(source, ci, params[2]);
			else if (params[1].equals_ci("DEL"))
				this->DoDel(source, ci, params[2]);
			else
			{
				success = false;
				this->OnSyntaxError(source, "");
			}
			if (success)
				Log(IsFounder(u, ci) ? LOG_COMMAND : LOG_OVERRIDE, u, this, ci) << " to " << params[1] << " a message";
		}
		else
			source.Reply(ACCESS_DENIED);

		return;
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
	{
		this->SendSyntax(source);
		source.Reply(" ");
		source.Reply(_("Controls what messages will be sent to users when they join the channel."));
		return true;
	}
};

class CSEntryMessage : public Module
{
	SerializeType entrymsg_type;
	CommandEntryMessage commandentrymsg;

 public:
	CSEntryMessage(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE), entrymsg_type("EntryMsg", EntryMsg::unserialize), commandentrymsg(this)
	{
		this->SetAuthor("Anope");

		Implementation i[] = { I_OnReload, I_OnJoinChannel };
		ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));

		this->OnReload();
	}

	void OnJoinChannel(User *u, Channel *c) anope_override
	{
		if (u && c && c->ci && u->server->IsSynced())
		{
			EntryMessageList *messages = c->ci->GetExt<EntryMessageList *>("cs_entrymsg");

			if (messages != NULL)
				for (unsigned i = 0; i < messages->size(); ++i)
					u->SendMessage(c->ci->WhoSends(), "[%s] %s", c->ci->name.c_str(), (*messages)[i].message.c_str());
		}
	}
		
	void OnReload() anope_override
	{
		ConfigReader config;
		MaxEntries = config.ReadInteger("cs_entrymsg", "maxentries", "5", 0, true);
	}
};

MODULE_INIT(CSEntryMessage)