summaryrefslogtreecommitdiff
path: root/modules/core/os_news.cpp
blob: f67a2a9ca8df2f42189dea926d20f3dc1944c667 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* OperServ core functions
 *
 * (C) 2003-2011 Anope Team
 * Contact us at info@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 lenof(a)        (sizeof(a) / sizeof(*(a)))

/* List of messages for each news type.  This simplifies message sending. */

enum
{
	MSG_SYNTAX,
	MSG_LIST_HEADER,
	MSG_LIST_NONE,
	MSG_ADD_SYNTAX,
	MSG_ADDED,
	MSG_DEL_SYNTAX,
	MSG_DEL_NOT_FOUND,
	MSG_DELETED,
	MSG_DEL_NONE,
	MSG_DELETED_ALL
};

struct NewsMessages msgarray[] = {
	{NEWS_LOGON, "LOGON",
	 {_("LOGONNEWS {ADD|DEL|LIST} [\037text\037|\037num\037]\002"),
	  _("Logon news items:"),
	  _("There is no logon news."),
	  _("Syntax: \002LOGONNEWS ADD \037text\037\002"),
	  _("Added new logon news item (#%d)."),
	  _("Syntax: \002LOGONNEWS DEL {\037num\037 | ALL}"),
	  _("Logon news item #%s not found!"),
	  _("Logon news item #%d deleted."),
	  _("No logon news items to delete!"),
	  _("All logon news items deleted.")}
	 },
	{NEWS_OPER, "OPER",
	 {_("OPERNEWS {ADD|DEL|LIST} [\037text\037|\037num\037]\002"),
	  _("Oper news items:"),
	  _("There is no oper news."),
	  _("Syntax: \002OPERNEWS ADD \037text\037\002"),
	  _("Added new oper news item (#%d)."),
	  _("Syntax: \002OPERNEWS DEL {\037num\037 | ALL}"),
	  _("Oper news item #%s not found!"),
	  _("Oper news item #%d deleted."),
	  _("No oper news items to delete!"),
	  _("All oper news items deleted.")}
	 },
	{NEWS_RANDOM, "RANDOM",
	 {_("RANDOMNEWS {ADD|DEL|LIST} [\037text\037|\037num\037]\002"),
	  _("Random news items:"),
	  _("There is no random news."),
	  _("Syntax: \002RANDOMNEWS ADD \037text\037\002"),
	  _("Added new random news item (#%d)."),
	  _("Syntax: \002RANDOMNEWS DEL {\037num\037 | ALL}"),
	  _("Random news item #%s not found!"),
	  _("Random news item #%d deleted."),
	  _("No random news items to delete!"),
	  _("All random news items deleted.")}
	 }
};

static void DisplayNews(User *u, NewsType Type)
{
	static unsigned current_news = 0;
	Anope::string msg;

	if (Type == NEWS_LOGON)
		msg = _("[\002Logon News\002 - %s] %s");
	else if (Type == NEWS_OPER)
		msg = _("[\002Oper News\002 - %s] %s");
	else if (Type == NEWS_RANDOM)
		msg = _("[\002Random News\002 - %s] %s");
	else
		throw CoreException("news: Invalid type (" + stringify(Type) + ") to display_news()");

	unsigned displayed = 0;
	bool NewsExists = false;
	for (unsigned i = 0, end = News.size(); i < end; ++i)
	{
		if (News[i]->type == Type)
		{
			NewsExists = true;

			if (Type == NEWS_RANDOM && i == current_news)
				continue;

			u->SendMessage(Type != NEWS_OPER && Global ? Global : OperServ, msg.c_str(), do_strftime(News[i]->time).c_str(), News[i]->Text.c_str());

			++displayed;

			if (Type == NEWS_RANDOM)
			{
				current_news = i;
				return;
			}
			else if (displayed >= Config->NewsCount)
				return;
		}

		/* Reset to head of list to get first random news value */
		if (i + 1 == News.size() && Type == NEWS_RANDOM && NewsExists)
			i = 0;
	}
}

