begin to move rfc2047 into lib-mime.
[apps/madmutt.git] / pgpkey.c
index b6e0795..a14ec77 100644 (file)
--- a/pgpkey.c
+++ b/pgpkey.c
 # include "config.h"
 #endif
 
+#include <lib-lib/mem.h>
+#include <lib-lib/str.h>
+#include <lib-lib/ascii.h>
+#include <lib-lib/macros.h>
+#include <lib-lib/file.h>
+
+#include <lib-mime/mime.h>
+
 #include "mutt.h"
+#include "enter.h"
+#include "recvattach.h"
 #include "mutt_curses.h"
 #include "mutt_menu.h"
-#include "mime.h"
 #include "pgp.h"
 #include "pager.h"
 #include "sort.h"
 
+#include "lib/debug.h"
+
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
@@ -273,10 +284,10 @@ static int _pgp_compare_address (const void *a, const void *b)
   pgp_uid_t **s = (pgp_uid_t **) a;
   pgp_uid_t **t = (pgp_uid_t **) b;
 
-  if ((r = mutt_strcasecmp ((*s)->addr, (*t)->addr)))
+  if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
     return r > 0;
   else
-    return (mutt_strcasecmp (_pgp_keyid ((*s)->parent),
+    return (m_strcasecmp(_pgp_keyid ((*s)->parent),
                              _pgp_keyid ((*t)->parent)) > 0);
 }
 
@@ -295,11 +306,11 @@ static int _pgp_compare_keyid (const void *a, const void *b)
   pgp_uid_t **s = (pgp_uid_t **) a;
   pgp_uid_t **t = (pgp_uid_t **) b;
 
-  if ((r = mutt_strcasecmp (_pgp_keyid ((*s)->parent),
+  if ((r = m_strcasecmp(_pgp_keyid ((*s)->parent),
                             _pgp_keyid ((*t)->parent))))
     return r > 0;
   else
-    return (mutt_strcasecmp ((*s)->addr, (*t)->addr)) > 0;
+    return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
 }
 
 static int pgp_compare_keyid (const void *a, const void *b)
@@ -316,7 +327,7 @@ static int _pgp_compare_date (const void *a, const void *b)
 
   if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
     return r > 0;
-  return (mutt_strcasecmp ((*s)->addr, (*t)->addr)) > 0;
+  return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
 }
 
 static int pgp_compare_date (const void *a, const void *b)
@@ -341,9 +352,9 @@ static int _pgp_compare_trust (const void *a, const void *b)
     return r < 0;
   if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
     return r < 0;
-  if ((r = mutt_strcasecmp ((*s)->addr, (*t)->addr)))
+  if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
     return r > 0;
-  return (mutt_strcasecmp (_pgp_keyid ((*s)->parent),
+  return (m_strcasecmp(_pgp_keyid ((*s)->parent),
                            _pgp_keyid ((*t)->parent))) > 0;
 }
 
@@ -390,7 +401,7 @@ static int pgp_id_is_valid (pgp_uid_t * uid)
 
 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
 
-static int pgp_id_matches_addr (ADDRESS * addr, ADDRESS * u_addr,
+static int pgp_id_matches_addr (address_t * addr, address_t * u_addr,
                                 pgp_uid_t * uid)
 {
   int rv = 0;
@@ -402,17 +413,17 @@ static int pgp_id_matches_addr (ADDRESS * addr, ADDRESS * u_addr,
     rv |= PGP_KV_STRONGID;
 
   if (addr->mailbox && u_addr->mailbox
-      && mutt_strcasecmp (addr->mailbox, u_addr->mailbox) == 0)
+      && m_strcasecmp(addr->mailbox, u_addr->mailbox) == 0)
     rv |= PGP_KV_ADDR;
 
   if (addr->personal && u_addr->personal
-      && mutt_strcasecmp (addr->personal, u_addr->personal) == 0)
+      && m_strcasecmp(addr->personal, u_addr->personal) == 0)
     rv |= PGP_KV_STRING;
 
   return rv;
 }
 
-static pgp_key_t pgp_select_key (pgp_key_t keys, ADDRESS * p, const char *s)
+static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
 {
   int keymax;
   pgp_uid_t **KeyTable;
@@ -445,7 +456,7 @@ static pgp_key_t pgp_select_key (pgp_key_t keys, ADDRESS * p, const char *s)
 
       if (i == keymax) {
         keymax += 5;
-        safe_realloc (&KeyTable, sizeof (pgp_uid_t *) * keymax);
+        p_realloc(&KeyTable, keymax);
       }
 
       KeyTable[i++] = a;
@@ -514,13 +525,13 @@ static pgp_key_t pgp_select_key (pgp_key_t keys, ADDRESS * p, const char *s)
 
       mutt_mktemp (tempfile);
       if ((devnull = fopen ("/dev/null", "w")) == NULL) {       /* __FOPEN_CHECKED__ */
-        mutt_perror _("Can't open /dev/null");
+        mutt_perror (_("Can't open /dev/null"));
 
         break;
       }
       if ((fp = safe_fopen (tempfile, "w")) == NULL) {
         fclose (devnull);
-        mutt_perror _("Can't create temporary file");
+        mutt_perror (_("Can't create temporary file"));
 
         break;
       }
@@ -534,7 +545,7 @@ static pgp_key_t pgp_select_key (pgp_key_t keys, ADDRESS * p, const char *s)
       if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
                                            fileno (fp), fileno (devnull),
                                            tmpbuf)) == -1) {
-        mutt_perror _("Can't create filter");
+        mutt_perror (_("Can't create filter"));
 
         unlink (tempfile);
         fclose (fp);
@@ -573,7 +584,7 @@ static pgp_key_t pgp_select_key (pgp_key_t keys, ADDRESS * p, const char *s)
       if (option (OPTPGPCHECKTRUST) &&
           (!pgp_id_is_valid (KeyTable[menu->current])
            || !pgp_id_is_strong (KeyTable[menu->current]))) {
-        char *s = "";
+        const char *s = "";
         char buff[LONG_STRING];
 
         if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
@@ -617,7 +628,7 @@ static pgp_key_t pgp_select_key (pgp_key_t keys, ADDRESS * p, const char *s)
   }
 
   mutt_menuDestroy (&menu);
-  FREE (&KeyTable);
+  p_delete(&KeyTable);
 
   set_option (OPTNEEDREDRAW);
 
@@ -637,27 +648,27 @@ pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
   if (whatfor) {
 
     for (l = id_defaults; l; l = l->next)
-      if (!mutt_strcasecmp (whatfor, l->what)) {
-        strfcpy (resp, NONULL (l->dflt), sizeof (resp));
+      if (!m_strcasecmp(whatfor, l->what)) {
+        m_strcpy(resp, sizeof(resp), NONULL(l->dflt));
         break;
       }
   }
 
 
-  FOREVER {
+  for (;;) {
     resp[0] = 0;
     if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
       return NULL;
 
     if (whatfor) {
       if (l)
-        mutt_str_replace (&l->dflt, resp);
+        str_replace (&l->dflt, resp);
       else {
-        l = safe_malloc (sizeof (struct pgp_cache));
+        l = p_new(struct pgp_cache, 1);
         l->next = id_defaults;
         id_defaults = l;
-        l->what = safe_strdup (whatfor);
-        l->dflt = safe_strdup (resp);
+        l->what = m_strdup(whatfor);
+        l->dflt = m_strdup(resp);
       }
     }
 
@@ -699,13 +710,13 @@ BODY *pgp_make_key_attachment (char *tempf)
   }
 
   if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL) {
-    mutt_perror _("Can't create temporary file");
+    mutt_perror (_("Can't create temporary file"));
 
     return NULL;
   }
 
   if ((devnull = fopen ("/dev/null", "w")) == NULL) {   /* __FOPEN_CHECKED__ */
-    mutt_perror _("Can't open /dev/null");
+    mutt_perror (_("Can't open /dev/null"));
 
     fclose (tempfp);
     if (tempf == tempfb)
@@ -719,7 +730,7 @@ BODY *pgp_make_key_attachment (char *tempf)
   if ((thepid =
        pgp_invoke_export (NULL, NULL, NULL, -1,
                           fileno (tempfp), fileno (devnull), tmp)) == -1) {
-    mutt_perror _("Can't create filter");
+    mutt_perror (_("Can't create filter"));
 
     unlink (tempf);
     fclose (tempfp);
@@ -733,13 +744,13 @@ BODY *pgp_make_key_attachment (char *tempf)
   fclose (devnull);
 
   att = mutt_new_body ();
-  att->filename = safe_strdup (tempf);
+  att->filename = m_strdup(tempf);
   att->unlink = 1;
   att->use_disp = 0;
   att->type = TYPEAPPLICATION;
-  att->subtype = safe_strdup ("pgp-keys");
+  att->subtype = m_strdup("pgp-keys");
   snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
-  att->description = safe_strdup (buff);
+  att->description = m_strdup(buff);
   mutt_update_encoding (att);
 
   stat (tempf, &sb);
@@ -753,16 +764,16 @@ static LIST *pgp_add_string_to_hints (LIST * hints, const char *str)
   char *scratch;
   char *t;
 
-  if ((scratch = safe_strdup (str)) == NULL)
+  if ((scratch = m_strdup(str)) == NULL)
     return hints;
 
   for (t = strtok (scratch, " ,.:\"()<>\n"); t;
        t = strtok (NULL, " ,.:\"()<>\n")) {
-    if (strlen (t) > 3)
+    if (m_strlen(t) > 3)
       hints = mutt_add_list (hints, t);
   }
 
-  FREE (&scratch);
+  p_delete(&scratch);
   return hints;
 }
 
@@ -775,9 +786,9 @@ static pgp_key_t *pgp_get_lastp (pgp_key_t p)
   return NULL;
 }
 
-pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
+pgp_key_t pgp_getkeybyaddr (address_t * a, short abilities, pgp_ring_t keyring)
 {
-  ADDRESS *r, *p;
+  address_t *r, *p;
   LIST *hints = NULL;
 
   int weak = 0;
@@ -807,17 +818,15 @@ pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
   if (!keys)
     return NULL;
 
-  dprint (5, (debugfile, "pgp_getkeybyaddr: looking for %s <%s>.",
-              a->personal, a->mailbox));
-
+  debug_print (5, ("looking for %s <%s>\n", a->personal, a->mailbox));
 
   for (k = keys; k; k = kn) {
     kn = k->next;
 
-    dprint (5, (debugfile, "  looking at key: %s\n", pgp_keyid (k)));
+    debug_print (5, ("  looking at key: %s\n", pgp_keyid (k)));
 
     if (abilities && !(k->flags & abilities)) {
-      dprint (5, (debugfile, "  insufficient abilities: Has %x, want %x\n",
+      debug_print (5, ("  insufficient abilities: Has %x, want %x\n",
                   k->flags, abilities));
       continue;
     }
@@ -852,7 +861,7 @@ pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
           this_key_has_weak = 1;
       }
 
-      rfc822_free_address (&r);
+      address_delete (&r);
     }
 
     if (match && !this_key_has_strong && this_key_has_invalid)
@@ -924,17 +933,14 @@ pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
     match = 0;
 
     for (a = k->address; a; a = a->next) {
-      dprint (5,
-              (debugfile,
-               "pgp_getkeybystr: matching \"%s\" against key %s, \"%s\": ", p,
-               pgp_keyid (k), a->addr));
-      if (!*p || mutt_strcasecmp (p, pgp_keyid (k)) == 0
-          || (!mutt_strncasecmp (p, "0x", 2)
-              && !mutt_strcasecmp (p + 2, pgp_keyid (k)))
-          || (option (OPTPGPLONGIDS) && !mutt_strncasecmp (p, "0x", 2)
-              && !mutt_strcasecmp (p + 2, k->keyid + 8))
-          || mutt_stristr (a->addr, p)) {
-        dprint (5, (debugfile, "match.\n"));
+      debug_print (5, ("matching \"%s\" against key %s, \"%s\":\n", p, pgp_keyid (k), a->addr));
+      if (!*p || m_strcasecmp(p, pgp_keyid (k)) == 0
+          || (!m_strncasecmp(p, "0x", 2)
+              && !m_strcasecmp(p + 2, pgp_keyid (k)))
+          || (option (OPTPGPLONGIDS) && !m_strncasecmp(p, "0x", 2)
+              && !m_strcasecmp(p + 2, k->keyid + 8))
+          || str_isstr (a->addr, p)) {
+        debug_print (5, ("match.\n"));
         match = 1;
         break;
       }