more constness.
[apps/madmutt.git] / pgpkey.c
index aa68642..3804817 100644 (file)
--- a/pgpkey.c
+++ b/pgpkey.c
@@ -1,23 +1,11 @@
 /*
+ * Copyright notice from original mutt:
  * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
  * Copyright (c) 1998,1999 Thomas Roessler <roessler@does-not-exist.org>
- * 
- *     This program is free software; you can redistribute it
- *     and/or modify it under the terms of the GNU General Public
- *     License as published by the Free Software Foundation; either
- *     version 2 of the License, or (at your option) any later
- *     version.
- * 
- *     This program is distributed in the hope that it will be
- *     useful, but WITHOUT ANY WARRANTY; without even the implied
- *     warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- *     PURPOSE.  See the GNU General Public License for more
- *     details.
- * 
- *     You should have received a copy of the GNU General Public
- *     License along with this program; if not, write to the Free
- *     Software Foundation, Inc., 59 Temple Place - Suite 330,
- *     Boston, MA  02111, USA.
+ *
+ * This file is part of mutt-ng, see http://www.muttng.org/.
+ * It's licensed under the GNU General Public License,
+ * please see the file GPL in the top level source directory.
  */
 
 #if HAVE_CONFIG_H
@@ -25,6 +13,9 @@
 #endif
 
 #include "mutt.h"
+#include "enter.h"
+#include "ascii.h"
+#include "recvattach.h"
 #include "mutt_curses.h"
 #include "mutt_menu.h"
 #include "mime.h"
 #include "pager.h"
 #include "sort.h"
 
+#include "lib/mem.h"
+#include "lib/intl.h"
+#include "lib/str.h"
+#include "lib/debug.h"
+
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
@@ -43,8 +39,7 @@
 
 #ifdef CRYPT_BACKEND_CLASSIC_PGP
 
