use correct expiration values
[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   struct GNUNET_TIME_Absolute expiration;
230   char *emsg;
231
232   cfg = c;
233   h = GNUNET_FS_start (cfg,
234                        "gnunet-pseudonym",
235                        &progress_cb,
236                        NULL,
237                        GNUNET_FS_FLAGS_NONE,
238                        GNUNET_FS_OPTIONS_END);
239   if (NULL != delete_ns)
240     {
241       ns = GNUNET_FS_namespace_create (h, delete_ns);
242       if (ns == NULL)
243         {
244           ret = 1;
245         }
246       else
247         {
248           if (GNUNET_OK !=
249               GNUNET_FS_namespace_delete (ns,
250                                           GNUNET_YES))
251             ret = 1;
252           ns = NULL;
253         }
254     }
255   if (NULL != create_ns)
256     {
257       ns = GNUNET_FS_namespace_create (h, create_ns);
258       if (ns == NULL)
259         {
260           ret = 1;
261         }
262       else
263         {
264           if (NULL != root_identifier)
265             {
266               expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS);
267               if (ksk_uri == NULL)
268                 {
269                   emsg = NULL;
270                   ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/namespace", &emsg);
271                   GNUNET_assert (NULL == emsg);
272                 }
273               GNUNET_FS_namespace_advertise (h,
274                                              ksk_uri,
275                                              ns,
276                                              adv_metadata,
277                                              &bo,
278                                              root_identifier,
279                                              &post_advertising,
280                                              NULL);
281               return;
282             }
283           else
284             {
285               if (ksk_uri != NULL)
286                 fprintf (stderr, _("Option `%s' ignored\n"), "-k");   
287             }
288         }
289     }
290   else
291     {
292       if (root_identifier != NULL) 
293         fprintf (stderr, _("Option `%s' ignored\n"), "-r");
294       if (ksk_uri != NULL)
295         fprintf (stderr, _("Option `%s' ignored\n"), "-k");   
296     }    
297     
298   post_advertising (NULL, NULL, NULL);
299 }
300
301
302 /**
303  * The main function to manipulate GNUnet pseudonyms (and publish
304  * to namespaces).
305  *
306  * @param argc number of arguments from the command line
307  * @param argv command line arguments
308  * @return 0 ok, 1 on error
309  */
310 int
311 main (int argc, char *const *argv)
312 {
313   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
314     {'a', "anonymity", "LEVEL",
315      gettext_noop ("set the desired LEVEL of sender-anonymity"),
316      1, &GNUNET_GETOPT_set_uint, &bo.anonymity_level},
317     {'C', "create", "NAME",
318      gettext_noop
319      ("create or advertise namespace NAME"),
320      1, &GNUNET_GETOPT_set_string, &create_ns},
321     {'D', "delete", "NAME",
322      gettext_noop
323      ("delete namespace NAME "),
324      1, &GNUNET_GETOPT_set_string, &delete_ns},
325     {'k', "keyword", "VALUE",
326      gettext_noop
327      ("add an additional keyword for the advertisment"
328       " (this option can be specified multiple times)"),
329      1, &GNUNET_FS_getopt_set_keywords, &ksk_uri},
330     {'m', "meta", "TYPE:VALUE",
331      gettext_noop ("set the meta-data for the given TYPE to the given VALUE"),
332      1, &GNUNET_FS_getopt_set_metadata, &adv_metadata},
333     {'o', "only-local", NULL,
334      gettext_noop ("print names of local namespaces"),
335      0, &GNUNET_GETOPT_set_one, &print_local_only},
336     {'p', "priority", "PRIORITY",
337      gettext_noop ("use the given PRIORITY for the advertisments"),
338      1, &GNUNET_GETOPT_set_uint, &bo.content_priority},
339     {'q', "quiet", NULL,
340      gettext_noop ("do not print names of remote namespaces"),
341      0, &GNUNET_GETOPT_set_one, &no_remote_printing},
342     {'r', "replication", "LEVEL",
343      gettext_noop ("set the desired replication LEVEL"),
344      1, &GNUNET_GETOPT_set_uint, &bo.replication_level},
345     {'R', "root", "ID",
346      gettext_noop
347      ("specify ID of the root of the namespace"),
348      1, &GNUNET_GETOPT_set_string, &root_identifier},
349     {'s', "set-rating", "ID:VALUE",
350      gettext_noop
351      ("change rating of namespace ID by VALUE"),
352      1, &GNUNET_GETOPT_set_string, &rating_change},
353     GNUNET_GETOPT_OPTION_END
354   };
355   bo.expiration_time = GNUNET_FS_year_to_time (GNUNET_FS_get_current_year () + 2);
356   return (GNUNET_OK ==
357           GNUNET_PROGRAM_run (argc,
358                               argv,
359                               "gnunet-pseudonym",
360                               gettext_noop
361                               ("Manage GNUnet pseudonyms."),
362                               options, &run, NULL)) ? ret : 1;
363 }
364
365 /* end of gnunet-pseudonym.c */