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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
/* OperServ 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"
#include "modules/operserv/news.h"
// TODO: msgarray breaks the format string checking
#ifdef __GNUC__
# pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* List of messages for each news type. This simplifies message sending. */
enum
{
MSG_SYNTAX,
MSG_NEWS_SHORT,
MSG_NEWS_LONG,
MSG_LIST_HEADER,
MSG_LIST_NONE,
MSG_ADDED,
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"),
_("[\002Logon News\002] %s"),
_("[\002Logon News\002 - %s] %s"),
_("Logon news items:"),
_("There is no logon news."),
_("Added new logon news item."),
_("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"),
_("[\002Oper News\002] %s"),
_("[\002Oper News\002 - %s] %s"),
_("Oper news items:"),
_("There is no oper news."),
_("Added new oper news item."),
_("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"),
_("[\002Random News\002] %s"),
_("[\002Random News\002 - %s] %s"),
_("Random news items:"),
_("There is no random news."),
_("Added new random news item."),
_("Random news item #%s not found!"),
_("Random news item #%d deleted."),
_("No random news items to delete!"),
_("All random news items deleted.")}
}
};
struct NewsItemType final
: Serialize::Type
{
NewsItemType()
: Serialize::Type("NewsItem")
{
}
void Serialize(const Serializable *obj, Serialize::Data &data) const override
{
const auto *ni = static_cast<const NewsItem *>(obj);
data.Store("type", ni->type);
data.Store("text", ni->text);
data.Store("who", ni->who);
data.Store("time", ni->time);
}
Serializable *Unserialize(Serializable *obj, Serialize::Data &data) const override
{
if (!news_service)
return NULL;
NewsItem *ni;
if (obj)
ni = anope_dynamic_static_cast<NewsItem *>(obj);
else
ni = new NewsItem();
unsigned int t;
data["type"] >> t;
ni->type = static_cast<NewsType>(t);
data["text"] >> ni->text;
data["who"] >> ni->who;
data["time"] >> ni->time;
if (!obj)
news_service->AddNewsItem(ni);
return ni;
}
};
class MyNewsService final
: public NewsService
{
std::vector<NewsItem *> newsItems[3];
public:
MyNewsService(Module *m) : NewsService(m) { }
~MyNewsService() override
{
for (const auto &newstype : newsItems)
{
for (const auto *newsitem : newstype)
delete newsitem;
}
}
NewsItem *CreateNewsItem() override
{
return new NewsItem();
}
void AddNewsItem(NewsItem *n) override
{
this->newsItems[n->type].push_back(n);
}
void DelNewsItem(NewsItem *n) override
{
std::vector<NewsItem *> &list = this->GetNewsList(n->type);
std::vector<NewsItem *>::iterator it = std::find(list.begin(), list.end(), n);
if (it != list.end())
list.erase(it);
delete n;
}
std::vector<NewsItem *> &GetNewsList(NewsType t) override
{
return this->newsItems[t];
}
};
#define lenof(a) (sizeof(a) / sizeof(*(a)))
static const char **findmsgs(NewsType type)
{
for (auto &msg : msgarray)
if (msg.type == type)
return msg.msgs;
return NULL;
}
class NewsBase
: public Command
{
ServiceReference<NewsService> ns;
protected:
void DoList(CommandSource &source, NewsType ntype, const char **msgs)
{
std::vector<NewsItem *> &list = this->ns->GetNewsList(ntype);
if (list.empty())
source.Reply(msgs[MSG_LIST_NONE]);
else
{
ListFormatter lflist(source.GetAccount());
lflist.AddColumn(_("Number")).AddColumn(_("Creator")).AddColumn(_("Created")).AddColumn(_("Text"));
for (unsigned i = 0, end = list.size(); i < end; ++i)
{
ListFormatter::ListEntry entry;
entry["Number"] = Anope::ToString(i + 1);
entry["Creator"] = list[i]->who;
entry["Created"] = Anope::strftime(list[i]->time, NULL, true);
entry["Text"] = list[i]->text;
lflist.AddEntry(entry);
}
source.Reply(msgs[MSG_LIST_HEADER]);
std::vector<Anope::string> replies;
lflist.Process(replies);
for (const auto &reply : replies)
source.Reply(reply);
source.Reply(_("End of news list."));
}
return;
}
void DoAdd(CommandSource &source, const std::vector<Anope::string> ¶ms, NewsType ntype, const char **msgs)
{
const Anope::string text = params.size() > 1 ? params[1] : "";
if (text.empty())
this->OnSyntaxError(source, "ADD");
else
{
if (Anope::ReadOnly)
source.Reply(READ_ONLY_MODE);
NewsItem *news = new NewsItem();
news->type = ntype;
news->text = text;
news->time = Anope::CurTime;
news->who = source.GetNick();
this->ns->AddNewsItem(news);
source.Reply(msgs[MSG_ADDED]);
Log(LOG_ADMIN, source, this) << "to add a news item";
}
return;
}
void DoDel(CommandSource &source, const std::vector<Anope::string> ¶ms, NewsType ntype, const char **msgs)
{
const Anope::string &text = params.size() > 1 ? params[1] : "";
if (text.empty())
this->OnSyntaxError(source, "DEL");
else
{
std::vector<NewsItem *> &list = this->ns->GetNewsList(ntype);
if (list.empty())
source.Reply(msgs[MSG_LIST_NONE]);
else
{
if (Anope::ReadOnly)
source.Reply(READ_ONLY_MODE);
if (!text.equals_ci("ALL"))
{
unsigned num = Anope::Convert<unsigned>(text, 0);
if (num > 0 && num <= list.size())
{
this->ns->DelNewsItem(list[num - 1]);
source.Reply(msgs[MSG_DELETED], num);
Log(LOG_ADMIN, source, this) << "to delete a news item";
return;
}
source.Reply(msgs[MSG_DEL_NOT_FOUND], text.c_str());
}
else
{
for (unsigned i = list.size(); i > 0; --i)
this->ns->DelNewsItem(list[i - 1]);
source.Reply(msgs[MSG_DELETED_ALL]);
Log(LOG_ADMIN, source, this) << "to delete all news items";
}
}
}
return;
}
void DoNews(CommandSource &source, const std::vector<Anope::string> ¶ms, NewsType ntype)
{
if (!this->ns)
return;
const Anope::string &cmd = params[0];
const char **msgs = findmsgs(ntype);
if (!msgs)
throw CoreException("news: Invalid type to DoNews()");
if (cmd.equals_ci("LIST"))
return this->DoList(source, ntype, msgs);
else if (cmd.equals_ci("ADD"))
return this->DoAdd(source, params, ntype, msgs);
else if (cmd.equals_ci("DEL"))
return this->DoDel(source, params, ntype, msgs);
else
this->OnSyntaxError(source, "");
return;
}
public:
NewsBase(Module *creator, const Anope::string &newstype) : Command(creator, newstype, 1, 2), ns("NewsService", "news")
{
this->SetSyntax(_("ADD \037text\037"));
this->SetSyntax(_("DEL {\037num\037 | ALL}"));
this->SetSyntax("LIST");
}
~NewsBase() override
{
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) override = 0;
bool OnHelp(CommandSource &source, const Anope::string &subcommand) override = 0;
};
class CommandOSLogonNews final
: public NewsBase
{
public:
CommandOSLogonNews(Module *creator) : NewsBase(creator, "operserv/logonnews")
{
this->SetDesc(_("Define messages to be shown to users at logon"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) override
{
return this->DoNews(source, params, NEWS_LOGON);
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_(
"Edits or displays the list of logon news messages. When a "
"user connects to the network, these messages will be sent "
"to them. However, no more than \002%d\002 messages will be "
"sent in order to avoid flooding the user. If there are "
"more news messages, only the most recent will be sent."
),
Config->GetModule(this->owner).Get<unsigned>("newscount", "3"));
return true;
}
};
class CommandOSOperNews final
: public NewsBase
{
public:
CommandOSOperNews(Module *creator) : NewsBase(creator, "operserv/opernews")
{
this->SetDesc(_("Define messages to be shown to users who oper"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) override
{
return this->DoNews(source, params, NEWS_OPER);
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_(
"Edits or displays the list of oper news messages. When a "
"user opers up (with the /OPER command), these messages will "
"be sent to them. However, no more than \002%d\002 messages will "
"be sent in order to avoid flooding the user. If there are "
"more news messages, only the most recent will be sent."
),
Config->GetModule(this->owner).Get<unsigned>("newscount", "3"));
return true;
}
};
class CommandOSRandomNews final
: public NewsBase
{
public:
CommandOSRandomNews(Module *creator) : NewsBase(creator, "operserv/randomnews")
{
this->SetDesc(_("Define messages to be randomly shown to users at logon"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) override
{
return this->DoNews(source, params, NEWS_RANDOM);
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_(
"Edits or displays the list of random news messages. When a "
"user connects to the network, one (and only one) of the "
"random news will be randomly chosen and sent to them."
));
return true;
}
};
static unsigned cur_rand_news = 0;
class OSNews final
: public Module
{
MyNewsService newsservice;
NewsItemType newsitem_type;
CommandOSLogonNews commandoslogonnews;
CommandOSOperNews commandosopernews;
CommandOSRandomNews commandosrandomnews;
Anope::string oper_announcer, announcer;
unsigned news_count;
void DisplayNews(User *u, NewsType Type)
{
std::vector<NewsItem *> &newsList = this->newsservice.GetNewsList(Type);
if (newsList.empty())
return;
const auto &modconf = Config->GetModule(this);
BotInfo *bi = NULL;
if (Type == NEWS_OPER)
bi = BotInfo::Find(modconf.Get<const Anope::string>("oper_announcer", "OperServ"), true);
else
bi = BotInfo::Find(modconf.Get<const Anope::string>("announcer", "Global"), true);
if (bi == NULL)
return;
const auto **msgs = findmsgs(Type);
if (!msgs)
return; // BUG
int start = 0;
if (Type != NEWS_RANDOM)
{
start = newsList.size() - news_count;
if (start < 0)
start = 0;
}
const auto showdate = modconf.Get<bool>("showdate", "yes");
for (unsigned i = start, end = newsList.size(); i < end; ++i)
{
if (Type == NEWS_RANDOM && i != cur_rand_news)
continue;
const auto *news = newsList[i];
if (showdate)
u->SendMessage(bi, msgs[MSG_NEWS_LONG], Anope::strftime(news->time, u->Account(), true).c_str(), news->text.c_str());
else
u->SendMessage(bi, msgs[MSG_NEWS_SHORT], news->text.c_str());
if (Type == NEWS_RANDOM)
{
++cur_rand_news;
break;
}
}
/* Reset to head of list to get first random news value */
if (Type == NEWS_RANDOM && cur_rand_news >= newsList.size())
cur_rand_news = 0;
}
public:
OSNews(const Anope::string &modname, const Anope::string &creator)
: Module(modname, creator, VENDOR)
, newsservice(this)
, commandoslogonnews(this)
, commandosopernews(this)
, commandosrandomnews(this)
{
}
void OnReload(Configuration::Conf &conf) override
{
oper_announcer = conf.GetModule(this).Get<const Anope::string>("oper_announcer", "OperServ");
announcer = conf.GetModule(this).Get<const Anope::string>("announcer", "Global");
news_count = conf.GetModule(this).Get<unsigned>("newscount", "3");
}
void OnUserModeSet(const MessageSource &setter, User *u, const Anope::string &mname) override
{
if (mname == "OPER")
DisplayNews(u, NEWS_OPER);
}
void OnUserConnect(User *user, bool &) override
{
if (user->Quitting() || !user->server->IsSynced())
return;
DisplayNews(user, NEWS_LOGON);
DisplayNews(user, NEWS_RANDOM);
}
};
MODULE_INIT(OSNews)
|