-struct pgp_cache
-{
+struct pgp_cache {
   char *what;
   char *dflt;
   struct pgp_cache *next;
@@ -87,7 +82,7 @@ static char pgp_flags (int flags)
     return 'd';
   else if (flags & KEYFLAG_CRITICAL)
     return 'c';
-  else 
+  else
     return ' ';
 }
 
@@ -113,21 +108,19 @@ static pgp_key_t pgp_principal_key (pgp_key_t key)
  * %[...] date of key using strftime(3)
  */
 
-typedef struct pgp_entry
-{
+typedef struct pgp_entry {
   size_t num;
   pgp_uid_t *uid;
 } pgp_entry_t;
 
 static const char *pgp_entry_fmt (char *dest,
-                                 size_t destlen,
-                                 char op,
-                                 const char *src,
-                                 const char *prefix,
-                                 const char *ifstring,
-                                 const char *elsestring,
-                                 unsigned long data,
-                                 format_flag flags)
+                                  size_t destlen,
+                                  char op,
+                                  const char *src,
+                                  const char *prefix,
+                                  const char *ifstring,
+                                  const char *elsestring,
+                                  unsigned long data, format_flag flags)
 {
   char fmt[16];
   pgp_entry_t *entry;
@@ -137,143 +130,129 @@ static const char *pgp_entry_fmt (char *dest,
   int optional = (flags & M_FORMAT_OPTIONAL);
 
   entry = (pgp_entry_t *) data;
-  uid   = entry->uid;
-  key   = uid->parent;
-  pkey  = pgp_principal_key (key);
+  uid = entry->uid;
+  key = uid->parent;
+  pkey = pgp_principal_key (key);
 
   if (isupper ((unsigned char) op))
     key = pkey;
 
   kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
     | uid->flags;
-  
-  switch (ascii_tolower (op))
-  {
-    case '[':
-
-      {
-       const char *cp;
-       char buf2[SHORT_STRING], *p;
-       int do_locales;
-       struct tm *tm;
-       size_t len;
-
-       p = dest;
-
-       cp = src;
-       if (*cp == '!')
-       {
-         do_locales = 0;
-         cp++;
-       }
-       else
-         do_locales = 1;
-
-       len = destlen - 1;
-       while (len > 0 && *cp != ']')
-       {
-         if (*cp == '%')
-         {
-           cp++;
-           if (len >= 2)
-           {
-             *p++ = '%';
-             *p++ = *cp;
-             len -= 2;
-           }
-           else
-             break; /* not enough space */
-           cp++;
-         }
-         else
-         {
-           *p++ = *cp++;
-           len--;
-         }
-       }
-       *p = 0;
-
-       if (do_locales && Locale)
-         setlocale (LC_TIME, Locale);
-
-       tm = localtime (&key->gen_time);
-
-       strftime (buf2, sizeof (buf2), dest, tm);
-
-       if (do_locales)
-         setlocale (LC_TIME, "C");
-
-       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
-       snprintf (dest, destlen, fmt, buf2);
-       if (len > 0)
-         src = cp + 1;
-      }
-      break;
-    case 'n':
-      if (!optional)
-      {
-       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
-       snprintf (dest, destlen, fmt, entry->num);
-      }
-      break;
-    case 'k':
-      if (!optional)
-      {
-       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
-       snprintf (dest, destlen, fmt, _pgp_keyid (key));
-      }
-      break;
-    case 'u':
-      if (!optional)
-      {
-       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
-       snprintf (dest, destlen, fmt, uid->addr);
-      }
-      break;
-    case 'a':
-      if (!optional)
-      {
-       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
-       snprintf (dest, destlen, fmt, key->algorithm);
-      }
-      break;
-    case 'l':
-      if (!optional)
-      {
-       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
-       snprintf (dest, destlen, fmt, key->keylen);
-      }
-      break;
-    case 'f':
-      if (!optional)
-      {
-       snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
-       snprintf (dest, destlen, fmt, pgp_flags (kflags));
-      }
-      else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
-        optional = 0;
-      break;
-    case 'c':
-      if (!optional)
-      {
-       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
-       snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
+
+  switch (ascii_tolower (op)) {
+  case '[':
+
+    {
+      const char *cp;
+      char buf2[SHORT_STRING], *p;
+      int do_locales;
+      struct tm *tm;
+      size_t len;
+
+      p = dest;
+
+      cp = src;
+      if (*cp == '!') {
+        do_locales = 0;
+        cp++;
       }
-      else if (!(kflags & (KEYFLAG_ABILITIES)))
-        optional = 0;
-      break;
-    case 't':
-      if (!optional)
-      {
-       snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
-       snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
+      else
+        do_locales = 1;
+
+      len = destlen - 1;
+      while (len > 0 && *cp != ']') {
+        if (*cp == '%') {
+          cp++;
+          if (len >= 2) {
+            *p++ = '%';
+            *p++ = *cp;
+            len -= 2;
+          }
+          else
+            break;              /* not enough space */
+          cp++;
+        }
+        else {
+          *p++ = *cp++;
+          len--;
+        }
       }
-      else if (!(uid->trust & 0x03))
-        /* undefined trust */
-        optional = 0;
-      break;
-    default:
-      *dest = '\0';
+      *p = 0;
+
+      if (do_locales && Locale)
+        setlocale (LC_TIME, Locale);
+
+      tm = localtime (&key->gen_time);
+
+      strftime (buf2, sizeof (buf2), dest, tm);
+
+      if (do_locales)
+        setlocale (LC_TIME, "C");
+
+      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
+      snprintf (dest, destlen, fmt, buf2);
+      if (len > 0)
+        src = cp + 1;
+    }
+    break;
+  case 'n':
+    if (!optional) {
+      snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
+      snprintf (dest, destlen, fmt, entry->num);
+    }
+    break;
+  case 'k':
+    if (!optional) {
+      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
+      snprintf (dest, destlen, fmt, _pgp_keyid (key));
+    }
+    break;
+  case 'u':
+    if (!optional) {
+      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
+      snprintf (dest, destlen, fmt, uid->addr);
+    }
+    break;
+  case 'a':
+    if (!optional) {
+      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
+      snprintf (dest, destlen, fmt, key->algorithm);
+    }
+    break;
+  case 'l':
+    if (!optional) {
+      snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
+      snprintf (dest, destlen, fmt, key->keylen);
+    }
+    break;
+  case 'f':
+    if (!optional) {
+      snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
+      snprintf (dest, destlen, fmt, pgp_flags (kflags));
+    }
+    else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
+      optional = 0;
+    break;
+  case 'c':
+    if (!optional) {
+      snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
+      snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
+    }
+    else if (!(kflags & (KEYFLAG_ABILITIES)))
+      optional = 0;
+    break;
+  case 't':
+    if (!optional) {
+      snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
+      snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
+    }
+    else if (!(uid->trust & 0x03))
+      /* undefined trust */
+      optional = 0;
+    break;
+  default:
+    *dest = '\0';
   }
 
   if (optional)
@@ -282,17 +261,17 @@ static const char *pgp_entry_fmt (char *dest,
     mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
   return (src);
 }
-      
+
 static void pgp_entry (char *s, size_t l, MUTTMENU * menu, int num)
 {
   pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
   pgp_entry_t entry;
-  
+
   entry.uid = KeyTable[num];
   entry.num = num + 1;
 
-  mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt, 
-                    (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
+  mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
+                     (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
 }
 
 static int _pgp_compare_address (const void *a, const void *b)
@@ -302,17 +281,17 @@ 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 = str_casecmp ((*s)->addr, (*t)->addr)))
     return r > 0;
   else
-    return (mutt_strcasecmp (_pgp_keyid ((*s)->parent),
-                            _pgp_keyid ((*t)->parent)) > 0);
+    return (str_casecmp (_pgp_keyid ((*s)->parent),
+                             _pgp_keyid ((*t)->parent)) > 0);
 }
 
 static int pgp_compare_address (const void *a, const void *b)
 {
   return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
-                                      : _pgp_compare_address (a, b));
+          : _pgp_compare_address (a, b));
 }
 
 
@@ -324,17 +303,17 @@ 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), 
-                           _pgp_keyid ((*t)->parent))))
+  if ((r = str_casecmp (_pgp_keyid ((*s)->parent),
+                            _pgp_keyid ((*t)->parent))))
     return r > 0;
   else
