]>
Commit | Line | Data |
---|---|---|
1 | /* helpfile.c - Help file loading and display | |
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 "helpfile.h" | |
23 | #include "log.h" | |
24 | #include "modcmd.h" | |
25 | #include "nickserv.h" | |
26 | #include "spamserv.h" | |
27 | ||
28 | #if defined(HAVE_DIRENT_H) | |
29 | #include <dirent.h> | |
30 | #endif | |
31 | ||
32 | #if defined(HAVE_SYS_STAT_H) | |
33 | #include <sys/stat.h> | |
34 | #endif | |
35 | ||
36 | static const struct message_entry msgtab[] = { | |
37 | { "HFMSG_MISSING_HELPFILE", "The help file could not be found. Sorry!" }, | |
38 | { "HFMSG_HELP_NOT_STRING", "Help file error (help data was not a string)." }, | |
39 | { NULL, NULL } | |
40 | }; | |
41 | ||
42 | #define DEFAULT_LINE_SIZE MAX_LINE_SIZE | |
43 | #define DEFAULT_TABLE_SIZE 80 | |
44 | ||
45 | extern struct userNode *global, *chanserv, *opserv, *nickserv, *spamserv; | |
46 | struct userNode *message_dest; | |
47 | struct userNode *message_source; | |
48 | struct language *lang_C; | |
49 | struct dict *languages; | |
50 | ||
51 | static void language_cleanup(void) | |
52 | { | |
53 | dict_delete(languages); | |
54 | } | |
55 | ||
56 | static void language_free_helpfile(void *data) | |
57 | { | |
58 | struct helpfile *hf = data; | |
59 | close_helpfile(hf); | |
60 | } | |
61 | ||
62 | static void language_free(void *data) | |
63 | { | |
64 | struct language *lang = data; | |
65 | dict_delete(lang->messages); | |
66 | dict_delete(lang->helpfiles); | |
67 | free(lang->name); | |
68 | free(lang); | |
69 | } | |
70 | ||
71 | static struct language *language_alloc(const char *name) | |
72 | { | |
73 | struct language *lang = calloc(1, sizeof(*lang)); | |
74 | lang->name = strdup(name); | |
75 | lang->parent = lang_C; | |
76 | if (!languages) { | |
77 | languages = dict_new(); | |
78 | dict_set_free_data(languages, language_free); | |
79 | } | |
80 | dict_insert(languages, lang->name, lang); | |
81 | return lang; | |
82 | } | |
83 | ||
84 | /* Language names should use a lang or lang_COUNTRY type system, where | |
85 | * lang is a two-letter code according to ISO-639-1 (or three-letter | |
86 | * code according to ISO-639-2 for languages not in ISO-639-1), and | |
87 | * COUNTRY is the ISO 3166 country code in all upper case. | |
88 | * | |
89 | * See also: | |
90 | * http://www.loc.gov/standards/iso639-2/ | |
91 | * http://www.loc.gov/standards/iso639-2/langhome.html | |
92 | * http://www.iso.ch/iso/en/prods-services/iso3166ma/index.html | |
93 | */ | |
94 | struct language *language_find(const char *name) | |
95 | { | |
96 | struct language *lang; | |
97 | char alt_name[MAXLEN]; | |
98 | const char *uscore; | |
99 | ||
100 | if ((lang = dict_find(languages, name, NULL))) | |
101 | return lang; | |
102 | if ((uscore = strchr(name, '_'))) { | |
103 | strncpy(alt_name, name, uscore-name); | |
104 | alt_name[uscore-name] = 0; | |
105 | if ((lang = dict_find(languages, alt_name, NULL))) | |
106 | return lang; | |
107 | } | |
108 | if (!lang_C) { | |
109 | lang_C = language_alloc("C"); | |
110 | lang_C->messages = dict_new(); | |
111 | lang_C->helpfiles = dict_new(); | |
112 | } | |
113 | return lang_C; | |
114 | } | |
115 | ||
116 | static void language_set_messages(struct language *lang, dict_t dict) | |
117 | { | |
118 | dict_iterator_t it, it2; | |
119 | struct record_data *rd; | |
120 | char *msg; | |
121 | int extra, missing; | |
122 | ||
123 | extra = missing = 0; | |
124 | for (it = dict_first(dict), it2 = dict_first(lang_C->messages); it; ) { | |
125 | const char *msgid = iter_key(it); | |
126 | int diff = it2 ? irccasecmp(msgid, iter_key(it2)) : -1; | |
127 | if (diff < 0) { | |
128 | extra++; | |
129 | it = iter_next(it); | |
130 | continue; | |
131 | } else if (diff > 0) { | |
132 | missing++; | |
133 | it2 = iter_next(it2); | |
134 | continue; | |
135 | } | |
136 | rd = iter_data(it); | |
137 | switch (rd->type) { | |
138 | case RECDB_QSTRING: | |
139 | msg = strdup(rd->d.qstring); | |
140 | break; | |
141 | case RECDB_STRING_LIST: | |
142 | /* XXX: maybe do an unlistify_help() type thing */ | |
143 | default: | |
144 | log_module(MAIN_LOG, LOG_WARNING, "Unsupported record type for message %s in language %s", msgid, lang->name); | |
145 | continue; | |
146 | } | |
147 | dict_insert(lang->messages, strdup(msgid), msg); | |
148 | it = iter_next(it); | |
149 | it2 = iter_next(it2); | |
150 | } | |
151 | while (it2) { | |
152 | missing++; | |
153 | it2 = iter_next(it2); | |
154 | } | |
155 | if (extra || missing) | |
156 | log_module(MAIN_LOG, LOG_WARNING, "In language %s, %d extra and %d missing messages.", lang->name, extra, missing); | |
157 | } | |
158 | ||
159 | static struct language *language_read(const char *name) | |
160 | { | |
161 | DIR *dir; | |
162 | struct dirent *dirent; | |
163 | struct language *lang; | |
164 | struct helpfile *hf; | |
165 | char filename[MAXLEN], *uscore; | |
166 | FILE *file; | |
167 | dict_t dict; | |
168 | ||
169 | /* Never try to read the C language from disk. */ | |
170 | if (!irccasecmp(name, "C")) | |
171 | return lang_C; | |
172 | ||
173 | /* Open the directory stream; if we can't, fail. */ | |
174 | snprintf(filename, sizeof(filename), "languages/%s", name); | |
175 | if (!(dir = opendir(filename))) { | |
176 | log_module(MAIN_LOG, LOG_ERROR, "Unable to open language directory languages/%s: %s", name, strerror(errno)); | |
177 | return NULL; | |
178 | } | |
179 | if (!(lang = dict_find(languages, name, NULL))) | |
180 | lang = language_alloc(name); | |
181 | ||
182 | /* Find the parent language. */ | |
183 | snprintf(filename, sizeof(filename), "languages/%s/parent", name); | |
184 | if (!(file = fopen(filename, "r")) | |
185 | || !fgets(filename, sizeof(filename), file)) { | |
186 | strcpy(filename, "C"); | |
187 | } | |
188 | if (!(lang->parent = language_find(filename))) { | |
189 | uscore = strchr(filename, '_'); | |
190 | if (uscore) { | |
191 | *uscore = 0; | |
192 | lang->parent = language_find(filename); | |
193 | } | |
194 | if (!lang->parent) | |
195 | lang->parent = lang_C; | |
196 | } | |
197 | ||
198 | /* (Re-)initialize the language's dicts. */ | |
199 | dict_delete(lang->messages); | |
200 | lang->messages = dict_new(); | |
201 | dict_set_free_keys(lang->messages, free); | |
202 | dict_set_free_data(lang->messages, free); | |
203 | lang->helpfiles = dict_new(); | |
204 | dict_set_free_data(lang->helpfiles, language_free_helpfile); | |
205 | ||
206 | /* Read all the translations from the directory. */ | |
207 | while ((dirent = readdir(dir))) { | |
208 | snprintf(filename, sizeof(filename), "languages/%s/%s", name, dirent->d_name); | |
209 | if (!strcmp(dirent->d_name,"parent")) { | |
210 | continue; | |
211 | } else if (!strcmp(dirent->d_name, "strings.db")) { | |
212 | dict = parse_database(filename); | |
213 | language_set_messages(lang, dict); | |
214 | free_database(dict); | |
215 | } else if ((hf = dict_find(lang_C->helpfiles, dirent->d_name, NULL))) { | |
216 | hf = open_helpfile(filename, hf->expand); | |
217 | dict_insert(lang->helpfiles, hf->name, hf); | |
218 | } | |
219 | } | |
220 | ||
221 | /* All done. */ | |
222 | closedir(dir); | |
223 | return lang; | |
224 | } | |
225 | ||
226 | static void language_read_list(void) | |
227 | { | |
228 | struct stat sbuf; | |
229 | struct dirent *dirent; | |
230 | DIR *dir; | |
231 | char namebuf[MAXLEN]; | |
232 | ||
233 | if (!(dir = opendir("languages"))) | |
234 | return; | |
235 | while ((dirent = readdir(dir))) { | |
236 | if (dirent->d_name[0] == '.') | |
237 | continue; | |
238 | snprintf(namebuf, sizeof(namebuf), "languages/%s", dirent->d_name); | |
239 | if (stat(namebuf, &sbuf) < 0) { | |
240 | log_module(MAIN_LOG, LOG_INFO, "Skipping language entry '%s' (unable to stat).", dirent->d_name); | |
241 | continue; | |
242 | } | |
243 | if (!S_ISDIR(sbuf.st_mode)) { | |
244 | log_module(MAIN_LOG, LOG_INFO, "Skipping language entry '%s' (not directory).", dirent->d_name); | |
245 | continue; | |
246 | } | |
247 | language_alloc(dirent->d_name); | |
248 | } | |
249 | closedir(dir); | |
250 | } | |
251 | ||
252 | const char *language_find_message(struct language *lang, const char *msgid) { | |
253 | struct language *curr; | |
254 | const char *msg; | |
255 | if (!lang) | |
256 | lang = lang_C; | |
257 | for (curr = lang; curr; curr = curr->parent) | |
258 | if ((msg = dict_find(curr->messages, msgid, NULL))) | |
259 | return msg; | |
260 | log_module(MAIN_LOG, LOG_ERROR, "Tried to find unregistered message \"%s\" (original language %s)", msgid, lang->name); | |
261 | return NULL; | |
262 | } | |
263 | ||
264 | int strlen_vis(char *str) | |
265 | { | |
266 | int count; | |
267 | for(count=0;*str;str++) | |
268 | if(!iscntrl(*str)) | |
269 | count++; | |
270 | return count; | |
271 | } | |
272 | ||
273 | void | |
274 | table_send(struct userNode *from, const char *to, unsigned int size, irc_send_func irc_send, struct helpfile_table table) { | |
275 | unsigned int ii, jj, len, nreps, reps, tot_width, pos, spaces, *max_width; | |
276 | char line[MAX_LINE_SIZE+1]; | |
277 | struct handle_info *hi; | |
278 | char *sepstr = NULL; | |
279 | int sepsize = 0; | |
280 | ||
281 | if (IsChannelName(to) || *to == '$') { | |
282 | message_dest = NULL; | |
283 | hi = NULL; | |
284 | } else { | |
285 | message_dest = GetUserH(to); | |
286 | if (!message_dest) { | |
287 | log_module(MAIN_LOG, LOG_ERROR, "Unable to find user with nickname %s (in table_send from %s).", to, from->nick); | |
288 | return; | |
289 | } | |
290 | hi = message_dest->handle_info; | |
291 | #ifdef WITH_PROTOCOL_P10 | |
292 | to = message_dest->numeric; | |
293 | #endif | |
294 | } | |
295 | message_source = from; | |
296 | ||
297 | /* If size or irc_send are 0, we should try to use a default. */ | |
298 | if (size) | |
299 | {} /* keep size */ | |
300 | else if (!hi) | |
301 | size = DEFAULT_TABLE_SIZE; | |
302 | else if (hi->table_width) | |
303 | size = hi->table_width; | |
304 | else if (hi->screen_width) | |
305 | size = hi->screen_width; | |
306 | else | |
307 | size = DEFAULT_TABLE_SIZE; | |
308 | ||
309 | if (irc_send) | |
310 | {} /* use that function */ | |
311 | else if (hi) | |
312 | irc_send = HANDLE_FLAGGED(hi, USE_PRIVMSG) ? irc_privmsg : irc_notice; | |
313 | else | |
314 | irc_send = IsChannelName(to) ? irc_privmsg : irc_notice; | |
315 | ||
316 | /* Limit size to how much we can show at once */ | |
317 | if (size > sizeof(line)) | |
318 | size = sizeof(line); | |
319 | ||
320 | /* Figure out how wide columns should be */ | |
321 | max_width = alloca(table.width * sizeof(int)); | |
322 | for (jj=tot_width=0; jj<table.width; jj++) { | |
323 | /* Find the widest width for this column */ | |
324 | max_width[jj] = 0; | |
325 | for (ii=0; ii<table.length; ii++) { | |
326 | len = strlen(table.contents[ii][jj]); | |
327 | if (len > max_width[jj]) | |
328 | max_width[jj] = len; | |
329 | } | |
330 | /* Separate columns with spaces */ | |
331 | tot_width += max_width[jj] + 1; | |
332 | } | |
333 | /* How many rows to put in a line? */ | |
334 | if ((table.flags & TABLE_REPEAT_ROWS) && (size > tot_width)) | |
335 | nreps = size / tot_width; | |
336 | else | |
337 | nreps = 1; | |
338 | /* Send headers line.. */ | |
339 | if (table.flags & TABLE_NO_HEADERS) { | |
340 | ii = 0; | |
341 | } else { | |
342 | /* Sending headers needs special treatment: either show them | |
343 | * once, or repeat them as many times as we repeat the columns | |
344 | * in a row. */ | |
345 | for (pos=ii=0; ii<((table.flags & TABLE_REPEAT_HEADERS)?nreps:1); ii++) { | |
346 | for (jj=0; 1; ) { | |
347 | len = strlen(table.contents[0][jj]); | |
348 | //line[pos++] = '\02'; /* bold header */ | |
349 | spaces = max_width[jj] - len; | |
350 | if (table.flags & TABLE_PAD_LEFT) | |
351 | while (spaces--) | |
352 | line[pos++] = ' '; | |
353 | memcpy(line+pos, table.contents[0][jj], len); | |
354 | pos += len; | |
355 | //line[pos++] = '\02'; /* end bold header */ | |
356 | if (++jj == table.width) | |
357 | break; | |
358 | if (!(table.flags & TABLE_PAD_LEFT)) | |
359 | while (spaces--) | |
360 | line[pos++] = ' '; | |
361 | line[pos++] = ' '; | |
362 | } | |
363 | line[pos++] = ' '; | |
364 | } | |
365 | line[pos] = 0; | |
366 | sepsize = strlen_vis(line); | |
367 | sepstr = malloc(sepsize + 1); | |
368 | memset(sepstr, '-', sepsize); | |
369 | sepstr[sepsize] = 0; | |
370 | if(hi && hi->userlist_style != HI_STYLE_CLEAN) | |
371 | irc_send(from, to, sepstr); /* ----------------- */ | |
372 | irc_send(from, to, line); /* alpha beta roe */ | |
373 | if(hi && hi->userlist_style != HI_STYLE_CLEAN) | |
374 | irc_send(from, to, sepstr); /* ----------------- */ | |
375 | ii = 1; | |
376 | } | |
377 | /* Send the table. */ | |
378 | for (jj=0, pos=0, reps=0; ii<table.length; ) { | |
379 | while (1) { | |
380 | len = strlen(table.contents[ii][jj]); | |
381 | spaces = max_width[jj] - len; | |
382 | if (table.flags & TABLE_PAD_LEFT) | |
383 | while (spaces--) line[pos++] = ' '; | |
384 | memcpy(line+pos, table.contents[ii][jj], len); | |
385 | pos += len; | |
386 | if (++jj == table.width) { | |
387 | jj = 0, ++ii, ++reps; | |
388 | if ((reps == nreps) || (ii == table.length)) { | |
389 | line[pos] = 0; | |
390 | irc_send(from, to, line); | |
391 | pos = reps = 0; | |
392 | break; | |
393 | } | |
394 | } | |
395 | if (!(table.flags & TABLE_PAD_LEFT)) | |
396 | while (spaces--) | |
397 | line[pos++] = ' '; | |
398 | line[pos++] = ' '; | |
399 | } | |
400 | } | |
401 | if (!(table.flags & TABLE_NO_HEADERS)) { | |
402 | /* Send end bar & free its memory */ | |
403 | if(sepsize > 5) | |
404 | { | |
405 | sepstr[sepsize/2-1] = 'E'; | |
406 | sepstr[sepsize/2] = 'n'; | |
407 | sepstr[sepsize/2+1]= 'd'; | |
408 | } | |
409 | ||
410 | if(hi && hi->userlist_style != HI_STYLE_CLEAN) | |
411 | irc_send(from, to, sepstr); | |
412 | free(sepstr); | |
413 | } | |
414 | ||
415 | if (!(table.flags & TABLE_NO_FREE)) { | |
416 | /* Deallocate table memory (but not the string memory). */ | |
417 | for (ii=0; ii<table.length; ii++) | |
418 | free(table.contents[ii]); | |
419 | free(table.contents); | |
420 | } | |
421 | } | |
422 | ||
423 | static int | |
424 | vsend_message(const char *dest, struct userNode *src, struct handle_info *handle, int msg_type, expand_func_t expand_f, const char *format, va_list al) | |
425 | { | |
426 | void (*irc_send)(struct userNode *from, const char *to, const char *msg); | |
427 | static struct string_buffer input; | |
428 | unsigned int size, ipos, pos, length, chars_sent, use_color; | |
429 | unsigned int expand_pos, expand_ipos, newline_ipos; | |
430 | char line[MAX_LINE_SIZE]; | |
431 | ||
432 | if (IsChannelName(dest) || *dest == '$') { | |
433 | message_dest = NULL; | |
434 | } else if (!(message_dest = GetUserH(dest))) { | |
435 | log_module(MAIN_LOG, LOG_ERROR, "Unable to find user with nickname %s (in vsend_message from %s).", dest, src->nick); | |
436 | return 0; | |
437 | } else if (message_dest->dead) { | |
438 | /* No point in sending to a user who is leaving. */ | |
439 | return 0; | |
440 | } else { | |
441 | #ifdef WITH_PROTOCOL_P10 | |
442 | dest = message_dest->numeric; | |
443 | #endif | |
444 | } | |
445 | message_source = src; | |
446 | if (!(msg_type & MSG_TYPE_NOXLATE) | |
447 | && !(format = handle_find_message(handle, format))) | |
448 | return 0; | |
449 | /* fill in a buffer with the string */ | |
450 | input.used = 0; | |
451 | string_buffer_append_vprintf(&input, format, al); | |
452 | ||
453 | /* figure out how to send the messages */ | |
454 | if (handle) { | |
455 | msg_type |= (HANDLE_FLAGGED(handle, USE_PRIVMSG) ? 1 : 0); | |
456 | use_color = HANDLE_FLAGGED(handle, MIRC_COLOR); | |
457 | size = handle->screen_width; | |
458 | if (size > sizeof(line)) | |
459 | size = sizeof(line); | |
460 | } else { | |
461 | size = sizeof(line); | |
462 | use_color = 1; | |
463 | } | |
464 | if (!size || !(msg_type & MSG_TYPE_MULTILINE)) | |
465 | size = DEFAULT_LINE_SIZE; | |
466 | switch (msg_type & 3) { | |
467 | case 0: | |
468 | irc_send = irc_notice; | |
469 | break; | |
470 | case 2: | |
471 | irc_send = irc_wallchops; | |
472 | break; | |
473 | case 1: | |
474 | default: | |
475 | irc_send = irc_privmsg; | |
476 | } | |
477 | ||
478 | /* This used to be two passes, but if you do that and allow | |
479 | * arbitrary sizes for ${}-expansions (as with help indexes), | |
480 | * that requires a very big intermediate buffer. | |
481 | */ | |
482 | expand_ipos = newline_ipos = ipos = 0; | |
483 | expand_pos = pos = 0; | |
484 | chars_sent = 0; | |
485 | while (input.list[ipos]) { | |
486 | char ch, *value, *free_value; | |
487 | ||
488 | while ((ch = input.list[ipos]) && (ch != '$') && (ch != '\n') && (pos < size)) { | |
489 | line[pos++] = ch; | |
490 | ipos++; | |
491 | } | |
492 | ||
493 | if (!input.list[ipos]) | |
494 | goto send_line; | |
495 | if (input.list[ipos] == '\n') { | |
496 | ipos++; | |
497 | goto send_line; | |
498 | } | |
499 | if (pos == size) { | |
500 | unsigned int new_ipos; | |
501 | /* Scan backwards for a space in the input, until we hit | |
502 | * either the last newline or the last variable expansion. | |
503 | * Print the line up to that point, and start from there. | |
504 | */ | |
505 | for (new_ipos = ipos; | |
506 | (new_ipos > expand_ipos) && (new_ipos > newline_ipos); | |
507 | --new_ipos) | |
508 | if (input.list[new_ipos] == ' ') | |
509 | break; | |
510 | pos -= ipos - new_ipos; | |
511 | if (new_ipos == newline_ipos) { | |
512 | /* Single word was too big to fit on one line; skip | |
513 | * forward to its end and print it as a whole. | |
514 | */ | |
515 | while (input.list[new_ipos] | |
516 | && (input.list[new_ipos] != ' ') | |
517 | && (input.list[new_ipos] != '\n') | |
518 | && (input.list[new_ipos] != '$')) | |
519 | line[pos++] = input.list[new_ipos++]; | |
520 | } | |
521 | ipos = new_ipos; | |
522 | while (input.list[ipos] == ' ') | |
523 | ipos++; | |
524 | goto send_line; | |
525 | } | |
526 | ||
527 | free_value = 0; | |
528 | switch (input.list[++ipos]) { | |
529 | /* Literal '$' or end of string. */ | |
530 | case 0: | |
531 | ipos--; | |
532 | case '$': | |
533 | value = "$"; | |
534 | break; | |
535 | /* The following two expand to mIRC color codes if enabled | |
536 | by the user. */ | |
537 | case 'b': | |
538 | value = use_color ? "\002" : ""; | |
539 | break; | |
540 | case 'o': | |
541 | value = use_color ? "\017" : ""; | |
542 | break; | |
543 | case 'r': | |
544 | value = use_color ? "\026" : ""; | |
545 | break; | |
546 | case 'u': | |
547 | value = use_color ? "\037" : ""; | |
548 | break; | |
549 | /* Service nicks. */ | |
550 | case 'S': | |
551 | value = src->nick; | |
552 | break; | |
553 | case 'G': | |
554 | value = global ? global->nick : "Global"; | |
555 | break; | |
556 | case 'C': | |
557 | value = chanserv ? chanserv->nick : "ChanServ"; | |
558 | break; | |
559 | case 'O': | |
560 | value = opserv ? opserv->nick : "OpServ"; | |
561 | break; | |
562 | case 'N': | |
563 | value = nickserv ? nickserv->nick : "NickServ"; | |
564 | break; | |
565 | case 'X': | |
566 | value = spamserv ? spamserv->nick : "SpamServ"; | |
567 | break; | |
568 | case 's': | |
569 | value = self->name; | |
570 | break; | |
571 | case 'H': | |
572 | value = handle ? handle->handle : "Account"; | |
573 | break; | |
574 | #define SEND_LINE(TRUNCED) do { \ | |
575 | line[pos] = 0; \ | |
576 | if (pos > 0) { \ | |
577 | if (!(msg_type & MSG_TYPE_MULTILINE) && (pos > 1) && TRUNCED) \ | |
578 | line[pos-2] = line[pos-1] = '.'; \ | |
579 | irc_send(src, dest, line); \ | |
580 | } \ | |
581 | chars_sent += pos; \ | |
582 | pos = 0; \ | |
583 | newline_ipos = ipos; \ | |
584 | if (!(msg_type & MSG_TYPE_MULTILINE)) return chars_sent; \ | |
585 | } while (0) | |
586 | /* Custom expansion handled by helpfile-specific function. */ | |
587 | case '{': | |
588 | case '(': { | |
589 | struct helpfile_expansion exp; | |
590 | char *name_end = input.list + ipos + 1, *colon = NULL; | |
591 | ||
592 | while (*name_end != '}' && *name_end != ')' && *name_end) { | |
593 | if (*name_end == ':') { | |
594 | colon = name_end; | |
595 | *colon = '\0'; | |
596 | } | |
597 | name_end++; | |
598 | } | |
599 | if (!*name_end) | |
600 | goto fallthrough; | |
601 | *name_end = '\0'; | |
602 | if (colon) { | |
603 | struct module *module = module_find(input.list + ipos + 1); | |
604 | if (module && module->expand_help) | |
605 | exp = module->expand_help(colon + 1); | |
606 | else { | |
607 | *colon = ':'; | |
608 | goto fallthrough; | |
609 | } | |
610 | } else if (expand_f) | |
611 | exp = expand_f(input.list + ipos + 1); | |
612 | else | |
613 | goto fallthrough; | |
614 | switch (exp.type) { | |
615 | case HF_STRING: | |
616 | free_value = value = exp.value.str; | |
617 | if (!value) | |
618 | value = ""; | |
619 | break; | |
620 | case HF_TABLE: | |
621 | /* Must send current line, then emit table. */ | |
622 | SEND_LINE(0); | |
623 | table_send(src, (message_dest ? message_dest->nick : dest), 0, irc_send, exp.value.table); | |
624 | value = ""; | |
625 | break; | |
626 | default: | |
627 | value = ""; | |
628 | log_module(MAIN_LOG, LOG_ERROR, "Invalid exp.type %d from expansion function %p.", exp.type, expand_f); | |
629 | break; | |
630 | } | |
631 | ipos = name_end - input.list; | |
632 | break; | |
633 | } | |
634 | default: | |
635 | fallthrough: | |
636 | value = alloca(3); | |
637 | value[0] = '$'; | |
638 | value[1] = input.list[ipos]; | |
639 | value[2] = 0; | |
640 | } | |
641 | ipos++; | |
642 | while ((pos + strlen(value) > size) || strchr(value, '\n')) { | |
643 | unsigned int avail; | |
644 | avail = size - pos - 1; | |
645 | length = strcspn(value, "\n "); | |
646 | if (length <= avail) { | |
647 | strncpy(line+pos, value, length); | |
648 | pos += length; | |
649 | value += length; | |
650 | /* copy over spaces, until (possible) end of line */ | |
651 | while (*value == ' ') { | |
652 | if (pos < size-1) | |
653 | line[pos++] = *value; | |
654 | value++; | |
655 | } | |
656 | } else { | |
657 | /* word to send is too big to send now.. what to do? */ | |
658 | if (pos > 0) { | |
659 | /* try to put it on a separate line */ | |
660 | SEND_LINE(1); | |
661 | } else { | |
662 | /* already at start of line; only send part of it */ | |
663 | strncpy(line, value, avail); | |
664 | pos += avail; | |
665 | value += length; | |
666 | /* skip any trailing spaces */ | |
667 | while (*value == ' ') | |
668 | value++; | |
669 | } | |
670 | } | |
671 | /* if we're looking at a newline, send the accumulated text */ | |
672 | if (*value == '\n') { | |
673 | SEND_LINE(0); | |
674 | value++; | |
675 | } | |
676 | } | |
677 | length = strlen(value); | |
678 | memcpy(line + pos, value, length); | |
679 | if (free_value) | |
680 | free(free_value); | |
681 | pos += length; | |
682 | if ((pos < size-1) && input.list[ipos]) { | |
683 | expand_pos = pos; | |
684 | expand_ipos = ipos; | |
685 | continue; | |
686 | } | |
687 | send_line: | |
688 | expand_pos = pos; | |
689 | expand_ipos = ipos; | |
690 | SEND_LINE(0); | |
691 | #undef SEND_LINE | |
692 | } | |
693 | return chars_sent; | |
694 | } | |
695 | ||
696 | int | |
697 | send_message(struct userNode *dest, struct userNode *src, const char *format, ...) | |
698 | { | |
699 | int res; | |
700 | va_list ap; | |
701 | ||
702 | if (IsLocal(dest)) return 0; | |
703 | va_start(ap, format); | |
704 | res = vsend_message(dest->nick, src, dest->handle_info, 0, NULL, format, ap); | |
705 | va_end(ap); | |
706 | return res; | |
707 | } | |
708 | ||
709 | int | |
710 | send_message_type(int msg_type, struct userNode *dest, struct userNode *src, const char *format, ...) { | |
711 | int res; | |
712 | va_list ap; | |
713 | ||
714 | if (IsLocal(dest)) return 0; | |
715 | va_start(ap, format); | |
716 | res = vsend_message(dest->nick, src, dest->handle_info, msg_type, NULL, format, ap); | |
717 | va_end(ap); | |
718 | return res; | |
719 | } | |
720 | ||
721 | int | |
722 | send_target_message(int msg_type, const char *dest, struct userNode *src, const char *format, ...) | |
723 | { | |
724 | int res; | |
725 | va_list ap; | |
726 | ||
727 | va_start(ap, format); | |
728 | res = vsend_message(dest, src, NULL, msg_type, NULL, format, ap); | |
729 | va_end(ap); | |
730 | return res; | |
731 | } | |
732 | ||
733 | int | |
734 | _send_help(struct userNode *dest, struct userNode *src, expand_func_t expand, const char *format, ...) | |
735 | { | |
736 | int res; | |
737 | ||
738 | va_list ap; | |
739 | va_start(ap, format); | |
740 | res = vsend_message(dest->nick, src, dest->handle_info, 12, expand, format, ap); | |
741 | va_end(ap); | |
742 | return res; | |
743 | } | |
744 | ||
745 | int | |
746 | send_help(struct userNode *dest, struct userNode *src, struct helpfile *hf, const char *topic) | |
747 | { | |
748 | struct helpfile *lang_hf; | |
749 | struct record_data *rec; | |
750 | struct language *curr; | |
751 | ||
752 | if (!topic) | |
753 | topic = "<index>"; | |
754 | if (!hf) { | |
755 | _send_help(dest, src, NULL, "HFMSG_MISSING_HELPFILE"); | |
756 | return false; | |
757 | } | |
758 | for (curr = (dest->handle_info ? dest->handle_info->language : lang_C); | |
759 | curr; | |
760 | curr = curr->parent) { | |
761 | lang_hf = dict_find(curr->helpfiles, hf->name, NULL); | |
762 | if (!lang_hf) | |
763 | continue; | |
764 | rec = dict_find(lang_hf->db, topic, NULL); | |
765 | if (rec && rec->type == RECDB_QSTRING) { | |
766 | _send_help(dest, src, hf->expand, rec->d.qstring); | |
767 | return true; | |
768 | } | |
769 | } | |
770 | rec = dict_find(hf->db, "<missing>", NULL); | |
771 | if (!rec) | |
772 | return false; | |
773 | if (rec->type != RECDB_QSTRING) { | |
774 | send_message(dest, src, "HFMSG_HELP_NOT_STRING"); | |
775 | return false; | |
776 | } | |
777 | _send_help(dest, src, hf->expand, rec->d.qstring); | |
778 | return true; | |
779 | } | |
780 | ||
781 | int | |
782 | send_help_brief(struct userNode *dest, struct userNode *src, struct helpfile *hf, const char *topic) | |
783 | { | |
784 | struct helpfile *lang_hf; | |
785 | struct record_data *rec; | |
786 | struct language *curr; | |
787 | ||
788 | if (!topic) | |
789 | topic = "<index>"; | |
790 | if (!hf) { | |
791 | _send_help(dest, src, NULL, "HFMSG_MISSING_HELPFILE"); | |
792 | return 0; | |
793 | } | |
794 | for (curr = (dest->handle_info ? dest->handle_info->language : lang_C); | |
795 | curr; | |
796 | curr = curr->parent) { | |
797 | lang_hf = dict_find(curr->helpfiles, hf->name, NULL); | |
798 | if (!lang_hf) | |
799 | continue; | |
800 | rec = dict_find(lang_hf->db, topic, NULL); | |
801 | if (rec && rec->type == RECDB_QSTRING) | |
802 | { | |
803 | char* buf; | |
804 | int res; | |
805 | ||
806 | buf = malloc(strlen(rec->d.qstring) + 1); | |
807 | strcpy(buf, rec->d.qstring); | |
808 | *strchr(buf, '\n') = 0; | |
809 | ||
810 | res = _send_help(dest, src, hf->expand, buf); | |
811 | free(buf); | |
812 | return res; | |
813 | } | |
814 | } | |
815 | rec = dict_find(hf->db, "<missing>", NULL); | |
816 | if (!rec) | |
817 | return 0; /* send_message(dest, src, "MSG_TOPIC_UNKNOWN"); */ | |
818 | if (rec->type != RECDB_QSTRING) | |
819 | return 0; /* send_message(dest, src, "HFMSG_HELP_NOT_STRING"); */ | |
820 | return _send_help(dest, src, hf->expand, rec->d.qstring); | |
821 | } | |
822 | ||
823 | /* Grammar supported by this parser: | |
824 | * condition = expr | prefix expr | |
825 | * expr = atomicexpr | atomicexpr op atomicexpr | |
826 | * op = '&&' | '||' | 'and' | 'or' | |
827 | * atomicexpr = '(' expr ')' | identifier | |
828 | * identifier = ( '0'-'9' 'A'-'Z' 'a'-'z' '-' '_' '/' )+ | ! identifier | |
829 | * | |
830 | * Whitespace is ignored. The parser is implemented as a recursive | |
831 | * descent parser by functions like: | |
832 | * static int helpfile_eval_<element>(const char *start, const char **end); | |
833 | */ | |
834 | ||
835 | enum helpfile_op { | |
836 | OP_INVALID, | |
837 | OP_BOOL_AND, | |
838 | OP_BOOL_OR | |
839 | }; | |
840 | ||
841 | static const struct { | |
842 | const char *str; | |
843 | enum helpfile_op op; | |
844 | } helpfile_operators[] = { | |
845 | { "&&", OP_BOOL_AND }, | |
846 | { "and", OP_BOOL_AND }, | |
847 | { "||", OP_BOOL_OR }, | |
848 | { "or", OP_BOOL_OR }, | |
849 | { NULL, OP_INVALID } | |
850 | }; | |
851 | ||
852 | static const char *identifier_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_/"; | |
853 | ||
854 | static int helpfile_eval_expr(const char *start, const char **end); | |
855 | static int helpfile_eval_atomicexpr(const char *start, const char **end); | |
856 | static int helpfile_eval_identifier(const char *start, const char **end); | |
857 | ||
858 | static int | |
859 | helpfile_eval_identifier(const char *start, const char **end) | |
860 | { | |
861 | /* Skip leading whitespace. */ | |
862 | while (isspace(*start) && (start < *end)) | |
863 | start++; | |
864 | if (start == *end) { | |
865 | log_module(MAIN_LOG, LOG_FATAL, "Expected identifier in helpfile condition."); | |
866 | return -1; | |
867 | } | |
868 | ||
869 | if (start[0] == '!') { | |
870 | int res = helpfile_eval_identifier(start+1, end); | |
871 | if (res < 0) | |
872 | return res; | |
873 | return !res; | |
874 | } else if (start[0] == '/') { | |
875 | const char *sep; | |
876 | char *id_str, *value; | |
877 | ||
878 | for (sep = start; | |
879 | strchr(identifier_chars, sep[0]) && (sep < *end); | |
880 | ++sep) ; | |
881 | memcpy(id_str = alloca(sep+1-start), start, sep-start); | |
882 | id_str[sep-start] = '\0'; | |
883 | value = conf_get_data(id_str+1, RECDB_QSTRING); | |
884 | *end = sep; | |
885 | if (!value) | |
886 | return 0; | |
887 | return enabled_string(value) || true_string(value); | |
888 | } else if ((*end - start >= 4) && !ircncasecmp(start, "true", 4)) { | |
889 | *end = start + 4; | |
890 | return 1; | |
891 | } else if ((*end - start >= 5) && !ircncasecmp(start, "false", 5)) { | |
892 | *end = start + 5; | |
893 | return 0; | |
894 | } else { | |
895 | log_module(MAIN_LOG, LOG_FATAL, "Unexpected helpfile identifier '%.*s'.", (int)(*end-start), start); | |
896 | return -1; | |
897 | } | |
898 | } | |
899 | ||
900 | static int | |
901 | helpfile_eval_atomicexpr(const char *start, const char **end) | |
902 | { | |
903 | const char *sep; | |
904 | int res; | |
905 | ||
906 | /* Skip leading whitespace. */ | |
907 | while (isspace(*start) && (start < *end)) | |
908 | start++; | |
909 | if (start == *end) { | |
910 | log_module(MAIN_LOG, LOG_FATAL, "Expected atomic expression in helpfile condition."); | |
911 | return -1; | |
912 | } | |
913 | ||
914 | /* If it's not parenthesized, it better be a valid identifier. */ | |
915 | if (*start != '(') | |
916 | return helpfile_eval_identifier(start, end); | |
917 | ||
918 | /* Parse the internal expression. */ | |
919 | start++; | |
920 | sep = *end; | |
921 | res = helpfile_eval_expr(start, &sep); | |
922 | ||
923 | /* Check for the closing parenthesis. */ | |
924 | while (isspace(*sep) && (sep < *end)) | |
925 | sep++; | |
926 | if ((sep == *end) || (sep[0] != ')')) { | |
927 | log_module(MAIN_LOG, LOG_FATAL, "Expected close parenthesis at '%.*s'.", (int)(*end-sep), sep); | |
928 | return -1; | |
929 | } | |
930 | ||
931 | /* Report the end location and result. */ | |
932 | *end = sep + 1; | |
933 | return res; | |
934 | } | |
935 | ||
936 | static int | |
937 | helpfile_eval_expr(const char *start, const char **end) | |
938 | { | |
939 | const char *sep, *sep2; | |
940 | unsigned int ii, len; | |
941 | int res_a, res_b; | |
942 | enum helpfile_op op; | |
943 | ||
944 | /* Parse the first atomicexpr. */ | |
945 | sep = *end; | |
946 | res_a = helpfile_eval_atomicexpr(start, &sep); | |
947 | if (res_a < 0) | |
948 | return res_a; | |
949 | ||
950 | /* Figure out what follows that. */ | |
951 | while (isspace(*sep) && (sep < *end)) | |
952 | sep++; | |
953 | if (sep == *end) | |
954 | return res_a; | |
955 | op = OP_INVALID; | |
956 | for (ii = 0; helpfile_operators[ii].str; ++ii) { | |
957 | len = strlen(helpfile_operators[ii].str); | |
958 | if (ircncasecmp(sep, helpfile_operators[ii].str, len)) | |
959 | continue; | |
960 | op = helpfile_operators[ii].op; | |
961 | sep += len; | |
962 | } | |
963 | if (op == OP_INVALID) { | |
964 | log_module(MAIN_LOG, LOG_FATAL, "Unrecognized helpfile operator at '%.*s'.", (int)(*end-sep), sep); | |
965 | return -1; | |
966 | } | |
967 | ||
968 | /* Parse the next atomicexpr. */ | |
969 | sep2 = *end; | |
970 | res_b = helpfile_eval_atomicexpr(sep, &sep2); | |
971 | if (res_b < 0) | |
972 | return res_b; | |
973 | ||
974 | /* Make sure there's no trailing garbage */ | |
975 | while (isspace(*sep2) && (sep2 < *end)) | |
976 | sep2++; | |
977 | if (sep2 != *end) { | |
978 | log_module(MAIN_LOG, LOG_FATAL, "Trailing garbage in helpfile expression: '%.*s'.", (int)(*end-sep2), sep2); | |
979 | return -1; | |
980 | } | |
981 | ||
982 | /* Record where we stopped parsing. */ | |
983 | *end = sep2; | |
984 | ||
985 | /* Do the logic on the subexpressions. */ | |
986 | switch (op) { | |
987 | case OP_BOOL_AND: | |
988 | return res_a && res_b; | |
989 | case OP_BOOL_OR: | |
990 | return res_a || res_b; | |
991 | default: | |
992 | return -1; | |
993 | } | |
994 | } | |
995 | ||
996 | static int | |
997 | helpfile_eval_condition(const char *start, const char **end) | |
998 | { | |
999 | const char *term; | |
1000 | ||
1001 | /* Skip the prefix if there is one. */ | |
1002 | for (term = start; isalnum(*term) && (term < *end); ++term) ; | |
1003 | if (term != start) { | |
1004 | if ((term + 2 >= *end) || (term[0] != ':') || (term[1] != ' ')) { | |
1005 | log_module(MAIN_LOG, LOG_FATAL, "In helpfile condition '%.*s' expected prefix to end with ': '.",(int)(*end-start), start); | |
1006 | return -1; | |
1007 | } | |
1008 | start = term + 2; | |
1009 | } | |
1010 | ||
1011 | /* Evaluate the remaining string as an expression. */ | |
1012 | return helpfile_eval_expr(start, end); | |
1013 | } | |
1014 | ||
1015 | static int | |
1016 | unlistify_help(const char *key, void *data, void *extra) | |
1017 | { | |
1018 | struct record_data *rd = data; | |
1019 | dict_t newdb = extra; | |
1020 | ||
1021 | switch (rd->type) { | |
1022 | case RECDB_QSTRING: | |
1023 | dict_insert(newdb, strdup(key), alloc_record_data_qstring(GET_RECORD_QSTRING(rd))); | |
1024 | return 0; | |
1025 | case RECDB_STRING_LIST: { | |
1026 | struct string_list *slist = GET_RECORD_STRING_LIST(rd); | |
1027 | char *dest; | |
1028 | unsigned int totlen, len, i; | |
1029 | ||
1030 | for (i=totlen=0; i<slist->used; i++) | |
1031 | totlen = totlen + strlen(slist->list[i]) + 1; | |
1032 | dest = alloca(totlen+1); | |
1033 | for (i=totlen=0; i<slist->used; i++) { | |
1034 | len = strlen(slist->list[i]); | |
1035 | memcpy(dest+totlen, slist->list[i], len); | |
1036 | dest[totlen+len] = '\n'; | |
1037 | totlen = totlen + len + 1; | |
1038 | } | |
1039 | dest[totlen] = 0; | |
1040 | dict_insert(newdb, strdup(key), alloc_record_data_qstring(dest)); | |
1041 | return 0; | |
1042 | } | |
1043 | case RECDB_OBJECT: { | |
1044 | dict_iterator_t it; | |
1045 | ||
1046 | for (it = dict_first(GET_RECORD_OBJECT(rd)); it; it = iter_next(it)) { | |
1047 | const char *k2, *end; | |
1048 | int res; | |
1049 | ||
1050 | /* Evaluate the expression for this subentry. */ | |
1051 | k2 = iter_key(it); | |
1052 | end = k2 + strlen(k2); | |
1053 | res = helpfile_eval_condition(k2, &end); | |
1054 | /* If the evaluation failed, bail. */ | |
1055 | if (res < 0) { | |
1056 | log_module(MAIN_LOG, LOG_FATAL, " .. while processing entry '%s' condition '%s'.", key, k2); | |
1057 | return 1; | |
1058 | } | |
1059 | /* If the condition was false, try another. */ | |
1060 | if (!res) | |
1061 | continue; | |
1062 | /* If we cannot unlistify the contents, bail. */ | |
1063 | if (unlistify_help(key, iter_data(it), extra)) | |
1064 | return 1; | |
1065 | return 0; | |
1066 | } | |
1067 | /* If none of the conditions apply, just omit the entry. */ | |
1068 | return 0; | |
1069 | } | |
1070 | default: | |
1071 | return 1; | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | struct helpfile * | |
1076 | open_helpfile(const char *fname, expand_func_t expand) | |
1077 | { | |
1078 | struct helpfile *hf; | |
1079 | char *slash; | |
1080 | dict_t db = parse_database(fname); | |
1081 | hf = calloc(1, sizeof(*hf)); | |
1082 | hf->expand = expand; | |
1083 | hf->db = alloc_database(); | |
1084 | dict_set_free_keys(hf->db, free); | |
1085 | if ((slash = strrchr(fname, '/'))) { | |
1086 | hf->name = strdup(slash + 1); | |
1087 | } else { | |
1088 | hf->name = strdup(fname); | |
1089 | dict_insert(language_find("C")->helpfiles, hf->name, hf); | |
1090 | } | |
1091 | if (db) { | |
1092 | dict_foreach(db, unlistify_help, hf->db); | |
1093 | free_database(db); | |
1094 | } | |
1095 | return hf; | |
1096 | } | |
1097 | ||
1098 | void close_helpfile(struct helpfile *hf) | |
1099 | { | |
1100 | if (!hf) | |
1101 | return; | |
1102 | free((char*)hf->name); | |
1103 | free_database(hf->db); | |
1104 | free(hf); | |
1105 | } | |
1106 | ||
1107 | void message_register_table(const struct message_entry *table) | |
1108 | { | |
1109 | if (!lang_C) | |
1110 | language_find("C"); | |
1111 | while (table->msgid) { | |
1112 | dict_insert(lang_C->messages, table->msgid, (char*)table->format); | |
1113 | table++; | |
1114 | } | |
1115 | } | |
1116 | ||
1117 | void helpfile_init(void) | |
1118 | { | |
1119 | message_register_table(msgtab); | |
1120 | language_read_list(); | |
1121 | } | |
1122 | ||
1123 | void helpfile_finalize(void) | |
1124 | { | |
1125 | dict_iterator_t it; | |
1126 | for (it = dict_first(languages); it; it = iter_next(it)) | |
1127 | language_read(iter_key(it)); | |
1128 | reg_exit_func(language_cleanup); | |
1129 | } |