2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file identity/gnunet-identity.c
22 * @brief IDENTITY management command line tool
23 * @author Christian Grothoff
26 * - add options to get/set default egos
27 * - print short hashes of egos when printing
30 #include "gnunet_util_lib.h"
31 #include "gnunet_identity_service.h"
34 * Handle to IDENTITY service.
36 static struct GNUNET_IDENTITY_Handle *sh;
39 * Was "list" specified?
44 * Was "monitor" specified?
51 static char *create_ego;
56 static char *delete_ego;
59 * Handle for create operation.
61 static struct GNUNET_IDENTITY_Operation *create_op;
64 * Handle for delete operation.
66 static struct GNUNET_IDENTITY_Operation *delete_op;
70 * Task run on shutdown.
76 shutdown_task (void *cls,
77 const struct GNUNET_SCHEDULER_TaskContext *tc)
79 GNUNET_IDENTITY_disconnect (sh);
86 * Test if we are finished yet.
91 if ( (NULL == create_op) &&
92 (NULL == delete_op) &&
95 GNUNET_SCHEDULER_shutdown ();
100 * Deletion operation finished.
102 * @param cls pointer to operation handle
103 * @param emsg NULL on success, otherwise an error message
106 delete_finished (void *cls,
109 struct GNUNET_IDENTITY_Operation **op = cls;
121 * Creation operation finished.
123 * @param cls pointer to operation handle
124 * @param ego ego handle
125 * @param ego_ctx context for application to store data for this ego
126 * (during the lifetime of this process, initially NULL)
127 * @param identifier identifier assigned by the user for this ego
130 create_finished (void *cls,
131 struct GNUNET_IDENTITY_Ego *ego,
133 const char *identifier)
135 struct GNUNET_IDENTITY_Operation **op = cls;
141 _("Failed to create ego\n"));
147 * If listing is enabled, prints information about the egos.
149 * This function is initially called for all egos and then again
150 * whenever a ego's identifier changes or if it is deleted. At the
151 * end of the initial pass over all egos, the function is once called
152 * with 'NULL' for 'ego'. That does NOT mean that the callback won't
153 * be invoked in the future or that there was an error.
155 * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
156 * this function is only called ONCE, and 'NULL' being passed in
157 * 'ego' does indicate an error (i.e. name is taken or no default
158 * value is known). If 'ego' is non-NULL and if '*ctx'
159 * is set in those callbacks, the value WILL be passed to a subsequent
160 * call to the identity callback of 'GNUNET_IDENTITY_connect' (if
161 * that one was not NULL).
163 * When an identity is renamed, this function is called with the
164 * (known) ego but the NEW identifier.
166 * When an identity is deleted, this function is called with the
167 * (known) ego and "NULL" for the 'identifier'. In this case,
168 * the 'ego' is henceforth invalid (and the 'ctx' should also be
172 * @param ego ego handle
173 * @param ego_ctx context for application to store data for this ego
174 * (during the lifetime of this process, initially NULL)
175 * @param identifier identifier assigned by the user for this ego,
176 * NULL if the user just deleted the ego and it
177 * must thus no longer be used
180 print_ego (void *cls,
181 struct GNUNET_IDENTITY_Ego *ego,
183 const char *identifier)
186 if (! (list | monitor))
188 if ( (NULL == ego) && (! monitor) )
190 GNUNET_SCHEDULER_shutdown ();
193 fprintf (stderr, "%s\n", identifier);
198 * Main function that will be run by the scheduler.
201 * @param args remaining command-line arguments
202 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
203 * @param cfg configuration
206 run (void *cls, char *const *args, const char *cfgfile,
207 const struct GNUNET_CONFIGURATION_Handle *cfg)
209 sh = GNUNET_IDENTITY_connect (cfg, &print_ego, NULL);
210 if (NULL != delete_ego)
211 delete_op = GNUNET_IDENTITY_delete (sh,
215 if (NULL != create_ego)
216 create_op = GNUNET_IDENTITY_create (sh,
220 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
221 &shutdown_task, NULL);
229 * @param argc number of arguments from the command line
230 * @param argv command line arguments
231 * @return 0 ok, 1 on error
234 main (int argc, char *const *argv)
238 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
239 {'C', "create", "NAME",
240 gettext_noop ("create ego NAME"),
241 1, &GNUNET_GETOPT_set_string, &create_ego},
242 {'D', "delete", "NAME",
243 gettext_noop ("delete ego NAME "),
244 1, &GNUNET_GETOPT_set_string, &delete_ego},
246 gettext_noop ("list all egos"),
247 0, &GNUNET_GETOPT_set_one, &list},
248 {'m', "monitor", NULL,
249 gettext_noop ("run in monitor mode egos"),
250 0, &GNUNET_GETOPT_set_one, &monitor},
251 GNUNET_GETOPT_OPTION_END
254 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
257 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-identity",
258 gettext_noop ("Maintain egos"),
261 GNUNET_free ((void *) argv);
263 if (GNUNET_OK != res)
268 /* end of gnunet-identity.c */