Multi-language support fixes
[srvx.git] / src / helpfile.c
index b04c7838b28f42355c064ca6aa590cac7e39ff6f..d7edfbe608f49dd3a6c9284b66ecf5a013fb2017 100644 (file)
@@ -21,6 +21,7 @@
 #include "conf.h"
 #include "helpfile.h"
 #include "log.h"
+#include "modcmd.h"
 #include "nickserv.h"
 
 #include <dirent.h>
@@ -64,7 +65,7 @@ static struct language *language_alloc(const char *name)
 {
     struct language *lang = calloc(1, sizeof(*lang));
     lang->name = strdup(name);
-    lang->parent = language_find("C");
+    lang->parent = lang_C;
     if (!languages) {
         languages = dict_new();
         dict_set_free_data(languages, language_free);
@@ -136,7 +137,7 @@ static void language_set_messages(struct language *lang, dict_t dict)
             log_module(MAIN_LOG, LOG_WARNING, "Unsupported record type for message %s in language %s", msgid, lang->name);
             continue;
         }
-        dict_insert(lang->messages, msgid, msg);
+        dict_insert(lang->messages, strdup(msgid), msg);
         it = iter_next(it);
         it2 = iter_next(it2);
     }
@@ -144,7 +145,8 @@ static void language_set_messages(struct language *lang, dict_t dict)
         missing++;
         it2 = iter_next(it2);
     }
-    log_module(MAIN_LOG, LOG_WARNING, "In language %s, %d extra and %d missing messages", lang->name, extra, missing);
+    if (extra || missing)
+        log_module(MAIN_LOG, LOG_WARNING, "In language %s, %d extra and %d missing messages.", lang->name, extra, missing);
 }
 
 static struct language *language_read(const char *name)
@@ -187,6 +189,7 @@ static struct language *language_read(const char *name)
     /* (Re-)initialize the language's dicts. */
     dict_delete(lang->messages);
     lang->messages = dict_new();
+    dict_set_free_keys(lang->messages, free);
     dict_set_free_data(lang->messages, free);
     lang->helpfiles = dict_new();
     dict_set_free_data(lang->helpfiles, language_free_helpfile);
@@ -218,8 +221,11 @@ static void language_read_list(void)
 
     if (!(dir = opendir("languages")))
         return;
-    while ((dirent = readdir(dir)))
+    while ((dirent = readdir(dir))) {
+        if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, ".."))
+            continue;
         language_alloc(dirent->d_name);
+    }
     closedir(dir);
 }
 
@@ -533,44 +539,58 @@ vsend_message(const char *dest, struct userNode *src, struct handle_info *handle
 #define SEND_LINE() do { line[pos] = 0; if (pos > 0) irc_send(src, dest, line); chars_sent += pos; pos = 0; newline_ipos = ipos; } while (0)
        /* Custom expansion handled by helpfile-specific function. */
        case '{':
-       case '(':
-           if (expand_f) {
-               char *name_end = input.list + ipos + 1;
-
-               while (*name_end != '}' && *name_end != ')' && *name_end) name_end++;
-               if (*name_end) {
-                    struct helpfile_expansion exp;
-                   *name_end = 0;
-                   exp = expand_f(input.list + ipos + 1);
-                    switch (exp.type) {
-                    case HF_STRING:
-                        free_value = value = exp.value.str;
-                        if (!value)
-                            value = "";
-                        break;
-                    case HF_TABLE:
-                        /* Must send current line, then emit table. */
-                        SEND_LINE();
-                        table_send(src, (message_dest ? message_dest->nick : dest), 0, irc_send, exp.value.table);
-                        value = "";
-                        break;
-                    default:
-                        value = "";
-                        log_module(MAIN_LOG, LOG_ERROR, "Invalid exp.type %d from expansion function %p.", exp.type, expand_f);
-                        break;
-                    }
-                   ipos = name_end - input.list;
-                   break;
-               }
-           }
-
-       /* Let it fall through when there's no expansion function or
-       terminating ')'. */
+       case '(': {
+            struct helpfile_expansion exp;
+            char *name_end = input.list + ipos + 1, *colon = NULL;
+
+            while (*name_end != '}' && *name_end != ')' && *name_end) {
+                if (*name_end == ':') {
+                    colon = name_end;
+                    *colon = '\0';
+                }
+                name_end++;
+            }
+            if (!*name_end)
+                goto fallthrough;
+            *name_end = '\0';
+            if (colon) {
+                struct module *module = module_find(input.list + ipos + 1);
+                if (module && module->expand_help)
+                    exp = module->expand_help(colon + 1);
+                else {
+                    *colon = ':';
+                    goto fallthrough;
+                }
+            } else if (expand_f)
+                exp = expand_f(input.list + ipos + 1);
+            else
+                goto fallthrough;
+            switch (exp.type) {
+            case HF_STRING:
+                free_value = value = exp.value.str;
+                if (!value)
+                    value = "";
+                break;
+            case HF_TABLE:
+                /* Must send current line, then emit table. */
+                SEND_LINE();
+                table_send(src, (message_dest ? message_dest->nick : dest), 0, irc_send, exp.value.table);
+                value = "";
+                break;
+            default:
+                value = "";
+                log_module(MAIN_LOG, LOG_ERROR, "Invalid exp.type %d from expansion function %p.", exp.type, expand_f);
+                break;
+            }
+            ipos = name_end - input.list;
+            break;
+        }
        default:
-               value = alloca(3);
-               value[0] = '$';
-               value[1] = input.list[ipos];
-               value[2] = 0;
+        fallthrough:
+            value = alloca(3);
+            value[0] = '$';
+            value[1] = input.list[ipos];
+            value[2] = 0;
        }
        ipos++;
         while ((pos + strlen(value) > size) || strchr(value, '\n')) {