b8bd12b35dcbe7897b8224c222d7b2a37e59c2ae
[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 2, 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  * -a optiton.
30  */
31 static unsigned int anonymity;
32
33 /**
34  * -A option.
35  */
36 static int start_automate;
37
38 /**
39  * -e option
40  */
41 static int stop_automate;
42
43 /**
44  * -C option
45  */
46 static char *create_ns;
47
48 /**
49  * -D option
50  */
51 static char *delete_ns;
52
53 /**
54  * -k option
55  */
56 static struct GNUNET_FS_Uri *ksk_uri;
57
58 /**
59  * -l option.
60  */
61 static int print_local_only;
62
63 /**
64  * -m option.
65  */
66 static struct GNUNET_CONTAINER_MetaData *adv_metadata;
67
68 /**
69  * -p option.
70  */
71 static unsigned int priority = 365;
72
73 /**
74  * -q option given.
75  */
76 static int no_remote_printing; 
77
78 /**
79  * -r option.
80  */
81 static char *root_identifier;
82
83 /**
84  * -s option.
85  */
86 static char *rating_change;
87
88 /**
89  * Handle to fs service.
90  */
91 static struct GNUNET_FS_Handle *h;
92
93 /**
94  * Namespace we are looking at.
95  */
96 static struct GNUNET_FS_Namespace *ns;
97
98 /**
99  * Our configuration.
100  */
101 static const struct GNUNET_CONFIGURATION_Handle *cfg;
102
103 static int ret;
104
105 static void* 
106 progress_cb (void *cls,
107              const struct GNUNET_FS_ProgressInfo *info)
108 {
109   return NULL;
110 }
111
112
113 static void
114 ns_printer (void *cls,
115             const char *name,
116             const GNUNET_HashCode *id)
117 {
118   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
119
120   GNUNET_CRYPTO_hash_to_enc (id, &enc);
121   fprintf (stdout, 
122            "%s (%s)\n",
123            name,
124            (const char*) &enc);
125 }
126
127
128 static int
129 pseudo_printer (void *cls,
130                 const GNUNET_HashCode *
131                 pseudonym,
132                 const struct
133                 GNUNET_CONTAINER_MetaData * md,
134                 int rating)
135 {
136   char *id;
137
138   id = GNUNET_PSEUDONYM_id_to_name (cfg,
139                                     pseudonym);
140   if (id == NULL)
141     {
142       GNUNET_break (0);
143       return GNUNET_OK;
144     }
145   fprintf (stdout, 
146            "%s (%d):\n",
147            id,
148            rating);
149   GNUNET_CONTAINER_meta_data_iterate (md,
150                                       &EXTRACTOR_meta_data_print, 
151                                       stdout);
152   fprintf (stdout, "\n");
153   GNUNET_free (id);
154   return GNUNET_OK;
155 }
156
157
158 static void
159 post_advertising (void *cls,
160                   const struct GNUNET_FS_Uri *uri,
161                   const char *emsg)
162 {
163   GNUNET_HashCode nsid;
164   char *set;
165   int delta;
166
167   if (emsg != NULL)
168     {
169       fprintf (stderr, "%s", emsg);
170       ret = 1;
171     }
172   if (ns != NULL)
173     {
174       if (GNUNET_OK !=
175           GNUNET_FS_namespace_delete (ns,
176                                       GNUNET_NO))
177         ret = 1;
178     }
179   if (0 != stop_automate)
180     {
181       GNUNET_break (0); // FIXME: not implemented
182     }
183   if (0 != start_automate)
184     {
185       GNUNET_break (0); // FIXME: not implemented
186     }
187   if (NULL != rating_change)
188     {
189       set = rating_change;
190       while ((*set != '\0') && (*set != ':'))
191         set++;
192       if (*set != ':')
193         {
194           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195                       _("Invalid argument `%s'\n"),
196                       rating_change);
197         }
198       else
199         {
200           *set = '\0';
201           delta = strtol (&set[1], NULL, /* no error handling yet */
202                           10);
203           if (GNUNET_OK ==
204               GNUNET_PSEUDONYM_name_to_id (cfg,
205                                            rating_change,
206                                            &nsid))
207             {
208               GNUNET_PSEUDONYM_rank (cfg,
209                                      &nsid,
210                                      delta);
211               
212             }
213           else
214             {
215               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
216                           _("Namespace `%s' unknown.\n"),
217                           rating_change);
218             }
219         }
220       GNUNET_free (rating_change);
221       rating_change = NULL;
222     }
223   if (0 != print_local_only)
224     {
225       GNUNET_FS_namespace_list (h,
226                                 &ns_printer, 
227                                 NULL);
228     }  
229   else if (0 == no_remote_printing)
230     {
231       GNUNET_PSEUDONYM_list_all (cfg,
232                                  &pseudo_printer,
233                                  NULL);
234     }
235   GNUNET_FS_stop (h);
236 }
237
238
239 /**
240  * Main function that will be run by the scheduler.
241  *
242  * @param cls closure
243  * @param sched the scheduler to use
244  * @param args remaining command-line arguments
245  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
246  * @param c configuration
247  */
248 static void
249 run (void *cls,
250      struct GNUNET_SCHEDULER_Handle *sched,
251      char *const *args,
252      const char *cfgfile,
253      const struct GNUNET_CONFIGURATION_Handle *c)
254 {
255   struct GNUNET_FS_Uri *ns_uri;
256   struct GNUNET_TIME_Absolute expiration;
257   char *emsg;
258
259   cfg = c;
260   h = GNUNET_FS_start (sched,
261                        cfg,
262                        "gnunet-pseudonym",
263                        &progress_cb,
264                        NULL,
265                        GNUNET_FS_FLAGS_NONE);
266   if (NULL != delete_ns)
267     {
268       ns = GNUNET_FS_namespace_create (h, delete_ns);
269       if (ns == NULL)
270         {
271           ret = 1;
272         }
273       else
274         {
275           if (GNUNET_OK !=
276               GNUNET_FS_namespace_delete (ns,
277                                           GNUNET_YES))
278             ret = 1;
279           ns = NULL;
280         }
281     }
282   if (NULL != create_ns)
283     {
284       ns = GNUNET_FS_namespace_create (h, create_ns);
285       if (ns == NULL)
286         {
287           ret = 1;
288         }
289       else
290         {
291           if (NULL != root_identifier)
292             {
293               emsg = NULL;
294               ns_uri = GNUNET_FS_uri_sks_create (ns, root_identifier, &emsg);
295               GNUNET_assert (emsg == NULL);
296               expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS);
297               if (ksk_uri == NULL)
298                 {
299                   emsg = NULL;
300                   ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/namespace", &emsg);
301                   GNUNET_assert (NULL == emsg);
302                 }
303               GNUNET_FS_publish_ksk (h,
304                                      ksk_uri,
305                                      adv_metadata,
306                                      ns_uri,
307                                      expiration,
308                                      anonymity,
309                                      priority,
310                                      GNUNET_FS_PUBLISH_OPTION_NONE,
311                                      &post_advertising,
312                                      NULL);
313               return;
314             }
315         }
316     }
317   post_advertising (NULL, NULL, NULL);
318 }
319
320 /**
321  * gnunet-pseudonym command line options
322  */
323 static struct GNUNET_GETOPT_CommandLineOption options[] = {
324   {'a', "anonymity", "LEVEL",
325    gettext_noop ("set the desired LEVEL of sender-anonymity"),
326    1, &GNUNET_GETOPT_set_uint, &anonymity},
327   {'A', "automate", NULL,
328    gettext_noop ("start a collection"),
329    0, &GNUNET_GETOPT_set_one, &start_automate},
330   {'C', "create", "NAME",
331    gettext_noop
332    ("create or advertise namespace NAME"),
333    1, &GNUNET_GETOPT_set_string, &create_ns},
334   {'D', "delete", "NAME",
335    gettext_noop
336    ("delete namespace NAME "),
337    1, &GNUNET_GETOPT_set_string, &delete_ns},
338   {'e', "end", NULL,
339    gettext_noop ("end current collection"),
340    0, &GNUNET_GETOPT_set_one, &stop_automate},
341   {'k', "keyword", "VALUE",
342   gettext_noop
343    ("add an additional keyword for the advertisment"
344     " (this option can be specified multiple times)"),
345    1, &GNUNET_FS_getopt_set_keywords, &ksk_uri},
346   {'l', "local-only", NULL,
347    gettext_noop ("print names of local namespaces"),
348    0, &GNUNET_GETOPT_set_one, &print_local_only},
349   {'m', "meta", "TYPE:VALUE",
350    gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
351    1, &GNUNET_FS_getopt_set_metadata, &adv_metadata},
352   {'p', "priority", "PRIORITY",
353    gettext_noop ("use the given PRIORITY for the advertisments"),
354    1, &GNUNET_GETOPT_set_uint, &priority},
355   {'q', "quiet", NULL,
356    gettext_noop ("do not print names of remote namespaces"),
357    0, &GNUNET_GETOPT_set_one, &no_remote_printing},
358   {'r', "root", "ID",
359    gettext_noop
360    ("specify ID of the root of the namespace"),
361    1, &GNUNET_GETOPT_set_string, &root_identifier},
362   {'s', "set-rating", "ID:VALUE",
363    gettext_noop
364    ("change rating of namespace ID by VALUE"),
365    1, &GNUNET_GETOPT_set_string, &rating_change},
366   GNUNET_GETOPT_OPTION_END
367 };
368
369
370 /**
371  * The main function to inspect GNUnet directories.
372  *
373  * @param argc number of arguments from the command line
374  * @param argv command line arguments
375  * @return 0 ok, 1 on error
376  */
377 int
378 main (int argc, char *const *argv)
379 {
380   return (GNUNET_OK ==
381           GNUNET_PROGRAM_run (argc,
382                               argv,
383                               "gnunet-pseudonym",
384                               gettext_noop
385                               ("Manage GNUnet pseudonyms."),
386                               options, &run, NULL)) ? ret : 1;
387 }
388
389 /* end of gnunet-pseudonym.c */