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