static int add_newsitem(CommandSource &source, const Anope::string &text, NewsType type)
{
	int num = 0;

	for (unsigned i = News.size(); i > 0; --i)
		if (News[i - 1]->type == type)
		{
			num = News[i - 1]->num;
			break;
		}

	NewsItem *news = new NewsItem();
	news->type = type;
	news->num = num + 1;
	news->Text = text;
	news->time = Anope::CurTime;
	news->who = source.u->nick;

	News.push_back(news);

	return num + 1;
}

static int del_newsitem(unsigned num, NewsType type)
{
	int count = 0;

	for (unsigned i = News.size(); i > 0; --i)
		if (News[i - 1]->type == type && (num == 0 || News[i - 1]->num == num))
		{
			delete News[i - 1];
			News.erase(News.begin() + i - 1);
			++count;
		}

	return count;
}

static const char **findmsgs(NewsType type, Anope::string &type_name)
{
	for (unsigned i = 0; i < lenof(msgarray); ++i)
		if (msgarray[i].type == type)
		{
			type_name = msgarray[i].name;
			return msgarray[i].msgs;
		}
	return NULL;
}

class NewsBase : public Command
{
 protected:
	CommandReturn DoList(CommandSource &source, NewsType type, const char **msgs)
	{
		int count = 0;

		for (unsigned i = 0, end = News.size(); i < end; ++i)
			if (News[i]->type == type)
			{
				if (!count)
					source.Reply(msgs[MSG_LIST_HEADER]);
				source.Reply(_("%5d (%s by %s)\n""    %s"), News[i]->num, do_strftime(News[i]->time).c_str(), !News[i]->who.empty() ? News[i]->who.c_str() : "<unknown>", News[i]->Text.c_str());
				++count;
			}
		if (!count)
			source.Reply(msgs[MSG_LIST_NONE]);
		else
			source.Reply(_(END_OF_ANY_LIST), "News");

		return MOD_CONT;
	}

	CommandReturn DoAdd(CommandSource &source, const std::vector<Anope::string> &params, NewsType type, const char **msgs)
	{
		const Anope::string text = params.size() > 1 ? params[1] : "";
		int n;

		if (text.empty())
			this->OnSyntaxError(source, "ADD");
		else
		{
			if (readonly)
			{
				source.Reply(_(READ_ONLY_MODE));
				return MOD_CONT;
			}
			n = add_newsitem(source, text, type);
			if (n < 0)
				source.Reply(_("News list is full!"));
			else
				source.Reply(msgs[MSG_ADDED], n);
		}

		return MOD_CONT;
	}

	CommandReturn DoDel(CommandSource &source, const std::vector<Anope::string> &params, NewsType type, const char **msgs)
	{
		const Anope::string &text = params.size() > 1 ? params[1] : "";

		if (text.empty())
			this->OnSyntaxError(source, "DEL");
		else
		{
			if (readonly)
			{
				source.Reply(_(READ_ONLY_MODE));
				return MOD_CONT;
			}
			if (!text.equals_ci("ALL"))
			{
				try
				{
					unsigned num = convertTo<unsigned>(text);
					if (num > 0 && del_newsitem(num, type))
					{
						source.Reply(msgs[MSG_DELETED], num);
						for (unsigned i = 0, end = News.size(); i < end; ++i)
							if (News[i]->type == type && News[i]->num > num)
								--News[i]->num;
						return MOD_CONT;
					}
				}
				catch (const ConvertException &) { }

				source.Reply(msgs[MSG_DEL_NOT_FOUND], text.c_str());
			}
			else
			{
				if (del_newsitem(0, type))
					source.Reply(msgs[MSG_DELETED_ALL]);
				else
					source.Reply(msgs[MSG_DEL_NONE]);
			}
		}

		return MOD_CONT;
	}

