doxygen fix
[oweals/gnunet.git] / src / fs / gnunet-pseudonym.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009, 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file fs/gnunet-pseudonym.c
22  * @brief manage GNUnet namespaces / pseudonyms
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_fs_service.h"
27
28 /**
29  * -C option
30  */
31 static char *create_ns;
32
33 /**
34  * -D option
35  */
36 static char *delete_ns;
37
38 /**
39  * -k option
40  */
41 static struct GNUNET_FS_Uri *ksk_uri;
42
43 /**
44  * -l option.
45  */
46 static int print_local_only;
47
48 /**
49  * -m option.
50  */
51 static struct GNUNET_CONTAINER_MetaData *adv_metadata;
52
53 /**
54  * Our block options (-p, -r, -a).
55  */
56 static struct GNUNET_FS_BlockOptions bo = { {0LL}, 1, 365, 1 };
57
58 /**
59  * -q option given.
60  */
61 static int no_remote_printing;
62
63 /**
64  * -r option.
65  */
66 static char *root_identifier;
67
68 /**
69  * -s option.
70  */
71 static char *rating_change;
72
73 /**
74  * Handle to fs service.
75  */
76 static struct GNUNET_FS_Handle *h;
77
78 /**
79  * Namespace we are looking at.
80  */
81 static struct GNUNET_FS_Namespace *ns;
82
83 /**
84  * Our configuration.
85  */
86 static const struct GNUNET_CONFIGURATION_Handle *cfg;
87
88 static int ret;
89
90 static void *
91 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
92 {
93   return NULL;
94 }
95
96
97 static void
98 ns_printer (void *cls, const char *name, const GNUNET_HashCode * id)
99 {
100   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
101
102   GNUNET_CRYPTO_hash_to_enc (id, &enc);
103   FPRINTF (stdout, "%s (%s)\n", name, (const char *) &enc);
104 }
105
106
107 static int
108 pseudo_printer (void *cls, const GNUNET_HashCode * pseudonym,
109                 const char *name, const char *unique_name,
110                 const struct GNUNET_CONTAINER_MetaData *md, int rating)
111 {
112   char *id, *unique_id;
113   int getinfo_result;
114
115   /* While we get a name from the caller, it might be NULL.
116    * GNUNET_PSEUDONYM_get_info () never returns NULL.
117    */
118   getinfo_result = GNUNET_PSEUDONYM_get_info (cfg, pseudonym,
119       NULL, NULL, &id, NULL);
120   if (getinfo_result != GNUNET_OK)
121   {
122     GNUNET_break (0);
123     return GNUNET_OK;
124   }
125   unique_id = GNUNET_PSEUDONYM_name_uniquify (cfg, pseudonym, id, NULL);
126   GNUNET_free (id);
127   FPRINTF (stdout, "%s (%d):\n", unique_id, rating);
128   GNUNET_CONTAINER_meta_data_iterate (md, &EXTRACTOR_meta_data_print, stdout);
129   FPRINTF (stdout, "%s",  "\n");
130   GNUNET_free (unique_id);
131   return GNUNET_OK;
132 }
133
134
135 static void
136 post_advertising (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
137 {
138   GNUNET_HashCode nsid;
139   char *set;
140   int delta;
141
142   if (emsg != NULL)
143   {
144     FPRINTF (stderr, "%s", emsg);
145     ret = 1;
146   }
147   if (ns != NULL)
148   {
149     if (GNUNET_OK != GNUNET_FS_namespace_delete (ns, GNUNET_NO))
150       ret = 1;
151   }
152   if (NULL != rating_change)
153   {
154     set = rating_change;
155     while ((*set != '\0') && (*set != ':'))
156       set++;
157     if (*set != ':')
158     {
159       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid argument `%s'\n"),
160                   rating_change);
161     }
162     else
163     {
164       *set = '\0';
165       delta = strtol (&set[1], NULL,    /* no error handling yet */
166                       10);
167       if (GNUNET_OK == GNUNET_PSEUDONYM_name_to_id (cfg, rating_change, &nsid))
168       {
169         (void) GNUNET_PSEUDONYM_rank (cfg, &nsid, delta);
170       }
171       else
172       {
173         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
174                     ("Namespace `%s' unknown. Make sure you specify its numeric suffix, if any.\n"),
175                     rating_change);
176       }
177     }
178     GNUNET_free (rating_change);
179     rating_change = NULL;
180   }
181   if (0 != print_local_only)
182   {
183     GNUNET_FS_namespace_list (h, &ns_printer, NULL);
184   }
185   else if (0 == no_remote_printing)
186   {
187     GNUNET_PSEUDONYM_list_all (cfg, &pseudo_printer, NULL);
188   }
189   GNUNET_FS_stop (h);
190 }
191
192
193 /**
194  * Main function that will be run by the scheduler.
195  *
196  * @param cls closure
197  * @param args remaining command-line arguments
198  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
199  * @param c configuration
200  */
201 static void
202 run (void *cls, char *const *args, const char *cfgfile,
203      const struct GNUNET_CONFIGURATION_Handle *c)
204 {
205   char *emsg;
206
207   cfg = c;
208   h = GNUNET_FS_start (cfg, "gnunet-pseudonym", &progress_cb, NULL,
209                        GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
210   if (NULL != delete_ns)
211   {
212     ns = GNUNET_FS_namespace_create (h, delete_ns);
213     if (ns == NULL)
214     {
215       ret = 1;
216     }
217     else
218     {
219       if (GNUNET_OK != GNUNET_FS_namespace_delete (ns, GNUNET_YES))
220         ret = 1;
221       ns = NULL;
222     }
223   }
224   if (NULL != create_ns)
225   {
226     ns = GNUNET_FS_namespace_create (h, create_ns);
227     if (ns == NULL)
228     {
229       ret = 1;
230     }
231     else
232     {
233       if (NULL != root_identifier)
234       {
235         if (ksk_uri == NULL)
236         {
237           emsg = NULL;
238           ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/namespace", &emsg);
239           GNUNET_assert (NULL == emsg);
240         }
241         GNUNET_FS_namespace_advertise (h, ksk_uri, ns, adv_metadata, &bo,
242                                        root_identifier, &post_advertising,
243                                        NULL);
244         return;
245       }
246       else
247       {
248         if (ksk_uri != NULL)
249           FPRINTF (stderr, _("Option `%s' ignored\n"), "-k");
250       }
251     }
252   }
253   else
254   {
255     if (root_identifier != NULL)
256       FPRINTF (stderr, _("Option `%s' ignored\n"), "-r");
257     if (ksk_uri != NULL)
258       FPRINTF (stderr, _("Option `%s' ignored\n"), "-k");
259   }
260
261   post_advertising (NULL, NULL, NULL);
262 }
263
264
265 /**
266  * The main function to manipulate GNUnet pseudonyms (and publish
267  * to namespaces).
268  *
269  * @param argc number of arguments from the command line
270  * @param argv command line arguments
271  * @return 0 ok, 1 on error
272  */
273 int
274 main (int argc, char *const *argv)
275 {
276   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
277     {'a', "anonymity", "LEVEL",
278      gettext_noop ("set the desired LEVEL of sender-anonymity"),
279      1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
280     {'C', "create", "NAME",
281      gettext_noop ("create or advertise namespace NAME"),
282      1, &GNUNET_GETOPT_set_string, &create_ns},
283     {'D', "delete", "NAME",
284      gettext_noop ("delete namespace NAME "),
285      1, &GNUNET_GETOPT_set_string, &delete_ns},
286     {'k', "keyword", "VALUE",
287      gettext_noop ("add an additional keyword for the advertisment"
288                    " (this option can be specified multiple times)"),
289      1, &GNUNET_FS_getopt_set_keywords, &ksk_uri},
290     {'m', "meta", "TYPE:VALUE",
291      gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
292      1, &GNUNET_FS_getopt_set_metadata, &adv_metadata},
293     {'o', "only-local", NULL,
294      gettext_noop ("print names of local namespaces"),
295      0, &GNUNET_GETOPT_set_one, &print_local_only},
296     {'p', "priority", "PRIORITY",
297      gettext_noop ("use the given PRIORITY for the advertisments"),
298      1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
299     {'q', "quiet", NULL,
300      gettext_noop ("do not print names of remote namespaces"),
301      0, &GNUNET_GETOPT_set_one, &no_remote_printing},
302     {'r', "replication", "LEVEL",
303      gettext_noop ("set the desired replication LEVEL"),
304      1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
305     {'R', "root", "ID",
306      gettext_noop ("specify ID of the root of the namespace"),
307      1, &GNUNET_GETOPT_set_string, &root_identifier},
308     {'s', "set-rating", "ID:VALUE",
309      gettext_noop ("change rating of namespace ID by VALUE"),
310      1, &GNUNET_GETOPT_set_string, &rating_change},
311     GNUNET_GETOPT_OPTION_END
312   };
313   bo.expiration_time =
314       GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
315   return (GNUNET_OK ==
316           GNUNET_PROGRAM_run (argc, argv, "gnunet-pseudonym [OPTIONS]",
317                               gettext_noop ("Manage GNUnet pseudonyms."),
318                               options, &run, NULL)) ? ret : 1;
319 }
320
321 /* end of gnunet-pseudonym.c */