-round expiration times to full seconds
[oweals/gnunet.git] / src / fs / gnunet-pseudonym.c
index 742111c4e6384ccd7bacec0dc3fe99002e7361b8..a9c6be6236c220a3fc4b91b2c57d1409af228b8d 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009, 2010 Christian Grothoff (and other contributing authors)
+     (C) 2001-2013 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
  */
 #include "platform.h"
 #include "gnunet_fs_service.h"
+#include "gnunet_identity_service.h"
 
-/**
- * -C option
- */
-static char *create_ns;
 
 /**
- * -D option
+ * -A option
  */
-static char *delete_ns;
+static char *advertise_ns;
 
 /**
  * -k option
  */
 static struct GNUNET_FS_Uri *ksk_uri;
 
-/**
- * -l option.
- */
-static int print_local_only;
-
 /**
  * -m option.
  */
@@ -76,17 +68,37 @@ static char *rating_change;
 static struct GNUNET_FS_Handle *h;
 
 /**
- * Namespace we are looking at.
+ * Our configuration.
  */
-static struct GNUNET_FS_Namespace *ns;
+static const struct GNUNET_CONFIGURATION_Handle *cfg;
 
 /**
- * Our configuration.
+ * Handle to identity service.
  */
-static const struct GNUNET_CONFIGURATION_Handle *cfg;
+static struct GNUNET_IDENTITY_Handle *identity;
+
+/**
+ * Target namespace.
+ */
+static struct GNUNET_IDENTITY_Ego *namespace;
 
+/**
+ * URI to advertise.
+ */
+static struct GNUNET_FS_Uri *sks_uri;
+
+/**
+ * Global return value.
+ */ 
 static int ret;
 
+
+/**
+ * Progress callback given to FS.
+ * 
+ * @param cls unused
+ * @param info progress information, unused
+ */ 
 static void *
 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
 {
@@ -94,92 +106,132 @@ progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
 }
 
 
-static void
-ns_printer (void *cls, const char *name, const GNUNET_HashCode * id)
-{
-  struct GNUNET_CRYPTO_HashAsciiEncoded enc;
-
-  GNUNET_CRYPTO_hash_to_enc (id, &enc);
-  fprintf (stdout, "%s (%s)\n", name, (const char *) &enc);
-}
-
-
+/**
+ * Output information about a pseudonym.
+ *
+ * @param cls closure
+ * @param pseudonym hash code of public key of pseudonym
+ * @param name name of the pseudonym (might be NULL)
+ * @param unique_name unique name of the pseudonym (might be NULL)
+ * @param md meta data known about the pseudonym
+ * @param rating the local rating of the pseudonym
+ * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
+ */
 static int
-pseudo_printer (void *cls,
-                const GNUNET_HashCode *
-                pseudonym,
-                const struct GNUNET_CONTAINER_MetaData *md, int rating)
+pseudo_printer (void *cls, 
+               const struct GNUNET_CRYPTO_EccPublicKey *pseudonym,
+                const char *name, 
+               const char *unique_name,
+                const struct GNUNET_CONTAINER_MetaData *md, 
+               int32_t rating)
 {
   char *id;
-
-  id = GNUNET_PSEUDONYM_id_to_name (cfg, pseudonym);
-  if (id == NULL)
+  char *unique_id;
+  int getinfo_result;
+
+  /* While we get a name from the caller, it might be NULL.
+   * GNUNET_FS_pseudonym_get_info () never returns NULL.
+   */
+  getinfo_result = GNUNET_FS_pseudonym_get_info (cfg, pseudonym,
+                                                NULL, NULL, &id, NULL);
+  if (GNUNET_OK != getinfo_result)
   {
     GNUNET_break (0);
     return GNUNET_OK;
   }
-  fprintf (stdout, "%s (%d):\n", id, rating);
-  GNUNET_CONTAINER_meta_data_iterate (md, &EXTRACTOR_meta_data_print, stdout);
-  fprintf (stdout, "\n");
+  unique_id = GNUNET_FS_pseudonym_name_uniquify (cfg, pseudonym, id, NULL);
   GNUNET_free (id);
+  FPRINTF (stdout, 
+          "%s (%d):\n", 
+          unique_id, rating);
+  GNUNET_CONTAINER_meta_data_iterate (md, &EXTRACTOR_meta_data_print, stdout);
+  FPRINTF (stdout, 
+          "%s",
+          "\n");
+  GNUNET_free (unique_id);
   return GNUNET_OK;
 }
 
 
+/**
+ * Function called once advertising is finished.
+ * 
+ * @param cls closure (NULL)
+ * @param uri the advertised URI
+ * @param emsg error message, NULL on success
+ */
 static void