-    return (mutt_strcasecmp ((*s)->addr, (*t)->addr)) > 0;
+    return (str_casecmp ((*s)->addr, (*t)->addr)) > 0;
 }
 
 static int pgp_compare_keyid (const void *a, const void *b)
 {
   return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
-                                      : _pgp_compare_keyid (a, b));
+          : _pgp_compare_keyid (a, b));
 }
 
 static int _pgp_compare_date (const void *a, const void *b)
@@ -345,13 +324,13 @@ 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 (str_casecmp ((*s)->addr, (*t)->addr)) > 0;
 }
 
 static int pgp_compare_date (const void *a, const void *b)
 {
   return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
-                                      : _pgp_compare_date (a, b));
+          : _pgp_compare_date (a, b));
 }
 
 static int _pgp_compare_trust (const void *a, const void *b)
@@ -362,7 +341,7 @@ static int _pgp_compare_trust (const void *a, const void *b)
   pgp_uid_t **t = (pgp_uid_t **) b;
 
   if ((r = (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS))
-           - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
+            - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
     return r > 0;
   if ((r = ((*s)->trust - (*t)->trust)))
     return r < 0;
@@ -370,30 +349,31 @@ 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 = str_casecmp ((*s)->addr, (*t)->addr)))
     return r > 0;
-  return (mutt_strcasecmp (_pgp_keyid ((*s)->parent), 
-                          _pgp_keyid ((*t)->parent))) > 0;
+  return (str_casecmp (_pgp_keyid ((*s)->parent),
+                           _pgp_keyid ((*t)->parent))) > 0;
 }
 
 static int pgp_compare_trust (const void *a, const void *b)
 {
   return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
-                                      : _pgp_compare_trust (a, b));
+          : _pgp_compare_trust (a, b));
 }
 
 static int pgp_key_is_valid (pgp_key_t k)
 {
   pgp_key_t pk = pgp_principal_key (k);
+
   if (k->flags & KEYFLAG_CANTUSE)
     return 0;
   if (pk->flags & KEYFLAG_CANTUSE)
     return 0;
-  
+
   return 1;
 }
 
-static int pgp_id_is_strong (pgp_uid_t *uid)
+static int pgp_id_is_strong (pgp_uid_t * uid)
 {
   if ((uid->trust & 3) < 3)
     return 0;
@@ -401,7 +381,7 @@ static int pgp_id_is_strong (pgp_uid_t *uid)
   return 1;
 }
 
