PoC support for lua.
[apps/madmutt.git] / init.c
diff --git a/init.c b/init.c
index 94c82b2..eb92e17 100644 (file)
--- a/init.c
+++ b/init.c
 #include <lib-lib/lib-lib.h>
 #include <sys/utsname.h>
 
+#include <lib-lua/lib-lua.h>
 #include <lib-sys/unix.h>
 #include <lib-sys/mutt_ssl.h>
-
 #include <lib-ui/curses.h>
 #include <lib-ui/history.h>
 #include <lib-mx/mx.h>
+#include <lib-crypt/crypt.h>
 
 #include "mutt.h"
 #include "keymap.h"
 #include "charset.h"
 #include "thread.h"
-#include <lib-crypt/crypt.h>
 #include "mutt_idna.h"
 
 #if defined (USE_LIBESMTP) && (defined (USE_SSL) || defined (USE_GNUTLS))
@@ -585,41 +585,27 @@ int query_quadoption (int opt, const char *prompt)
   /* not reached */
 }
 
-static void add_to_list (string_list_t ** list, const char *str)
+static void add_to_list(string_list_t **list, const char *str)
 {
-  string_list_t *t, *last = NULL;
-
-  /* don't add a NULL or empty string to the list */
-  if (!str || *str == '\0')
-    return;
+    /* don't add a NULL or empty string to the list */
+    if (m_strisempty(str))
+        return;
 
-  /* check to make sure the item is not already on this list */
-  for (last = *list; last; last = last->next) {
-    if (ascii_strcasecmp (str, last->data) == 0) {
-      /* already on the list, so just ignore it */
-      last = NULL;
-      break;
+    /* check to make sure the item is not already on this list */
+    while (*list) {
+        if (!ascii_strcasecmp(str, (*list)->data))
+            return;
+        list = &(*list)->next;
     }
-    if (!last->next)
-      break;
-  }
 
-  if (!*list || last) {
-    t = p_new(string_list_t, 1);
-    t->data = m_strdup(str);
-    if (last) {
-      last->next = t;
-      last = last->next;
-    }
-    else
-      *list = last = t;
-  }
+    *list = p_new(string_list_t, 1);
+    (*list)->data = m_strdup(str);
 }
 
 static int
 add_to_rx_list(rx_t **list, const char *s, int flags, BUFFER *err)
 {
-    rx_trx;
+    rx_t *rx;
 
     if (m_strisempty(s))
         return 0;
@@ -640,7 +626,7 @@ add_to_rx_list(rx_t **list, const char *s, int flags, BUFFER *err)
 static int add_to_spam_list(rx_t **list, const char *pat,
                              const char *templ, BUFFER * err)
 {
-    rx_t **last, *rx;
+    rx_t *rx;
 
     if (m_strisempty(pat) || !templ)
         return 0;
@@ -651,62 +637,53 @@ static int add_to_spam_list(rx_t **list, const char *pat,
     }
 
     /* check to make sure the item is not already on this list */
-    for (last = list; *last; last = &(*last)->next) {
-        if (!ascii_strcasecmp(rx->pattern, (*last)->pattern) == 0) {
-            rx_t *tmp = rx_list_pop(last);
+    while (*list) {
+        if (!ascii_strcasecmp(rx->pattern, (*list)->pattern)) {
+            rx_t *tmp = rx_list_pop(list);
             rx_delete(&tmp);
-            last = rx_list_last(last);
-            break;
+        } else {
+            list = &(*list)->next;
         }
     }
 
-    *last = rx;
+    *list = rx;
     rx_set_template(rx, templ);
     return 0;
 }
 
 static int remove_from_spam_list (rx_t ** list, const char *pat)
 {
-  int nremoved = 0;
-
-  while (*list) {
-      if (!m_strcmp((*list)->pattern, pat)) {
-          rx_t *spam = rx_list_pop(list);
-          rx_delete(&spam);
-          nremoved++;
-      } else {
-          list = &(*list)->next;
-      }
-  }
+    int nremoved = 0;
+
+    while (*list) {
+        if (!m_strcmp((*list)->pattern, pat)) {
+            rx_t *spam = rx_list_pop(list);
+            rx_delete(&spam);
+            nremoved++;
+        } else {
+            list = &(*list)->next;
+        }
+    }
 
-  return nremoved;
+    return nremoved;
 }
 
 
-static void remove_from_list (string_list_t ** l, const char *str)
+static void remove_from_list(string_list_t **l, const char *str)
 {
-  string_list_t *p, *last = NULL;
+    if (!m_strcmp("*", str)) {
+        string_list_wipe(l);  /* ``unCMD *'' means delete all current entries */
+        return;
+    }
 
-  if (m_strcmp("*", str) == 0)
-    string_list_wipe(l);         /* ``unCMD *'' means delete all current entries */
-  else {
-    p = *l;
-    last = NULL;
-    while (p) {
-      if (ascii_strcasecmp (str, p->data) == 0) {
-        p_delete(&p->data);
-        if (last)
-          last->next = p->next;
-        else
-          (*l) = p->next;
-        p_delete(&p);
-      }
-      else {
-        last = p;
-        p = p->next;
-      }
+    while (*l) {
+        if (!ascii_strcasecmp(str, (*l)->data)) {
+            string_list_t *it = string_list_pop(l);
+            string_item_delete(&it);
+        } else {
+            l = &(*l)->next;
+        }
     }
-  }
 }
 
 static int remove_from_rx_list(rx_t **l, const char *str)
@@ -755,9 +732,8 @@ static int parse_ignore (BUFFER * buf, BUFFER * s,
   return 0;
 }
 
-static int parse_list (BUFFER * buf, BUFFER * s,
-                       unsigned long data __attribute__ ((unused)),
-                       BUFFER * err __attribute__ ((unused)))
+static int parse_list(BUFFER * buf, BUFFER * s, unsigned long data,
+                      BUFFER * err __attribute__ ((unused)))
 {
   do {
     mutt_extract_token (buf, s, 0);
@@ -1206,142 +1182,117 @@ static int parse_unalias (BUFFER * buf, BUFFER * s,
                           unsigned long data __attribute__ ((unused)),
                           BUFFER * err __attribute__ ((unused)))
 {
-  alias_t *tmp, *last = NULL;
-
-  do {
-    mutt_extract_token (buf, s, 0);
-
-    if (m_strcmp("*", buf->data) == 0) {
-      if (CurrentMenu == MENU_ALIAS) {
-        for (tmp = Aliases; tmp; tmp = tmp->next)
-          tmp->del = 1;
-        set_option (OPTFORCEREDRAWINDEX);
-      }
-      else
-        alias_list_wipe(&Aliases);
-      break;
-    }
-    else
-      for (tmp = Aliases; tmp; tmp = tmp->next) {
-        if (m_strcasecmp(buf->data, tmp->name) == 0) {
-          if (CurrentMenu == MENU_ALIAS) {
-            tmp->del = 1;
-            set_option (OPTFORCEREDRAWINDEX);
+    alias_t *tmp, **last;
+
+    do {
+        mutt_extract_token (buf, s, 0);
+
+        if (!m_strcmp("*", buf->data) == 0) {
+            if (CurrentMenu == MENU_ALIAS) {
+                for (tmp = Aliases; tmp; tmp = tmp->next)
+                    tmp->del = 1;
+                set_option(OPTFORCEREDRAWINDEX);
+            } else {
+                alias_list_wipe(&Aliases);
+            }
             break;
-          }
+        }
 
-          if (last)
-            last->next = tmp->next;
-          else
-            Aliases = tmp->next;
-          tmp->next = NULL;
-          alias_list_wipe(&tmp);
-          break;
+        last = &Aliases;
+        for (last = &Aliases; *last; last = &(*last)->next) {
+            if (!m_strcasecmp(buf->data, (*last)->name)) {
+                if (CurrentMenu == MENU_ALIAS) {
+                    (*last)->del = 1;
+                    set_option (OPTFORCEREDRAWINDEX);
+                } else {
+                    tmp = alias_list_pop(last);
+                    alias_delete(&tmp);
+                }
+                break;
+            }
         }
-        last = tmp;
-      }
-  }
-  while (MoreArgs (s));
-  return 0;
+    } while (MoreArgs(s));
+
+    return 0;
 }
 
 static int parse_alias (BUFFER * buf, BUFFER * s,
                         unsigned long data __attribute__ ((unused)),
                         BUFFER * err)
 {
-  alias_t *tmp = Aliases;
-  alias_t *last = NULL;
-  char *estr = NULL;
+    alias_t **last;
+    char *estr = NULL;
 
-  if (!MoreArgs (s)) {
-    m_strcpy(err->data, err->dsize, _("alias: no address"));
-    return (-1);
-  }
+    if (!MoreArgs (s)) {
+        m_strcpy(err->data, err->dsize, _("alias: no address"));
+        return (-1);
+    }
 
-  mutt_extract_token (buf, s, 0);
+    mutt_extract_token (buf, s, 0);
 
-  /* check to see if an alias with this name already exists */
-  for (; tmp; tmp = tmp->next) {
-    if (!m_strcasecmp(tmp->name, buf->data))
-      break;
-    last = tmp;
-  }
+    /* check to see if an alias with this name already exists */
+    for (last = &Aliases; *last; last = &(*last)->next) {
+        if (!m_strcasecmp((*last)->name, buf->data))
+            break;
+    }
 
-  if (!tmp) {
-    /* create a new alias */
-    tmp = alias_new();
-    tmp->name = m_strdup(buf->data);
-    /* give the main addressbook code a chance */
-    if (CurrentMenu == MENU_ALIAS)
-      set_option (OPTMENUCALLER);
-  }
-  else {
-    /* override the previous value */
-    address_list_wipe(&tmp->addr);
-    if (CurrentMenu == MENU_ALIAS)
-      set_option (OPTFORCEREDRAWINDEX);
-  }
+    if (!*last) {
+        /* create a new alias */
+        *last = alias_new();
+        (*last)->name = m_strdup(buf->data);
+        /* give the main addressbook code a chance */
+        if (CurrentMenu == MENU_ALIAS)
+            set_option (OPTMENUCALLER);
+    } else {
+        /* override the previous value */
+        address_list_wipe(&(*last)->addr);
+        if (CurrentMenu == MENU_ALIAS)
+            set_option (OPTFORCEREDRAWINDEX);
+    }
 
-  mutt_extract_token (buf, s,
-                      M_TOKEN_QUOTE | M_TOKEN_SPACE | M_TOKEN_SEMICOLON);
-  tmp->addr = mutt_parse_adrlist (tmp->addr, buf->data);
-  if (last)
-    last->next = tmp;
-  else
-    Aliases = tmp;
-  if (mutt_addrlist_to_idna (tmp->addr, &estr)) {
-    snprintf (err->data, err->dsize,
-              _("Warning: Bad IDN '%s' in alias '%s'.\n"), estr, tmp->name);
-    p_delete(&estr);
-    return -1;
-  }
+    mutt_extract_token(buf, s, M_TOKEN_QUOTE | M_TOKEN_SPACE);
+    (*last)->addr = mutt_parse_adrlist((*last)->addr, buf->data);
+    if (mutt_addrlist_to_idna((*last)->addr, &estr)) {
+        snprintf (err->data, err->dsize,
+                  _("Warning: Bad IDN '%s' in alias '%s'.\n"), estr, (*last)->name);
+        p_delete(&estr);
+        return -1;
+    }
 
-  return 0;
+    return 0;
 }
 
 static int
-parse_unmy_hdr (BUFFER * buf, BUFFER * s,
-                unsigned long data __attribute__ ((unused)),
-                BUFFER * err __attribute__ ((unused)))
+parse_unmy_hdr(BUFFER * buf, BUFFER * s,
+               unsigned long data __attribute__ ((unused)),
+               BUFFER * err __attribute__ ((unused)))
 {
-  string_list_t *last = NULL;
-  string_list_t *tmp = UserHeader;
-  string_list_t *ptr;
-  ssize_t l;
-
-  do {
-    mutt_extract_token (buf, s, 0);
-    if (m_strcmp("*", buf->data) == 0)
-      string_list_wipe(&UserHeader);
-    else {
-      tmp = UserHeader;
-      last = NULL;
-
-      l = m_strlen(buf->data);
-      if (buf->data[l - 1] == ':')
-        l--;
-
-      while (tmp) {
-        if (ascii_strncasecmp (buf->data, tmp->data, l) == 0
-            && tmp->data[l] == ':') {
-          ptr = tmp;
-          if (last)
-            last->next = tmp->next;
-          else
-            UserHeader = tmp->next;
-          tmp = tmp->next;
-          ptr->next = NULL;
-          string_list_wipe(&ptr);
+    do {
+        mutt_extract_token (buf, s, 0);
+
+        if (!m_strcmp("*", buf->data)) {
+            string_list_wipe(&UserHeader);
+        } else {
+            string_list_t **last = &UserHeader;
+            ssize_t l = m_strlen(buf->data);
+
+            if (buf->data[l - 1] == ':')
+                l--;
+
+            while (*last) {
+                if (!ascii_strncasecmp(buf->data, (*last)->data, l)
+                && (*last)->data[l] == ':')
+                {
+                    string_list_t *tmp = string_list_pop(last);
+                    string_item_delete(&tmp);
+                } else {
+                    last = &(*last)->next;
+                }
+            }
         }
-        else {
-          last = tmp;
-          tmp = tmp->next;
-        }
-      }
-    }
-  }
-  while (MoreArgs (s));
-  return 0;
+    } while (MoreArgs(s));
+
+    return 0;
 }
 
 static int parse_my_hdr (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
@@ -1975,7 +1926,7 @@ static int parse_source (BUFFER * tmp, BUFFER * s,
                every call to this function.
 
    err         where to write error messages */
-int mutt_parse_rc_line ( /* const */ char *line, BUFFER * token, BUFFER * err)
+int mutt_parse_rc_line (const char *line, BUFFER * token, BUFFER * err)
 {
   int i, r = -1;
   BUFFER expn;
@@ -2511,6 +2462,7 @@ void mutt_init (int skip_sys_rc, string_list_t * commands)
     From = rfc822_parse_adrlist (NULL, p);
 
   charset_initialize();
+  mlua_initialize();
 
   /* Set standard defaults */
   hash_map (ConfigOptions, mutt_set_default, 0);
@@ -2582,6 +2534,15 @@ void mutt_init (int skip_sys_rc, string_list_t * commands)
     exit (1);
   }
 
+  /* LUA {{{ */
+  snprintf(buffer, sizeof(buffer), "%s/.madmutt.lua", NONULL(Homedir));
+  if (access(buffer, F_OK) < 0)
+      snprintf(buffer, sizeof(buffer), "%s/.madmutt/cfg.lua", NONULL(Homedir));
+  if (!access(buffer, F_OK)) {
+      need_pause = mlua_wrap(mutt_error, mlua_dofile(buffer));
+  }
+  /* }}} */
+
   if (mutt_execute_commands (commands) != 0)
     need_pause = 1;