]> jfr.im git - irc/evilnet/x3.git/blob - src/global.c
Couple of srvx updates.
[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 3 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 /* chanserv.c */
71 { "CSMSG_REGISTERED_TO", "%s registered to %s by %s." },
72 { "CSMSG_CHANNEL_MOVED", "%s moved to %s by %s." },
73 { "CSMSG_SUSPENSION_MODIFIED", "%s suspension modified by %s." },
74 { "CSMSG_SUSPENDED_BY", "%s suspended by %s." },
75 { "CSMSG_UNSUSPENDED_BY", "%s unsuspended by %s." },
76 { "CSMSG_OWNERSHIP_TRANSFERRED", "%s ownership transferred to %s by %s." },
77
78 /* mod-helpserv.c */
79 { "HSMSG_BOT_RENAMED", "HelpServ bot %s (in %s) renamed to %s by %s." },
80 { "HSMSG_BOT_MOVED", "HelpServ %s (%s) moved to %s by %s." },
81 { "HSMSG_BOT_REGISTERED", "HelpServ %s (%s) registered to %s by %s." },
82 { "HSMSG_BOT_EXPIRED", "HelpServ %s (%s) expired at request of %s." },
83 { "HSMSG_BOT_UNREGISTERED", "HelpServ %s (%s) unregistered by %s." },
84 { "HSMSG_SUSPENDED_BY", "%s suspended by %s. (HelpServ)" },
85 { "HSMSG_UNSUSPENDED_BY", "%s unsuspended by %s. (HelpServ)" },
86
87 /* nickserv.c */
88 { "NSMSG_ACCOUNT_RENAMED", "%s renamed account %s to %s." },
89 { "NSMSG_ACCOUNT_MERGED", "%s (%s) merged account %s into %s." },
90
91 /* opserv.c */
92 { "DEFCON_NETWORK_CHANGED", "Network DefCon level has changed to level %d" },
93 { "DEFCON_OPER_LEVEL_CHANGE", "%s is changing the DefCon level to %d" },
94 { "DEFCON_TIMEOUT_LEVEL_CHANGE", "The DefCon has changed back to level %d (timeout)" },
95 { "OSMSG_CHANNEL_ACTIVITY_WARN", "Channel activity warning for channel %s: %s" },
96
97 /* spamserv.c */
98 { "SSMSG_CHANNEL_MERGED", "$X (channel %s) merged into %s by %s." },
99 { "SSMSG_CHANNEL_MOVED", "$X (channel %s) moved into %s by %s." },
100 { "SSMSG_UNREG_MANUAL", "$X (channel %s) %s by %s." },
101 { "SSMSG_REG_EXPIRED", "$X (channel %s) registration expired." },
102 { "SSMSG_LOST_ALL_USERS", "$X (channel %s) lost all users." },
103 { "SSMSG_REGISTERED_BY", "$X (channel %s) registered by %s." },
104 { "SSMSG_UNREGISTERED_BY", "$X (channel %s) unregistered by %s." },
105
106 { NULL, NULL }
107 };
108
109 #define GLOBAL_SYNTAX() svccmd_send_help_brief(user, global, cmd)
110 #define GLOBAL_FUNC(NAME) MODCMD_FUNC(NAME)
111
112 struct globalMessage
113 {
114 unsigned long id;
115 long flags;
116
117 time_t posted;
118 char posted_s[24];
119 unsigned long duration;
120
121 char *from;
122 char *message;
123
124 struct globalMessage *prev;
125 struct globalMessage *next;
126 };
127
128 struct userNode *global;
129
130 static struct module *global_module;
131 static struct service *global_service;
132 static struct globalMessage *messageList;
133 extern struct string_list *autojoin_channels;
134 static long messageCount;
135 static time_t last_max_alert;
136 static struct log_type *G_LOG;
137
138 static struct
139 {
140 unsigned long db_backup_frequency;
141 unsigned int announcements_default : 1;
142 } global_conf;
143
144 #if defined(GCC_VARMACROS)
145 # define global_notice(target, ARGS...) send_message(target, global, ARGS)
146 #elif defined(C99_VARMACROS)
147 # define global_notice(target, ...) send_message(target, global, __VA_ARGS__)
148 #endif
149
150 void message_expire(void *data);
151
152 static struct globalMessage*
153 message_add(long flags, time_t posted, unsigned long duration, char *from, const char *msg)
154 {
155 struct globalMessage *message;
156 struct tm tm;
157
158 message = malloc(sizeof(struct globalMessage));
159 if(!message)
160 {
161 return NULL;
162 }
163
164 message->id = messageCount++;
165 message->flags = flags;
166 message->posted = posted;
167 message->duration = duration;
168 message->from = strdup(from);
169 message->message = strdup(msg);
170
171 if ((flags & MESSAGE_OPTION_IMMEDIATE) == 0) {
172 localtime_r(&message->posted, &tm);
173 strftime(message->posted_s, sizeof(message->posted_s),
174 "%I:%M %p, %m/%d/%Y", &tm);
175 }
176
177 if(messageList)
178 {
179 messageList->prev = message;
180 }
181 message->prev = NULL;
182 message->next = messageList;
183
184 messageList = message;
185
186 if(duration)
187 {
188 timeq_add(now + duration, message_expire, message);
189 }
190
191 return message;
192 }
193
194 static void
195 message_del(struct globalMessage *message)
196 {
197 if(message->duration)
198 {
199 timeq_del(0, NULL, message, TIMEQ_IGNORE_FUNC | TIMEQ_IGNORE_WHEN);
200 }
201
202 if(message->prev) message->prev->next = message->next;
203 else messageList = message->next;
204
205 if(message->next) message->next->prev = message->prev;
206
207 free(message->from);
208 free(message->message);
209 free(message);
210 }
211
212 void message_expire(void *data)
213 {
214 struct globalMessage *message = data;
215
216 message->duration = 0;
217 message_del(message);
218 }
219
220 static struct globalMessage*
221 message_create(struct userNode *user, unsigned int argc, char *argv[])
222 {
223 unsigned long duration = 0;
224 char *text = NULL;
225 char *sender;
226 long flags = 0;
227 unsigned int i;
228
229 sender = user->handle_info->handle;
230
231 for(i = 0; i < argc; i++)
232 {
233 if((i + 1) > argc)
234 {
235 global_notice(user, "MSG_MISSING_PARAMS", argv[argc]);
236 return NULL;
237 }
238
239 if(!irccasecmp(argv[i], "text"))
240 {
241 i++;
242 text = unsplit_string(argv + i, argc - i, NULL);
243 break;
244 } else if (!irccasecmp(argv[i], "sourceless")) {
245 i++;
246 flags |= MESSAGE_OPTION_SOURCELESS;
247 } else if (!irccasecmp(argv[i], "target")) {
248 i++;
249
250 if(!irccasecmp(argv[i], "all")) {
251 flags |= MESSAGE_RECIPIENT_ALL;
252 } else if(!irccasecmp(argv[i], "authed")) {
253 flags |= MESSAGE_RECIPIENT_AUTHED;
254 } else if(!irccasecmp(argv[i], "users")) {
255 flags |= MESSAGE_RECIPIENT_LUSERS;
256 } else if(!irccasecmp(argv[i], "helpers")) {
257 flags |= MESSAGE_RECIPIENT_HELPERS;
258 } else if(!irccasecmp(argv[i], "opers")) {
259 flags |= MESSAGE_RECIPIENT_OPERS;
260 } else if(!irccasecmp(argv[i], "staff") || !irccasecmp(argv[i], "privileged")) {
261 flags |= MESSAGE_RECIPIENT_STAFF;
262 } else if(!irccasecmp(argv[i], "channels")) {
263 flags |= MESSAGE_RECIPIENT_CHANNELS;
264 } else if(!irccasecmp(argv[i], "rchannels")) {
265 flags |= MESSAGE_RECIPIENT_RCHANNELS;
266 } else if(!irccasecmp(argv[i], "announcement") || !irccasecmp(argv[i], "announce")) {
267 flags |= MESSAGE_RECIPIENT_ANNOUNCE;
268 } else {
269 global_notice(user, "GMSG_INVALID_TARGET", argv[i]);
270 return NULL;
271 }
272 } else if (irccasecmp(argv[i], "duration") == 0) {
273 duration = ParseInterval(argv[++i]);
274 } else if (irccasecmp(argv[i], "from") == 0) {
275 sender = argv[++i];
276 } else {
277 global_notice(user, "MSG_INVALID_CRITERIA", argv[i]);
278 return NULL;
279 }
280 }
281
282 if(!flags)
283 {
284 flags = MESSAGE_RECIPIENT_LUSERS;
285 }
286
287 if(!text) {
288 global_notice(user, "GMSG_MESSAGE_REQUIRED");
289 return NULL;
290 }
291
292 return message_add(flags, now, duration, sender, text);
293 }
294
295 static const char *
296 messageType(const struct globalMessage *message)
297 {
298 if((message->flags & MESSAGE_RECIPIENT_ALL) == MESSAGE_RECIPIENT_ALL)
299 {
300 return "all";
301 }
302 if((message->flags & MESSAGE_RECIPIENT_STAFF) == MESSAGE_RECIPIENT_STAFF)
303 {
304 return "staff";
305 }
306 else if(message->flags & MESSAGE_RECIPIENT_ANNOUNCE)
307 {
308 return "announcement";
309 }
310 else if(message->flags & MESSAGE_RECIPIENT_OPERS)
311 {
312 return "opers";
313 }
314 else if(message->flags & MESSAGE_RECIPIENT_HELPERS)
315 {
316 return "helpers";
317 }
318 else if(message->flags & MESSAGE_RECIPIENT_LUSERS)
319 {
320 return "users";
321 }
322 else if(message->flags & MESSAGE_RECIPIENT_AUTHED)
323 {
324 return "authed";
325 }
326 else if(message->flags & MESSAGE_RECIPIENT_RCHANNELS)
327 {
328 return "rchannels";
329 }
330 else
331 {
332 return "channels";
333 }
334 }
335
336 static void
337 notice_target(const char *target, struct globalMessage *message)
338 {
339 if(!(message->flags & MESSAGE_OPTION_SOURCELESS))
340 {
341 if(message->flags & MESSAGE_OPTION_IMMEDIATE)
342 {
343 send_target_message(0, target, global, "GMSG_NOTICE_SOURCE", messageType(message), message->from);
344 }
345 else
346 {
347 send_target_message(0, target, global, "GMSG_MESSAGE_SOURCE", messageType(message), message->from, message->posted_s);
348 }
349 }
350
351 send_target_message(4, target, global, "%s", message->message);
352 }
353
354 static void
355 message_send(struct globalMessage *message)
356 {
357 struct userNode *user;
358 unsigned long n;
359 dict_iterator_t it;
360
361 if(message->flags & MESSAGE_RECIPIENT_CHANNELS)
362 {
363 dict_iterator_t it;
364
365 for (it = dict_first(channels); it; it = iter_next(it)) {
366 struct chanNode *chan = iter_data(it);
367
368 notice_target(chan->name, message);
369 }
370 }
371
372 if(message->flags & MESSAGE_RECIPIENT_RCHANNELS)
373 {
374 dict_iterator_t it;
375
376 for (it = dict_first(channels); it; it = iter_next(it)) {
377 struct chanNode *chan = iter_data(it);
378
379 if (chan->channel_info)
380 notice_target(chan->name, message);
381 }
382 }
383
384 if(message->flags & MESSAGE_RECIPIENT_LUSERS)
385 {
386 notice_target("$*", message);
387 return;
388 }
389
390 if(message->flags & MESSAGE_RECIPIENT_ANNOUNCE)
391 {
392 char announce;
393
394 for (it = dict_first(clients); it; it = iter_next(it)) {
395 user = iter_data(it);
396 if (user->uplink == self) continue;
397 announce = user->handle_info ? user->handle_info->announcements : '?';
398 if (announce == 'n') continue;
399 if ((announce == '?') && !global_conf.announcements_default) continue;
400 notice_target(user->nick, message);
401 }
402 }
403
404 if(message->flags & MESSAGE_RECIPIENT_OPERS)
405 {
406 for(n = 0; n < curr_opers.used; n++)
407 {
408 user = curr_opers.list[n];
409
410 if(user->uplink != self)
411 {
412 notice_target(user->nick, message);
413 }
414 }
415 }
416
417 if(message->flags & MESSAGE_RECIPIENT_HELPERS)
418 {
419 for(n = 0; n < curr_helpers.used; n++)
420 {
421 user = curr_helpers.list[n];
422 if (IsOper(user))
423 continue;
424 notice_target(user->nick, message);
425 }
426 }
427
428 if(message->flags & MESSAGE_RECIPIENT_AUTHED)
429 {
430 dict_iterator_t it;
431 for (it = dict_first(clients); it; it = iter_next(it)) {
432 struct userNode *luser = iter_data(it);
433 if (luser->handle_info)
434 notice_target(luser->nick, message);
435 }
436 }
437 }
438
439 void
440 global_message_args(long targets, const char *language_entry, ...)
441 {
442 struct globalMessage *message = NULL;
443 va_list arg_list;
444 dict_iterator_t it;
445 char response[MAXLEN];
446 const char *fmt;
447
448 if(!targets || !global)
449 return;
450
451 fmt = strdup(language_entry);
452
453 /* Notice users/opers/helpers */
454 for (it = dict_first(clients); it; it = iter_next(it)) {
455 struct userNode *luser = iter_data(it);
456
457 language_entry = user_find_message(luser, fmt);
458
459 va_start(arg_list, language_entry);
460 vsnprintf(response, MAXLEN-2, language_entry, arg_list);
461 response[MAXLEN-1] = 0;
462
463 if (message)
464 message_del(message);
465
466 message = message_add(targets | MESSAGE_OPTION_SOURCELESS, now, 0, "", response);
467 if (!message)
468 continue;
469
470 /* opers */
471 if(message->flags & MESSAGE_RECIPIENT_OPERS && IsOper(luser)) {
472 if(luser->uplink != self)
473 notice_target(luser->nick, message);
474
475 if ((message->flags & MESSAGE_RECIPIENT_LUSERS) || (message->flags & MESSAGE_RECIPIENT_HELPERS) ||
476 (message->flags & MESSAGE_RECIPIENT_AUTHED))
477 continue;
478 }
479
480 /* helpers */
481 if (message->flags & MESSAGE_RECIPIENT_HELPERS && IsHelper(luser)) {
482 notice_target(luser->nick, message);
483
484 if ((message->flags & MESSAGE_RECIPIENT_LUSERS) || (message->flags & MESSAGE_RECIPIENT_AUTHED))
485 continue;
486 }
487
488 /* authed */
489 if ((message->flags & MESSAGE_RECIPIENT_AUTHED) && luser->handle_info) {
490 notice_target(luser->nick, message);
491
492 if (message->flags & MESSAGE_RECIPIENT_LUSERS)
493 continue;
494 }
495
496 /* users */
497 if (message->flags & MESSAGE_RECIPIENT_LUSERS) {
498 notice_target(luser->nick, message);
499
500 }
501 }
502
503 message_del(message);
504 }
505
506 void
507 global_message(long targets, char *text)
508 {
509 struct globalMessage *message;
510
511 if(!targets || !global)
512 return;
513
514 message = message_add(targets | MESSAGE_OPTION_SOURCELESS, now, 0, "", text);
515 if(!message)
516 return;
517
518 message_send(message);
519 message_del(message);
520 }
521
522 static GLOBAL_FUNC(cmd_notice)
523 {
524 struct globalMessage *message = NULL;
525 const char *recipient = NULL, *text;
526 char *sender;
527 long target = 0;
528
529 assert(argc >= 3);
530 sender = user->handle_info->handle;
531 if(!irccasecmp(argv[1], "all")) {
532 target = MESSAGE_RECIPIENT_ALL;
533 } else if(!irccasecmp(argv[1], "users")) {
534 target = MESSAGE_RECIPIENT_LUSERS;
535 } else if(!irccasecmp(argv[1], "authed")) {
536 target = MESSAGE_RECIPIENT_AUTHED;
537 } else if(!irccasecmp(argv[1], "helpers")) {
538 target = MESSAGE_RECIPIENT_HELPERS;
539 } else if(!irccasecmp(argv[1], "opers")) {
540 target = MESSAGE_RECIPIENT_OPERS;
541 } else if(!irccasecmp(argv[1], "staff") || !irccasecmp(argv[1], "privileged")) {
542 target |= MESSAGE_RECIPIENT_HELPERS | MESSAGE_RECIPIENT_OPERS;
543 } else if(!irccasecmp(argv[1], "announcement") || !irccasecmp(argv[1], "announce")) {
544 target |= MESSAGE_RECIPIENT_ANNOUNCE;
545 } else if(!irccasecmp(argv[1], "channels")) {
546 target = MESSAGE_RECIPIENT_CHANNELS;
547 } else if(!irccasecmp(argv[1], "rchannels")) {
548 target = MESSAGE_RECIPIENT_RCHANNELS;
549 } else {
550 global_notice(user, "GMSG_INVALID_TARGET", argv[1]);
551 return 0;
552 }
553 if(!irccasecmp(argv[2], "from")) {
554 if (argc < 5) {
555 reply("MSG_MISSING_PARAMS", argv[0]);
556 GLOBAL_SYNTAX();
557 return 0;
558 }
559 sender = argv[3];
560 text = unsplit_string(argv + 4, argc - 4, NULL);
561 } else {
562 text = unsplit_string(argv + 2, argc - 2, NULL);
563 }
564
565 message = message_add(target | MESSAGE_OPTION_IMMEDIATE, now, 0, sender, text);
566 if(!message)
567 return 0;
568
569 recipient = messageType(message);
570 message_send(message);
571 message_del(message);
572
573 global_notice(user, "GMSG_MESSAGE_SENT", recipient);
574 return 1;
575 }
576
577 static GLOBAL_FUNC(cmd_message)
578 {
579 struct globalMessage *message = NULL;
580 const char *recipient = NULL;
581
582 assert(argc >= 3);
583 message = message_create(user, argc - 1, argv + 1);
584 if(!message)
585 return 0;
586 recipient = messageType(message);
587 global_notice(user, "GMSG_MESSAGE_ADDED", recipient, message->id);
588 return 1;
589 }
590
591 static GLOBAL_FUNC(cmd_list)
592 {
593 struct globalMessage *message;
594 struct helpfile_table table;
595 unsigned int length, nn;
596
597 if(!messageList)
598 {
599 global_notice(user, "GMSG_NO_MESSAGES");
600 return 1;
601 }
602
603 for(nn=0, message = messageList; message; nn++, message=message->next) ;
604 table.length = nn+1;
605 table.width = 5;
606 table.flags = TABLE_NO_FREE;
607 table.contents = calloc(table.length, sizeof(char**));
608 table.contents[0] = calloc(table.width, sizeof(char*));
609 table.contents[0][0] = "ID";
610 table.contents[0][1] = "Target";
611 table.contents[0][2] = "Expires";
612 table.contents[0][3] = "From";
613 table.contents[0][4] = "Message";
614
615 for(nn=1, message = messageList; message; nn++, message = message->next)
616 {
617 char buffer[64];
618
619 table.contents[nn] = calloc(table.width, sizeof(char*));
620 snprintf(buffer, sizeof(buffer), "%lu", message->id);
621 table.contents[nn][0] = strdup(buffer);
622 table.contents[nn][1] = messageType(message);
623 if(message->duration)
624 intervalString(buffer, message->posted + message->duration - now, user->handle_info);
625 else
626 strcpy(buffer, "Never.");
627 table.contents[nn][2] = strdup(buffer);
628 table.contents[nn][3] = message->from;
629 length = strlen(message->message);
630 safestrncpy(buffer, message->message, sizeof(buffer));
631 if(length > (sizeof(buffer) - 4))
632 {
633 buffer[sizeof(buffer) - 1] = 0;
634 buffer[sizeof(buffer) - 2] = buffer[sizeof(buffer) - 3] = buffer[sizeof(buffer) - 4] = '.';
635 }
636 table.contents[nn][4] = strdup(buffer);
637 }
638 table_send(global, user->nick, 0, NULL, table);
639 for (nn=1; nn<table.length; nn++)
640 {
641 free((char*)table.contents[nn][0]);
642 free((char*)table.contents[nn][2]);
643 free((char*)table.contents[nn][4]);
644 free(table.contents[nn]);
645 }
646 free(table.contents[0]);
647 free(table.contents);
648
649 return 1;
650 }
651
652 static GLOBAL_FUNC(cmd_remove)
653 {
654 struct globalMessage *message = NULL;
655 unsigned long id;
656
657 assert(argc >= 2);
658 id = strtoul(argv[1], NULL, 0);
659
660 for(message = messageList; message; message = message->next)
661 {
662 if(message->id == id)
663 {
664 message_del(message);
665 global_notice(user, "GMSG_MESSAGE_DELETED", argv[1]);
666 return 1;
667 }
668 }
669
670 global_notice(user, "GMSG_ID_INVALID", argv[1]);
671 return 0;
672 }
673
674 static unsigned int
675 send_messages(struct userNode *user, long mask, int obstreperize)
676 {
677 struct globalMessage *message = messageList;
678 unsigned int count = 0;
679
680 while(message)
681 {
682 if(message->flags & mask)
683 {
684 if (obstreperize && !count)
685 {
686 send_target_message(0, user->nick, global, "GMSG_MOTD_HEADER");
687 send_target_message(0, user->nick, global, "GMSG_MOTD_BAR");
688 }
689 notice_target(user->nick, message);
690 count++;
691 }
692
693 message = message->next;
694 }
695 if (obstreperize && count)
696 send_target_message(0, user->nick, global, "GMSG_MOTD_FOOTER");
697 return count;
698 }
699
700 static GLOBAL_FUNC(cmd_messages)
701 {
702 long mask = MESSAGE_RECIPIENT_AUTHED | MESSAGE_RECIPIENT_LUSERS | MESSAGE_RECIPIENT_CHANNELS | MESSAGE_RECIPIENT_RCHANNELS;
703 unsigned int count;
704
705 if(IsOper(user))
706 mask |= MESSAGE_RECIPIENT_OPERS;
707
708 if(IsHelper(user))
709 mask |= MESSAGE_RECIPIENT_HELPERS;
710
711 count = send_messages(user, mask, 0);
712 if(count)
713 global_notice(user, "GMSG_MESSAGE_COUNT", count);
714 else
715 global_notice(user, "GMSG_NO_MESSAGES");
716
717 return 1;
718 }
719
720 static int
721 global_process_user(struct userNode *user, UNUSED_ARG(void *extra))
722 {
723 if(IsLocal(user) || self->uplink->burst || user->uplink->burst)
724 return 0;
725 send_messages(user, MESSAGE_RECIPIENT_LUSERS, 1);
726
727 /* only alert on new usercount if the record was broken in the last
728 * 30 seconds, and no alert had been sent in that time.
729 */
730 if((now - max_clients_time) <= 30 && (now - last_max_alert) > 30)
731 {
732 char *message;
733 message = alloca(36);
734 sprintf(message, "New user count record: %d", max_clients);
735 global_message(MESSAGE_RECIPIENT_OPERS, message);
736 last_max_alert = now;
737 }
738
739 return 0;
740 }
741
742 static void
743 global_process_auth(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle), UNUSED_ARG(void *extra))
744 {
745 if(IsHelper(user))
746 send_messages(user, MESSAGE_RECIPIENT_HELPERS, 0);
747 }
748
749 static void
750 global_process_oper(struct userNode *user, UNUSED_ARG(void *extra))
751 {
752 if(user->uplink->burst)
753 return;
754 send_messages(user, MESSAGE_RECIPIENT_OPERS, 0);
755 }
756
757 static void
758 global_conf_read(void)
759 {
760 dict_t conf_node;
761 const char *str;
762
763 if (!(conf_node = conf_get_data(GLOBAL_CONF_NAME, RECDB_OBJECT))) {
764 log_module(G_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", GLOBAL_CONF_NAME);
765 return;
766 }
767
768 str = database_get_data(conf_node, KEY_DB_BACKUP_FREQ, RECDB_QSTRING);
769 global_conf.db_backup_frequency = str ? ParseInterval(str) : 7200;
770 str = database_get_data(conf_node, KEY_ANNOUNCEMENTS_DEFAULT, RECDB_QSTRING);
771 global_conf.announcements_default = str ? enabled_string(str) : 1;
772
773 str = database_get_data(conf_node, KEY_NICK, RECDB_QSTRING);
774 if(global && str)
775 NickChange(global, str, 0);
776 }
777
778 static int
779 global_saxdb_read(struct dict *db)
780 {
781 struct record_data *hir;
782 time_t posted;
783 long flags;
784 unsigned long duration;
785 char *str, *from, *message;
786 dict_iterator_t it;
787
788 for(it=dict_first(db); it; it=iter_next(it))
789 {
790 hir = iter_data(it);
791 if(hir->type != RECDB_OBJECT)
792 {
793 log_module(G_LOG, LOG_WARNING, "Unexpected rectype %d for %s.", hir->type, iter_key(it));
794 continue;
795 }
796
797 str = database_get_data(hir->d.object, KEY_FLAGS, RECDB_QSTRING);
798 flags = str ? strtoul(str, NULL, 0) : 0;
799
800 str = database_get_data(hir->d.object, KEY_POSTED, RECDB_QSTRING);
801 posted = str ? strtoul(str, NULL, 0) : 0;
802
803 str = database_get_data(hir->d.object, KEY_DURATION, RECDB_QSTRING);
804 duration = str ? strtoul(str, NULL, 0) : 0;
805
806 from = database_get_data(hir->d.object, KEY_FROM, RECDB_QSTRING);
807 message = database_get_data(hir->d.object, KEY_MESSAGE, RECDB_QSTRING);
808
809 message_add(flags, posted, duration, from, message);
810 }
811 return 0;
812 }
813
814 static int
815 global_saxdb_write(struct saxdb_context *ctx)
816 {
817 struct globalMessage *message;
818 char str[16];
819
820 for(message = messageList; message; message = message->next) {
821 snprintf(str, sizeof(str), "%li", message->id);
822 saxdb_start_record(ctx, str, 0);
823 saxdb_write_int(ctx, KEY_FLAGS, message->flags);
824 saxdb_write_int(ctx, KEY_POSTED, message->posted);
825 saxdb_write_int(ctx, KEY_DURATION, message->duration);
826 saxdb_write_string(ctx, KEY_FROM, message->from);
827 saxdb_write_string(ctx, KEY_MESSAGE, message->message);
828 saxdb_end_record(ctx);
829 }
830 return 0;
831 }
832
833 static void
834 global_db_cleanup(UNUSED_ARG(void *extra))
835 {
836 while(messageList)
837 message_del(messageList);
838 }
839
840 void
841 init_global(const char *nick)
842 {
843 struct chanNode *chan;
844 unsigned int i;
845 G_LOG = log_register_type("Global", "file:global.log");
846 reg_new_user_func(global_process_user, NULL);
847 reg_auth_func(global_process_auth, NULL);
848 reg_oper_func(global_process_oper, NULL);
849
850 conf_register_reload(global_conf_read);
851
852 global_module = module_register("Global", G_LOG, "global.help", NULL);
853 modcmd_register(global_module, "LIST", cmd_list, 1, 0, "flags", "+oper", NULL);
854 modcmd_register(global_module, "MESSAGE", cmd_message, 3, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
855 modcmd_register(global_module, "MESSAGES", cmd_messages, 1, 0, NULL);
856 modcmd_register(global_module, "NOTICE", cmd_notice, 3, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
857 modcmd_register(global_module, "REMOVE", cmd_remove, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
858
859 if(nick)
860 {
861 const char *modes = conf_get_data("services/global/modes", RECDB_QSTRING);
862 global = AddLocalUser(nick, nick, NULL, "Global Services", modes);
863 global_service = service_register(global);
864 }
865
866 if(autojoin_channels && global) {
867 for (i = 0; i < autojoin_channels->used; i++) {
868 chan = AddChannel(autojoin_channels->list[i], now, "+nt", NULL, NULL);
869 AddChannelUser(global, chan)->modes |= MODE_CHANOP;
870 }
871 }
872
873 saxdb_register("Global", global_saxdb_read, global_saxdb_write);
874 reg_exit_func(global_db_cleanup, NULL);
875 message_register_table(msgtab);
876 }