]>
Commit | Line | Data |
---|---|---|
d76ed9a9 | 1 | /* global.c - Global notice service |
2 | * Copyright 2000-2004 srvx Development Team | |
3 | * | |
83ff05c3 | 4 | * This file is part of x3. |
d76ed9a9 | 5 | * |
d0f04f71 | 6 | * x3 is free software; you can redistribute it and/or modify |
d76ed9a9 | 7 | * it under the terms of the GNU General Public License as published by |
348683aa | 8 | * the Free Software Foundation; either version 3 of the License, or |
d76ed9a9 | 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" | |
985d4109 | 23 | #include "hash.h" |
d76ed9a9 | 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" | |
0f6fe38c | 36 | #define KEY_ANNOUNCEMENTS_DEFAULT "announcements_default" |
d76ed9a9 | 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." }, | |
1478fd13 | 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--------------" }, | |
985d4109 | 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 | */ | |
09a3057c | 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 | ||
57692f5e | 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." }, | |
3da28d8e | 84 | { "HSMSG_SUSPENDED_BY", "%s suspended by %s. (HelpServ)" }, |
85 | { "HSMSG_UNSUSPENDED_BY", "%s unsuspended by %s. (HelpServ)" }, | |
57692f5e | 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 | ||
09a3057c | 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)" }, | |
57692f5e | 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." }, | |
985d4109 | 105 | |
d76ed9a9 | 106 | { NULL, NULL } |
107 | }; | |
108 | ||
b1bf690d | 109 | #define GLOBAL_SYNTAX() svccmd_send_help_brief(user, global, cmd) |
d76ed9a9 | 110 | #define GLOBAL_FUNC(NAME) MODCMD_FUNC(NAME) |
111 | ||
dee9951d | 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 | ||
d76ed9a9 | 128 | struct userNode *global; |
129 | ||
130 | static struct module *global_module; | |
131 | static struct service *global_service; | |
132 | static struct globalMessage *messageList; | |
7637f48f | 133 | extern struct string_list *autojoin_channels; |
d76ed9a9 | 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; | |
0f6fe38c | 141 | unsigned int announcements_default : 1; |
d76ed9a9 | 142 | } global_conf; |
143 | ||
1136f709 | 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 | |
d76ed9a9 | 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; | |
dee9951d | 156 | struct tm tm; |
d76ed9a9 | 157 | |
158 | message = malloc(sizeof(struct globalMessage)); | |
d76ed9a9 | 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 | ||
dee9951d | 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 | ||
d76ed9a9 | 177 | if(messageList) |
178 | { | |
179 | messageList->prev = message; | |
180 | } | |
d76ed9a9 | 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; | |
dee9951d | 225 | char *sender; |
d76ed9a9 | 226 | long flags = 0; |
227 | unsigned int i; | |
228 | ||
dee9951d | 229 | sender = user->handle_info->handle; |
230 | ||
d76ed9a9 | 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; | |
42c38d42 | 252 | } else if(!irccasecmp(argv[i], "authed")) { |
253 | flags |= MESSAGE_RECIPIENT_AUTHED; | |
d76ed9a9 | 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; | |
40c18c28 | 264 | } else if(!irccasecmp(argv[i], "rchannels")) { |
265 | flags |= MESSAGE_RECIPIENT_RCHANNELS; | |
0f6fe38c | 266 | } else if(!irccasecmp(argv[i], "announcement") || !irccasecmp(argv[i], "announce")) { |
267 | flags |= MESSAGE_RECIPIENT_ANNOUNCE; | |
d76ed9a9 | 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]); | |
dee9951d | 274 | } else if (irccasecmp(argv[i], "from") == 0) { |
275 | sender = argv[++i]; | |
d76ed9a9 | 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 | ||
dee9951d | 292 | return message_add(flags, now, duration, sender, text); |
d76ed9a9 | 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 | } | |
0f6fe38c | 306 | else if(message->flags & MESSAGE_RECIPIENT_ANNOUNCE) |
307 | { | |
308 | return "announcement"; | |
309 | } | |
d76ed9a9 | 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 | } | |
42c38d42 | 322 | else if(message->flags & MESSAGE_RECIPIENT_AUTHED) |
323 | { | |
324 | return "authed"; | |
325 | } | |
40c18c28 | 326 | else if(message->flags & MESSAGE_RECIPIENT_RCHANNELS) |
327 | { | |
328 | return "rchannels"; | |
329 | } | |
d76ed9a9 | 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 | { | |
dee9951d | 347 | send_target_message(0, target, global, "GMSG_MESSAGE_SOURCE", messageType(message), message->from, message->posted_s); |
d76ed9a9 | 348 | } |
349 | } | |
350 | ||
351 | send_target_message(4, target, global, "%s", message->message); | |
352 | } | |
353 | ||
354 | static int | |
355 | notice_channel(const char *key, void *data, void *extra) | |
356 | { | |
357 | struct chanNode *channel = data; | |
358 | /* It should be safe to assume channel is not NULL. */ | |
359 | if(channel->channel_info) | |
360 | notice_target(key, extra); | |
361 | return 0; | |
362 | } | |
363 | ||
364 | static void | |
365 | message_send(struct globalMessage *message) | |
366 | { | |
367 | struct userNode *user; | |
368 | unsigned long n; | |
0f6fe38c | 369 | dict_iterator_t it; |
d76ed9a9 | 370 | |
371 | if(message->flags & MESSAGE_RECIPIENT_CHANNELS) | |
372 | { | |
40c18c28 | 373 | dict_iterator_t it; |
374 | ||
375 | for (it = dict_first(channels); it; it = iter_next(it)) { | |
376 | struct chanNode *chan = iter_data(it); | |
377 | ||
378 | notice_target(chan->name, message); | |
379 | } | |
380 | } | |
381 | ||
382 | if(message->flags & MESSAGE_RECIPIENT_RCHANNELS) | |
383 | { | |
384 | dict_iterator_t it; | |
385 | ||
386 | for (it = dict_first(channels); it; it = iter_next(it)) { | |
387 | struct chanNode *chan = iter_data(it); | |
388 | ||
389 | if (chan->channel_info) | |
390 | notice_target(chan->name, message); | |
391 | } | |
d76ed9a9 | 392 | } |
393 | ||
394 | if(message->flags & MESSAGE_RECIPIENT_LUSERS) | |
395 | { | |
396 | notice_target("$*", message); | |
397 | return; | |
398 | } | |
399 | ||
0f6fe38c | 400 | if(message->flags & MESSAGE_RECIPIENT_ANNOUNCE) |
401 | { | |
402 | char announce; | |
403 | ||
404 | for (it = dict_first(clients); it; it = iter_next(it)) { | |
405 | user = iter_data(it); | |
406 | if (user->uplink == self) continue; | |
407 | announce = user->handle_info ? user->handle_info->announcements : '?'; | |
408 | if (announce == 'n') continue; | |
409 | if ((announce == '?') && !global_conf.announcements_default) continue; | |
410 | notice_target(user->nick, message); | |
411 | } | |
412 | } | |
413 | ||
d76ed9a9 | 414 | if(message->flags & MESSAGE_RECIPIENT_OPERS) |
415 | { | |
416 | for(n = 0; n < curr_opers.used; n++) | |
417 | { | |
418 | user = curr_opers.list[n]; | |
419 | ||
420 | if(user->uplink != self) | |
421 | { | |
422 | notice_target(user->nick, message); | |
423 | } | |
424 | } | |
425 | } | |
426 | ||
427 | if(message->flags & MESSAGE_RECIPIENT_HELPERS) | |
428 | { | |
429 | for(n = 0; n < curr_helpers.used; n++) | |
430 | { | |
431 | user = curr_helpers.list[n]; | |
432 | if (IsOper(user)) | |
433 | continue; | |
434 | notice_target(user->nick, message); | |
435 | } | |
436 | } | |
42c38d42 | 437 | |
438 | if(message->flags & MESSAGE_RECIPIENT_AUTHED) | |
439 | { | |
440 | dict_iterator_t it; | |
441 | for (it = dict_first(clients); it; it = iter_next(it)) { | |
442 | struct userNode *luser = iter_data(it); | |
443 | if (luser->handle_info) | |
444 | notice_target(luser->nick, message); | |
445 | } | |
446 | } | |
d76ed9a9 | 447 | } |
448 | ||
985d4109 | 449 | void |
450 | global_message_args(long targets, const char *language_entry, ...) | |
451 | { | |
7827220c | 452 | struct globalMessage *message = NULL; |
985d4109 | 453 | va_list arg_list; |
454 | dict_iterator_t it; | |
455 | char response[MAXLEN]; | |
456 | const char *fmt; | |
457 | ||
458 | if(!targets || !global) | |
459 | return; | |
460 | ||
461 | fmt = strdup(language_entry); | |
462 | ||
463 | /* Notice users/opers/helpers */ | |
464 | for (it = dict_first(clients); it; it = iter_next(it)) { | |
465 | struct userNode *luser = iter_data(it); | |
466 | ||
467 | language_entry = user_find_message(luser, fmt); | |
468 | ||
469 | va_start(arg_list, language_entry); | |
470 | vsnprintf(response, MAXLEN-2, language_entry, arg_list); | |
471 | response[MAXLEN-1] = 0; | |
472 | ||
a2dbccef | 473 | if (message) |
474 | message_del(message); | |
475 | ||
985d4109 | 476 | message = message_add(targets | MESSAGE_OPTION_SOURCELESS, now, 0, "", response); |
477 | if (!message) | |
478 | continue; | |
479 | ||
480 | /* opers */ | |
481 | if(message->flags & MESSAGE_RECIPIENT_OPERS && IsOper(luser)) { | |
482 | if(luser->uplink != self) | |
483 | notice_target(luser->nick, message); | |
4d99495c | 484 | |
42c38d42 | 485 | if ((message->flags & MESSAGE_RECIPIENT_LUSERS) || (message->flags & MESSAGE_RECIPIENT_HELPERS) || |
486 | (message->flags & MESSAGE_RECIPIENT_AUTHED)) | |
4d99495c | 487 | continue; |
985d4109 | 488 | } |
489 | ||
490 | /* helpers */ | |
491 | if (message->flags & MESSAGE_RECIPIENT_HELPERS && IsHelper(luser)) { | |
985d4109 | 492 | notice_target(luser->nick, message); |
4d99495c | 493 | |
42c38d42 | 494 | if ((message->flags & MESSAGE_RECIPIENT_LUSERS) || (message->flags & MESSAGE_RECIPIENT_AUTHED)) |
4d99495c | 495 | continue; |
985d4109 | 496 | } |
497 | ||
42c38d42 | 498 | /* authed */ |
499 | if ((message->flags & MESSAGE_RECIPIENT_AUTHED) && luser->handle_info) { | |
500 | notice_target(luser->nick, message); | |
501 | ||
502 | if (message->flags & MESSAGE_RECIPIENT_LUSERS) | |
503 | continue; | |
504 | } | |
505 | ||
985d4109 | 506 | /* users */ |
42c38d42 | 507 | if (message->flags & MESSAGE_RECIPIENT_LUSERS) { |
985d4109 | 508 | notice_target(luser->nick, message); |
42c38d42 | 509 | |
510 | } | |
985d4109 | 511 | } |
512 | ||
513 | message_del(message); | |
514 | } | |
515 | ||
d76ed9a9 | 516 | void |
517 | global_message(long targets, char *text) | |
518 | { | |
519 | struct globalMessage *message; | |
520 | ||
521 | if(!targets || !global) | |
522 | return; | |
523 | ||
524 | message = message_add(targets | MESSAGE_OPTION_SOURCELESS, now, 0, "", text); | |
525 | if(!message) | |
526 | return; | |
527 | ||
528 | message_send(message); | |
529 | message_del(message); | |
530 | } | |
531 | ||
532 | static GLOBAL_FUNC(cmd_notice) | |
533 | { | |
534 | struct globalMessage *message = NULL; | |
535 | const char *recipient = NULL, *text; | |
dee9951d | 536 | char *sender; |
d76ed9a9 | 537 | long target = 0; |
538 | ||
539 | assert(argc >= 3); | |
dee9951d | 540 | sender = user->handle_info->handle; |
d76ed9a9 | 541 | if(!irccasecmp(argv[1], "all")) { |
542 | target = MESSAGE_RECIPIENT_ALL; | |
543 | } else if(!irccasecmp(argv[1], "users")) { | |
544 | target = MESSAGE_RECIPIENT_LUSERS; | |
42c38d42 | 545 | } else if(!irccasecmp(argv[1], "authed")) { |
546 | target = MESSAGE_RECIPIENT_AUTHED; | |
d76ed9a9 | 547 | } else if(!irccasecmp(argv[1], "helpers")) { |
548 | target = MESSAGE_RECIPIENT_HELPERS; | |
549 | } else if(!irccasecmp(argv[1], "opers")) { | |
550 | target = MESSAGE_RECIPIENT_OPERS; | |
551 | } else if(!irccasecmp(argv[1], "staff") || !irccasecmp(argv[1], "privileged")) { | |
552 | target |= MESSAGE_RECIPIENT_HELPERS | MESSAGE_RECIPIENT_OPERS; | |
0f6fe38c | 553 | } else if(!irccasecmp(argv[1], "announcement") || !irccasecmp(argv[1], "announce")) { |
554 | target |= MESSAGE_RECIPIENT_ANNOUNCE; | |
d76ed9a9 | 555 | } else if(!irccasecmp(argv[1], "channels")) { |
556 | target = MESSAGE_RECIPIENT_CHANNELS; | |
40c18c28 | 557 | } else if(!irccasecmp(argv[1], "rchannels")) { |
558 | target = MESSAGE_RECIPIENT_RCHANNELS; | |
d76ed9a9 | 559 | } else { |
560 | global_notice(user, "GMSG_INVALID_TARGET", argv[1]); | |
561 | return 0; | |
562 | } | |
dee9951d | 563 | if(!irccasecmp(argv[2], "from")) { |
564 | if (argc < 5) { | |
565 | reply("MSG_MISSING_PARAMS", argv[0]); | |
566 | GLOBAL_SYNTAX(); | |
567 | return 0; | |
568 | } | |
569 | sender = argv[3]; | |
570 | text = unsplit_string(argv + 4, argc - 4, NULL); | |
571 | } else { | |
572 | text = unsplit_string(argv + 2, argc - 2, NULL); | |
573 | } | |
d76ed9a9 | 574 | |
dee9951d | 575 | message = message_add(target | MESSAGE_OPTION_IMMEDIATE, now, 0, sender, text); |
d76ed9a9 | 576 | if(!message) |
d76ed9a9 | 577 | return 0; |
d76ed9a9 | 578 | |
579 | recipient = messageType(message); | |
d76ed9a9 | 580 | message_send(message); |
581 | message_del(message); | |
582 | ||
583 | global_notice(user, "GMSG_MESSAGE_SENT", recipient); | |
584 | return 1; | |
585 | } | |
586 | ||
587 | static GLOBAL_FUNC(cmd_message) | |
588 | { | |
589 | struct globalMessage *message = NULL; | |
590 | const char *recipient = NULL; | |
591 | ||
592 | assert(argc >= 3); | |
593 | message = message_create(user, argc - 1, argv + 1); | |
594 | if(!message) | |
595 | return 0; | |
596 | recipient = messageType(message); | |
597 | global_notice(user, "GMSG_MESSAGE_ADDED", recipient, message->id); | |
598 | return 1; | |
599 | } | |
600 | ||
601 | static GLOBAL_FUNC(cmd_list) | |
602 | { | |
603 | struct globalMessage *message; | |
604 | struct helpfile_table table; | |
605 | unsigned int length, nn; | |
606 | ||
607 | if(!messageList) | |
608 | { | |
609 | global_notice(user, "GMSG_NO_MESSAGES"); | |
610 | return 1; | |
611 | } | |
612 | ||
613 | for(nn=0, message = messageList; message; nn++, message=message->next) ; | |
614 | table.length = nn+1; | |
615 | table.width = 5; | |
616 | table.flags = TABLE_NO_FREE; | |
617 | table.contents = calloc(table.length, sizeof(char**)); | |
618 | table.contents[0] = calloc(table.width, sizeof(char*)); | |
619 | table.contents[0][0] = "ID"; | |
620 | table.contents[0][1] = "Target"; | |
621 | table.contents[0][2] = "Expires"; | |
622 | table.contents[0][3] = "From"; | |
623 | table.contents[0][4] = "Message"; | |
624 | ||
625 | for(nn=1, message = messageList; message; nn++, message = message->next) | |
626 | { | |
627 | char buffer[64]; | |
628 | ||
629 | table.contents[nn] = calloc(table.width, sizeof(char*)); | |
630 | snprintf(buffer, sizeof(buffer), "%lu", message->id); | |
631 | table.contents[nn][0] = strdup(buffer); | |
632 | table.contents[nn][1] = messageType(message); | |
633 | if(message->duration) | |
634 | intervalString(buffer, message->posted + message->duration - now, user->handle_info); | |
635 | else | |
636 | strcpy(buffer, "Never."); | |
637 | table.contents[nn][2] = strdup(buffer); | |
638 | table.contents[nn][3] = message->from; | |
639 | length = strlen(message->message); | |
640 | safestrncpy(buffer, message->message, sizeof(buffer)); | |
641 | if(length > (sizeof(buffer) - 4)) | |
642 | { | |
643 | buffer[sizeof(buffer) - 1] = 0; | |
644 | buffer[sizeof(buffer) - 2] = buffer[sizeof(buffer) - 3] = buffer[sizeof(buffer) - 4] = '.'; | |
645 | } | |
646 | table.contents[nn][4] = strdup(buffer); | |
647 | } | |
648 | table_send(global, user->nick, 0, NULL, table); | |
649 | for (nn=1; nn<table.length; nn++) | |
650 | { | |
651 | free((char*)table.contents[nn][0]); | |
652 | free((char*)table.contents[nn][2]); | |
653 | free((char*)table.contents[nn][4]); | |
654 | free(table.contents[nn]); | |
655 | } | |
656 | free(table.contents[0]); | |
657 | free(table.contents); | |
658 | ||
659 | return 1; | |
660 | } | |
661 | ||
662 | static GLOBAL_FUNC(cmd_remove) | |
663 | { | |
664 | struct globalMessage *message = NULL; | |
665 | unsigned long id; | |
666 | ||
667 | assert(argc >= 2); | |
668 | id = strtoul(argv[1], NULL, 0); | |
669 | ||
670 | for(message = messageList; message; message = message->next) | |
671 | { | |
672 | if(message->id == id) | |
673 | { | |
674 | message_del(message); | |
675 | global_notice(user, "GMSG_MESSAGE_DELETED", argv[1]); | |
676 | return 1; | |
677 | } | |
678 | } | |
679 | ||
680 | global_notice(user, "GMSG_ID_INVALID", argv[1]); | |
681 | return 0; | |
682 | } | |
683 | ||
684 | static unsigned int | |
685 | send_messages(struct userNode *user, long mask, int obstreperize) | |
686 | { | |
687 | struct globalMessage *message = messageList; | |
688 | unsigned int count = 0; | |
689 | ||
690 | while(message) | |
691 | { | |
692 | if(message->flags & mask) | |
693 | { | |
694 | if (obstreperize && !count) | |
1478fd13 | 695 | { |
d76ed9a9 | 696 | send_target_message(0, user->nick, global, "GMSG_MOTD_HEADER"); |
1478fd13 | 697 | send_target_message(0, user->nick, global, "GMSG_MOTD_BAR"); |
698 | } | |
d76ed9a9 | 699 | notice_target(user->nick, message); |
700 | count++; | |
701 | } | |
702 | ||
703 | message = message->next; | |
704 | } | |
705 | if (obstreperize && count) | |
706 | send_target_message(0, user->nick, global, "GMSG_MOTD_FOOTER"); | |
707 | return count; | |
708 | } | |
709 | ||
710 | static GLOBAL_FUNC(cmd_messages) | |
711 | { | |
40c18c28 | 712 | long mask = MESSAGE_RECIPIENT_AUTHED | MESSAGE_RECIPIENT_LUSERS | MESSAGE_RECIPIENT_CHANNELS | MESSAGE_RECIPIENT_RCHANNELS; |
d76ed9a9 | 713 | unsigned int count; |
714 | ||
715 | if(IsOper(user)) | |
716 | mask |= MESSAGE_RECIPIENT_OPERS; | |
717 | ||
718 | if(IsHelper(user)) | |
719 | mask |= MESSAGE_RECIPIENT_HELPERS; | |
720 | ||
721 | count = send_messages(user, mask, 0); | |
722 | if(count) | |
723 | global_notice(user, "GMSG_MESSAGE_COUNT", count); | |
724 | else | |
725 | global_notice(user, "GMSG_NO_MESSAGES"); | |
726 | ||
727 | return 1; | |
728 | } | |
729 | ||
730 | static int | |
731 | global_process_user(struct userNode *user) | |
732 | { | |
733 | if(IsLocal(user) || self->uplink->burst || user->uplink->burst) | |
734 | return 0; | |
735 | send_messages(user, MESSAGE_RECIPIENT_LUSERS, 1); | |
736 | ||
737 | /* only alert on new usercount if the record was broken in the last | |
738 | * 30 seconds, and no alert had been sent in that time. | |
739 | */ | |
740 | if((now - max_clients_time) <= 30 && (now - last_max_alert) > 30) | |
741 | { | |
742 | char *message; | |
743 | message = alloca(36); | |
744 | sprintf(message, "New user count record: %d", max_clients); | |
745 | global_message(MESSAGE_RECIPIENT_OPERS, message); | |
746 | last_max_alert = now; | |
747 | } | |
748 | ||
749 | return 0; | |
750 | } | |
751 | ||
752 | static void | |
753 | global_process_auth(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle)) | |
754 | { | |
755 | if(IsHelper(user)) | |
756 | send_messages(user, MESSAGE_RECIPIENT_HELPERS, 0); | |
757 | } | |
758 | ||
759 | static void | |
760 | global_process_oper(struct userNode *user) | |
761 | { | |
762 | if(user->uplink->burst) | |
763 | return; | |
764 | send_messages(user, MESSAGE_RECIPIENT_OPERS, 0); | |
765 | } | |
766 | ||
767 | static void | |
768 | global_conf_read(void) | |
769 | { | |
770 | dict_t conf_node; | |
771 | const char *str; | |
772 | ||
773 | if (!(conf_node = conf_get_data(GLOBAL_CONF_NAME, RECDB_OBJECT))) { | |
774 | log_module(G_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", GLOBAL_CONF_NAME); | |
775 | return; | |
776 | } | |
777 | ||
778 | str = database_get_data(conf_node, KEY_DB_BACKUP_FREQ, RECDB_QSTRING); | |
779 | global_conf.db_backup_frequency = str ? ParseInterval(str) : 7200; | |
0f6fe38c | 780 | str = database_get_data(conf_node, KEY_ANNOUNCEMENTS_DEFAULT, RECDB_QSTRING); |
781 | global_conf.announcements_default = str ? enabled_string(str) : 1; | |
d76ed9a9 | 782 | |
783 | str = database_get_data(conf_node, KEY_NICK, RECDB_QSTRING); | |
784 | if(global && str) | |
785 | NickChange(global, str, 0); | |
786 | } | |
787 | ||
788 | static int | |
789 | global_saxdb_read(struct dict *db) | |
790 | { | |
791 | struct record_data *hir; | |
792 | time_t posted; | |
793 | long flags; | |
794 | unsigned long duration; | |
795 | char *str, *from, *message; | |
796 | dict_iterator_t it; | |
797 | ||
798 | for(it=dict_first(db); it; it=iter_next(it)) | |
799 | { | |
800 | hir = iter_data(it); | |
801 | if(hir->type != RECDB_OBJECT) | |
802 | { | |
803 | log_module(G_LOG, LOG_WARNING, "Unexpected rectype %d for %s.", hir->type, iter_key(it)); | |
804 | continue; | |
805 | } | |
806 | ||
807 | str = database_get_data(hir->d.object, KEY_FLAGS, RECDB_QSTRING); | |
808 | flags = str ? strtoul(str, NULL, 0) : 0; | |
809 | ||
810 | str = database_get_data(hir->d.object, KEY_POSTED, RECDB_QSTRING); | |
811 | posted = str ? strtoul(str, NULL, 0) : 0; | |
812 | ||
813 | str = database_get_data(hir->d.object, KEY_DURATION, RECDB_QSTRING); | |
814 | duration = str ? strtoul(str, NULL, 0) : 0; | |
815 | ||
816 | from = database_get_data(hir->d.object, KEY_FROM, RECDB_QSTRING); | |
817 | message = database_get_data(hir->d.object, KEY_MESSAGE, RECDB_QSTRING); | |
818 | ||
819 | message_add(flags, posted, duration, from, message); | |
820 | } | |
821 | return 0; | |
822 | } | |
823 | ||
824 | static int | |
825 | global_saxdb_write(struct saxdb_context *ctx) | |
826 | { | |
827 | struct globalMessage *message; | |
828 | char str[16]; | |
829 | ||
830 | for(message = messageList; message; message = message->next) { | |
831 | snprintf(str, sizeof(str), "%li", message->id); | |
832 | saxdb_start_record(ctx, str, 0); | |
833 | saxdb_write_int(ctx, KEY_FLAGS, message->flags); | |
834 | saxdb_write_int(ctx, KEY_POSTED, message->posted); | |
835 | saxdb_write_int(ctx, KEY_DURATION, message->duration); | |
836 | saxdb_write_string(ctx, KEY_FROM, message->from); | |
837 | saxdb_write_string(ctx, KEY_MESSAGE, message->message); | |
838 | saxdb_end_record(ctx); | |
839 | } | |
840 | return 0; | |
841 | } | |
842 | ||
843 | static void | |
844 | global_db_cleanup(void) | |
845 | { | |
846 | while(messageList) | |
847 | message_del(messageList); | |
848 | } | |
849 | ||
850 | void | |
851 | init_global(const char *nick) | |
852 | { | |
7637f48f | 853 | struct chanNode *chan; |
854 | unsigned int i; | |
d76ed9a9 | 855 | G_LOG = log_register_type("Global", "file:global.log"); |
856 | reg_new_user_func(global_process_user); | |
857 | reg_auth_func(global_process_auth); | |
858 | reg_oper_func(global_process_oper); | |
859 | ||
860 | conf_register_reload(global_conf_read); | |
861 | ||
862 | global_module = module_register("Global", G_LOG, "global.help", NULL); | |
863 | modcmd_register(global_module, "LIST", cmd_list, 1, 0, "flags", "+oper", NULL); | |
864 | modcmd_register(global_module, "MESSAGE", cmd_message, 3, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL); | |
865 | modcmd_register(global_module, "MESSAGES", cmd_messages, 1, 0, NULL); | |
866 | modcmd_register(global_module, "NOTICE", cmd_notice, 3, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL); | |
867 | modcmd_register(global_module, "REMOVE", cmd_remove, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL); | |
868 | ||
869 | if(nick) | |
870 | { | |
a32da4c7 | 871 | const char *modes = conf_get_data("services/global/modes", RECDB_QSTRING); |
1136f709 | 872 | global = AddLocalUser(nick, nick, NULL, "Global Services", modes); |
d76ed9a9 | 873 | global_service = service_register(global); |
874 | } | |
7637f48f | 875 | |
876 | if(autojoin_channels && global) { | |
877 | for (i = 0; i < autojoin_channels->used; i++) { | |
878 | chan = AddChannel(autojoin_channels->list[i], now, "+nt", NULL, NULL); | |
879 | AddChannelUser(global, chan)->modes |= MODE_CHANOP; | |
880 | } | |
881 | } | |
882 | ||
d76ed9a9 | 883 | saxdb_register("Global", global_saxdb_read, global_saxdb_write); |
884 | reg_exit_func(global_db_cleanup); | |
885 | message_register_table(msgtab); | |
886 | } |