Replace deprecated luaL_openlib() by luaL_register()
[apps/madmutt.git] / handler.c
index 28739c1..f75fab1 100644 (file)
--- a/handler.c
+++ b/handler.c
@@ -12,7 +12,7 @@
 #include <lib-mime/mime.h>
 #include <lib-mime/rfc3676.h>
 
-#include <lib-ui/curses.h>
+#include <lib-ui/lib-ui.h>
 
 #include <lib-sys/unix.h>
 
@@ -25,8 +25,6 @@
 #include "crypt.h"
 #include "state.h"
 #include "attach.h"
-#include "lib.h"
-
 
 typedef int handler_f (BODY *, STATE *);
 typedef handler_f *handler_t;
@@ -141,8 +139,8 @@ static void qp_decode_line (char *dest, char *src, ssize_t * l, int last)
 
 static void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
 {
-  char line[STRING];
-  char decline[2 * STRING];
+  char line[10 * STRING];
+  char decline[20 * STRING];
   ssize_t l = 0;
   ssize_t linelen;               /* number of input bytes in `line' */
   ssize_t l3;
@@ -184,6 +182,7 @@ static void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
     /* decode and do character set conversion */
     qp_decode_line (decline + l, line, &l3, last);
     l += l3;
+    assert (l < sizeof(decline));
     mutt_convert_to_state (cd, decline, &l, s);
   }
 
@@ -191,85 +190,6 @@ static void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
   state_reset_prefix (s);
 }
 
-void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
-{
-  char buf[5];
-  int c1, c2, c3, c4, ch, cr = 0, i;
-  char bufi[BUFI_SIZE];
-  ssize_t l = 0;
-
-  buf[4] = 0;
-
-  if (istext)
-    state_set_prefix (s);
-
-  while (len > 0) {
-    for (i = 0; i < 4 && len > 0; len--) {
-      if ((ch = fgetc (s->fpin)) == EOF)
-        break;
-      if (base64val(ch) >= 0 || ch == '=')
-        buf[i++] = ch;
-    }
-    if (i != 4) {
-      break;
-    }
-
-    c1 = base64val(buf[0]);
-    c2 = base64val(buf[1]);
-    ch = (c1 << 2) | (c2 >> 4);
-
-    if (cr && ch != '\n')
-      bufi[l++] = '\r';
-
-    cr = 0;
-
-    if (istext && ch == '\r')
-      cr = 1;
-    else
-      bufi[l++] = ch;
-
-    if (buf[2] == '=')
-      break;
-    c3 = base64val(buf[2]);
-    ch = ((c2 & 0xf) << 4) | (c3 >> 2);
-
-    if (cr && ch != '\n')
-      bufi[l++] = '\r';
-
-    cr = 0;
-
-    if (istext && ch == '\r')
-      cr = 1;
-    else
-      bufi[l++] = ch;
-
-    if (buf[3] == '=')
-      break;
-    c4 = base64val(buf[3]);
-    ch = ((c3 & 0x3) << 6) | c4;
-
-    if (cr && ch != '\n')
-      bufi[l++] = '\r';
-    cr = 0;
-
-    if (istext && ch == '\r')
-      cr = 1;
-    else
-      bufi[l++] = ch;
-
-    if (l + 8 >= ssizeof (bufi))
-      mutt_convert_to_state (cd, bufi, &l, s);
-  }
-
-  if (cr)
-    bufi[l++] = '\r';
-
-  mutt_convert_to_state (cd, bufi, &l, s);
-  mutt_convert_to_state (cd, 0, 0, s);
-
-  state_reset_prefix (s);
-}
-
 static unsigned char decode_byte(int ch)
 {
     return ch == 96 ? 0 : ch - 32;
@@ -641,9 +561,8 @@ static int text_enriched_handler (BODY * a, STATE * s)
 
   p_clear(&stte, 1);
   stte.s = s;
-  stte.WrapMargin =
-    ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
-                                            72) ? (COLS - 4) : 72);
+  stte.WrapMargin = ((s->flags & M_DISPLAY) ? (getmaxx(main_w) - 4) :
+                     ((getmaxx(main_w) - 4) < 72) ? (getmaxx(main_w) - 4) : 72);
   stte.line_max = stte.WrapMargin * 4;
   stte.line = p_new(char, stte.line_max + 1);
   stte.param = p_new(char, STRING);