-static int pgp_id_is_valid (pgp_uid_t *uid)
+static int pgp_id_is_valid (pgp_uid_t * uid)
 {
   if (!pgp_key_is_valid (uid->parent))
     return 0;
@@ -418,29 +398,29 @@ 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, pgp_uid_t *uid)
+static int pgp_id_matches_addr (ADDRESS * addr, ADDRESS * u_addr,
+                                pgp_uid_t * uid)
 {
   int rv = 0;
-  
+
   if (pgp_id_is_valid (uid))
     rv |= PGP_KV_VALID;
 
   if (pgp_id_is_strong (uid))
     rv |= PGP_KV_STRONGID;
-  
+
   if (addr->mailbox && u_addr->mailbox
-      && mutt_strcasecmp (addr->mailbox, u_addr->mailbox) == 0)
+      && str_casecmp (addr->mailbox, u_addr->mailbox) == 0)
     rv |= PGP_KV_ADDR;
-    
+
   if (addr->personal && u_addr->personal
-      && mutt_strcasecmp (addr->personal, u_addr->personal) == 0)
+      && str_casecmp (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 * p, const char *s)
 {
   int keymax;
   pgp_uid_t **KeyTable;
@@ -459,67 +439,62 @@ static pgp_key_t pgp_select_key (pgp_key_t keys,
   keymax = 0;
   KeyTable = NULL;
 
-  for (i = 0, kp = keys; kp; kp = kp->next)
-  {
-    if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE))
-    {
+  for (i = 0, kp = keys; kp; kp = kp->next) {
+    if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE)) {
       unusable = 1;
       continue;
     }
 
-    for (a = kp->address; a; a = a->next)
-    {
-      if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE))
-      {
-       unusable = 1;
-       continue;
+    for (a = kp->address; a; a = a->next) {
+      if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE)) {
+        unusable = 1;
+        continue;
       }
-      
-      if (i == keymax)
-      {
-       keymax += 5;
-       safe_realloc (&KeyTable, sizeof (pgp_uid_t *) * keymax);
+
+      if (i == keymax) {
+        keymax += 5;
+        mem_realloc (&KeyTable, sizeof (pgp_uid_t *) * keymax);
       }
-      
+
       KeyTable[i++] = a;
     }
   }
 
