1 /* global.c - Global notice service
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of x3.
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.
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.
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.
29 #define GLOBAL_CONF_NAME "services/global"
31 #define GLOBAL_DB "global.db"
32 #define GLOBAL_TEMP_DB "global.db.new"
35 #define KEY_DB_BACKUP_FREQ "db_backup_freq"
36 #define KEY_ANNOUNCEMENTS_DEFAULT "announcements_default"
37 #define KEY_NICK "nick"
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"
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
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--------------" },
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
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 */
77 #define GLOBAL_SYNTAX() svccmd_send_help_brief(user, global, cmd)
78 #define GLOBAL_FUNC(NAME) MODCMD_FUNC(NAME)
87 unsigned long duration
;
92 struct globalMessage
*prev
;
93 struct globalMessage
*next
;
96 struct userNode
*global
;
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
;
108 unsigned long db_backup_frequency
;
109 unsigned int announcements_default
: 1;
112 #define global_notice(target, format...) send_message(target , global , ## format)
114 void message_expire(void *data
);
116 static struct globalMessage
*
117 message_add(long flags
, time_t posted
, unsigned long duration
, char *from
, const char *msg
)
119 struct globalMessage
*message
;
122 message
= malloc(sizeof(struct globalMessage
));
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
);
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
);
143 messageList
->prev
= message
;
145 message
->prev
= NULL
;
146 message
->next
= messageList
;
148 messageList
= message
;
152 timeq_add(now
+ duration
, message_expire
, message
);
159 message_del(struct globalMessage
*message
)
161 if(message
->duration
)
163 timeq_del(0, NULL
, message
, TIMEQ_IGNORE_FUNC
| TIMEQ_IGNORE_WHEN
);
166 if(message
->prev
) message
->prev
->next
= message
->next
;
167 else messageList
= message
->next
;
169 if(message
->next
) message
->next
->prev
= message
->prev
;
172 free(message
->message
);
176 void message_expire(void *data
)
178 struct globalMessage
*message
= data
;
180 message
->duration
= 0;
181 message_del(message
);
184 static struct globalMessage
*
185 message_create(struct userNode
*user
, unsigned int argc
, char *argv
[])
187 unsigned long duration
= 0;
193 sender
= user
->handle_info
->handle
;
195 for(i
= 0; i
< argc
; i
++)
199 global_notice(user
, "MSG_MISSING_PARAMS", argv
[argc
]);
203 if(!irccasecmp(argv
[i
], "text"))
206 text
= unsplit_string(argv
+ i
, argc
- i
, NULL
);
208 } else if (!irccasecmp(argv
[i
], "sourceless")) {
210 flags
|= MESSAGE_OPTION_SOURCELESS
;
211 } else if (!irccasecmp(argv
[i
], "target")) {
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
;
229 global_notice(user
, "GMSG_INVALID_TARGET", argv
[i
]);
232 } else if (irccasecmp(argv
[i
], "duration") == 0) {
233 duration
= ParseInterval(argv
[++i
]);
234 } else if (irccasecmp(argv
[i
], "from") == 0) {
237 global_notice(user
, "MSG_INVALID_CRITERIA", argv
[i
]);
244 flags
= MESSAGE_RECIPIENT_LUSERS
;
248 global_notice(user
, "GMSG_MESSAGE_REQUIRED");
252 return message_add(flags
, now
, duration
, sender
, text
);
256 messageType(const struct globalMessage
*message
)
258 if((message
->flags
& MESSAGE_RECIPIENT_ALL
) == MESSAGE_RECIPIENT_ALL
)
262 if((message
->flags
& MESSAGE_RECIPIENT_STAFF
) == MESSAGE_RECIPIENT_STAFF
)
266 else if(message
->flags
& MESSAGE_RECIPIENT_ANNOUNCE
)
268 return "announcement";
270 else if(message
->flags
& MESSAGE_RECIPIENT_OPERS
)
274 else if(message
->flags
& MESSAGE_RECIPIENT_HELPERS
)
278 else if(message
->flags
& MESSAGE_RECIPIENT_LUSERS
)
289 notice_target(const char *target
, struct globalMessage
*message
)
291 if(!(message
->flags
& MESSAGE_OPTION_SOURCELESS
))
293 if(message
->flags
& MESSAGE_OPTION_IMMEDIATE
)
295 send_target_message(0, target
, global
, "GMSG_NOTICE_SOURCE", messageType(message
), message
->from
);
299 send_target_message(0, target
, global
, "GMSG_MESSAGE_SOURCE", messageType(message
), message
->from
, message
->posted_s
);
303 send_target_message(4, target
, global
, "%s", message
->message
);
307 notice_channel(const char *key
, void *data
, void *extra
)
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
);
317 message_send(struct globalMessage
*message
)
319 struct userNode
*user
;
323 if(message
->flags
& MESSAGE_RECIPIENT_CHANNELS
)
325 dict_foreach(channels
, notice_channel
, message
);
328 if(message
->flags
& MESSAGE_RECIPIENT_LUSERS
)
330 notice_target("$*", message
);
334 if(message
->flags
& MESSAGE_RECIPIENT_ANNOUNCE
)
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
);
348 if(message
->flags
& MESSAGE_RECIPIENT_OPERS
)
350 for(n
= 0; n
< curr_opers
.used
; n
++)
352 user
= curr_opers
.list
[n
];
354 if(user
->uplink
!= self
)
356 notice_target(user
->nick
, message
);
361 if(message
->flags
& MESSAGE_RECIPIENT_HELPERS
)
363 for(n
= 0; n
< curr_helpers
.used
; n
++)
365 user
= curr_helpers
.list
[n
];
368 notice_target(user
->nick
, message
);
374 global_message_args(long targets
, const char *language_entry
, ...)
376 struct globalMessage
*message
;
379 char response
[MAXLEN
];
382 if(!targets
|| !global
)
385 fmt
= strdup(language_entry
);
387 /* Notice users/opers/helpers */
388 for (it
= dict_first(clients
); it
; it
= iter_next(it
)) {
389 struct userNode
*luser
= iter_data(it
);
391 language_entry
= user_find_message(luser
, fmt
);
393 va_start(arg_list
, language_entry
);
394 vsnprintf(response
, MAXLEN
-2, language_entry
, arg_list
);
395 response
[MAXLEN
-1] = 0;
397 message
= message_add(targets
| MESSAGE_OPTION_SOURCELESS
, now
, 0, "", response
);
402 if(message
->flags
& MESSAGE_RECIPIENT_OPERS
&& IsOper(luser
)) {
403 if(luser
->uplink
!= self
)
404 notice_target(luser
->nick
, message
);
408 if (message
->flags
& MESSAGE_RECIPIENT_HELPERS
&& IsHelper(luser
)) {
411 notice_target(luser
->nick
, message
);
415 if (message
->flags
& MESSAGE_RECIPIENT_LUSERS
)
416 notice_target(luser
->nick
, message
);
419 message_del(message
);
423 global_message(long targets
, char *text
)
425 struct globalMessage
*message
;
427 if(!targets
|| !global
)
430 message
= message_add(targets
| MESSAGE_OPTION_SOURCELESS
, now
, 0, "", text
);
434 message_send(message
);
435 message_del(message
);
438 static GLOBAL_FUNC(cmd_notice
)
440 struct globalMessage
*message
= NULL
;
441 const char *recipient
= NULL
, *text
;
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
;
462 global_notice(user
, "GMSG_INVALID_TARGET", argv
[1]);
465 if(!irccasecmp(argv
[2], "from")) {
467 reply("MSG_MISSING_PARAMS", argv
[0]);
472 text
= unsplit_string(argv
+ 4, argc
- 4, NULL
);
474 text
= unsplit_string(argv
+ 2, argc
- 2, NULL
);
477 message
= message_add(target
| MESSAGE_OPTION_IMMEDIATE
, now
, 0, sender
, text
);
481 recipient
= messageType(message
);
482 message_send(message
);
483 message_del(message
);
485 global_notice(user
, "GMSG_MESSAGE_SENT", recipient
);
489 static GLOBAL_FUNC(cmd_message
)
491 struct globalMessage
*message
= NULL
;
492 const char *recipient
= NULL
;
495 message
= message_create(user
, argc
- 1, argv
+ 1);
498 recipient
= messageType(message
);
499 global_notice(user
, "GMSG_MESSAGE_ADDED", recipient
, message
->id
);
503 static GLOBAL_FUNC(cmd_list
)
505 struct globalMessage
*message
;
506 struct helpfile_table table
;
507 unsigned int length
, nn
;
511 global_notice(user
, "GMSG_NO_MESSAGES");
515 for(nn
=0, message
= messageList
; message
; nn
++, message
=message
->next
) ;
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";
527 for(nn
=1, message
= messageList
; message
; nn
++, message
= message
->next
)
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
);
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))
545 buffer
[sizeof(buffer
) - 1] = 0;
546 buffer
[sizeof(buffer
) - 2] = buffer
[sizeof(buffer
) - 3] = buffer
[sizeof(buffer
) - 4] = '.';
548 table
.contents
[nn
][4] = strdup(buffer
);
550 table_send(global
, user
->nick
, 0, NULL
, table
);
551 for (nn
=1; nn
<table
.length
; nn
++)
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
]);
558 free(table
.contents
[0]);
559 free(table
.contents
);
564 static GLOBAL_FUNC(cmd_remove
)
566 struct globalMessage
*message
= NULL
;
570 id
= strtoul(argv
[1], NULL
, 0);
572 for(message
= messageList
; message
; message
= message
->next
)
574 if(message
->id
== id
)
576 message_del(message
);
577 global_notice(user
, "GMSG_MESSAGE_DELETED", argv
[1]);
582 global_notice(user
, "GMSG_ID_INVALID", argv
[1]);
587 send_messages(struct userNode
*user
, long mask
, int obstreperize
)
589 struct globalMessage
*message
= messageList
;
590 unsigned int count
= 0;
594 if(message
->flags
& mask
)
596 if (obstreperize
&& !count
)
598 send_target_message(0, user
->nick
, global
, "GMSG_MOTD_HEADER");
599 send_target_message(0, user
->nick
, global
, "GMSG_MOTD_BAR");
601 notice_target(user
->nick
, message
);
605 message
= message
->next
;
607 if (obstreperize
&& count
)
608 send_target_message(0, user
->nick
, global
, "GMSG_MOTD_FOOTER");
612 static GLOBAL_FUNC(cmd_messages
)
614 long mask
= MESSAGE_RECIPIENT_LUSERS
| MESSAGE_RECIPIENT_CHANNELS
;
618 mask
|= MESSAGE_RECIPIENT_OPERS
;
621 mask
|= MESSAGE_RECIPIENT_HELPERS
;
623 count
= send_messages(user
, mask
, 0);
625 global_notice(user
, "GMSG_MESSAGE_COUNT", count
);
627 global_notice(user
, "GMSG_NO_MESSAGES");
633 global_process_user(struct userNode
*user
)
635 if(IsLocal(user
) || self
->uplink
->burst
|| user
->uplink
->burst
)
637 send_messages(user
, MESSAGE_RECIPIENT_LUSERS
, 1);
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.
642 if((now
- max_clients_time
) <= 30 && (now
- last_max_alert
) > 30)
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
;
655 global_process_auth(struct userNode
*user
, UNUSED_ARG(struct handle_info
*old_handle
))
658 send_messages(user
, MESSAGE_RECIPIENT_HELPERS
, 0);
662 global_process_oper(struct userNode
*user
)
664 if(user
->uplink
->burst
)
666 send_messages(user
, MESSAGE_RECIPIENT_OPERS
, 0);
670 global_conf_read(void)
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
);
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;
685 str
= database_get_data(conf_node
, KEY_NICK
, RECDB_QSTRING
);
687 NickChange(global
, str
, 0);
691 global_saxdb_read(struct dict
*db
)
693 struct record_data
*hir
;
696 unsigned long duration
;
697 char *str
, *from
, *message
;
700 for(it
=dict_first(db
); it
; it
=iter_next(it
))
703 if(hir
->type
!= RECDB_OBJECT
)
705 log_module(G_LOG
, LOG_WARNING
, "Unexpected rectype %d for %s.", hir
->type
, iter_key(it
));
709 str
= database_get_data(hir
->d
.object
, KEY_FLAGS
, RECDB_QSTRING
);
710 flags
= str
? strtoul(str
, NULL
, 0) : 0;
712 str
= database_get_data(hir
->d
.object
, KEY_POSTED
, RECDB_QSTRING
);
713 posted
= str
? strtoul(str
, NULL
, 0) : 0;
715 str
= database_get_data(hir
->d
.object
, KEY_DURATION
, RECDB_QSTRING
);
716 duration
= str
? strtoul(str
, NULL
, 0) : 0;
718 from
= database_get_data(hir
->d
.object
, KEY_FROM
, RECDB_QSTRING
);
719 message
= database_get_data(hir
->d
.object
, KEY_MESSAGE
, RECDB_QSTRING
);
721 message_add(flags
, posted
, duration
, from
, message
);
727 global_saxdb_write(struct saxdb_context
*ctx
)
729 struct globalMessage
*message
;
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
);
746 global_db_cleanup(void)
749 message_del(messageList
);
753 init_global(const char *nick
)
755 struct chanNode
*chan
;
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
);
762 conf_register_reload(global_conf_read
);
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
);
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
);
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
;
785 saxdb_register("Global", global_saxdb_read
, global_saxdb_write
);
786 reg_exit_func(global_db_cleanup
);
787 message_register_table(msgtab
);