@@ -741,7 +660,7 @@ static int text_enriched_handler (BODY * a, STATE * s)
   p_delete(&(stte.line));
   p_delete(&(stte.param));
 
-  return (0);
+  return 0;
 }
 
 #define TXTHTML     1
@@ -897,7 +816,7 @@ static int alternative_handler (BODY * a, STATE * s)
   if (mustfree)
     body_list_wipe(&a);
 
-  return (rc);
+  return rc;
 }
 
 /* handles message/rfc822 body parts */
@@ -938,7 +857,7 @@ static int message_handler (BODY * a, STATE * s)
       a->encoding == ENCUUENCODED)
     body_list_wipe(&b);
 
-  return (rc);
+  return rc;
 }
 
 /* returns 1 if decoding the attachment will produce output */
@@ -948,11 +867,11 @@ int mutt_can_decode (BODY * a)
 
   snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
   if (mutt_is_autoview (a, type))
-    return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
+    return rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW);
   else if (a->type == TYPETEXT)
-    return (1);
+    return 1;
   else if (a->type == TYPEMESSAGE)
-    return (1);
+    return 1;
   else if (a->type == TYPEMULTIPART) {
     BODY *p;
     int tok = mime_which_token(a->subtype, -1);
@@ -962,17 +881,17 @@ int mutt_can_decode (BODY * a)
 
     for (p = a->parts; p; p = p->next) {
       if (mutt_can_decode (p))
-        return (1);
+        return 1;
     }
   }
   else if (a->type == TYPEAPPLICATION) {
     if (mutt_is_application_pgp(a))
-      return (1);
+      return 1;
     if (mutt_is_application_smime (a))
-      return (1);
+      return 1;
   }
 
-  return (0);
+  return 0;
 }
 
 static int multipart_handler (BODY * a, STATE * s)
@@ -1029,8 +948,7 @@ static int multipart_handler (BODY * a, STATE * s)
     }
     rc = mutt_body_handler (p, s);
     state_putc ('\n', s);
-    if (rc || ((s->flags & M_REPLYING)
-        && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
+    if (rc)
       break;
   }
 
@@ -1038,7 +956,7 @@ static int multipart_handler (BODY * a, STATE * s)
       a->encoding == ENCUUENCODED)
     body_list_wipe(&b);
 
-  return (rc);
+  return rc;
 }
 
 static int autoview_handler (BODY * a, STATE * s)
@@ -1081,7 +999,7 @@ static int autoview_handler (BODY * a, STATE * s)
     if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
       mutt_perror ("fopen");
       rfc1524_entry_delete(&entry);
-      return (-1);
+      return -1;
     }
 
     mutt_copy_bytes (s->fpin, fpin, a->length);
@@ -1155,7 +1073,7 @@ static int autoview_handler (BODY * a, STATE * s)
       mutt_clear_error ();
   }
   rfc1524_entry_delete(&entry);
-  return (rc);
+  return rc;
 }
 
 static int external_body_handler (BODY * b, STATE * s)
@@ -1170,7 +1088,7 @@ static int external_body_handler (BODY * b, STATE * s)
       state_mark_attach (s);
       state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
     }
-    return (-1);
+    return -1;
   }
 
   expiration = parameter_getval(b->parameter, "expiration");
@@ -1237,7 +1155,86 @@ static int external_body_handler (BODY * b, STATE * s)
                      CH_DECODE, NULL);
     }
   }
