Use gpgme interface for error handling to avoid linking with gpg-error.
[gpgme.git] / src / engine-uiserver.c
index cdcf683e99a129a4c18458b407bdf60bbc2f7821..92f75363dfb949d8ed391bef44c6a1b127b310c8 100644 (file)
 
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
+#ifdef HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
 #include <assert.h>
-#include <unistd.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
 #include <locale.h>
 #include <fcntl.h> /* FIXME */
 #include <errno.h>
@@ -1011,26 +1015,26 @@ set_recipients (engine_uiserver_t uiserver, gpgme_key_t recp[])
   char *line;
   int linelen;
   int invalid_recipients = 0;
-  int i = 0;
+  int i;
 
   linelen = 10 + 40 + 1;       /* "RECIPIENT " + guess + '\0'.  */
   line = malloc (10 + 40 + 1);
   if (!line)
     return gpg_error_from_errno (errno);
   strcpy (line, "RECIPIENT ");
-  while (!err && recp[i])
+  for (i=0; !err && recp[i]; i++)
     {
-      char *fpr;
+      char *uid;
       int newlen;
 
-      if (!recp[i]->subkeys || !recp[i]->subkeys->fpr)
+      /* We use only the first user ID of the key.  */
+      if (!recp[i]->uids || !(uid=recp[i]->uids->uid) || !*uid)
        {
          invalid_recipients++;
          continue;
        }
-      fpr = recp[i]->subkeys->fpr;
 
-      newlen = 11 + strlen (fpr);
+      newlen = 11 + strlen (uid);
       if (linelen < newlen)
        {
          char *newline = realloc (line, newlen);
@@ -1043,11 +1047,12 @@ set_recipients (engine_uiserver_t uiserver, gpgme_key_t recp[])
          line = newline;
          linelen = newlen;
        }
-      strcpy (&line[10], fpr);
+      /* FIXME: need to do proper escaping  */
+      strcpy (&line[10], uid);
 
       err = uiserver_assuan_simple_command (ctx, line, uiserver->status.fnc,
-                                        uiserver->status.fnc_value);
-      /* FIXME: This requires more work.  */
+                                            uiserver->status.fnc_value);
+      /* FIXME: This might requires more work.  */
       if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY)
        invalid_recipients++;
       else if (err)
@@ -1055,7 +1060,6 @@ set_recipients (engine_uiserver_t uiserver, gpgme_key_t recp[])
          free (line);
          return err;
        }
-      i++;
     }
   free (line);
   return gpg_error (invalid_recipients
@@ -1153,6 +1157,7 @@ uiserver_sign (void *engine, gpgme_data_t in, gpgme_data_t out,
   gpgme_error_t err = 0;
   const char *protocol;
   char *cmd;
+  gpgme_key_t key;
 
   if (!uiserver || !in || !out)
     return gpg_error (GPG_ERR_INV_VALUE);
@@ -1169,26 +1174,27 @@ uiserver_sign (void *engine, gpgme_data_t in, gpgme_data_t out,
                (mode == GPGME_SIG_MODE_DETACH) ? " --detached" : "") < 0)
     return gpg_error_from_errno (errno);
 
-  {
-    gpgme_key_t key = gpgme_signers_enum (ctx, 0);
-    const char *s = NULL;
-
-    if (key && key->uids)
-      s = key->uids->email;
-    
-    if (s && strlen (s) < 80)
-      {
-       char buf[100];
-       
-       strcpy (stpcpy (buf, "SENDER --info "), s);
-       err = uiserver_assuan_simple_command (uiserver->assuan_ctx, buf,
-                                             uiserver->status.fnc,
-                                             uiserver->status.fnc_value);
-      }
-    else
-      err = gpg_error (GPG_ERR_INV_VALUE);
-    gpgme_key_unref (key);
-    if (err) 
+  key = gpgme_signers_enum (ctx, 0);
+  if (key)
+    {
+      const char *s = NULL;
+      
+      if (key && key->uids)
+        s = key->uids->email;
+      
+      if (s && strlen (s) < 80)
+        {
+          char buf[100];
+          
+          strcpy (stpcpy (buf, "SENDER --info "), s);
+          err = uiserver_assuan_simple_command (uiserver->assuan_ctx, buf,
+                                                uiserver->status.fnc,
+                                                uiserver->status.fnc_value);
+        }
+      else
+        err = gpg_error (GPG_ERR_INV_VALUE);
+      gpgme_key_unref (key);
+      if (err) 
       {
        free (cmd);
        return err;