-fixing main FS build, updating man page of gnunet-pseudonym
[oweals/gnunet.git] / src / fs / gnunet-pseudonym.c
index b8bd12b35dcbe7897b8224c222d7b2a37e59c2ae..c87679641db1fd893edbe41492d29bbc4d29c0f4 100644 (file)
@@ -1,10 +1,10 @@
 /*
      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
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
  */
 #include "platform.h"
 #include "gnunet_fs_service.h"
+#include "gnunet_identity_service.h"
 
-/**
- * -a optiton.
- */
-static unsigned int anonymity;
-
-/**
- * -A option.
- */
-static int start_automate;
 
 /**
- * -e option
+ * -A option
  */
-static int stop_automate;
-
-/**
- * -C option
- */
-static char *create_ns;
-
-/**
- * -D 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.
  */
 static struct GNUNET_CONTAINER_MetaData *adv_metadata;
 
 /**
- * -p option.
+ * Our block options (-p, -r, -a).
  */
-static unsigned int priority = 365;
+static struct GNUNET_FS_BlockOptions bo = { {0LL}, 1, 365, 1 };
 
 /**
  * -q option given.
  */
-static int no_remote_printing; 
+static int no_remote_printing;
 
 /**
  * -r option.
@@ -91,148 +68,170 @@ 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;
 
-static int ret;
+/**
+ * Target namespace.
+ */
+static struct GNUNET_IDENTITY_Ego *namespace;
 
-static void* 
-progress_cb (void *cls,
-            const struct GNUNET_FS_ProgressInfo *info)
-{
-  return NULL;
-}
+/**
+ * URI to advertise.
+ */
+static struct GNUNET_FS_Uri *sks_uri;
+
+/**
+ * Global return value.
+ */ 
+static int ret;
 
 
-static void
-ns_printer (void *cls,
-           const char *name,
-           const GNUNET_HashCode *id)
+/**
+ * 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)
 {
-  struct GNUNET_CRYPTO_HashAsciiEncoded enc;
-
-  GNUNET_CRYPTO_hash_to_enc (id, &enc);
-  fprintf (stdout, 
-          "%s (%s)\n",
-          name,
-          (const char*) &enc);
+  return NULL;
 }
 
 
+/**
+ * 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)
-    {
-      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");
+  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;
+  }
+  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 struct GNUNET_FS_Uri *uri, 
                  const char *emsg)
 {
-  GNUNET_HashCode nsid;
-  char *set;
-  int delta;
-
   if (emsg != NULL)
+  {
+    FPRINTF (stderr, "%s", emsg);
+    ret = 1;
+  }
+  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 ego_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) 
+  {
+    if (NULL == namespace)
     {
-      fprintf (stderr, "%s", emsg);
       ret = 1;
+      return;
     }
-  if (ns != NULL)
+    if (NULL != root_identifier)
     {
-      if (GNUNET_OK !=
-         GNUNET_FS_namespace_delete (ns,
-                                     GNUNET_NO))
-       ret = 1;
+      if (NULL == ksk_uri)
+      {
+       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;
     }
-  if (0 != stop_automate)
+    else
     {
-      GNUNET_break (0); // FIXME: not implemented
+      if (NULL != ksk_uri)
+       FPRINTF (stderr, _("Option `%s' ignored\n"), "-k");
+      if (NULL != advertise_ns)
+       FPRINTF (stderr, _("Option `%s' ignored\n"), "-A");
     }
-  if (0 != start_automate)
-    {
-      GNUNET_break (0); // FIXME: not implemented
-    }
-  if (NULL != rating_change)
-    {
-      set = rating_change;
-      while ((*set != '\0') && (*set != ':'))
-        set++;
-      if (*set != ':')
-       {
-         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                     _("Invalid argument `%s'\n"),
-                     rating_change);
-       }
-      else
-       {
-         *set = '\0';
-         delta = strtol (&set[1], NULL, /* no error handling yet */
-                          10);
-         if (GNUNET_OK ==
-             GNUNET_PSEUDONYM_name_to_id (cfg,
-                                          rating_change,
-                                          &nsid))
-           {
-             GNUNET_PSEUDONYM_rank (cfg,
-                                    &nsid,
-                                    delta);
-             
-           }
-         else
-           {
-             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                         _("Namespace `%s' unknown.\n"),
-                         rating_change);
-           }
-       }
-      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);
-    }
-  GNUNET_FS_stop (h);
+    return;
+  }
+  if (0 == strcmp (name, advertise_ns))
+    namespace = ego;
 }
 
 
