let m_strformat accept NULL formats.
[apps/madmutt.git] / lib-crypt / pgpkey.c
index d9e2de3..87dee7d 100644 (file)
@@ -8,39 +8,20 @@
  * please see the file GPL in the top level source directory.
  */
 
-#if HAVE_CONFIG_H
-# 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-lib/debug.h>
+#include <lib-lib/lib-lib.h>
 
 #include <lib-mime/mime.h>
+#include <lib-sys/unix.h>
 
 #include <lib-ui/curses.h>
 #include <lib-ui/enter.h>
 #include <lib-ui/menu.h>
 
-#include "mutt.h"
 #include "recvattach.h"
 #include "pgp.h"
 #include "pager.h"
 #include "sort.h"
 
-
-#include <string.h>
-#include <ctype.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/wait.h>
-
-#include <locale.h>
-
 struct pgp_cache {
   char *what;
   char *dflt;
@@ -111,18 +92,15 @@ static pgp_key_t pgp_principal_key (pgp_key_t key)
  */
 
 typedef struct pgp_entry {
-  size_t num;
+  ssize_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)
+static const char *
+pgp_entry_fmt (char *dest, ssize_t destlen, char op,
+               const char *src, const char *prefix,
+               const char *ifstr, const char *elstr,
+               anytype data, format_flag flags)
 {
   char fmt[16];
   pgp_entry_t *entry;
@@ -131,7 +109,7 @@ static const char *pgp_entry_fmt (char *dest,
   int kflags = 0;
   int optional = (flags & M_FORMAT_OPTIONAL);
 
-  entry = (pgp_entry_t *) data;
+  entry = data.ptr;
   uid = entry->uid;
   key = uid->parent;
   pkey = pgp_principal_key (key);
@@ -147,10 +125,10 @@ static const char *pgp_entry_fmt (char *dest,
 
     {
       const char *cp;
-      char buf2[SHORT_STRING], *p;
+      char buf2[STRING], *p;
       int do_locales;
       struct tm *tm;
-      size_t len;
+      ssize_t len;
 
       p = dest;
 
@@ -257,14 +235,13 @@ static const char *pgp_entry_fmt (char *dest,
     *dest = '\0';
   }
 
-  if (optional)
-    mutt_FormatString (dest, destlen, ifstring, mutt_attach_fmt, data, 0);
-  else if (flags & M_FORMAT_OPTIONAL)
-    mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
-  return (src);
+  if (flags & M_FORMAT_OPTIONAL)
+    m_strformat(dest, destlen, optional ? ifstr : elstr,
+                mutt_attach_fmt, data, 0);
+  return src;
 }
 
-static void pgp_entry (char *s, size_t l, MUTTMENU * menu, int num)
+static void pgp_entry (char *s, ssize_t l, MUTTMENU * menu, int num)
 {
   pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
   pgp_entry_t entry;
@@ -272,8 +249,8 @@ static void pgp_entry (char *s, size_t l, MUTTMENU * menu, int num)
   entry.uid = KeyTable[num];
   entry.num = num + 1;
 
-  mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
-                     (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
+  m_strformat(s, l, PgpEntryFormat, pgp_entry_fmt, &entry,
+              option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0);
 }
 
 static int _pgp_compare_address (const void *a, const void *b)
@@ -428,7 +405,7 @@ static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
   pgp_uid_t **KeyTable;
   MUTTMENU *menu;
   int i, done = 0;
-  char helpstr[SHORT_STRING], buf[LONG_STRING], tmpbuf[STRING];
+  char helpstr[STRING], buf[LONG_STRING], tmpbuf[STRING];
   char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
   FILE *fp, *devnull;
   pid_t thepid;
@@ -488,15 +465,15 @@ static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
 
   helpstr[0] = 0;
   mutt_make_help (buf, sizeof (buf), _("Exit  "), MENU_PGP, OP_EXIT);
-  strcat (helpstr, buf);        /* __STRCAT_CHECKED__ */
+  m_strcat(helpstr, sizeof(helpstr), buf);
   mutt_make_help (buf, sizeof (buf), _("Select  "), MENU_PGP,
                   OP_GENERIC_SELECT_ENTRY);
-  strcat (helpstr, buf);        /* __STRCAT_CHECKED__ */
+  m_strcat(helpstr, sizeof(helpstr), buf);
   mutt_make_help (buf, sizeof (buf), _("Check key  "), MENU_PGP,
                   OP_VERIFY_KEY);
-  strcat (helpstr, buf);        /* __STRCAT_CHECKED__ */
+  m_strcat(helpstr, sizeof(helpstr), buf);
   mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
-  strcat (helpstr, buf);        /* __STRCAT_CHECKED__ */
+  m_strcat(helpstr, sizeof(helpstr), buf);
 
   menu = mutt_new_menu ();
   menu->max = i;
@@ -522,14 +499,15 @@ static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
 
     case OP_VERIFY_KEY:
 
-      mutt_mktemp (tempfile);
-      if ((devnull = fopen ("/dev/null", "w")) == NULL) {       /* __FOPEN_CHECKED__ */
+      if ((devnull = fopen("/dev/null", "w")) == NULL) {
         mutt_perror (_("Can't open /dev/null"));
 
         break;
       }
-      if ((fp = safe_fopen (tempfile, "w")) == NULL) {
-        fclose (devnull);
+
+      fp = m_tempfile (tempfile, sizeof(tempfile), NONULL(Tempdir), NULL);
+      if (!fp) {
+        m_fclose(&devnull);
         mutt_perror (_("Can't create temporary file"));
 
         break;
@@ -547,13 +525,13 @@ static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
         mutt_perror (_("Can't create filter"));
 
         unlink (tempfile);
-        fclose (fp);
-        fclose (devnull);
+        m_fclose(&fp);
+        m_fclose(&devnull);
       }
 
       mutt_wait_filter (thepid);
-      fclose (fp);
-      fclose (devnull);
+      m_fclose(&fp);
+      m_fclose(&devnull);
       mutt_clear_error ();
       snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
                 pgp_keyid (pgp_principal_key
@@ -583,26 +561,26 @@ static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
       if (option (OPTPGPCHECKTRUST) &&
           (!pgp_id_is_valid (KeyTable[menu->current])
            || !pgp_id_is_strong (KeyTable[menu->current]))) {
-        const char *s = "";
+        const char *q = "";
         char buff[LONG_STRING];
 
         if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
-          s = N_("ID is expired/disabled/revoked.");
+          q = N_("ID is expired/disabled/revoked.");
         else
           switch (KeyTable[menu->current]->trust & 0x03) {
           case 0:
-            s = N_("ID has undefined validity.");
+            q = N_("ID has undefined validity.");
             break;
           case 1:
-            s = N_("ID is not valid.");
+            q = N_("ID is not valid.");
             break;
           case 2:
-            s = N_("ID is only marginally valid.");
+            q = N_("ID is only marginally valid.");
             break;
           }
 
         snprintf (buff, sizeof (buff),
-                  _("%s Do you really want to use the key?"), _(s));
+                  _("%s Do you really want to use the key?"), _(q));
 
         if (mutt_yesorno (buff, M_NO) != M_YES) {
           mutt_clear_error ();
@@ -638,7 +616,7 @@ pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
                            short abilities, pgp_ring_t keyring)
 {
   pgp_key_t key;
-  char resp[SHORT_STRING];
+  char resp[STRING];
   struct pgp_cache *l = NULL;
 
   mutt_clear_error ();
@@ -704,20 +682,21 @@ BODY *pgp_make_key_attachment (char *tempf)
   pgp_free_key (&key);
 
   if (!tempf) {
-    mutt_mktemp (tempfb);
+    tempfp = m_tempfile (tempfb, sizeof(tempfb), NONULL(Tempdir), NULL);
     tempf = tempfb;
+  } else {
+    tempfp = safe_fopen(tempf, "a");
   }
 
-  if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL) {
+  if (!tempfp) {
     mutt_perror (_("Can't create temporary file"));
-
     return NULL;
   }
 
-  if ((devnull = fopen ("/dev/null", "w")) == NULL) {   /* __FOPEN_CHECKED__ */
+  if ((devnull = fopen("/dev/null", "w")) == NULL) {
     mutt_perror (_("Can't open /dev/null"));
 
-    fclose (tempfp);
+    m_fclose(&tempfp);
     if (tempf == tempfb)
       unlink (tempf);
     return NULL;
@@ -732,17 +711,17 @@ BODY *pgp_make_key_attachment (char *tempf)
     mutt_perror (_("Can't create filter"));
 
     unlink (tempf);
-    fclose (tempfp);
-    fclose (devnull);
+    m_fclose(&tempfp);
+    m_fclose(&devnull);
     return NULL;
   }
 
   mutt_wait_filter (thepid);
 
-  fclose (tempfp);
-  fclose (devnull);
+  m_fclose(&tempfp);
+  m_fclose(&devnull);
 
-  att = mutt_new_body ();
+  att = body_new();
   att->filename = m_strdup(tempf);
   att->unlink = 1;
   att->use_disp = 0;
@@ -817,16 +796,10 @@ pgp_key_t pgp_getkeybyaddr (address_t * a, short abilities, pgp_ring_t keyring)
   if (!keys)
     return NULL;
 
-  debug_print (5, ("looking for %s <%s>\n", a->personal, a->mailbox));
-
   for (k = keys; k; k = kn) {
     kn = k->next;
 
-    debug_print (5, ("  looking at key: %s\n", pgp_keyid (k)));
-
     if (abilities && !(k->flags & abilities)) {
-      debug_print (5, ("  insufficient abilities: Has %x, want %x\n",
-                  k->flags, abilities));
       continue;
     }
 
@@ -904,7 +877,7 @@ pgp_key_t pgp_getkeybyaddr (address_t * a, short abilities, pgp_ring_t keyring)
   return NULL;
 }
 
-pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
+pgp_key_t pgp_getkeybystr (const char *p, short abilities, pgp_ring_t keyring)
 {
   string_list_t *hints = NULL;
   pgp_key_t keys;
@@ -932,14 +905,12 @@ pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
     match = 0;
 
     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 || 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))
           || m_stristr(a->addr, p)) {
-        debug_print (5, ("match.\n"));
         match = 1;
         break;
       }