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