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
|
/* ChanServ core functions
*
* (C) 2003-2021 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/cs_entrymsg.h"
struct EntryMsgImpl : EntryMsg, Serializable
{
EntryMsgImpl() : Serializable("EntryMsg")
{
}
EntryMsgImpl(ChannelInfo *c, const Anope::string &cname, const Anope::string &cmessage, time_t ct = Anope::CurTime) : Serializable("EntryMsg")
{
this->chan = c->name;
this->creator = cname;
this->message = cmessage;
this->when = ct;
}
~EntryMsgImpl();
void Serialize(Serialize::Data &data) const anope_override
{
data["ci"] << this->chan;
data["creator"] << this->creator;
data["message"] << this->message;
data.SetType("when", Serialize::Data::DT_INT); data["when"] << this->when;
}
static Serializable* Unserialize(Serializable *obj, Serialize::Data &data);
};
struct EntryMessageListImpl : EntryMessageList
{
EntryMessageListImpl(Extensible *) { }
EntryMsg* Create() anope_override
{
return new EntryMsgImpl();
}
};
EntryMsgImpl::~EntryMsgImpl()
{
ChannelInfo *ci = ChannelInfo::Find(this->chan);
if (!ci)
return;
EntryMessageList *messages = ci->GetExt<EntryMessageList>("entrymsg");
if (!messages)
return;
std::vector<EntryMsg *>::iterator it = std::find((*messages)->begin(), (*messages)->end(), this);
if (it != (*messages)->end())
(*messages)->erase(it);
}
Serializable* EntryMsgImpl::Unserialize(Serializable *obj, Serialize::Data &data)
{
Anope::string sci, screator, smessage;
time_t swhen;
data["ci"] >> sci;
data["creator"] >> screator;
data["message"] >> smessage;
ChannelInfo *ci = ChannelInfo::Find(sci);
if (!ci)
return NULL;
if (obj)
{
EntryMsgImpl *msg = anope_dynamic_static_cast<EntryMsgImpl *>(obj);
msg->chan = ci->name;
data["creator"] >> msg->creator;
data["message"] >> msg->message;
data["when"] >> msg->when;
return msg;
}
EntryMessageList *messages = ci->Require<EntryMessageList>("entrymsg");
data["when"] >> swhen;
EntryMsgImpl *m = new EntryMsgImpl(ci, screator, smessage, swhen);
(*messages)->push_back(m);
return m;
}
class CommandEntryMessage : public Command
{
private:
void DoList(CommandSource &source, ChannelInfo *ci)
{
EntryMessageList *messages = ci->Require<EntryMessageList>("entrymsg");
if ((*messages)->empty())
{
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(source.GetAccount());
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"] = Anope::strftime(msg->when, NULL, true);
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->Require<EntryMessageList>("entrymsg");
if ((*messages)->size() >= Config->GetModule(this->owner)->Get<unsigned>("maxentries"))
source.Reply(_("The entry message list for \002%s\002 is full."), ci->name.c_str());
else
{
(*messages)->push_back(new EntryMsgImpl(ci, source.GetNick(), message));
Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to add a 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->Require<EntryMessageList>("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)->empty())
source.Reply(_("Entry message list for \002%s\002 is empty."), ci->name.c_str());
else
{
try
{
unsigned i = convertTo<unsigned>(message);
if (i > 0 && i <= (*messages)->size())
{
delete (*messages)->at(i - 1);
if ((*messages)->empty())
ci->Shrink<EntryMessageList>("entrymsg");
Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to remove a message";
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());
}
}
}
void DoClear(CommandSource &source, ChannelInfo *ci)
{
ci->Shrink<EntryMessageList>("entrymsg");
Log(source.AccessFor(ci).HasPriv("SET") ? LOG_COMMAND : LOG_OVERRIDE, source, this, ci) << "to remove all messages";
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 \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) anope_override
{
ChannelInfo *ci = ChannelInfo::Find(params[0]);
if (ci == NULL)
{
source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str());
return;
}
if (Anope::ReadOnly && !params[1].equals_ci("LIST"))
{
source.Reply(READ_ONLY_MODE);
return;
}
if (!source.AccessFor(ci).HasPriv("SET") && !source.HasPriv("chanserv/administration"))
{
source.Reply(ACCESS_DENIED);
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) anope_override
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_("Controls what messages will be sent to users when they join the channel."));
source.Reply(" ");
source.Reply(_("The \002ENTRYMSG ADD\002 command adds the given message to\n"
"the list of messages shown to users when they join\n"
"the channel."));
source.Reply(" ");
source.Reply(_("The \002ENTRYMSG DEL\002 command removes the specified message from\n"
"the list of messages shown to users when they join\n"
"the channel. You can remove a message by specifying its number\n"
"which you can get by listing the messages as explained below."));
source.Reply(" ");
source.Reply(_("The \002ENTRYMSG LIST\002 command displays a listing of messages\n"
"shown to users when they join the channel."));
source.Reply(" ");
source.Reply(_("The \002ENTRYMSG CLEAR\002 command clears all entries from\n"
"the list of messages shown to users when they join\n"
"the channel, effectively disabling entry messages."));
source.Reply(" ");
source.Reply(_("Adding, deleting, or clearing entry messages requires the\n"
"SET permission."));
return true;
}
};
class CSEntryMessage : public Module
{
CommandEntryMessage commandentrymsg;
ExtensibleItem<EntryMessageListImpl> eml;
Serialize::Type entrymsg_type;
public:
CSEntryMessage(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
commandentrymsg(this),
eml(this, "entrymsg"), entrymsg_type("EntryMsg", EntryMsgImpl::Unserialize)
{
}
void OnJoinChannel(User *u, Channel *c) anope_override
{
if (u && c && c->ci && u->server->IsSynced())
{
EntryMessageList *messages = c->ci->GetExt<EntryMessageList>("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)->at(i)->message.c_str());
}
}
};
MODULE_INIT(CSEntryMessage)
|