-  if (!i && unusable)
-  {
+  if (!i && unusable) {
     mutt_error _("All matching keys are expired, revoked, or disabled.");
+
     mutt_sleep (1);
     return NULL;
   }
 
-  switch (PgpSortKeys & SORT_MASK)
-  {
-    case SORT_DATE:
-      f = pgp_compare_date;
-      break;
-    case SORT_KEYID:
-      f = pgp_compare_keyid;
-      break;
-    case SORT_ADDRESS:
-      f = pgp_compare_address;
-      break;
-    case SORT_TRUST:
-    default:
-      f = pgp_compare_trust;
-      break;
+  switch (PgpSortKeys & SORT_MASK) {
+  case SORT_DATE:
+    f = pgp_compare_date;
+    break;
+  case SORT_KEYID:
+    f = pgp_compare_keyid;
+    break;
+  case SORT_ADDRESS:
+    f = pgp_compare_address;
+    break;
+  case SORT_TRUST:
+  default:
+    f = pgp_compare_trust;
+    break;
   }
   qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
 
   helpstr[0] = 0;
   mutt_make_help (buf, sizeof (buf), _("Exit  "), MENU_PGP, OP_EXIT);
-  strcat (helpstr, buf);       /* __STRCAT_CHECKED__ */
+  strcat (helpstr, buf);        /* __STRCAT_CHECKED__ */
   mutt_make_help (buf, sizeof (buf), _("Select  "), MENU_PGP,
-                 OP_GENERIC_SELECT_ENTRY);
-  strcat (helpstr, buf);       /* __STRCAT_CHECKED__ */
-  mutt_make_help (buf, sizeof (buf), _("Check key  "), MENU_PGP, OP_VERIFY_KEY);
-  strcat (helpstr, buf);       /* __STRCAT_CHECKED__ */
+                  OP_GENERIC_SELECT_ENTRY);
+  strcat (helpstr, buf);        /* __STRCAT_CHECKED__ */
+  mutt_make_help (buf, sizeof (buf), _("Check key  "), MENU_PGP,
+                  OP_VERIFY_KEY);
+  strcat (helpstr, buf);        /* __STRCAT_CHECKED__ */
   mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
-  strcat (helpstr, buf);       /* __STRCAT_CHECKED__ */
+  strcat (helpstr, buf);        /* __STRCAT_CHECKED__ */
 
   menu = mutt_new_menu ();
   menu->max = i;
@@ -532,53 +507,55 @@ static pgp_key_t pgp_select_key (pgp_key_t keys,
     snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
   else
     snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
-    
-  
+
+
   menu->title = buf;
 
   kp = NULL;
 
   mutt_clear_error ();
-  
-  while (!done)
-  {
-    switch (mutt_menuLoop (menu))
-    {
+
+  while (!done) {
+    switch (mutt_menuLoop (menu)) {
 
     case OP_VERIFY_KEY:
 
       mutt_mktemp (tempfile);
-      if ((devnull = fopen ("/dev/null", "w")) == NULL)        /* __FOPEN_CHECKED__ */
-      {
-       mutt_perror _("Can't open /dev/null");
-       break;
+      if ((devnull = fopen ("/dev/null", "w")) == NULL) {       /* __FOPEN_CHECKED__ */
+        mutt_perror (_("Can't open /dev/null"));
+
+        break;
       }
-      if ((fp = safe_fopen (tempfile, "w")) == NULL)
-      {
-       fclose (devnull);
-       mutt_perror _("Can't create temporary file");
-       break;
+      if ((fp = safe_fopen (tempfile, "w")) == NULL) {
+        fclose (devnull);
+        mutt_perror (_("Can't create temporary file"));
+
+        break;
       }
 
       mutt_message _("Invoking PGP...");
 
-      snprintf (tmpbuf, sizeof (tmpbuf), "0x%s", pgp_keyid (pgp_principal_key (KeyTable[menu->current]->parent)));
+      snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
+                pgp_keyid (pgp_principal_key
+                           (KeyTable[menu->current]->parent)));
 
       if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
-                   fileno (fp), fileno (devnull), tmpbuf)) == -1)
-      {
-       mutt_perror _("Can't create filter");
-       unlink (tempfile);
-       fclose (fp);
-       fclose (devnull);
+                                           fileno (fp), fileno (devnull),
+                                           tmpbuf)) == -1) {
+        mutt_perror (_("Can't create filter"));
+
+        unlink (tempfile);
+        fclose (fp);
+        fclose (devnull);
       }
 
       mutt_wait_filter (thepid);
       fclose (fp);
       fclose (devnull);
       mutt_clear_error ();
-      snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"), 
-               pgp_keyid (pgp_principal_key (KeyTable[menu->current]->parent)));
+      snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
+                pgp_keyid (pgp_principal_key
+                           (KeyTable[menu->current]->parent)));
       mutt_do_pager (cmd, tempfile, 0, NULL);
       menu->redraw = REDRAW_FULL;
 
