Debian Codegraph @

modules/infoserv/main.c

raw
1 /*
2 * Copyright (c) 2005 Atheme Development Group
3 * Rights to this code are documented in doc/LICENSE.
4 *
5 * This file contains the main() routine.
6 *
7 * We're basically re-implementing everything to support
8 * oper info messages. Yes, I know this is a bit ugly, but
9 * I can't think of a saner way that is efficient, avoids a
10 * few bugs and doesn't break people's existing InfoServ DB entries.
11 *
12 */
13
14 #include "atheme.h"
15 #include <limits.h>
16
17 DECLARE_MODULE_V1
18 (
19 "infoserv/main", true, _modinit, _moddeinit,
20 PACKAGE_STRING,
21 VENDOR_STRING
22 );
23
24 struct logoninfo_ {
25 stringref nick;
26 char *subject;
27 time_t info_ts;
28 char *story;
29 };
30
31 typedef struct logoninfo_ logoninfo_t;
32
33 mowgli_list_t logon_info;
34
35 struct operlogoninfo_ {
36 stringref nick;
37 char *subject;
38 time_t info_ts;
39 char *story;
40 };
41
42 typedef struct operlogoninfo_ operlogoninfo_t;
43
44 mowgli_list_t operlogon_info;
45 unsigned int logoninfo_count = 0;
46
47 service_t *infoserv;
48
49 static void is_cmd_help(sourceinfo_t *si, const int parc, char *parv[]);
50 static void is_cmd_post(sourceinfo_t *si, int parc, char *parv[]);
51 static void is_cmd_del(sourceinfo_t *si, int parc, char *parv[]);
52 static void is_cmd_odel(sourceinfo_t *si, int parc, char *parv[]);
53 static void is_cmd_list(sourceinfo_t *si, int parc, char *parv[]);
54 static void is_cmd_olist(sourceinfo_t *si, int parc, char *parv[]);
55 static void display_info(hook_user_nick_t *data);
56 static void display_oper_info(user_t *u);
57
58 static void write_infodb(database_handle_t *db);
59 static void db_h_li(database_handle_t *db, const char *type);
60
61 command_t is_help = { "HELP", N_(N_("Displays contextual help information.")), AC_NONE, 2, is_cmd_help, { .path = "help" } };
62 command_t is_post = { "POST", N_("Post news items for users to view."), PRIV_GLOBAL, 3, is_cmd_post, { .path = "infoserv/post" } };
63 command_t is_del = { "DEL", N_("Delete news items."), PRIV_GLOBAL, 1, is_cmd_del, { .path = "infoserv/del" } };
64 command_t is_odel = { "ODEL", N_("Delete oper news items."), PRIV_GLOBAL, 1, is_cmd_odel, { .path = "infoserv/odel" } };
65 command_t is_list = { "LIST", N_("List previously posted news items."), AC_NONE, 1, is_cmd_list, { .path = "infoserv/list" } };
66 /* Should prolly change the priv for this. What would be a better priv for it though? */
67 command_t is_olist = { "OLIST", N_("List previously posted oper news items."), PRIV_GLOBAL, 1, is_cmd_olist, { .path = "infoserv/olist" } };
68
69 /* HELP <command> [params] */
70 void is_cmd_help(sourceinfo_t *si, int parc, char *parv[])
71 {
72 char *command = parv[0];
73
74 if (!command)
75 {
76 command_success_nodata(si, _("***** \2%s Help\2 *****"), si->service->nick);
77 command_success_nodata(si, _("\2%s\2 allows users to view informational messages."), si->service->nick);
78 command_success_nodata(si, " ");
79 command_success_nodata(si, _("For more information on a command, type:"));
80 command_success_nodata(si, "\2/%s%s help <command>\2", (ircd->uses_rcommand == false) ? "msg " : "", infoserv->disp);
81 command_success_nodata(si, " ");
82
83 command_help(si, si->service->commands);
84
85 command_success_nodata(si, _("***** \2End of Help\2 *****"));
86 return;
87 }
88
89 /* take the command through the hash table */
90 help_display(si, si->service, command, si->service->commands);
91 }
92
93 static void underscores_to_spaces(char *y)
94 {
95 do
96 {
97 if(*y == '_')
98 *y = ' ';
99 } while(*y++);
100 }
101
102 static void write_infodb(database_handle_t *db)
103 {
104 mowgli_node_t *n;
105
106 MOWGLI_ITER_FOREACH(n, logon_info.head)
107 {
108 logoninfo_t *l = n->data;
109
110 db_start_row(db, "LI");
111 db_write_word(db, l->nick);
112 db_write_word(db, l->subject);
113 db_write_time(db, l->info_ts);
114 db_write_str(db, l->story);
115 db_commit_row(db);
116 }
117
118 MOWGLI_ITER_FOREACH(n, operlogon_info.head)
119 {
120 operlogoninfo_t *o = n->data;
121
122 db_start_row(db, "LIO");
123 db_write_word(db, o->nick);
124 db_write_word(db, o->subject);
125 db_write_time(db, o->info_ts);
126 db_write_str(db, o->story);
127 db_commit_row(db);
128 }
129
130 }
131
132 static void db_h_li(database_handle_t *db, const char *type)
133 {
134 const char *nick = db_sread_word(db);
135 const char *subject = db_sread_word(db);
136 time_t info_ts = db_sread_time(db);
137 const char *story = db_sread_str(db);
138
139 logoninfo_t *l = smalloc(sizeof(logoninfo_t));
140 l->nick = strshare_get(nick);
141 l->subject = sstrdup(subject);
142 l->info_ts = info_ts;
143 l->story = sstrdup(story);
144 mowgli_node_add(l, mowgli_node_create(), &logon_info);
145 }
146
147 static void db_h_lio(database_handle_t *db, const char *type)
148 {
149 const char *nick = db_sread_word(db);
150 const char *subject = db_sread_word(db);
151 time_t info_ts = db_sread_time(db);
152 const char *story = db_sread_str(db);
153
154 operlogoninfo_t *o = smalloc(sizeof(operlogoninfo_t));
155 o->nick = strshare_get(nick);
156 o->subject = sstrdup(subject);
157 o->info_ts = info_ts;
158 o->story = sstrdup(story);
159 mowgli_node_add(o, mowgli_node_create(), &operlogon_info);
160 }
161
162 static void display_info(hook_user_nick_t *data)
163 {
164 user_t *u;
165 mowgli_node_t *n;
166 logoninfo_t *l;
167 char dBuf[BUFSIZE];
168 struct tm tm;
169 unsigned int count = 0;
170
171 u = data->u;
172 if (u == NULL)
173 return;
174
175 /* abort if it's an internal client */
176 if (is_internal_client(u))
177 return;
178 /* abort if user is coming back from split */
179 if (!(u->server->flags & SF_EOB))
180 return;
181
182 if (logon_info.count > 0)
183 {
184 notice(infoserv->nick, u->nick, "*** \2Message(s) of the Day\2 ***");
185
186 MOWGLI_ITER_FOREACH_PREV(n, logon_info.tail)
187 {
188 l = n->data;
189
190 char *y = sstrdup(l->subject);
191 underscores_to_spaces(y);
192
193 tm = *localtime(&l->info_ts);
194 strftime(dBuf, BUFSIZE, "%H:%M on %m/%d/%Y", &tm);
195 notice(infoserv->nick, u->nick, "[\2%s\2] Notice from %s, posted %s:",
196 y, l->nick, dBuf);
197 notice(infoserv->nick, u->nick, "%s", l->story);
198 free(y);
199 count++;
200
201 /* only display three latest entries, max. */
202 if (count == logoninfo_count)
203 break;
204 }
205
206 notice(infoserv->nick, u->nick, "*** \2End of Message(s) of the Day\2 ***");
207 }
208 }
209
210 static void display_oper_info(user_t *u)
211 {
212 mowgli_node_t *n;
213 operlogoninfo_t *o;
214 char dBuf[BUFSIZE];
215 struct tm tm;
216 unsigned int count = 0;
217
218 if (u == NULL)
219 return;
220
221 /* abort if it's an internal client */
222 if (is_internal_client(u))
223 return;
224 /* abort if user is coming back from split */
225 if (!(u->server->flags & SF_EOB))
226 return;
227
228 if (operlogon_info.count > 0)
229 {
230 notice(infoserv->nick, u->nick, "*** \2Oper Message(s) of the Day\2 ***");
231
232 MOWGLI_ITER_FOREACH_PREV(n, operlogon_info.tail)
233 {
234 o = n->data;
235
236 char *y = sstrdup(o->subject);
237 underscores_to_spaces(y);
238
239 tm = *localtime(&o->info_ts);
240 strftime(dBuf, BUFSIZE, "%H:%M on %m/%d/%Y", &tm);
241 notice(infoserv->nick, u->nick, "[\2%s\2] Notice from %s, posted %s:",
242 y, o->nick, dBuf);
243 notice(infoserv->nick, u->nick, "%s", o->story);
244 free(y);
245 count++;
246
247 /* only display three latest entries, max. */
248 if (count == logoninfo_count)
249 break;
250 }
251
252 notice(infoserv->nick, u->nick, "*** \2End of Oper Message(s) of the Day\2 ***");
253 }
254 }
255
256 static void osinfo_hook(sourceinfo_t *si)
257 {
258 return_if_fail(si != NULL);
259
260 command_success_nodata(si, "How many messages will be sent to users on connect: %u", logoninfo_count);
261 }
262
263 static void is_cmd_post(sourceinfo_t *si, int parc, char *parv[])
264 {
265 char *importance = parv[0];
266 char *subject = parv[1];
267 char *story = parv[2];
268 int imp;
269 logoninfo_t *l;
270 operlogoninfo_t *o;
271 mowgli_node_t *n;
272 char buf[BUFSIZE];
273
274 /* make sure they're logged in */
275 if (!si->smu)
276 {
277 command_fail(si, fault_noprivs, _("You are not logged in."));
278 return;
279 }
280
281 if (!subject || !story || !importance)
282 {
283 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "POST");
284 command_fail(si, fault_needmoreparams, _("Syntax: POST <importance> <subject> <message>"));
285 return;
286 }
287
288 imp = atoi(importance);
289
290 if ((imp < 0) || (imp >=5))
291 {
292 command_fail(si, fault_badparams, _("Importance must be a digit between 0 and 4"));
293 return;
294 }
295
296 char *y = sstrdup(subject);
297 underscores_to_spaces(y);
298
299 if (imp == 4)
300 {
301 snprintf(buf, sizeof buf, "[CRITICAL NETWORK NOTICE] %s - [%s] %s", get_source_name(si), y, story);
302 msg_global_sts(infoserv->me, "*", buf);
303 command_success_nodata(si, _("The InfoServ message has been sent"));
304 logcommand(si, CMDLOG_ADMIN, "INFO:POST: Importance: \2%s\2, Subject: \2%s\2, Message: \2%s\2", importance, y, story);
305 free(y);
306 return;
307 }
308
309 if (imp == 2)
310 {
311 snprintf(buf, sizeof buf, "[Network Notice] %s - [%s] %s", get_source_name(si), y, story);
312 notice_global_sts(infoserv->me, "*", buf);
313 command_success_nodata(si, _("The InfoServ message has been sent"));
314 logcommand(si, CMDLOG_ADMIN, "INFO:POST: Importance: \2%s\2, Subject: \2%s\2, Message: \2%s\2", importance, y, story);
315 free(y);
316 return;
317 }
318
319 if (imp == 0)
320 {
321 o = smalloc(sizeof(operlogoninfo_t));
322 o->nick = strshare_ref(entity(si->smu)->name);
323 o->info_ts = CURRTIME;
324 o->story = sstrdup(story);
325 o->subject = sstrdup(subject);
326
327 n = mowgli_node_create();
328 mowgli_node_add(o, n, &operlogon_info);
329 }
330
331 if (imp > 0)
332 {
333 l = smalloc(sizeof(logoninfo_t));
334 l->nick = strshare_ref(entity(si->smu)->name);
335 l->info_ts = CURRTIME;
336 l->story = sstrdup(story);
337 l->subject = sstrdup(subject);
338
339 n = mowgli_node_create();
340 mowgli_node_add(l, n, &logon_info);
341 }
342
343 command_success_nodata(si, _("Added entry to logon info"));
344 logcommand(si, CMDLOG_ADMIN, "INFO:POST: Importance: \2%s\2, Subject: \2%s\2, Message: \2%s\2", importance, y, story);
345
346 if (imp == 3)
347 {
348 snprintf(buf, sizeof buf, "[Network Notice] %s - [%s] %s", get_source_name(si), y, story);
349 notice_global_sts(infoserv->me, "*", buf);
350 }
351
352 free(y);
353
354 return;
355 }
356
357 static void is_cmd_del(sourceinfo_t *si, int parc, char *parv[])
358 {
359 char *target = parv[0];
360 int x = 0;
361 int id;
362 logoninfo_t *l;
363 mowgli_node_t *n;
364
365 if (!target)
366 {
367 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "DEL");
368 command_fail(si, fault_needmoreparams, "Syntax: DEL <id>");
369 return;
370 }
371
372 id = atoi(target);
373
374 if (id <= 0)
375 {
376 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "DEL");
377 command_fail(si, fault_badparams, "Syntax: DEL <id>");
378 return;
379 }
380
381 /* search for it */
382 MOWGLI_ITER_FOREACH(n, logon_info.head)
383 {
384 l = n->data;
385 x++;
386
387 if (x == id)
388 {
389 logcommand(si, CMDLOG_ADMIN, "INFO:DEL: \2%s\2, \2%s\2", l->subject, l->story);
390
391 mowgli_node_delete(n, &logon_info);
392
393 strshare_unref(l->nick);
394 free(l->subject);
395 free(l->story);
396 free(l);
397
398 command_success_nodata(si, _("Deleted entry %d from logon info."), id);
399 return;
400 }
401 }
402
403 command_fail(si, fault_nosuch_target, _("Entry %d not found in logon info."), id);
404 return;
405 }
406
407 static void is_cmd_odel(sourceinfo_t *si, int parc, char *parv[])
408 {
409 char *target = parv[0];
410 int x = 0;
411 int id;
412 operlogoninfo_t *o;
413 mowgli_node_t *n;
414
415 if (!target)
416 {
417 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "ODEL");
418 command_fail(si, fault_needmoreparams, "Syntax: ODEL <id>");
419 return;
420 }
421
422 id = atoi(target);
423
424 if (id <= 0)
425 {
426 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "ODEL");
427 command_fail(si, fault_badparams, "Syntax: ODEL <id>");
428 return;
429 }
430
431 /* search for it */
432 MOWGLI_ITER_FOREACH(n, operlogon_info.head)
433 {
434 o = n->data;
435 x++;
436
437 if (x == id)
438 {
439 logcommand(si, CMDLOG_ADMIN, "INFO:ODEL: \2%s\2, \2%s\2", o->subject, o->story);
440
441 mowgli_node_delete(n, &operlogon_info);
442
443 strshare_unref(o->nick);
444 free(o->subject);
445 free(o->story);
446 free(o);
447
448 command_success_nodata(si, _("Deleted entry %d from oper logon info."), id);
449 return;
450 }
451 }
452
453 command_fail(si, fault_nosuch_target, _("Entry %d not found in oper logon info."), id);
454 return;
455 }
456
457 static void is_cmd_list(sourceinfo_t *si, int parc, char *parv[])
458 {
459 logoninfo_t *l;
460 mowgli_node_t *n;
461 struct tm tm;
462 char dBuf[BUFSIZE];
463 int x = 0;
464
465 MOWGLI_ITER_FOREACH(n, logon_info.head)
466 {
467 l = n->data;
468 x++;
469
470 char *y = sstrdup(l->subject);
471 underscores_to_spaces(y);
472
473 tm = *localtime(&l->info_ts);
474 strftime(dBuf, BUFSIZE, "%H:%M on %m/%d/%Y", &tm);
475 command_success_nodata(si, "%d: [\2%s\2] by \2%s\2 at \2%s\2: \2%s\2",
476 x, y, l->nick, dBuf, l->story);
477 free(y);
478 }
479
480 command_success_nodata(si, _("End of list."));
481 logcommand(si, CMDLOG_GET, "LIST");
482 return;
483 }
484
485 static void is_cmd_olist(sourceinfo_t *si, int parc, char *parv[])
486 {
487 operlogoninfo_t *o;
488 mowgli_node_t *n;
489 struct tm tm;
490 char dBuf[BUFSIZE];
491 int x = 0;
492
493 MOWGLI_ITER_FOREACH(n, operlogon_info.head)
494 {
495 o = n->data;
496 x++;
497
498 char *y = sstrdup(o->subject);
499 underscores_to_spaces(y);
500
501 tm = *localtime(&o->info_ts);
502 strftime(dBuf, BUFSIZE, "%H:%M on %m/%d/%Y", &tm);
503 command_success_nodata(si, "%d: [\2%s\2] by \2%s\2 at \2%s\2: \2%s\2",
504 x, y, o->nick, dBuf, o->story);
505 free(y);
506 }
507
508 command_success_nodata(si, _("End of list."));
509 logcommand(si, CMDLOG_GET, "OLIST");
510 return;
511 }
512
513 void _modinit(module_t *m)
514 {
515 if (!module_find_published("backend/opensex"))
516 {
517 slog(LG_INFO, "Module %s requires use of the OpenSEX database backend, refusing to load.", m->name);
518 m->mflags = MODTYPE_FAIL;
519 return;
520 }
521
522 infoserv = service_add("infoserv", NULL);
523 add_uint_conf_item("LOGONINFO_COUNT", &infoserv->conf_table, 0, &logoninfo_count, 0, INT_MAX, 3);
524
525 hook_add_event("user_add");
526 hook_add_user_add(display_info);
527 hook_add_event("user_oper");
528 hook_add_user_oper(display_oper_info);
529 hook_add_event("operserv_info");
530 hook_add_operserv_info(osinfo_hook);
531 hook_add_db_write(write_infodb);
532
533 db_register_type_handler("LI", db_h_li);
534 db_register_type_handler("LIO", db_h_lio);
535
536 service_bind_command(infoserv, &is_help);
537 service_bind_command(infoserv, &is_post);
538 service_bind_command(infoserv, &is_del);
539 service_bind_command(infoserv, &is_odel);
540 service_bind_command(infoserv, &is_list);
541 service_bind_command(infoserv, &is_olist);
542 }
543
544 void _moddeinit(module_unload_intent_t intent)
545 {
546 del_conf_item("LOGONINFO_COUNT", &infoserv->conf_table);
547
548 if (infoserv)
549 {
550 service_delete(infoserv);
551 infoserv = NULL;
552 }
553
554 hook_del_user_add(display_info);
555 hook_del_user_oper(display_oper_info);
556 hook_del_operserv_info(osinfo_hook);
557 hook_del_db_write(write_infodb);
558
559 db_unregister_type_handler("LI");
560 db_unregister_type_handler("LIO");
561
562 service_unbind_command(infoserv, &is_help);
563 service_unbind_command(infoserv, &is_post);
564 service_unbind_command(infoserv, &is_del);
565 service_unbind_command(infoserv, &is_odel);
566 service_unbind_command(infoserv, &is_list);
567 service_unbind_command(infoserv, &is_olist);
568 }
569
570 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
571 * vim:ts=8
572 * vim:sw=8
573 * vim:noexpandtab
574 */