@@ -240,135 +239,64 @@ post_advertising (void *cls,
  * Main function that will be run by the scheduler.
  *
  * @param cls closure
- * @param sched the scheduler to use
  * @param args remaining command-line arguments
  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  * @param c configuration
  */
 static void
-run (void *cls,
-     struct GNUNET_SCHEDULER_Handle *sched,
-     char *const *args,
-     const char *cfgfile,
+run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
-  struct GNUNET_FS_Uri *ns_uri;
-  struct GNUNET_TIME_Absolute expiration;
-  char *emsg;
+  struct GNUNET_CRYPTO_EccPublicKey nsid;
+  char *set;
+  int delta;
 
   cfg = c;
-  h = GNUNET_FS_start (sched,
-                      cfg,
-                      "gnunet-pseudonym",
-                      &progress_cb,
-                      NULL,
-                      GNUNET_FS_FLAGS_NONE);
-  if (NULL != delete_ns)
+  h = GNUNET_FS_start (cfg, "gnunet-pseudonym", &progress_cb, NULL,
+                       GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
+  if (NULL != rating_change)
+  {
+    set = rating_change;
+    while ((*set != '\0') && (*set != ':'))
+      set++;
+    if (*set != ':')
     {
-      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;
-       }
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+                 _("Invalid argument `%s'\n"),
+                  rating_change);
     }
-  if (NULL != create_ns)
+    else
     {
-      ns = GNUNET_FS_namespace_create (h, create_ns);
-      if (ns == NULL)
-       {
-         ret = 1;
-       }
+      *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))
+      {
+        (void) GNUNET_FS_pseudonym_rank (cfg, &nsid, delta);
+      }
       else
-       {
-         if (NULL != root_identifier)
-           {
-             emsg = NULL;
-             ns_uri = GNUNET_FS_uri_sks_create (ns, root_identifier, &emsg);
-             GNUNET_assert (emsg == NULL);
-             expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS);
-             if (ksk_uri == NULL)
-               {
-                 emsg = NULL;
-                 ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/namespace", &emsg);
-                 GNUNET_assert (NULL == emsg);
-               }
-             GNUNET_FS_publish_ksk (h,
-                                    ksk_uri,
-                                    adv_metadata,
-                                    ns_uri,
-                                    expiration,
-                                    anonymity,
-                                    priority,
-                                    GNUNET_FS_PUBLISH_OPTION_NONE,
-                                    &post_advertising,
-                                    NULL);
-             return;
-           }
-       }
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+                    ("Namespace `%s' unknown. Make sure you specify its numeric suffix, if any.\n"),
+                    rating_change);
+      }
     }
-  post_advertising (NULL, NULL, NULL);
+    GNUNET_free (rating_change);
+    rating_change = NULL;
+  }
+  if (0 == no_remote_printing)
+    GNUNET_FS_pseudonym_list_all (cfg, &pseudo_printer, NULL);
+
+  if (NULL != advertise_ns)
+    identity = GNUNET_IDENTITY_connect (cfg, 
+                                       &identity_cb, 
+                                       NULL);
 }
 
-/**
- * gnunet-pseudonym command line options
- */
-static struct GNUNET_GETOPT_CommandLineOption options[] = {
-  {'a', "anonymity", "LEVEL",
-   gettext_noop ("set the desired LEVEL of sender-anonymity"),
-   1, &GNUNET_GETOPT_set_uint, &anonymity},
-  {'A', "automate", NULL,
-   gettext_noop ("start a collection"),
-   0, &GNUNET_GETOPT_set_one, &start_automate},
-  {'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},
-  {'e', "end", NULL,
-   gettext_noop ("end current collection"),
-   0, &GNUNET_GETOPT_set_one, &stop_automate},
-  {'k', "keyword", "VALUE",
-  gettext_noop
-   ("add an additional keyword for the advertisment"
-    " (this option can be specified multiple times)"),
-   1, &GNUNET_FS_getopt_set_keywords, &ksk_uri},
-  {'l', "local-only", NULL,
-   gettext_noop ("print names of local namespaces"),
-   0, &GNUNET_GETOPT_set_one, &print_local_only},
-  {'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},
-  {'p', "priority", "PRIORITY",
-   gettext_noop ("use the given PRIORITY for the advertisments"),
-   1, &GNUNET_GETOPT_set_uint, &priority},
-  {'q', "quiet", NULL,
-   gettext_noop ("do not print names of remote namespaces"),
-   0, &GNUNET_GETOPT_set_one, &no_remote_printing},
-  {'r', "root", "ID",
-   gettext_noop
-   ("specify ID of the root of the namespace"),
-   1, &GNUNET_GETOPT_set_string, &root_identifier},
-  {'s', "set-rating", "ID:VALUE",
-   gettext_noop
-   ("change rating of namespace ID by VALUE"),
-   1, &GNUNET_GETOPT_set_string, &rating_change},
-  GNUNET_GETOPT_OPTION_END
-};
-
 
 /**
- * The main function to inspect GNUnet directories.
+ * The main function to manipulate GNUnet pseudonyms (and publish
+ * to namespaces).
  *
  * @param argc number of arguments from the command line
  * @param argv command line arguments
@@ -377,13 +305,49 @@ static struct GNUNET_GETOPT_CommandLineOption options[] = {
 int
 main (int argc, char *const *argv)
 {
-  return (GNUNET_OK ==
-          GNUNET_PROGRAM_run (argc,
-                              argv,
-                              "gnunet-pseudonym",
-                              gettext_noop
-                              ("Manage GNUnet pseudonyms."),
-                              options, &run, NULL)) ? ret : 1;
+  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
+    {'a', "anonymity", "LEVEL",
+     gettext_noop ("set the desired LEVEL of sender-anonymity"),
+     1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
+    {'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)"),
+     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},
+    {'p', "priority", "PRIORITY",
+     gettext_noop ("use the given PRIORITY for the advertisments"),
+     1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
+    {'q', "quiet", NULL,
+     gettext_noop ("do not print names of remote namespaces"),
+     0, &GNUNET_GETOPT_set_one, &no_remote_printing},
+    {'r', "replication", "LEVEL",
+     gettext_noop ("set the desired replication LEVEL"),
+     1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
+    {'R', "root", "ID",
+     gettext_noop ("specify ID of the root of the namespace"),
+     1, &GNUNET_GETOPT_set_string, &root_identifier},
+    {'s', "set-rating", "ID:VALUE",
+     gettext_noop ("change rating of namespace ID by VALUE"),
+     1, &GNUNET_GETOPT_set_string, &rating_change},
+    GNUNET_GETOPT_OPTION_END
+  };
+  bo.expiration_time =
+      GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
+
+  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 */