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 conversation/gnunet-conversation.c
22 * @brief conversation implementation
23 * @author Simon Dieterle
24 * @author Andreas Fuchs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_conversation_service.h"
30 #include "gnunet_namestore_service.h"
34 * Maximum length allowed for the command line input.
36 #define MAX_MESSAGE_LENGTH 1024
40 * Possible states of the program.
42 enum ConversationState
45 * We're waiting for our own idenitty.
50 * We're listening for calls
55 * Our phone is ringing.
60 * We accepted an incoming phone call.
65 * We are looking up some other participant.
70 * We are now ringing the other participant.
75 * The other party accepted our call and we are now connected.
90 static struct GNUNET_CONVERSATION_Phone *phone;
95 static struct GNUNET_CONVERSATION_Call *call;
100 static unsigned int line;
103 * Task which handles the commands
105 static GNUNET_SCHEDULER_TaskIdentifier handle_cmd_task;
110 static struct GNUNET_SPEAKER_Handle *speaker;
115 static struct GNUNET_MICROPHONE_Handle *mic;
120 static struct GNUNET_CONFIGURATION_Handle *cfg;
125 static struct GNUNET_IDENTITY_Ego *caller_id;
128 * Handle to identity service.
130 static struct GNUNET_IDENTITY_Handle *id;
135 static char *ego_name;
138 * Name of conversation partner (if any).
140 static char *peer_name;
143 * File handle for stdin.
145 static struct GNUNET_DISK_FileHandle *stdin_fh;
150 static enum ConversationState state;
153 * GNS address for this phone.
155 static char *address;
164 * Function called with an event emitted by a phone.
167 * @param code type of the event on the phone
168 * @param ... additional information, depends on @a code
171 phone_event_handler (void *cls,
172 enum GNUNET_CONVERSATION_EventCode code,
180 case GNUNET_CONVERSATION_EC_RING:
181 GNUNET_break (CS_LISTEN == state);
182 GNUNET_free_non_null (peer_name);
183 peer_name = GNUNET_strdup (va_arg (va, const char *));
185 _("Incoming call from `%s'.\nPlease /accept or /cancel the call.\n"),
189 case GNUNET_CONVERSATION_EC_RINGING:
192 case GNUNET_CONVERSATION_EC_READY:
195 case GNUNET_CONVERSATION_EC_GNS_FAIL:
198 case GNUNET_CONVERSATION_EC_BUSY:
201 case GNUNET_CONVERSATION_EC_TERMINATED:
202 GNUNET_break ( (CS_RING == state) ||
203 (CS_ACCEPTED == state) );
205 _("Call terminated: %s\n"),
206 va_arg (va, const char *));
220 struct GNUNET_NAMESTORE_RecordData rd;
222 if (NULL == caller_id)
225 _("Ego `%s' no longer available, phone is now down.\n"),
227 state = CS_LOOKUP_EGO;
230 phone = GNUNET_CONVERSATION_phone_create (cfg,
232 &phone_event_handler, NULL);
233 /* FIXME: get record and print full GNS record info later here... */
238 _("Failed to setup phone (internal error)\n"));
243 GNUNET_CONVERSATION_phone_get_record (phone,
245 GNUNET_free_non_null (address);
246 address = GNUNET_NAMESTORE_value_to_string (rd.record_type,
251 _("Phone active on line %u\n"),
252 (unsigned int) line);
259 * Function called with an event emitted by a phone.
262 * @param code type of the event on the phone
263 * @param ... additional information, depends on @a code
266 call_event_handler (void *cls,
267 enum GNUNET_CONVERSATION_EventCode code,
275 case GNUNET_CONVERSATION_EC_RING:
278 case GNUNET_CONVERSATION_EC_RINGING:
279 GNUNET_break (CS_RESOLVING == state);
283 _("Resolved address. Now ringing other party.\n"));
286 case GNUNET_CONVERSATION_EC_READY:
287 GNUNET_break (CS_RINGING == state);
289 _("Connection established to `%s': %s\n"),
291 va_arg (va, const char *));
292 state = CS_CONNECTED;
294 case GNUNET_CONVERSATION_EC_GNS_FAIL:
295 GNUNET_break (CS_RESOLVING == state);
297 _("Failed to resolve `%s'\n"),
299 GNUNET_CONVERSATION_call_stop (call, NULL);
303 case GNUNET_CONVERSATION_EC_BUSY:
304 GNUNET_break (CS_RINGING == state);
308 GNUNET_CONVERSATION_call_stop (call, NULL);
312 case GNUNET_CONVERSATION_EC_TERMINATED:
313 GNUNET_break ( (CS_RINGING == state) ||
314 (CS_CONNECTED == state) );
316 _("Call terminated: %s\n"),
317 va_arg (va, const char *));
318 GNUNET_CONVERSATION_call_stop (call, NULL);
328 * Function declareation for executing a action
330 * @param arguments arguments given to the function
332 typedef void (*ActionFunction) (const char *arguments);
336 * Structure which defines a command
341 * Command the user needs to enter.
346 * Function to call on command.
348 ActionFunction Action;
351 * Help text for the command.
353 const char *helptext;
358 * Action function to print help for the command shell.
360 * @param arguments arguments given to the command
363 do_help (const char *args);
367 * Terminate the client
369 * @param args arguments given to the command
372 do_quit (const char *args)
374 GNUNET_SCHEDULER_shutdown ();
379 * Handler for unknown command.
381 * @param args arguments given to the command
384 do_unknown (const char *msg)
387 _("Unknown command `%s'\n"),
393 * Initiating a new call
395 * @param args arguments given to the command
398 do_call (const char *arg)
400 if (NULL == caller_id)
403 _("Ego `%s' not available\n"),
411 _("Ego `%s' not available\n"),
419 _("Hanging up on incoming phone call from `%s' to call `%s'.\n"),
422 GNUNET_CONVERSATION_phone_hang_up (phone, NULL);
426 _("You are already in a conversation with `%s', refusing to call `%s'.\n"),
433 _("Aborting call to `%s'\n"),
435 GNUNET_CONVERSATION_call_stop (call, NULL);
440 _("You are already in a conversation with `%s', refusing to call `%s'.\n"),
448 GNUNET_assert (NULL == call);
451 GNUNET_CONVERSATION_phone_destroy (phone);
454 GNUNET_free_non_null (peer_name);
455 peer_name = GNUNET_strdup (arg);
456 call = GNUNET_CONVERSATION_call_start (cfg,
461 &call_event_handler, NULL);
462 state = CS_RESOLVING;
467 * Accepting an incoming call
469 * @param args arguments given to the command
472 do_accept (const char *args)
480 _("There is no incoming call to be accepted!\n"));
483 /* this is the expected state */
487 _("You are already in a conversation with `%s'.\n"),
493 _("You are trying to call `%s', cannot accept incoming calls right now.\n"),
498 _("You are already in a conversation with `%s'.\n"),
502 GNUNET_assert (NULL != phone);
503 GNUNET_CONVERSATION_phone_pick_up (phone,
512 * Print address information for this phone.
514 * @param args arguments given to the command
517 do_address (const char *args)
523 _("We currently do not have an address.\n"));
533 * Accepting an incoming call
535 * @param args arguments given to the command
538 do_status (const char *args)
544 _("We are currently trying to locate the private key for the ego `%s'.\n"),
549 _("We are listening for incoming calls for ego `%s' on line %u.\n"),
555 _("The phone is rining. `%s' is trying to call us.\n"),
561 _("You are having a conversation with `%s'.\n"),
566 _("We are trying to find the network address to call `%s'.\n"),
571 _("We are calling `%s', his phone should be ringing.\n"),
576 _("We had an internal error setting up our phone line. You can still make calls.\n"));
585 * @param args arguments given to the command
588 do_reject (const char *args)
597 _("There is no call that could be cancelled right now.\n"));
604 /* expected state, do rejection logic */
609 GNUNET_assert (NULL != phone);
610 GNUNET_CONVERSATION_phone_hang_up (phone,
616 GNUNET_CONVERSATION_call_stop (call, args);
624 * List of supported commands.
626 static struct VoipCommand commands[] = {
627 {"/address", &do_address,
628 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
630 gettext_noop ("Use `/call USER.gnu' to call USER")},
631 {"/accept", &do_accept,
632 gettext_noop ("Use `/accept MESSAGE' to accept an incoming call")},
633 {"/cancel", &do_reject,
634 gettext_noop ("Use `/cancel MESSAGE' to reject or terminate a call")},
635 {"/status", &do_status,
636 gettext_noop ("Use `/status' to print status information")},
638 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
640 gettext_noop ("Use `/help command' to get help for a specific command")},
648 * Action function to print help for the command shell.
650 * @param arguments arguments given to the command
653 do_help (const char *args)
658 while ( (NULL != args) &&
659 (0 != strlen (args)) &&
660 (commands[i].Action != &do_help))
663 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
667 gettext (commands[i].helptext));
675 "Available commands:\n");
676 while (commands[i].Action != &do_help)
680 gettext (commands[i].command));
688 gettext (commands[i].helptext));
693 * Task run during shutdown.
699 do_stop_task (void *cls,
700 const struct GNUNET_SCHEDULER_TaskContext *tc)
704 GNUNET_CONVERSATION_call_stop (call, NULL);
709 GNUNET_CONVERSATION_phone_destroy (phone);
712 if (GNUNET_SCHEDULER_NO_TASK != handle_cmd_task)
714 GNUNET_SCHEDULER_cancel (handle_cmd_task);
715 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
719 GNUNET_IDENTITY_disconnect (id);
722 GNUNET_SPEAKER_destroy (speaker);
724 GNUNET_MICROPHONE_destroy (mic);
726 GNUNET_free (ego_name);
728 GNUNET_CONFIGURATION_destroy (cfg);
730 GNUNET_free_non_null (peer_name);
736 * Task to handle commands from the terminal.
739 * @param tc scheduler context
742 handle_command (void *cls,
743 const struct GNUNET_SCHEDULER_TaskContext *tc)
745 char message[MAX_MESSAGE_LENGTH + 1];
750 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
752 &handle_command, NULL);
753 /* read message from command line and handle it */
754 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
755 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
757 if (0 == strlen (message))
759 if (message[strlen (message) - 1] == '\n')
760 message[strlen (message) - 1] = '\0';
761 if (0 == strlen (message))
764 while ((NULL != commands[i].command) &&
765 (0 != strncasecmp (commands[i].command, message,
766 strlen (commands[i].command))))
768 ptr = &message[strlen (commands[i].command)];
769 while (isspace ((int) *ptr))
771 commands[i].Action (ptr);
776 * Function called by identity service with information about egos.
779 * @param ego ego handle
781 * @param name name of the ego
784 identity_cb (void *cls,
785 struct GNUNET_IDENTITY_Ego *ego,
791 if (ego == caller_id)
795 _("Name of our ego changed to `%s'\n"),
797 GNUNET_free (ego_name);
798 ego_name = GNUNET_strdup (name);
801 if (0 != strcmp (name,
808 _("Our ego `%s' was deleted!\n"),
814 GNUNET_CONFIGURATION_set_value_number (cfg,
823 * Main function that will be run by the scheduler.
826 * @param args remaining command-line arguments
827 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
828 * @param c configuration
834 const struct GNUNET_CONFIGURATION_Handle *c)
836 cfg = GNUNET_CONFIGURATION_dup (c);
837 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
838 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
839 if (NULL == ego_name)
843 _("You must specify the NAME of an ego to use\n"));
846 id = GNUNET_IDENTITY_connect (cfg,
850 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
851 &handle_command, NULL);
852 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
858 * The main function to conversation.
860 * @param argc number of arguments from the command line
861 * @param argv command line arguments
862 * @return 0 ok, 1 on error
865 main (int argc, char *const *argv)
867 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
869 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
870 1, &GNUNET_GETOPT_set_string, &ego_name},
871 {'p', "phone", "LINE",
872 gettext_noop ("sets the LINE to use for the phone"),
873 1, &GNUNET_GETOPT_set_uint, &line},
874 GNUNET_GETOPT_OPTION_END
879 flags = fcntl (0, F_GETFL, 0);
881 fcntl (0, F_SETFL, flags);
882 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
883 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
885 ret = GNUNET_PROGRAM_run (argc, argv,
886 "gnunet-conversation",
887 gettext_noop ("Enables having a conversation with other GNUnet users."),
888 options, &run, NULL);
889 GNUNET_free ((void *) argv);
890 return (GNUNET_OK == ret) ? 0 : 1;
893 /* end of gnunet-conversation.c */