@@ -593,44 +570,42 @@ static pgp_key_t pgp_select_key (pgp_key_t keys,
 
 
       /* XXX make error reporting more verbose */
-      
+
       if (option (OPTPGPCHECKTRUST))
-       if (!pgp_key_is_valid (KeyTable[menu->current]->parent))
-       {
-         mutt_error _("This key can't be used: expired/disabled/revoked.");
-         break;
-       }
-      
+        if (!pgp_key_is_valid (KeyTable[menu->current]->parent)) {
+          mutt_error _("This key can't be used: expired/disabled/revoked.");
+
+          break;
+        }
+
       if (option (OPTPGPCHECKTRUST) &&
-         (!pgp_id_is_valid (KeyTable[menu->current])
-          || !pgp_id_is_strong (KeyTable[menu->current])))
-      {
-       char *s = "";
-       char buff[LONG_STRING];
-       
-       if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
-         s = N_("ID is expired/disabled/revoked.");
-       else switch (KeyTable[menu->current]->trust & 0x03)
-       {
-         case 0:
-           s = N_("ID has undefined validity.");
-           break;
-         case 1:
-           s = N_("ID is not valid.");
-           break;
-         case 2:
-           s = N_("ID is only marginally valid.");
-           break;
-       }
-
-       snprintf (buff, sizeof (buff), _("%s Do you really want to use the key?"),
-                 _(s));
-
-       if (mutt_yesorno (buff, M_NO) != M_YES)
-       {
-         mutt_clear_error ();
-         break;
-       }
+          (!pgp_id_is_valid (KeyTable[menu->current])
+           || !pgp_id_is_strong (KeyTable[menu->current]))) {
+        const char *s = "";
+        char buff[LONG_STRING];
+
+        if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
+          s = N_("ID is expired/disabled/revoked.");
+        else
+          switch (KeyTable[menu->current]->trust & 0x03) {
+          case 0:
+            s = N_("ID has undefined validity.");
+            break;
+          case 1:
+            s = N_("ID is not valid.");
+            break;
+          case 2:
+            s = N_("ID is only marginally valid.");
+            break;
+          }
+
+        snprintf (buff, sizeof (buff),
+                  _("%s Do you really want to use the key?"), _(s));
+
+        if (mutt_yesorno (buff, M_NO) != M_YES) {
+          mutt_clear_error ();
+          break;
+        }
       }
 
 # if 0
@@ -650,10 +625,10 @@ static pgp_key_t pgp_select_key (pgp_key_t keys,
   }
 
   mutt_menuDestroy (&menu);
-  FREE (&KeyTable);
+  mem_free (&KeyTable);
 
   set_option (OPTNEEDREDRAW);
-  
+
   return (kp);
 }
 
@@ -665,37 +640,32 @@ pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
   struct pgp_cache *l = NULL;
 
   mutt_clear_error ();
-  
+
   resp[0] = 0;