-post_advertising (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
+post_advertising (void *cls,
+                 const struct GNUNET_FS_Uri *uri, 
+                 const char *emsg)
 {
-  GNUNET_HashCode nsid;
-  char *set;
-  int delta;
-
   if (emsg != NULL)
   {
-    fprintf (stderr, "%s", emsg);
+    FPRINTF (stderr, "%s", emsg);
     ret = 1;
   }
-  if (ns != NULL)
-  {
-    if (GNUNET_OK != GNUNET_FS_namespace_delete (ns, GNUNET_NO))
-      ret = 1;
-  }
-  if (NULL != rating_change)
+  GNUNET_FS_stop (h);
+  GNUNET_IDENTITY_disconnect (identity);
+}
+
+
+/**
+ * Function called by identity service with known pseudonyms.
+ *
+ * @param cls closure, NULL
+ * @param ego ego handle
+ * @param ctx context for application to store data for this ego
+ *                 (during the lifetime of this process, initially NULL)
+ * @param name name assigned by the user for this ego,
+ *                   NULL if the user just deleted the ego and it
+ *                   must thus no longer be used
+ */
+static void
+identity_cb (void *cls, 
+            struct GNUNET_IDENTITY_Ego *ego,
+            void **ctx,
+            const char *name)
+{
+  char *emsg;
+  struct GNUNET_CRYPTO_EccPublicKey pub;
+
+  if (NULL == ego) 
   {
-    set = rating_change;
-    while ((*set != '\0') && (*set != ':'))
-      set++;
-    if (*set != ':')
+    if (NULL == namespace)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  _("Invalid argument `%s'\n"), rating_change);
+      ret = 1;
+      return;
     }
-    else
+    if (NULL != root_identifier)
     {
-      *set = '\0';
-      delta = strtol (&set[1], NULL,    /* no error handling yet */
-                      10);
-      if (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, rating_change, &nsid))
-      {
-        (void) GNUNET_PSEUDONYM_rank (cfg, &nsid, delta);
-      }
-      else
+      if (NULL == ksk_uri)
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                    _("Namespace `%s' unknown.\n"), rating_change);
+       emsg = NULL;
+       ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/namespace", &emsg);
+       GNUNET_assert (NULL == emsg);
       }
+      GNUNET_IDENTITY_ego_get_public_key (namespace,
+                                         &pub);
+      sks_uri = GNUNET_FS_uri_sks_create (&pub,
+                                         root_identifier);
+      GNUNET_FS_publish_ksk (h, ksk_uri, adv_metadata, sks_uri,
+                            &bo,
+                            GNUNET_FS_PUBLISH_OPTION_NONE,
+                            &post_advertising, NULL);
+      GNUNET_FS_uri_destroy (sks_uri);
+      return;
     }
-    GNUNET_free (rating_change);
-    rating_change = NULL;
-  }
-  if (0 != print_local_only)
-  {
-    GNUNET_FS_namespace_list (h, &ns_printer, NULL);
-  }
-  else if (0 == no_remote_printing)
-  {
-    GNUNET_PSEUDONYM_list_all (cfg, &pseudo_printer, NULL);
+    else
+    {
+      if (NULL != ksk_uri)
+       FPRINTF (stderr, _("Option `%s' ignored\n"), "-k");
+      if (NULL != advertise_ns)
+       FPRINTF (stderr, _("Option `%s' ignored\n"), "-A");
+    }
+    return;
   }
-  GNUNET_FS_stop (h);
+  if (0 == strcmp (name, advertise_ns))
+    namespace = ego;
 }
 
 
