Debian Codegraph @

modules/chanserv/topic.c

raw
1 /*
2 * Copyright (c) 2005 William Pitcock, et al.
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * This file contains code for the CService TOPIC functions.
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1
12 (
13 "chanserv/topic", false, _modinit, _moddeinit,
14 PACKAGE_STRING,
15 VENDOR_STRING
16 );
17
18 static void cs_cmd_topic(sourceinfo_t *si, int parc, char *parv[]);
19 static void cs_cmd_topicappend(sourceinfo_t *si, int parc, char *parv[]);
20 static void cs_cmd_topicprepend(sourceinfo_t *si, int parc, char *parv[]);
21 static void cs_cmd_topicswap(sourceinfo_t *si, int parc, char *parv[]);
22
23 command_t cs_topic = { "TOPIC", N_("Sets a topic on a channel."),
24 AC_NONE, 2, cs_cmd_topic, { .path = "cservice/topic" } };
25 command_t cs_topicappend = { "TOPICAPPEND", N_("Appends a topic on a channel."),
26 AC_NONE, 2, cs_cmd_topicappend, { .path = "cservice/topicappend" } };
27 command_t cs_topicprepend = { "TOPICPREPEND", N_("Prepends a topic on a channel."),
28 AC_NONE, 2, cs_cmd_topicprepend, { .path = "cservice/topicprepend" } };
29 command_t cs_topicswap = { "TOPICSWAP", N_("Swap part of the topic on a channel."),
30 AC_NONE, 2, cs_cmd_topicswap, { .path = "cservice/topicswap" } };
31
32 void _modinit(module_t *m)
33 {
34 service_named_bind_command("chanserv", &cs_topic);
35 service_named_bind_command("chanserv", &cs_topicappend);
36 service_named_bind_command("chanserv", &cs_topicprepend);
37 service_named_bind_command("chanserv", &cs_topicswap);
38 }
39
40 void _moddeinit(module_unload_intent_t intent)
41 {
42 service_named_unbind_command("chanserv", &cs_topic);
43 service_named_unbind_command("chanserv", &cs_topicappend);
44 service_named_unbind_command("chanserv", &cs_topicprepend);
45 service_named_unbind_command("chanserv", &cs_topicswap);
46 }
47
48 static void cs_cmd_topic(sourceinfo_t *si, int parc, char *parv[])
49 {
50 char *chan = parv[0];
51 char *topic = parv[1];
52 mychan_t *mc;
53 channel_t *c;
54 const char *topicsetter;
55 time_t prevtopicts;
56
57 if (!chan || !topic)
58 {
59 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "TOPIC");
60 command_fail(si, fault_needmoreparams, _("Syntax: TOPIC <#channel> <topic>"));
61 return;
62 }
63
64 mc = mychan_find(chan);
65 if (!mc)
66 {
67 command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), chan);
68 return;
69 }
70
71 c = channel_find(chan);
72 if (!c)
73 {
74 command_fail(si, fault_nosuch_target, _("\2%s\2 is currently empty."), chan);
75 return;
76 }
77
78 if (metadata_find(mc, "private:close:closer"))
79 {
80 command_fail(si, fault_noprivs, _("\2%s\2 is closed."), chan);
81 return;
82 }
83
84 if (!chanacs_source_has_flag(mc, si, CA_TOPIC))
85 {
86 command_fail(si, fault_noprivs, _("You are not authorized to perform this operation."));
87 return;
88 }
89
90 if (!validtopic(topic))
91 {
92 command_fail(si, fault_badparams, _("The new topic is invalid or too long."));
93 return;
94 }
95
96 if (si->su != NULL)
97 topicsetter = si->su->nick;
98 else if (si->smu != NULL)
99 topicsetter = entity(si->smu)->name;
100 else
101 topicsetter = "unknown";
102 prevtopicts = c->topicts;
103 handle_topic(c, topicsetter, CURRTIME, topic);
104 topic_sts(c, si->service->me, topicsetter, CURRTIME, prevtopicts, topic);
105
106 logcommand(si, CMDLOG_DO, "TOPIC: \2%s\2", mc->name);
107 if (si->su == NULL || !chanuser_find(c, si->su))
108 command_success_nodata(si, _("Topic set to \2%s\2 on \2%s\2."), topic, chan);
109 }
110
111 static void cs_cmd_topicappend(sourceinfo_t *si, int parc, char *parv[])
112 {
113 char *chan = parv[0];
114 char *topic = parv[1];
115 mychan_t *mc;
116 char topicbuf[BUFSIZE];
117 channel_t *c;
118 const char *topicsetter;
119 time_t prevtopicts;
120
121 if (!chan || !topic)
122 {
123 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "TOPICAPPEND");
124 command_fail(si, fault_needmoreparams, _("Syntax: TOPICAPPEND <#channel> <topic>"));
125 return;
126 }
127
128 mc = mychan_find(chan);
129 if (!mc)
130 {
131 command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), chan);
132 return;
133 }
134
135 c = channel_find(chan);
136 if (!c)
137 {
138 command_fail(si, fault_nosuch_target, _("\2%s\2 is currently empty."), chan);
139 return;
140 }
141
142 if (!chanacs_source_has_flag(mc, si, CA_TOPIC))
143 {
144 command_fail(si, fault_noprivs, _("You are not authorized to perform this operation."));
145 return;
146 }
147
148 if (metadata_find(mc, "private:close:closer"))
149 {
150 command_fail(si, fault_noprivs, _("\2%s\2 is closed."), chan);
151 return;
152 }
153
154 topicbuf[0] = '\0';
155
156 if (c->topic)
157 {
158 mowgli_strlcpy(topicbuf, c->topic, BUFSIZE);
159 mowgli_strlcat(topicbuf, " | ", BUFSIZE);
160 mowgli_strlcat(topicbuf, topic, BUFSIZE);
161 }
162 else
163 mowgli_strlcpy(topicbuf, topic, BUFSIZE);
164
165 if (!validtopic(topicbuf))
166 {
167 command_fail(si, fault_badparams, _("The new topic is invalid or too long."));
168 return;
169 }
170
171 if (si->su != NULL)
172 topicsetter = si->su->nick;
173 else if (si->smu != NULL)
174 topicsetter = entity(si->smu)->name;
175 else
176 topicsetter = "unknown";
177 prevtopicts = c->topicts;
178 handle_topic(c, topicsetter, CURRTIME, topicbuf);
179 topic_sts(c, si->service->me, topicsetter, CURRTIME, prevtopicts, topicbuf);
180
181 logcommand(si, CMDLOG_DO, "TOPICAPPEND: \2%s\2", mc->name);
182 if (si->su == NULL || !chanuser_find(c, si->su))
183 command_success_nodata(si, _("Topic set to \2%s\2 on \2%s\2."), c->topic, chan);
184 }
185
186 static void cs_cmd_topicprepend(sourceinfo_t *si, int parc, char *parv[])
187 {
188 char *chan = parv[0];
189 char *topic = parv[1];
190 mychan_t *mc;
191 char topicbuf[BUFSIZE];
192 channel_t *c;
193 const char *topicsetter;
194 time_t prevtopicts;
195
196 if (!chan || !topic)
197 {
198 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "TOPICPREPEND");
199 command_fail(si, fault_needmoreparams, _("Syntax: TOPICPREPEND <#channel> <topic>"));
200 return;
201 }
202
203 mc = mychan_find(chan);
204 if (!mc)
205 {
206 command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), chan);
207 return;
208 }
209
210 c = channel_find(chan);
211 if (!c)
212 {
213 command_fail(si, fault_nosuch_target, _("\2%s\2 is currently empty."), chan);
214 return;
215 }
216
217 if (!chanacs_source_has_flag(mc, si, CA_TOPIC))
218 {
219 command_fail(si, fault_noprivs, _("You are not authorized to perform this operation."));
220 return;
221 }
222
223 if (metadata_find(mc, "private:close:closer"))
224 {
225 command_fail(si, fault_noprivs, _("\2%s\2 is closed."), chan);
226 return;
227 }
228
229 topicbuf[0] = '\0';
230
231 if (c->topic)
232 {
233 mowgli_strlcpy(topicbuf, topic, BUFSIZE);
234 mowgli_strlcat(topicbuf, " | ", BUFSIZE);
235 mowgli_strlcat(topicbuf, c->topic, BUFSIZE);
236 }
237 else
238 mowgli_strlcpy(topicbuf, topic, BUFSIZE);
239
240 if (!validtopic(topicbuf))
241 {
242 command_fail(si, fault_badparams, _("The new topic is invalid or too long."));
243 return;
244 }
245
246 if (si->su != NULL)
247 topicsetter = si->su->nick;
248 else if (si->smu != NULL)
249 topicsetter = entity(si->smu)->name;
250 else
251 topicsetter = "unknown";
252 prevtopicts = c->topicts;
253 handle_topic(c, topicsetter, CURRTIME, topicbuf);
254 topic_sts(c, si->service->me, topicsetter, CURRTIME, prevtopicts, topicbuf);
255
256 logcommand(si, CMDLOG_DO, "TOPICPREPEND: \2%s\2", mc->name);
257 if (si->su == NULL || !chanuser_find(c, si->su))
258 command_success_nodata(si, _("Topic set to \2%s\2 on \2%s\2."), c->topic, chan);
259 }
260
261 static void cs_cmd_topicswap(sourceinfo_t *si, int parc, char *parv[])
262 {
263 char *chan = parv[0];
264 char *topic = parv[1];
265 mychan_t *mc;
266 channel_t *c;
267 const char *topicsetter;
268 time_t prevtopicts;
269
270 char topicbuf[BUFSIZE];
271 char commbuf[BUFSIZE];
272 char *pos = NULL;
273 char *search = NULL;
274 char *replace = NULL;
275 size_t search_size = 0;
276 size_t replace_size = 0;
277 size_t copylen = 0;
278
279 if (parc < 2)
280 {
281 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "TOPICSWAP");
282 command_fail(si, fault_needmoreparams, _("Syntax: TOPICSWAP <#channel> <search>:[<replace>]"));
283 return;
284 }
285
286 mowgli_strlcpy(commbuf, parv[1], BUFSIZE);
287 search = commbuf;
288 pos = strrchr(commbuf, ':');
289
290 if (!pos || pos == commbuf)
291 {
292 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "TOPICSWAP");
293 command_fail(si, fault_badparams, _("Syntax: TOPICSWAP <#channel> <search>:[<replace>]"));
294 return;
295 }
296
297 mc = mychan_find(chan);
298 if (!mc)
299 {
300 command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), chan);
301 return;
302 }
303
304 c = channel_find(chan);
305 if (!c)
306 {
307 command_fail(si, fault_nosuch_target, _("\2%s\2 is currently empty."), chan);
308 return;
309 }
310
311 if (!chanacs_source_has_flag(mc, si, CA_TOPIC))
312 {
313 command_fail(si, fault_noprivs, _("You are not authorized to perform this operation."));
314 return;
315 }
316
317 if (metadata_find(mc, "private:close:closer"))
318 {
319 command_fail(si, fault_noprivs, _("\2%s\2 is closed."), chan);
320 return;
321 }
322
323 if (!c->topic)
324 topicbuf[0] = '\0';
325 else
326 mowgli_strlcpy(topicbuf, c->topic, BUFSIZE);
327
328 *pos = '\0';
329 replace = pos + 1;
330 search_size = strlen(search);
331 replace_size = strlen(replace);
332
333 pos = strstr(topicbuf, search);
334 if (!pos)
335 {
336 command_fail(si, fault_badparams, _("Channel \2%s\2 does not have \2%s\2 in its topic."), chan, search);
337 return;
338 }
339
340 copylen = strlen(pos + search_size) + 1;
341 if (pos - topicbuf + replace_size + copylen > BUFSIZE)
342 goto invalid_error;
343
344 memmove(pos + search_size + (replace_size - search_size), pos + search_size, copylen);
345 memcpy(pos, replace, replace_size);
346
347 if (!validtopic(topicbuf))
348 {
349 invalid_error:
350 command_fail(si, fault_badparams, _("The new topic is invalid or too long."));
351 return;
352 }
353
354 if (si->su != NULL)
355 topicsetter = si->su->nick;
356 else if (si->smu != NULL)
357 topicsetter = entity(si->smu)->name;
358 else
359 topicsetter = "unknown";
360 prevtopicts = c->topicts;
361 handle_topic(c, topicsetter, CURRTIME, topicbuf);
362 topic_sts(c, si->service->me, topicsetter, CURRTIME, prevtopicts, topicbuf);
363
364 logcommand(si, CMDLOG_DO, "TOPICSWAP: \2%s\2", mc->name);
365 if (si->su == NULL || !chanuser_find(c, si->su))
366 command_success_nodata(si, _("Topic set to \2%s\2 on \2%s\2."), c->topic, chan);
367 }
368
369 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
370 * vim:ts=8
371 * vim:sw=8
372 * vim:noexpandtab
373 */