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