]> jfr.im git - irc/evilnet/x3.git/blob - src/global.c
716de9b080d7d5ddcb1ffde7477d19b6f96f2cda
[irc/evilnet/x3.git] / src / global.c
1 /* global.c - Global notice service
2 * Copyright 2000-2004 srvx Development Team
3 *
4 * This file is part of x3.
5 *
6 * x3 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21 #include "conf.h"
22 #include "global.h"
23 #include "hash.h"
24 #include "modcmd.h"
25 #include "nickserv.h"
26 #include "saxdb.h"
27 #include "timeq.h"
28
29 #define GLOBAL_CONF_NAME "services/global"
30
31 #define GLOBAL_DB "global.db"
32 #define GLOBAL_TEMP_DB "global.db.new"
33
34 /* Global options */
35 #define KEY_DB_BACKUP_FREQ "db_backup_freq"
36 #define KEY_ANNOUNCEMENTS_DEFAULT "announcements_default"
37 #define KEY_NICK "nick"
38
39 /* Message data */
40 #define KEY_FLAGS "flags"
41 #define KEY_POSTED "posted"
42 #define KEY_DURATION "duration"
43 #define KEY_FROM "from"
44 #define KEY_MESSAGE "message"
45
46 /* Clarification: Notices are immediate, they are sent to matching users
47 _once_, then forgotten. Messages are stored in Global's database and
48 continually sent to users as they match the target specification until
49 they are deleted. */
50 static const struct message_entry msgtab[] = {
51 { "GMSG_INVALID_TARGET", "$b%s$b is an invalid message target." },
52 { "GMSG_MESSAGE_REQUIRED", "You $bmust$b provide a message to send." },
53 { "GMSG_MESSAGE_SENT", "Message to $b%s$b sent." },
54 { "GMSG_MESSAGE_ADDED", "Message to $b%s$b with ID %ld added." },
55 { "GMSG_MESSAGE_DELETED", "Message $b%s$b deleted." },
56 { "GMSG_ID_INVALID", "$b%s$b is an invalid message ID." },
57 { "GMSG_MESSAGE_COUNT", "$b%d$b messages found." },
58 { "GMSG_NO_MESSAGES", "There are no messages for you." },
59 { "GMSG_NOTICE_SOURCE", "Notice to [$b%s$b] from %s:" },
60 { "GMSG_MESSAGE_SOURCE", "Notice to [$b%s$b] from %s, posted %s:" },
61 //{ "GMSG_MOTD_HEADER", "$b------------- MESSAGE(S) OF THE DAY --------------$b" },
62 { "GMSG_MOTD_HEADER", "$bNetwork Announcements$b" },
63 { "GMSG_MOTD_BAR", "---------------------------------------" },
64 { "GMSG_MOTD_FOOTER", "--------------- Thank You--------------" },
65
66 /* These definitions are for other files that make use of global
67 * notices. Make sure you grep for them if you ever add args
68 * to the notice.
69 */
70 { "DEFCON_NETWORK_CHANGED", "Network DefCon level has changed to level %d" }, /* opserv.c */
71 { "DEFCON_OPER_LEVEL_CHANGE", "%s is changing the DefCon level to %d" }, /* opserv.c */
72 { "DEFCON_TIMEOUT_LEVEL_CHANGE", "The DefCon has changed back to level %d (timeout)" }, /* opserv.c */
73
74 { NULL, NULL }
75 };
76
77 #define GLOBAL_SYNTAX() svccmd_send_help_brief(user, global, cmd)
78 #define GLOBAL_FUNC(NAME) MODCMD_FUNC(NAME)
79
80 struct globalMessage
81 {
82 unsigned long id;
83 long flags;
84
85 time_t posted;
86 char posted_s[24];
87 unsigned long duration;
88
89 char *from;
90 char *message;
91
92 struct globalMessage *prev;
93 struct globalMessage *next;
94 };
95
96 struct userNode *global;
97
98 static struct module *global_module;
99 static struct service *global_service;
100 static struct globalMessage *messageList;
101 extern struct string_list *autojoin_channels;
102 static long messageCount;
103 static time_t last_max_alert;
104 static struct log_type *G_LOG;
105
106 static struct
107 {
108 unsigned long db_backup_frequency;
109 unsigned int announcements_default : 1;
110 } global_conf;
111
112 #define global_notice(target, format...) send_message(target , global , ## format)
113
114 void message_expire(void *data);
115
116 static struct globalMessage*
117 message_add(long flags, time_t posted, unsigned long duration, char *from, const char *msg)
118 {
119 struct globalMessage *message;
120 struct tm tm;
121
122 message = malloc(sizeof(struct globalMessage));
123 if(!message)
124 {
125 return NULL;
126 }
127
128 message->id = messageCount++;
129 message->flags = flags;
130 message->posted = posted;
131 message->duration = duration;
132 message->from = strdup(from);
133 message->message = strdup(msg);
134
135 if ((flags & MESSAGE_OPTION_IMMEDIATE) == 0) {
136 localtime_r(&message->posted, &tm);
137 strftime(message->posted_s, sizeof(message->posted_s),
138 "%I:%M %p, %m/%d/%Y", &tm);
139 }
140
141 if(messageList)
142 {
143 messageList->prev = message;
144 }
145 message->prev = NULL;
146 message->next = messageList;
147
148 messageList = message;
149
150 if(duration)
151 {
152 timeq_add(now + duration, message_expire, message);
153 }
154
155 return message;
156 }
157
158 static void
159 message_del(struct globalMessage *message)
160 {
161 if(message->duration)
162 {
163 timeq_del(0, NULL, message, TIMEQ_IGNORE_FUNC | TIMEQ_IGNORE_WHEN);
164 }
165
166 if(message->prev) message->prev->next = message->next;
167 else messageList = message->next;
168
169 if(message->next) message->next->prev = message->prev;
170
171 free(message->from);
172 free(message->message);
173 free(message);
174 }
175
176 void message_expire(void *data)
177 {
178 struct globalMessage *message = data;
179
180 message->duration = 0;
181 message_del(message);
182 }
183
184 static struct globalMessage*
185 message_create(struct userNode *user, unsigned int argc, char *argv[])
186 {
187 unsigned long duration = 0;
188 char *text = NULL;
189 char *sender;
190 long flags = 0;
191 unsigned int i;
192
193 sender = user->handle_info->handle;
194
195 for(i = 0; i < argc; i++)
196 {
197 if((i + 1) > argc)
198 {
199 global_notice(user, "MSG_MISSING_PARAMS", argv[argc]);
200 return NULL;
201 }
202
203 if(!irccasecmp(argv[i], "text"))
204 {
205 i++;
206 text = unsplit_string(argv + i, argc - i, NULL);
207 break;
208 } else if (!irccasecmp(argv[i], "sourceless")) {
209 i++;
210 flags |= MESSAGE_OPTION_SOURCELESS;
211 } else if (!irccasecmp(argv[i], "target")) {
212 i++;
213
214 if(!irccasecmp(argv[i], "all")) {
215 flags |= MESSAGE_RECIPIENT_ALL;
216 } else if(!irccasecmp(argv[i], "users")) {
217 flags |= MESSAGE_RECIPIENT_LUSERS;
218 } else if(!irccasecmp(argv[i], "helpers")) {
219 flags |= MESSAGE_RECIPIENT_HELPERS;
220 } else if(!irccasecmp(argv[i], "opers")) {
221 flags |= MESSAGE_RECIPIENT_OPERS;
222 } else if(!irccasecmp(argv[i], "staff") || !irccasecmp(argv[i], "privileged")) {
223 flags |= MESSAGE_RECIPIENT_STAFF;
224 } else if(!irccasecmp(argv[i], "channels")) {
225 flags |= MESSAGE_RECIPIENT_CHANNELS;
226 } else if(!irccasecmp(argv[i], "announcement") || !irccasecmp(argv[i], "announce")) {
227 flags |= MESSAGE_RECIPIENT_ANNOUNCE;
228 } else {
229 global_notice(user, "GMSG_INVALID_TARGET", argv[i]);
230 return NULL;
231 }
232 } else if (irccasecmp(argv[i], "duration") == 0) {
233 duration = ParseInterval(argv[++i]);
234 } else if (irccasecmp(argv[i], "from") == 0) {
235 sender = argv[++i];
236 } else {
237 global_notice(user, "MSG_INVALID_CRITERIA", argv[i]);
238 return NULL;
239 }
240 }
241
242 if(!flags)
243 {
244 flags = MESSAGE_RECIPIENT_LUSERS;
245 }
246
247 if(!text) {
248 global_notice(user, "GMSG_MESSAGE_REQUIRED");
249 return NULL;
250 }
251
252 return message_add(flags, now, duration, sender, text);
253 }
254
255 static const char *
256 messageType(const struct globalMessage *message)
257 {
258 if((message->flags & MESSAGE_RECIPIENT_ALL) == MESSAGE_RECIPIENT_ALL)
259 {
260 return "all";
261 }
262 if((message->flags & MESSAGE_RECIPIENT_STAFF) == MESSAGE_RECIPIENT_STAFF)
263 {
264 return "staff";
265 }
266 else if(message->flags & MESSAGE_RECIPIENT_ANNOUNCE)
267 {
268 return "announcement";
269 }
270 else if(message->flags & MESSAGE_RECIPIENT_OPERS)
271 {
272 return "opers";
273 }
274 else if(message->flags & MESSAGE_RECIPIENT_HELPERS)
275 {
276 return "helpers";
277 }
278 else if(message->flags & MESSAGE_RECIPIENT_LUSERS)
279 {
280 return "users";
281 }
282 else
283 {
284 return "channels";
285 }
286 }
287
288 static void
289 notice_target(const char *target, struct globalMessage *message)
290 {
291 if(!(message->flags & MESSAGE_OPTION_SOURCELESS))
292 {
293 if(message->flags & MESSAGE_OPTION_IMMEDIATE)
294 {
295 send_target_message(0, target, global, "GMSG_NOTICE_SOURCE", messageType(message), message->from);
296 }
297 else
298 {
299 send_target_message(0, target, global, "GMSG_MESSAGE_SOURCE", messageType(message), message->from, message->posted_s);
300 }
301 }
302
303 send_target_message(4, target, global, "%s", message->message);
304 }
305
306 static int
307 notice_channel(const char *key, void *data, void *extra)
308 {
309 struct chanNode *channel = data;
310 /* It should be safe to assume channel is not NULL. */
311 if(channel->channel_info)
312 notice_target(key, extra);
313 return 0;
314 }
315
316 static void
317 message_send(struct globalMessage *message)
318 {
319 struct userNode *user;
320 unsigned long n;
321 dict_iterator_t it;
322
323 if(message->flags & MESSAGE_RECIPIENT_CHANNELS)
324 {
325 dict_foreach(channels, notice_channel, message);
326 }
327
328 if(message->flags & MESSAGE_RECIPIENT_LUSERS)
329 {
330 notice_target("$*", message);
331 return;
332 }
333
334 if(message->flags & MESSAGE_RECIPIENT_ANNOUNCE)
335 {
336 char announce;
337
338 for (it = dict_first(clients); it; it = iter_next(it)) {
339 user = iter_data(it);
340 if (user->uplink == self) continue;
341 announce = user->handle_info ? user->handle_info->announcements : '?';
342 if (announce == 'n') continue;
343 if ((announce == '?') && !global_conf.announcements_default) continue;
344 notice_target(user->nick, message);
345 }
346 }
347
348 if(message->flags & MESSAGE_RECIPIENT_OPERS)
349 {
350 for(n = 0; n < curr_opers.used; n++)
351 {
352 user = curr_opers.list[n];
353
354 if(user->uplink != self)
355 {
356 notice_target(user->nick, message);
357 }
358 }
359 }
360
361 if(message->flags & MESSAGE_RECIPIENT_HELPERS)
362 {
363 for(n = 0; n < curr_helpers.used; n++)
364 {
365 user = curr_helpers.list[n];
366 if (IsOper(user))
367 continue;
368 notice_target(user->nick, message);
369 }
370 }
371 }
372
373 void
374 global_message_args(long targets, const char *language_entry, ...)
375 {
376 struct globalMessage *message;
377 va_list arg_list;
378 dict_iterator_t it;
379 char response[MAXLEN];
380 const char *fmt;
381
382 if(!targets || !global)
383 return;
384
385 fmt = strdup(language_entry);
386
387 /* Notice users/opers/helpers */
388 for (it = dict_first(clients); it; it = iter_next(it)) {
389 struct userNode *luser = iter_data(it);
390
391 language_entry = user_find_message(luser, fmt);
392
393 va_start(arg_list, language_entry);
394 vsnprintf(response, MAXLEN-2, language_entry, arg_list);
395 response[MAXLEN-1] = 0;
396
397 message = message_add(targets | MESSAGE_OPTION_SOURCELESS, now, 0, "", response);
398 if (!message)
399 continue;
400
401 /* opers */
402 if(message->flags & MESSAGE_RECIPIENT_OPERS && IsOper(luser)) {
403 if(luser->uplink != self)
404 notice_target(luser->nick, message);
405 }
406
407 /* helpers */
408 if (message->flags & MESSAGE_RECIPIENT_HELPERS && IsHelper(luser)) {
409 if (IsOper(luser))
410 continue;
411 notice_target(luser->nick, message);
412 }
413
414 /* users */
415 if (message->flags & MESSAGE_RECIPIENT_LUSERS)
416 notice_target(luser->nick, message);
417 }
418
419 message_del(message);
420 }
421
422 void
423 global_message(long targets, char *text)
424 {
425 struct globalMessage *message;
426
427 if(!targets || !global)
428 return;
429
430 message = message_add(targets | MESSAGE_OPTION_SOURCELESS, now, 0, "", text);
431 if(!message)
432 return;
433
434 message_send(message);
435 message_del(message);
436 }
437
438 static GLOBAL_FUNC(cmd_notice)
439 {
440 struct globalMessage *message = NULL;
441 const char *recipient = NULL, *text;
442 char *sender;
443 long target = 0;
444
445 assert(argc >= 3);
446 sender = user->handle_info->handle;
447 if(!irccasecmp(argv[1], "all")) {
448 target = MESSAGE_RECIPIENT_ALL;
449 } else if(!irccasecmp(argv[1], "users")) {
450 target = MESSAGE_RECIPIENT_LUSERS;
451 } else if(!irccasecmp(argv[1], "helpers")) {
452 target = MESSAGE_RECIPIENT_HELPERS;
453 } else if(!irccasecmp(argv[1], "opers")) {
454 target = MESSAGE_RECIPIENT_OPERS;
455 } else if(!irccasecmp(argv[1], "staff") || !irccasecmp(argv[1], "privileged")) {
456 target |= MESSAGE_RECIPIENT_HELPERS | MESSAGE_RECIPIENT_OPERS;
457 } else if(!irccasecmp(argv[1], "announcement") || !irccasecmp(argv[1], "announce")) {
458 target |= MESSAGE_RECIPIENT_ANNOUNCE;
459 } else if(!irccasecmp(argv[1], "channels")) {
460 target = MESSAGE_RECIPIENT_CHANNELS;
461 } else {
462 global_notice(user, "GMSG_INVALID_TARGET", argv[1]);
463 return 0;
464 }
465 if(!irccasecmp(argv[2], "from")) {
466 if (argc < 5) {
467 reply("MSG_MISSING_PARAMS", argv[0]);
468 GLOBAL_SYNTAX();
469 return 0;
470 }
471 sender = argv[3];
472 text = unsplit_string(argv + 4, argc - 4, NULL);
473 } else {
474 text = unsplit_string(argv + 2, argc - 2, NULL);
475 }
476
477 message = message_add(target | MESSAGE_OPTION_IMMEDIATE, now, 0, sender, text);
478 if(!message)
479 return 0;
480
481 recipient = messageType(message);
482 message_send(message);
483 message_del(message);
484
485 global_notice(user, "GMSG_MESSAGE_SENT", recipient);
486 return 1;
487 }
488
489 static GLOBAL_FUNC(cmd_message)
490 {
491 struct globalMessage *message = NULL;
492 const char *recipient = NULL;
493
494 assert(argc >= 3);
495 message = message_create(user, argc - 1, argv + 1);
496 if(!message)
497 return 0;
498 recipient = messageType(message);
499 global_notice(user, "GMSG_MESSAGE_ADDED", recipient, message->id);
500 return 1;
501 }
502
503 static GLOBAL_FUNC(cmd_list)
504 {
505 struct globalMessage *message;
506 struct helpfile_table table;
507 unsigned int length, nn;
508
509 if(!messageList)
510 {
511 global_notice(user, "GMSG_NO_MESSAGES");
512 return 1;
513 }
514
515 for(nn=0, message = messageList; message; nn++, message=message->next) ;
516 table.length = nn+1;
517 table.width = 5;
518 table.flags = TABLE_NO_FREE;
519 table.contents = calloc(table.length, sizeof(char**));
520 table.contents[0] = calloc(table.width, sizeof(char*));
521 table.contents[0][0] = "ID";
522 table.contents[0][1] = "Target";
523 table.contents[0][2] = "Expires";
524 table.contents[0][3] = "From";
525 table.contents[0][4] = "Message";
526
527 for(nn=1, message = messageList; message; nn++, message = message->next)
528 {
529 char buffer[64];
530
531 table.contents[nn] = calloc(table.width, sizeof(char*));
532 snprintf(buffer, sizeof(buffer), "%lu", message->id);
533 table.contents[nn][0] = strdup(buffer);
534 table.contents[nn][1] = messageType(message);
535 if(message->duration)
536 intervalString(buffer, message->posted + message->duration - now, user->handle_info);
537 else
538 strcpy(buffer, "Never.");
539 table.contents[nn][2] = strdup(buffer);
540 table.contents[nn][3] = message->from;
541 length = strlen(message->message);
542 safestrncpy(buffer, message->message, sizeof(buffer));
543 if(length > (sizeof(buffer) - 4))
544 {
545 buffer[sizeof(buffer) - 1] = 0;
546 buffer[sizeof(buffer) - 2] = buffer[sizeof(buffer) - 3] = buffer[sizeof(buffer) - 4] = '.';
547 }
548 table.contents[nn][4] = strdup(buffer);
549 }
550 table_send(global, user->nick, 0, NULL, table);
551 for (nn=1; nn<table.length; nn++)
552 {
553 free((char*)table.contents[nn][0]);
554 free((char*)table.contents[nn][2]);
555 free((char*)table.contents[nn][4]);
556 free(table.contents[nn]);
557 }
558 free(table.contents[0]);
559 free(table.contents);
560
561 return 1;
562 }
563
564 static GLOBAL_FUNC(cmd_remove)
565 {
566 struct globalMessage *message = NULL;
567 unsigned long id;
568
569 assert(argc >= 2);
570 id = strtoul(argv[1], NULL, 0);
571
572 for(message = messageList; message; message = message->next)
573 {
574 if(message->id == id)
575 {
576 message_del(message);
577 global_notice(user, "GMSG_MESSAGE_DELETED", argv[1]);
578 return 1;
579 }
580 }
581
582 global_notice(user, "GMSG_ID_INVALID", argv[1]);
583 return 0;
584 }
585
586 static unsigned int
587 send_messages(struct userNode *user, long mask, int obstreperize)
588 {
589 struct globalMessage *message = messageList;
590 unsigned int count = 0;
591
592 while(message)
593 {
594 if(message->flags & mask)
595 {
596 if (obstreperize && !count)
597 {
598 send_target_message(0, user->nick, global, "GMSG_MOTD_HEADER");
599 send_target_message(0, user->nick, global, "GMSG_MOTD_BAR");
600 }
601 notice_target(user->nick, message);
602 count++;
603 }
604
605 message = message->next;
606 }
607 if (obstreperize && count)
608 send_target_message(0, user->nick, global, "GMSG_MOTD_FOOTER");
609 return count;
610 }
611
612 static GLOBAL_FUNC(cmd_messages)
613 {
614 long mask = MESSAGE_RECIPIENT_LUSERS | MESSAGE_RECIPIENT_CHANNELS;
615 unsigned int count;
616
617 if(IsOper(user))
618 mask |= MESSAGE_RECIPIENT_OPERS;
619
620 if(IsHelper(user))
621 mask |= MESSAGE_RECIPIENT_HELPERS;
622
623 count = send_messages(user, mask, 0);
624 if(count)
625 global_notice(user, "GMSG_MESSAGE_COUNT", count);
626 else
627 global_notice(user, "GMSG_NO_MESSAGES");
628
629 return 1;
630 }
631
632 static int
633 global_process_user(struct userNode *user)
634 {
635 if(IsLocal(user) || self->uplink->burst || user->uplink->burst)
636 return 0;
637 send_messages(user, MESSAGE_RECIPIENT_LUSERS, 1);
638
639 /* only alert on new usercount if the record was broken in the last
640 * 30 seconds, and no alert had been sent in that time.
641 */
642 if((now - max_clients_time) <= 30 && (now - last_max_alert) > 30)
643 {
644 char *message;
645 message = alloca(36);
646 sprintf(message, "New user count record: %d", max_clients);
647 global_message(MESSAGE_RECIPIENT_OPERS, message);
648 last_max_alert = now;
649 }
650
651 return 0;
652 }
653
654 static void
655 global_process_auth(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle))
656 {
657 if(IsHelper(user))
658 send_messages(user, MESSAGE_RECIPIENT_HELPERS, 0);
659 }
660
661 static void
662 global_process_oper(struct userNode *user)
663 {
664 if(user->uplink->burst)
665 return;
666 send_messages(user, MESSAGE_RECIPIENT_OPERS, 0);
667 }
668
669 static void
670 global_conf_read(void)
671 {
672 dict_t conf_node;
673 const char *str;
674
675 if (!(conf_node = conf_get_data(GLOBAL_CONF_NAME, RECDB_OBJECT))) {
676 log_module(G_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", GLOBAL_CONF_NAME);
677 return;
678 }
679
680 str = database_get_data(conf_node, KEY_DB_BACKUP_FREQ, RECDB_QSTRING);
681 global_conf.db_backup_frequency = str ? ParseInterval(str) : 7200;
682 str = database_get_data(conf_node, KEY_ANNOUNCEMENTS_DEFAULT, RECDB_QSTRING);
683 global_conf.announcements_default = str ? enabled_string(str) : 1;
684
685 str = database_get_data(conf_node, KEY_NICK, RECDB_QSTRING);
686 if(global && str)
687 NickChange(global, str, 0);
688 }
689
690 static int
691 global_saxdb_read(struct dict *db)
692 {
693 struct record_data *hir;
694 time_t posted;
695 long flags;
696 unsigned long duration;
697 char *str, *from, *message;
698 dict_iterator_t it;
699
700 for(it=dict_first(db); it; it=iter_next(it))
701 {
702 hir = iter_data(it);
703 if(hir->type != RECDB_OBJECT)
704 {
705 log_module(G_LOG, LOG_WARNING, "Unexpected rectype %d for %s.", hir->type, iter_key(it));
706 continue;
707 }
708
709 str = database_get_data(hir->d.object, KEY_FLAGS, RECDB_QSTRING);
710 flags = str ? strtoul(str, NULL, 0) : 0;
711
712 str = database_get_data(hir->d.object, KEY_POSTED, RECDB_QSTRING);
713 posted = str ? strtoul(str, NULL, 0) : 0;
714
715 str = database_get_data(hir->d.object, KEY_DURATION, RECDB_QSTRING);
716 duration = str ? strtoul(str, NULL, 0) : 0;
717
718 from = database_get_data(hir->d.object, KEY_FROM, RECDB_QSTRING);
719 message = database_get_data(hir->d.object, KEY_MESSAGE, RECDB_QSTRING);
720
721 message_add(flags, posted, duration, from, message);
722 }
723 return 0;
724 }
725
726 static int
727 global_saxdb_write(struct saxdb_context *ctx)
728 {
729 struct globalMessage *message;
730 char str[16];
731
732 for(message = messageList; message; message = message->next) {
733 snprintf(str, sizeof(str), "%li", message->id);
734 saxdb_start_record(ctx, str, 0);
735 saxdb_write_int(ctx, KEY_FLAGS, message->flags);
736 saxdb_write_int(ctx, KEY_POSTED, message->posted);
737 saxdb_write_int(ctx, KEY_DURATION, message->duration);
738 saxdb_write_string(ctx, KEY_FROM, message->from);
739 saxdb_write_string(ctx, KEY_MESSAGE, message->message);
740 saxdb_end_record(ctx);
741 }
742 return 0;
743 }
744
745 static void
746 global_db_cleanup(void)
747 {
748 while(messageList)
749 message_del(messageList);
750 }
751
752 void
753 init_global(const char *nick)
754 {
755 struct chanNode *chan;
756 unsigned int i;
757 G_LOG = log_register_type("Global", "file:global.log");
758 reg_new_user_func(global_process_user);
759 reg_auth_func(global_process_auth);
760 reg_oper_func(global_process_oper);
761
762 conf_register_reload(global_conf_read);
763
764 global_module = module_register("Global", G_LOG, "global.help", NULL);
765 modcmd_register(global_module, "LIST", cmd_list, 1, 0, "flags", "+oper", NULL);
766 modcmd_register(global_module, "MESSAGE", cmd_message, 3, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
767 modcmd_register(global_module, "MESSAGES", cmd_messages, 1, 0, NULL);
768 modcmd_register(global_module, "NOTICE", cmd_notice, 3, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
769 modcmd_register(global_module, "REMOVE", cmd_remove, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
770
771 if(nick)
772 {
773 const char *modes = conf_get_data("services/global/modes", RECDB_QSTRING);
774 global = AddService(nick, modes ? modes : NULL, "Global Services", NULL);
775 global_service = service_register(global);
776 }
777
778 if(autojoin_channels && global) {
779 for (i = 0; i < autojoin_channels->used; i++) {
780 chan = AddChannel(autojoin_channels->list[i], now, "+nt", NULL, NULL);
781 AddChannelUser(global, chan)->modes |= MODE_CHANOP;
782 }
783 }
784
785 saxdb_register("Global", global_saxdb_read, global_saxdb_write);
786 reg_exit_func(global_db_cleanup);
787 message_register_table(msgtab);
788 }