-  return (0);
+  return 0;
+}
+
+static void mutt_decode_base64(STATE *s, long len, int istext, iconv_t cd)
+{
+  char buf[5];
+  int c1, c2, c3, c4, ch, cr = 0, i;
+  char bufi[BUFI_SIZE];
+  ssize_t l = 0;
+
+  buf[4] = 0;
+
+  if (istext)
+    state_set_prefix (s);
+
+  while (len > 0) {
+    for (i = 0; i < 4 && len > 0; len--) {
+      if ((ch = fgetc (s->fpin)) == EOF)
+        break;
+      if (base64val(ch) >= 0 || ch == '=')
+        buf[i++] = ch;
+    }
+    if (i != 4) {
+      break;
+    }
+
+    c1 = base64val(buf[0]);
+    c2 = base64val(buf[1]);
+    ch = (c1 << 2) | (c2 >> 4);
+
+    if (cr && ch != '\n')
+      bufi[l++] = '\r';
+
+    cr = 0;
+
+    if (istext && ch == '\r')
+      cr = 1;
+    else
+      bufi[l++] = ch;
+
+    if (buf[2] == '=')
+      break;
+    c3 = base64val(buf[2]);
+    ch = ((c2 & 0xf) << 4) | (c3 >> 2);
+
+    if (cr && ch != '\n')
+      bufi[l++] = '\r';
+
+    cr = 0;
+
+    if (istext && ch == '\r')
+      cr = 1;
+    else
+      bufi[l++] = ch;
+
+    if (buf[3] == '=')
+      break;
+    c4 = base64val(buf[3]);
+    ch = ((c3 & 0x3) << 6) | c4;
+
+    if (cr && ch != '\n')
+      bufi[l++] = '\r';
+    cr = 0;
+
+    if (istext && ch == '\r')
+      cr = 1;
+    else
+      bufi[l++] = ch;
+
+    if (l + 8 >= ssizeof (bufi))
+      mutt_convert_to_state (cd, bufi, &l, s);
+  }
+
+  if (cr)
+    bufi[l++] = '\r';
+
+  mutt_convert_to_state (cd, bufi, &l, s);
+  mutt_convert_to_state (cd, 0, 0, s);
+
+  state_reset_prefix (s);
 }
 
 void mutt_decode_attachment (BODY * b, STATE * s)
@@ -1252,12 +1249,12 @@ void mutt_decode_attachment (BODY * b, STATE * s)
       const char *charset = parameter_getval(b->parameter, "charset");
 
       if (!charset)
-        charset = charset_getfirst(MCharset.assumed_charset);
-      if (charset && MCharset.charset)
-        cd = mutt_iconv_open (MCharset.charset, charset, M_ICONV_HOOK_FROM);
+        charset = charset_getfirst(mod_cset.assumed_charset);
+      if (charset && mod_cset.charset)
+        cd = mutt_iconv_open (mod_cset.charset, charset, M_ICONV_HOOK_FROM);
     } else {
       if (b->file_charset)
-        cd = mutt_iconv_open (MCharset.charset, b->file_charset, M_ICONV_HOOK_FROM);
+        cd = mutt_iconv_open (mod_cset.charset, b->file_charset, M_ICONV_HOOK_FROM);
     }
   }
 
@@ -1385,7 +1382,7 @@ int mutt_body_handler (BODY * b, STATE * s)
       if (!plaintext) {
         /* decode to a tempfile, saving the original destination */
         fp = s->fpout;
-        s->fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
+        s->fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
         if (!s->fpout) {
           mutt_error _("Unable to open temporary file!");
           goto bail;
@@ -1440,7 +1437,6 @@ int mutt_body_handler (BODY * b, STATE * s)
         s->fpin = fp;
       }
     }
-    s->flags |= M_FIRSTDONE;
   }
   else if (s->flags & M_DISPLAY) {
     state_mark_attach (s);
@@ -1457,7 +1453,6 @@ int mutt_body_handler (BODY * b, STATE * s)
   }
 
 bail:
-  s->flags = oflags | (s->flags & M_FIRSTDONE);
-
-  return (rc);
+  s->flags = oflags;
+  return rc;
 }