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
|
/*
* Anope IRC Services
*
* Copyright (C) 2010-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/entrymsg.h"
class EntryMsgImpl : public EntryMsg
{
friend class EntryMsgType;
Serialize::Storage<ChanServ::Channel *> channel;
Serialize::Storage<Anope::string> creator, message;
Serialize::Storage<time_t> when;
public:
using EntryMsg::EntryMsg;
ChanServ::Channel *GetChannel() override;
void SetChannel(ChanServ::Channel *ci) override;
Anope::string GetCreator() override;
void SetCreator(const Anope::string &c) override;
Anope::string GetMessage() override;
void SetMessage(const Anope::string &m) override;
time_t GetWhen() override;
void SetWhen(const time_t &t) override;
};
class EntryMsgType : public Serialize::Type<EntryMsgImpl>
{
public:
Serialize::ObjectField<EntryMsgImpl, ChanServ::Channel *> chan;
Serialize::Field<EntryMsgImpl, Anope::string> creator, message;
Serialize::Field<EntryMsgImpl, time_t> when;
EntryMsgType(Module *me) : Serialize::Type<EntryMsgImpl>(me)
, chan(this, "chan", &EntryMsgImpl::channel, true)
, creator(this, "creator", &EntryMsgImpl::creator)
, message(this, "message", &EntryMsgImpl::message)
, when(this, "when", &EntryMsgImpl::when)
{
}
};
ChanServ::Channel *EntryMsgImpl::GetChannel()
{
return Get(&EntryMsgType::chan);
}
void EntryMsgImpl::SetChannel(ChanServ::Channel *ci)
{
Set(&EntryMsgType::chan, ci);
}
Anope::string EntryMsgImpl::GetCreator()
{
return Get(&EntryMsgType::creator);
}
void EntryMsgImpl::SetCreator(const Anope::string &c)
{
Set(&EntryMsgType::creator, c);
}
Anope::string EntryMsgImpl::GetMessage()
{
return Get(&EntryMsgType::message);
}
void EntryMsgImpl::SetMessage(const Anope::string &m)
{
Set(&EntryMsgType::message, m);
}
time_t EntryMsgImpl::GetWhen()
{
return Get(&EntryMsgType::when);
}
void EntryMsgImpl::SetWhen(const time_t &t)
{
Set(&EntryMsgType::when, t);
}
class CommandEntryMessage : public Command
{
private:
void DoList(CommandSource &source, ChanServ::Channel *ci)
{
std::vector<EntryMsg *> messages = ci->GetRefs<EntryMsg *>();
if (messages.empty())
{
source.Reply(_("Entry message list for \002{0}\002 is empty."), ci->GetName());
return;
}
source.Reply(_("Entry message list for \002{0}\002:"), ci->GetName());
ListFormatter list(source.GetAccount());
list.AddColumn(_("Number")).AddColumn(_("Creator")).AddColumn(_("Created")).AddColumn(_("Message"));
for (unsigned i = 0; i < messages.size(); ++i)
{
EntryMsg *msg = messages[i];
ListFormatter::ListEntry entry;
entry["Number"] = stringify(i + 1);
entry["Creator"] = msg->GetCreator();
entry["Created"] = Anope::strftime(msg->GetWhen(), NULL, true);
entry["Message"] = msg->GetMessage();
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, ChanServ::Channel *ci, const Anope::string &message)
{
std::vector<EntryMsg *> messages = ci->GetRefs<EntryMsg *>();
if (messages.size() >= Config->GetModule(this->GetOwner())->Get<unsigned>("maxentries"))
{
source.Reply(_("The entry message list for \002{0}\002 is full."), ci->GetName());
return;
}
EntryMsg *msg = Serialize::New<EntryMsg *>();
msg->SetChannel(ci);
msg->SetCreator(source.GetNick());
msg->SetMessage(message);
logger.Command(source, ci, _("{source} used {command} on {channel} to add a message"));
source.Reply(_("Entry message added to \002{0}\002"), ci->GetName());
}
void DoDel(CommandSource &source, ChanServ::Channel *ci, const Anope::string &message)
{
std::vector<EntryMsg *> messages = ci->GetRefs<EntryMsg *>();
if (!message.is_pos_number_only())
{
source.Reply(("Entry message \002{0}\002 not found on channel \002{1}\002."), message, ci->GetName());
return;
}
if (messages.empty())
{
source.Reply(_("Entry message list for \002{0}\002 is empty."), ci->GetName());
return;
}
try
{
unsigned i = convertTo<unsigned>(message);
if (i > 0 && i <= messages.size())
{
messages[i - 1]->Delete();
logger.Command(source, ci, _("{source} used {command} on {channel} to remove a message"));
source.Reply(_("Entry message \002{0}\002 for \002{1]\002 deleted."), i, ci->GetName());
}
else
throw ConvertException();
}
catch (const ConvertException &)
{
source.Reply(_("Entry message \002{0}\002 not found on channel \002{1}\002."), message, ci->GetName());
}
}
void DoClear(CommandSource &source, ChanServ::Channel *ci)
{
for (EntryMsg *e : ci->GetRefs<EntryMsg *>())
e->Delete();
logger.Command(source, ci, _("{source} used {command} on {channel} to remove all messages"));
source.Reply(_("Entry messages for \002{0}\002 have been cleared."), ci->GetName());
}
public:
CommandEntryMessage(Module *creator) : Command(creator, "chanserv/entrymsg", 2, 3)
{
this->SetDesc(_("Manage the channel's entry messages"));
this->SetSyntax(_("\037channel\037 ADD \037message\037"));
this->SetSyntax(_("\037channel\037 DEL \037num\037"));
this->SetSyntax(_("\037channel\037 LIST"));
this->SetSyntax(_("\037channel\037 CLEAR"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) override
{
const Anope::string &chan = params[0];
ChanServ::Channel *ci = ChanServ::Find(chan);
if (ci == NULL)
{
source.Reply(_("Channel \002{0}\002 isn't registered."), chan);
return;
}
if (Anope::ReadOnly && !params[1].equals_ci("LIST"))
{
source.Reply(_("Services are in read-only mode."));
return;
}
if (!source.AccessFor(ci).HasPriv("SET") && !source.HasOverridePriv("chanserv/administration"))
{
source.Reply(_("Access denied. You do not have privilege \002{0}\002 on \002{1}\002."), "FOUNDER", ci->GetName());
return;
}
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)
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
this->OnSyntaxError(source, "");
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
{
source.Reply(_("Controls what messages will be sent to users when they join \037channel\037.\n"
"\n"
"The \002{0} ADD\002 command adds the given message to the list of messages to be shown to users when they join the channel.\n"
"\n"
"The \002{0} DEL\002 command removes the given message from the entry message list.\n"
" You can remove the message by specifying its number which you can get by listing the messages as explained below.\n"
"\n"
"The \002{0} LIST\002 command displays the list of messages on the entry message list.\n"
"\n"
"The \002{0} CLEAR\002 command clears the entry message list.\n"
"\n"
"Use of this command requires the \002SET\002 privilege on \037channel\037."),
source.GetCommand());
return true;
}
};
class CSEntryMessage : public Module
, public EventHook<Event::JoinChannel>
{
CommandEntryMessage commandentrymsg;
EntryMsgType entrymsg_type;
public:
CSEntryMessage(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR)
, EventHook<Event::JoinChannel>(this)
, commandentrymsg(this)
, entrymsg_type(this)
{
}
void OnJoinChannel(User *u, Channel *c) override
{
if (u && c && c->ci && u->server->IsSynced())
for (EntryMsg *msg : c->ci->GetRefs<EntryMsg *>())
u->SendMessage(c->ci->WhoSends(), "[{0}] {1}", c->ci->GetName(), msg->GetMessage());
}
};
MODULE_INIT(CSEntryMessage)
|