summaryrefslogtreecommitdiff
path: root/modules/global/gl_queue.cpp
blob: 583a46f6aa18e31daa5470a6b68bea05b6f96138 (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
/* Global core functions
 *
 * (C) 2003-2025 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"

#define QUEUE_EMPTY _("You have no messages queued.")

class QueueDelCallback final
	: public NumberList
{
private:
	unsigned deleted = 0;
	ServiceReference<GlobalService> &global;
	CommandSource &source;

public:
	QueueDelCallback(CommandSource &src, ServiceReference<GlobalService>& gl, const Anope::string &list)
		: NumberList(list, true)
		, global(gl)
		, source(src)
	{
	}

	~QueueDelCallback() override
	{
		switch (deleted)
		{
			case 0:
				source.Reply(_("No matching entries in your message queue."));
				break;
			case 1:
				source.Reply(_("Deleted one entry from your message queue."));
				break;
			default:
				source.Reply(_("Deleted %u entries from your message queue."), deleted);
				break;
		}
	}

	void HandleNumber(unsigned number) override
	{
		if (!number || number > global->CountQueue(source.nc))
			return;

		if (global->Unqueue(source.nc, number - 1))
			deleted++;
	}
};

class CommandGLQueue final
	: public Command
{
private:
	ServiceReference<GlobalService> global;

	void DoAdd(CommandSource &source, const Anope::string &message)
	{
		if (message.empty())
		{
			this->OnSyntaxError(source, "ADD");
			return;
		}

		auto maxqueue = Config->GetModule(this->module)->Get<size_t>("maxqueue", "10");
		if (global->CountQueue(source.nc) >= maxqueue)
		{
			source.Reply(_("You can not queue any more messages."));
			return;
		}

		global->Queue(source.nc, message);
		source.Reply(_("Your message has been queued."));
		Log(LOG_ADMIN, source, this) << "to queue: " << message;
	}

	void DoClear(CommandSource &source)
	{
		if (!global->CountQueue(source.nc))
		{
			source.Reply(_("You do not have any queued messages."));
			return;
		}

		global->ClearQueue(source.nc);
		source.Reply(_("Your message queue has been cleared."));
		Log(LOG_ADMIN, source, this);
	}

	void DoDel(CommandSource &source, const Anope::string &what)
	{
		if (what.empty())
		{
			this->OnSyntaxError(source, "DEL");
			return;
		}

		if (!global->CountQueue(source.nc))
		{
			source.Reply(QUEUE_EMPTY);
			return;
		}

		QueueDelCallback(source, global, what).Process();
	}

	void DoList(CommandSource &source)
	{
		const auto *q = global->GetQueue(source.nc);
		if (!q || q->empty())
		{
			source.Reply(QUEUE_EMPTY);
			return;
		}

		ListFormatter list(source.nc);
		list.AddColumn(_("Number")).AddColumn(_("Message"));
		for (size_t i = 0; i < q->size(); ++i)
		{
			ListFormatter::ListEntry entry;
			entry["Number"] = Anope::ToString(i + 1);
			entry["Message"] = (*q)[i];
			list.AddEntry(entry);
		}

		std::vector<Anope::string> replies;
		list.Process(replies);
		for (const auto &reply : replies)
			source.Reply(reply);
	}

public:
	CommandGLQueue(Module *creator)
		: Command(creator, "global/queue", 1, 2)
		, global("GlobalService", "Global")
	{
		this->SetDesc(_("Manages your pending message queue."));
		this->SetSyntax(_("ADD \037message\037"));
		this->SetSyntax(_("DEL \037entry-num\037"));
		this->SetSyntax("LIST");
		this->SetSyntax("CLEAR");
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		if (!global)
		{
			source.Reply(SERVICE_UNAVAILABLE, source.service->nick.c_str());
			return;
		}

		const auto &cmd = params[0];
		const auto &what = params.size() > 1 ? params[1] : "";
		if (cmd.equals_ci("ADD"))
			this->DoAdd(source, what);
		else if (cmd.equals_ci("CLEAR"))
			this->DoClear(source);
		else if (cmd.equals_ci("DEL"))
			this->DoDel(source, what);
		else if (cmd.equals_ci("LIST"))
			this->DoList(source);
		else
			this->OnSyntaxError(source, "");
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
	{
		this->SendSyntax(source);
		source.Reply("");
		source.Reply(_(
			"Allows queueing messages to send to users on the network.\n"
			"\n"
			"The \002QUEUE ADD\002 command adds the given message to the message queue."
			"\n"
			"The \002QUEUE CLEAR\002 command clears the message queue."
			"\n"
			"The \002QUEUE DEL\002 command removes the specified message from the message queue. The\n"
			"message number can be obtained from the output of the \002QUEUE LIST\002 command."
			"\n"
			"The \002QUEUE LIST\002 command lists all messages that are currently in the message queue."
		));
		return true;
	}
};

class GLQueue final
	: public Module
{
private:
	CommandGLQueue commandglqueue;

public:
	GLQueue(const Anope::string &modname, const Anope::string &creator)
		: Module(modname, creator, VENDOR)
		, commandglqueue(this)
	{

	}
};

MODULE_INIT(GLQueue)