	CommandReturn DoNews(CommandSource &source, const std::vector<Anope::string> &params, NewsType type)
	{
		const Anope::string &cmd = params[0];
		Anope::string type_name;

		const char **msgs = findmsgs(type, type_name);
		if (!msgs)
			throw CoreException("news: Invalid type to do_news()");

		if (cmd.equals_ci("LIST"))
			return this->DoList(source, type, msgs);
		else if (cmd.equals_ci("ADD"))
			return this->DoAdd(source, params, type, msgs);
		else if (cmd.equals_ci("DEL"))
			return this->DoDel(source, params, type, msgs);
		else
			this->OnSyntaxError(source, "");

		return MOD_CONT;
	}
 public:
	NewsBase(const Anope::string &newstype) : Command(newstype, 1, 2, "operserv/news")
	{
	}

	virtual ~NewsBase()
	{
	}

	virtual CommandReturn Execute(CommandSource &source, const std::vector<Anope::string> &params) = 0;

	virtual bool OnHelp(CommandSource &source, const Anope::string &subcommand) = 0;

	virtual void OnSyntaxError(CommandSource &source, const Anope::string &subcommand) = 0;
};

class CommandOSLogonNews : public NewsBase
{
 public:
	CommandOSLogonNews() : NewsBase("LOGONNEWS")
	{
		this->SetDesc(_("Define messages to be shown to users at logon"));
	}

	CommandReturn Execute(CommandSource &source, const std::vector<Anope::string> &params)
	{
		return this->DoNews(source, params, NEWS_LOGON);
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand)
	{
		source.Reply(_("Syntax: \002LOGONNEWS ADD \037text\037\002\n"
			"        \002LOGONNEWS DEL {\037num\037 | ALL}\002\n"
			"        \002LOGONNEWS LIST\002\n"
			" \n"
			"Edits or displays the list of logon news messages.  When a\n"
			"user connects to the network, these messages will be sent\n"
			"to them.  (However, no more than \002%d\002 messages will be\n"
			"sent in order to avoid flooding the user.  If there are\n"
			"more news messages, only the most recent will be sent.)\n"
			"NewsCount can be configured in services.conf.\n"
			" \n"
			"LOGONNEWS may only be used by Services Operators."), Config->NewsCount);
		return true;
	}

	void OnSyntaxError(CommandSource &source, const Anope::string &subcommand)
	{
		SyntaxError(source, "LOGONNEWS", _("LOGONNEWS {ADD|DEL|LIST} [\037text\037|\037num\037]\002"));
	}
};

class CommandOSOperNews : public NewsBase
{
 public:
	CommandOSOperNews() : NewsBase("OPERNEWS")
	{
		this->SetDesc(_("Define messages to be shown to users who oper"));
	}

	CommandReturn Execute(CommandSource &source, const std::vector<Anope::string> &params)
	{
		return this->DoNews(source, params, NEWS_OPER);
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand)
	{
		source.Reply(_("Syntax: \002OPERNEWS ADD \037text\037\002\n"
				"        \002OPERNEWS DEL {\037num\037 | ALL}\002\n"
				"        \002OPERNEWS LIST\002\n"
				" \n"
				"Edits or displays the list of oper news messages.  When a\n"
				"user opers up (with the /OPER command), these messages will\n"
				"be sent to them.  (However, no more than \002%d\002 messages will\n"
				"be sent in order to avoid flooding the user.  If there are\n"
				"more news messages, only the most recent will be sent.)\n"
				"NewsCount can be configured in services.conf.\n"
				" \n"
				"OPERNEWS may only be used by Services Operators."), Config->NewsCount);
		return true;
	}

	void OnSyntaxError(CommandSource &source, const Anope::string &subcommand)
	{
		SyntaxError(source, "OPERNEWS", _("OPERNEWS {ADD|DEL|LIST} [\037text\037|\037num\037]\002"));
	}
};