-  if (whatfor)
-  {
+  if (whatfor) {
 
     for (l = id_defaults; l; l = l->next)
-      if (!mutt_strcasecmp (whatfor, l->what))
-      {
-       strfcpy (resp, NONULL (l->dflt), sizeof (resp));
-       break;
+      if (!str_casecmp (whatfor, l->what)) {
+        strfcpy (resp, NONULL (l->dflt), sizeof (resp));
+        break;
       }
   }
 
 
-  FOREVER
-  {
+  FOREVER {
     resp[0] = 0;
     if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
       return NULL;
 
-    if (whatfor)
-    {
+    if (whatfor) {
       if (l)
-       mutt_str_replace (&l->dflt, resp);
-      else
-      {
-       l = safe_malloc (sizeof (struct pgp_cache));
-       l->next = id_defaults;
-       id_defaults = l;
-       l->what = safe_strdup (whatfor);
-       l->dflt = safe_strdup (resp);
+        str_replace (&l->dflt, resp);
+      else {
+        l = mem_malloc (sizeof (struct pgp_cache));
+        l->next = id_defaults;
+        id_defaults = l;
+        l->what = str_dup (whatfor);
+        l->dflt = str_dup (resp);
       }
     }
 
@@ -719,30 +689,32 @@ BODY *pgp_make_key_attachment (char *tempf)
   struct stat sb;
   pid_t thepid;
   pgp_key_t key;
+
   unset_option (OPTPGPCHECKTRUST);
 
-  key = pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
+  key =
+    pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
 
-  if (!key)    return NULL;
+  if (!key)
+    return NULL;
 
   snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
   pgp_free_key (&key);
-  
-  if (!tempf)
-  {
+
+  if (!tempf) {
     mutt_mktemp (tempfb);
     tempf = tempfb;
   }
 
-  if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL)
-  {
-    mutt_perror _("Can't create temporary file");
+  if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL) {
+    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");
+  if ((devnull = fopen ("/dev/null", "w")) == NULL) {   /* __FOPEN_CHECKED__ */
+    mutt_perror (_("Can't open /dev/null"));
+
     fclose (tempfp);
     if (tempf == tempfb)
       unlink (tempf);
@@ -751,12 +723,12 @@ BODY *pgp_make_key_attachment (char *tempf)
 
   mutt_message _("Invoking pgp...");
 
-  
-  if ((thepid = 
+
+  if ((thepid =
        pgp_invoke_export (NULL, NULL, NULL, -1,
-                          fileno (tempfp), fileno (devnull), tmp)) == -1)
-  {
-    mutt_perror _("Can't create filter");
+                          fileno (tempfp), fileno (devnull), tmp)) == -1) {
+    mutt_perror (_("Can't create filter"));
+
     unlink (tempf);
     fclose (tempfp);
     fclose (devnull);
@@ -769,13 +741,13 @@ BODY *pgp_make_key_attachment (char *tempf)
   fclose (devnull);
 
   att = mutt_new_body ();
-  att->filename = safe_strdup (tempf);
+  att->filename = str_dup (tempf);
   att->unlink = 1;
   att->use_disp = 0;
   att->type = TYPEAPPLICATION;
-  att->subtype = safe_strdup ("pgp-keys");
+  att->subtype = str_dup ("pgp-keys");
   snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
-  att->description = safe_strdup (buff);
+  att->description = str_dup (buff);
   mutt_update_encoding (att);
 
   stat (tempf, &sb);
@@ -784,22 +756,21 @@ BODY *pgp_make_key_attachment (char *tempf)
   return att;
 }
 
-static LIST *pgp_add_string_to_hints (LIST *hints, const char *str)
+static LIST *pgp_add_string_to_hints (LIST * hints, const char *str)
 {
   char *scratch;
   char *t;
 
-  if ((scratch = safe_strdup (str)) == NULL)
+  if ((scratch = str_dup (str)) == NULL)
     return hints;
 
   for (t = strtok (scratch, " ,.:\"()<>\n"); t;
-                       t = strtok (NULL, " ,.:\"()<>\n"))
-  {
-    if (strlen (t) > 3)
+       t = strtok (NULL, " ,.:\"()<>\n")) {
+    if (str_len (t) > 3)
       hints = mutt_add_list (hints, t);
   }
 
-  FREE (&scratch);
+  mem_free (&scratch);
   return hints;
 }
 
@@ -817,9 +788,9 @@ pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
   ADDRESS *r, *p;
   LIST *hints = NULL;
 
-  int weak    = 0;
+  int weak = 0;
   int invalid = 0;
-  int multi   = 0;
+  int multi = 0;
   int this_key_has_strong;
   int this_key_has_weak;
   int this_key_has_invalid;
@@ -830,7 +801,7 @@ pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
   pgp_key_t matches = NULL;
   pgp_key_t *last = &matches;
   pgp_uid_t *q;
-  
+
   if (a && a->mailbox)
     hints = pgp_add_string_to_hints (hints, a->mailbox);
   if (a && a->personal)
@@ -840,58 +811,51 @@ pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
   keys = pgp_get_candidates (keyring, hints);
 
   mutt_free_list (&hints);
-  
+
   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)
-  {
+  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",
-                 k->flags, abilities));
+    if (abilities && !(k->flags & abilities)) {
+      debug_print (5, ("  insufficient abilities: Has %x, want %x\n",
+                  k->flags, abilities));
       continue;
     }
 
-    this_key_has_weak    = 0;  /* weak but valid match   */
+    this_key_has_weak = 0;      /* weak but valid match   */
     this_key_has_invalid = 0;   /* invalid match          */
-    this_key_has_strong  = 0;  /* strong and valid match */
-    match                = 0;   /* any match             */
+    this_key_has_strong = 0;    /* strong and valid match */
+    match = 0;                  /* any match              */
 
-    for (q = k->address; q; q = q->next)
-    {
+    for (q = k->address; q; q = q->next) {
       r = rfc822_parse_adrlist (NULL, q->addr);
 
-      for (p = r; p; p = p->next)
-      {
-       int validity = pgp_id_matches_addr (a, p, q);
-
-       if (validity & PGP_KV_MATCH)    /* something matches */
-         match = 1;
-
-       /* is this key a strong candidate? */
-       if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID) 
-           && (validity & PGP_KV_ADDR))
-       {
-         if (the_valid_key && the_valid_key != k)
-           multi             = 1;
-         the_valid_key       = k;
-         this_key_has_strong = 1;
-       }
-       else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
-         this_key_has_invalid = 1;
-       else if ((validity & PGP_KV_MATCH) 
-                && (!(validity & PGP_KV_STRONGID) || !(validity & PGP_KV_ADDR)))
-         this_key_has_weak    = 1;
+      for (p = r; p; p = p->next) {
+        int validity = pgp_id_matches_addr (a, p, q);
+
+        if (validity & PGP_KV_MATCH)    /* something matches */
+          match = 1;
+
+        /* is this key a strong candidate? */
+        if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
+            && (validity & PGP_KV_ADDR)) {
+          if (the_valid_key && the_valid_key != k)
+            multi = 1;
+          the_valid_key = k;
+          this_key_has_strong = 1;
+        }
+        else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
+          this_key_has_invalid = 1;
+        else if ((validity & PGP_KV_MATCH)
+                 && (!(validity & PGP_KV_STRONGID)
+                     || !(validity & PGP_KV_ADDR)))
+          this_key_has_weak = 1;
       }
 
       rfc822_free_address (&r);
@@ -901,22 +865,19 @@ pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
       invalid = 1;
     if (match && !this_key_has_strong && this_key_has_weak)
       weak = 1;
-    
-    if (match)
-    {
-      *last  = pgp_principal_key (k);
-      kn     = pgp_remove_key (&keys, *last);
-      last   = pgp_get_lastp (k);
+
+    if (match) {
+      *last = pgp_principal_key (k);
+      kn = pgp_remove_key (&keys, *last);
+      last = pgp_get_lastp (k);
     }
   }
 
   pgp_free_key (&keys);
-  
-  if (matches)
-  {
+
+  if (matches) {
     if (the_valid_key && !multi /* && !weak 
-       && !(invalid && option (OPTPGPSHOWUNUSABLE)) */)
-    {
+                                   && !(invalid && option (OPTPGPSHOWUNUSABLE)) */ ) {
       /*
        * There was precisely one strong match on a valid ID.
        * 
@@ -926,13 +887,12 @@ pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
       pgp_free_key (&matches);
       k = the_valid_key;
     }
-    else 
-    {
+    else {
       /* 
        * Else: Ask the user.
        */
       if ((k = pgp_select_key (matches, a, NULL)))
-       pgp_remove_key (&matches, k);
+        pgp_remove_key (&matches, k);
       pgp_free_key (&matches);
     }
 
@@ -953,54 +913,49 @@ pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
   short match;
 
   mutt_message (_("Looking for keys matching \"%s\"..."), p);
-  
+
   hints = pgp_add_string_to_hints (hints, p);
   keys = pgp_get_candidates (keyring, hints);
   mutt_free_list (&hints);
 
   if (!keys)
     return NULL;
-  
-  
-  for (k = keys; k; k = kn)
-  {
+
+
+  for (k = keys; k; k = kn) {
     kn = k->next;
     if (abilities && !(k->flags & abilities))
       continue;
 
     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"));
-       match = 1;
-       break;
+
+    for (a = k->address; a; a = a->next) {
+      debug_print (5, ("matching \"%s\" against key %s, \"%s\":\n", p, pgp_keyid (k), a->addr));
+      if (!*p || str_casecmp (p, pgp_keyid (k)) == 0
+          || (!str_ncasecmp (p, "0x", 2)
+              && !str_casecmp (p + 2, pgp_keyid (k)))
+          || (option (OPTPGPLONGIDS) && !str_ncasecmp (p, "0x", 2)
+              && !str_casecmp (p + 2, k->keyid + 8))
+          || str_isstr (a->addr, p)) {
+        debug_print (5, ("match.\n"));
+        match = 1;
+        break;
       }
     }
-    
-    if (match)
-    {
+
+    if (match) {
       *last = pgp_principal_key (k);
-      kn    = pgp_remove_key (&keys, *last);
-      last  = pgp_get_lastp (k);
+      kn = pgp_remove_key (&keys, *last);
+      last = pgp_get_lastp (k);
     }
   }
 
   pgp_free_key (&keys);
 
-  if (matches)
-  {
+  if (matches) {
     if ((k = pgp_select_key (matches, NULL, p)))
       pgp_remove_key (&matches, k);
-    
+
     pgp_free_key (&matches);
     return k;
   }