@@ -192,73 +244,53 @@ post_advertising (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
  * @param c configuration
  */
 static void
-run (void *cls,
-     char *const *args,
-     const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
+run (void *cls, char *const *args, const char *cfgfile,
+     const struct GNUNET_CONFIGURATION_Handle *c)
 {
-  char *emsg;
+  struct GNUNET_CRYPTO_EccPublicKey nsid;
+  char *set;
+  int delta;
 
   cfg = c;
-  h = GNUNET_FS_start (cfg,
-                       "gnunet-pseudonym",
-                       &progress_cb,
-                       NULL, GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
-  if (NULL != delete_ns)
-  {
-    ns = GNUNET_FS_namespace_create (h, delete_ns);
-    if (ns == NULL)
-    {
-      ret = 1;
-    }
-    else
-    {
-      if (GNUNET_OK != GNUNET_FS_namespace_delete (ns, GNUNET_YES))
-        ret = 1;
-      ns = NULL;
-    }
-  }
-  if (NULL != create_ns)
+  h = GNUNET_FS_start (cfg, "gnunet-pseudonym", &progress_cb, NULL,
+                       GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
+  if (NULL != rating_change)
   {
-    ns = GNUNET_FS_namespace_create (h, create_ns);
-    if (ns == NULL)
+    set = rating_change;
+    while ((*set != '\0') && (*set != ':'))
+      set++;
+    if (*set != ':')
     {
-      ret = 1;
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+                 _("Invalid argument `%s'\n"),
+                  rating_change);
     }
     else
     {
-      if (NULL != root_identifier)
+      *set = '\0';
+      delta = strtol (&set[1], NULL,    /* no error handling yet */
+                      10);
+      if (GNUNET_OK == GNUNET_FS_pseudonym_name_to_id (cfg, rating_change, &nsid))
       {
-        if (ksk_uri == NULL)
-        {
-          emsg = NULL;
-          ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/namespace", &emsg);
-          GNUNET_assert (NULL == emsg);
-        }
-        GNUNET_FS_namespace_advertise (h,
-                                       ksk_uri,
-                                       ns,
-                                       adv_metadata,
-                                       &bo,
-                                       root_identifier,
-                                       &post_advertising, NULL);
-        return;
+        (void) GNUNET_FS_pseudonym_rank (cfg, &nsid, delta);
       }
       else
       {
-        if (ksk_uri != NULL)
-          fprintf (stderr, _("Option `%s' ignored\n"), "-k");
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+                    ("Namespace `%s' unknown. Make sure you specify its numeric suffix, if any.\n"),
+                    rating_change);
       }
     }
+    GNUNET_free (rating_change);
+    rating_change = NULL;
   }
-  else
-  {
-    if (root_identifier != NULL)
-      fprintf (stderr, _("Option `%s' ignored\n"), "-r");
-    if (ksk_uri != NULL)
-      fprintf (stderr, _("Option `%s' ignored\n"), "-k");
-  }
+  if (0 == no_remote_printing)
+    GNUNET_FS_pseudonym_list_all (cfg, &pseudo_printer, NULL);
 
-  post_advertising (NULL, NULL, NULL);
+  if (NULL != advertise_ns)
+    identity = GNUNET_IDENTITY_connect (cfg, 
+                                       &identity_cb, 
+                                       NULL);
 }
 
 
@@ -277,23 +309,16 @@ main (int argc, char *const *argv)
     {'a', "anonymity", "LEVEL",
      gettext_noop ("set the desired LEVEL of sender-anonymity"),
      1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
-    {'C', "create", "NAME",
-     gettext_noop ("create or advertise namespace NAME"),
-     1, &GNUNET_GETOPT_set_string, &create_ns},
-    {'D', "delete", "NAME",
-     gettext_noop ("delete namespace NAME "),
-     1, &GNUNET_GETOPT_set_string, &delete_ns},
+    {'A', "advertise", "NAME",
+     gettext_noop ("advertise namespace NAME"),
+     1, &GNUNET_GETOPT_set_string, &advertise_ns},
     {'k', "keyword", "VALUE",
-     gettext_noop
-     ("add an additional keyword for the advertisment"
-      " (this option can be specified multiple times)"),
+     gettext_noop ("add an additional keyword for the advertisment"
+                   " (this option can be specified multiple times)"),
      1, &GNUNET_FS_getopt_set_keywords, &ksk_uri},
     {'m', "meta", "TYPE:VALUE",
      gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
      1, &GNUNET_FS_getopt_set_metadata, &adv_metadata},
-    {'o', "only-local", NULL,
-     gettext_noop ("print names of local namespaces"),
-     0, &GNUNET_GETOPT_set_one, &print_local_only},
     {'p', "priority", "PRIORITY",
      gettext_noop ("use the given PRIORITY for the advertisments"),
      1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
@@ -313,10 +338,16 @@ main (int argc, char *const *argv)
   };
   bo.expiration_time =
       GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
-  return (GNUNET_OK ==
-          GNUNET_PROGRAM_run (argc, argv, "gnunet-pseudonym [OPTIONS]",
-                              gettext_noop ("Manage GNUnet pseudonyms."),
-                              options, &run, NULL)) ? ret : 1;
+
+  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
+    return 2;
+
+  ret = (GNUNET_OK ==
+        GNUNET_PROGRAM_run (argc, argv, "gnunet-pseudonym [OPTIONS]",
+                            gettext_noop ("Manage GNUnet pseudonyms."),
+                            options, &run, NULL)) ? ret : 1;
+  GNUNET_free ((void*) argv);
+  return ret;
 }
 
 /* end of gnunet-pseudonym.c */