class CommandOSRandomNews : public NewsBase
{
 public:
	CommandOSRandomNews() : NewsBase("RANDOMNEWS")
	{
		this->SetDesc(_("Define messages to be randomly shown to users at logon"));
	}

	CommandReturn Execute(CommandSource &source, const std::vector<Anope::string> &params)
	{
		return this->DoNews(source, params, NEWS_RANDOM);
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand)
	{
		source.Reply(_("Syntax: \002RANDOMNEWS ADD \037text\037\002\n"
				"        \002RANDOMNEWS DEL {\037num\037 | ALL}\002\n"
				"        \002RANDOMNEWS LIST\002\n"
				" \n"
				"Edits or displays the list of random news messages.  When a\n"
				"user connects to the network, one (and only one) of the\n"
				"random news will be randomly chosen and sent to them.\n"
				" \n"
				"RANDOMNEWS may only be used by Services Operators."));
		return true;
	}

	void OnSyntaxError(CommandSource &source, const Anope::string &subcommand)
	{
		SyntaxError(source, "RANDOMNEWS", _("RANDOMNEWS {ADD|DEL|LIST} [\037text\037|\037num\037]\002"));
	}
};

class OSNews : public Module
{
	CommandOSLogonNews commandoslogonnews;
	CommandOSOperNews commandosopernews;
	CommandOSRandomNews commandosrandomnews;

 public:
	OSNews(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator)
	{
		this->SetAuthor("Anope");
		this->SetType(CORE);

		this->AddCommand(OperServ, &commandoslogonnews);
		this->AddCommand(OperServ, &commandosopernews);
		this->AddCommand(OperServ, &commandosrandomnews);

		Implementation i[] = { I_OnUserModeSet, I_OnUserConnect, I_OnDatabaseRead, I_OnDatabaseWrite };
		ModuleManager::Attach(i, this, 4);
	}

	~OSNews()
	{
		for (std::vector<NewsItem *>::iterator it = News.begin(), it_end = News.end(); it != it_end; ++it)
			delete *it;
		News.clear();
	}

	void OnUserModeSet(User *u, UserModeName Name)
	{
		if (Name == UMODE_OPER)
			DisplayNews(u, NEWS_OPER);
	}

	void OnUserConnect(User *u)
	{
		DisplayNews(u, NEWS_LOGON);
		DisplayNews(u, NEWS_RANDOM);
	}

	EventReturn OnDatabaseRead(const std::vector<Anope::string> &params)
	{
		if (params[0].equals_ci("OS") && params.size() >= 7 && params[1].equals_ci("NEWS"))
		{
			NewsItem *n = new NewsItem();
			n->num = params[2].is_pos_number_only() ? convertTo<unsigned>(params[2]) : 0;
			n->time = params[3].is_number_only() ? convertTo<time_t>(params[3]) : 0;
			n->who = params[4];
			if (params[5].equals_ci("LOGON"))
				n->type = NEWS_LOGON;
			else if (params[5].equals_ci("RANDOM"))
				n->type = NEWS_RANDOM;
			else if (params[5].equals_ci("OPER"))
				n->type = NEWS_OPER;
			n->Text = params[6];
			News.push_back(n);

			return EVENT_STOP;
		}

		return EVENT_CONTINUE;
	}

	void OnDatabaseWrite(void (*Write)(const Anope::string &))
	{
		for (std::vector<NewsItem *>::iterator it = News.begin(); it != News.end(); ++it)
		{
			NewsItem *n = *it;
			Anope::string ntype;
			if (n->type == NEWS_LOGON)
				ntype = "LOGON";
			else if (n->type == NEWS_RANDOM)
				ntype = "RANDOM";
			else if (n->type == NEWS_OPER)
				ntype = "OPER";
			Anope::string buf = "OS NEWS " + stringify(n->num) + " " + stringify(n->time) + " " + n->who + " " + ntype + " :" + n->Text;
			Write(buf);
		}
	}
};

MODULE_INIT(OSNews)