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"),
302 case GNUNET_CONVERSATION_EC_BUSY:
303 GNUNET_break (CS_RINGING == state);
310 case GNUNET_CONVERSATION_EC_TERMINATED:
311 GNUNET_break ( (CS_RINGING == state) ||
312 (CS_CONNECTED == state) );
314 _("Call terminated: %s\n"),
315 va_arg (va, const char *));
325 * Function declareation for executing a action
327 * @param arguments arguments given to the function
329 typedef void (*ActionFunction) (const char *arguments);
333 * Structure which defines a command
338 * Command the user needs to enter.
343 * Function to call on command.
345 ActionFunction Action;
348 * Help text for the command.
350 const char *helptext;
355 * Action function to print help for the command shell.
357 * @param args arguments given to the command
360 do_help (const char *args);
364 * Terminate the client
366 * @param args arguments given to the command
369 do_quit (const char *args)
371 GNUNET_SCHEDULER_shutdown ();
376 * Handler for unknown command.
378 * @param msg arguments given to the command
381 do_unknown (const char *msg)
384 _("Unknown command `%s'\n"),
390 * Initiating a new call
392 * @param arg arguments given to the command
395 do_call (const char *arg)
397 if (NULL == caller_id)
400 _("Ego `%s' not available\n"),
408 _("Ego `%s' not available\n"),
416 _("Hanging up on incoming phone call from `%s' to call `%s'.\n"),
419 GNUNET_CONVERSATION_phone_hang_up (phone, NULL);
423 _("You are already in a conversation with `%s', refusing to call `%s'.\n"),
430 _("Aborting call to `%s'\n"),
432 GNUNET_CONVERSATION_call_stop (call, NULL);
437 _("You are already in a conversation with `%s', refusing to call `%s'.\n"),
445 GNUNET_assert (NULL == call);
448 GNUNET_CONVERSATION_phone_destroy (phone);
451 GNUNET_free_non_null (peer_name);
452 peer_name = GNUNET_strdup (arg);
453 call = GNUNET_CONVERSATION_call_start (cfg,
458 &call_event_handler, NULL);
459 state = CS_RESOLVING;
464 * Accepting an incoming call
466 * @param args arguments given to the command
469 do_accept (const char *args)
477 _("There is no incoming call to be accepted!\n"));
480 /* this is the expected state */
484 _("You are already in a conversation with `%s'.\n"),
490 _("You are trying to call `%s', cannot accept incoming calls right now.\n"),
495 _("You are already in a conversation with `%s'.\n"),
499 GNUNET_assert (NULL != phone);
500 GNUNET_CONVERSATION_phone_pick_up (phone,
509 * Print address information for this phone.
511 * @param args arguments given to the command
514 do_address (const char *args)
520 _("We currently do not have an address.\n"));
530 * Accepting an incoming call
532 * @param args arguments given to the command
535 do_status (const char *args)
541 _("We are currently trying to locate the private key for the ego `%s'.\n"),
546 _("We are listening for incoming calls for ego `%s' on line %u.\n"),
552 _("The phone is rining. `%s' is trying to call us.\n"),
558 _("You are having a conversation with `%s'.\n"),
563 _("We are trying to find the network address to call `%s'.\n"),
568 _("We are calling `%s', his phone should be ringing.\n"),
573 _("We had an internal error setting up our phone line. You can still make calls.\n"));
582 * @param args arguments given to the command
585 do_reject (const char *args)
594 _("There is no call that could be cancelled right now.\n"));
601 /* expected state, do rejection logic */
606 GNUNET_assert (NULL != phone);
607 GNUNET_CONVERSATION_phone_hang_up (phone,
613 GNUNET_CONVERSATION_call_stop (call, args);
621 * List of supported commands.
623 static struct VoipCommand commands[] = {
624 {"/address", &do_address,
625 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
627 gettext_noop ("Use `/call USER.gnu' to call USER")},
628 {"/accept", &do_accept,
629 gettext_noop ("Use `/accept MESSAGE' to accept an incoming call")},
630 {"/cancel", &do_reject,
631 gettext_noop ("Use `/cancel MESSAGE' to reject or terminate a call")},
632 {"/status", &do_status,
633 gettext_noop ("Use `/status' to print status information")},
635 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
637 gettext_noop ("Use `/help command' to get help for a specific command")},
645 * Action function to print help for the command shell.
647 * @param arguments arguments given to the command
650 do_help (const char *args)
655 while ( (NULL != args) &&
656 (0 != strlen (args)) &&
657 (commands[i].Action != &do_help))
660 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
664 gettext (commands[i].helptext));
672 "Available commands:\n");
673 while (commands[i].Action != &do_help)
677 gettext (commands[i].command));
685 gettext (commands[i].helptext));
690 * Task run during shutdown.
696 do_stop_task (void *cls,
697 const struct GNUNET_SCHEDULER_TaskContext *tc)
701 GNUNET_CONVERSATION_call_stop (call, NULL);
706 GNUNET_CONVERSATION_phone_destroy (phone);
709 if (GNUNET_SCHEDULER_NO_TASK != handle_cmd_task)
711 GNUNET_SCHEDULER_cancel (handle_cmd_task);
712 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
716 GNUNET_IDENTITY_disconnect (id);
719 GNUNET_SPEAKER_destroy (speaker);
721 GNUNET_MICROPHONE_destroy (mic);
723 GNUNET_free (ego_name);
725 GNUNET_CONFIGURATION_destroy (cfg);
727 GNUNET_free_non_null (peer_name);
733 * Task to handle commands from the terminal.
736 * @param tc scheduler context
739 handle_command (void *cls,
740 const struct GNUNET_SCHEDULER_TaskContext *tc)
742 char message[MAX_MESSAGE_LENGTH + 1];
747 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
749 &handle_command, NULL);
750 /* read message from command line and handle it */
751 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
752 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
754 if (0 == strlen (message))
756 if (message[strlen (message) - 1] == '\n')
757 message[strlen (message) - 1] = '\0';
758 if (0 == strlen (message))
761 while ((NULL != commands[i].command) &&
762 (0 != strncasecmp (commands[i].command, message,
763 strlen (commands[i].command))))
765 ptr = &message[strlen (commands[i].command)];
766 while (isspace ((int) *ptr))
768 commands[i].Action (ptr);
773 * Function called by identity service with information about egos.
776 * @param ego ego handle
778 * @param name name of the ego
781 identity_cb (void *cls,
782 struct GNUNET_IDENTITY_Ego *ego,
788 if (ego == caller_id)
792 _("Name of our ego changed to `%s'\n"),
794 GNUNET_free (ego_name);
795 ego_name = GNUNET_strdup (name);
798 if (0 != strcmp (name,
805 _("Our ego `%s' was deleted!\n"),
811 GNUNET_CONFIGURATION_set_value_number (cfg,
820 * Main function that will be run by the scheduler.
823 * @param args remaining command-line arguments
824 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
825 * @param c configuration
831 const struct GNUNET_CONFIGURATION_Handle *c)
833 cfg = GNUNET_CONFIGURATION_dup (c);
834 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
835 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
836 if (NULL == ego_name)
840 _("You must specify the NAME of an ego to use\n"));
843 id = GNUNET_IDENTITY_connect (cfg,
847 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
848 &handle_command, NULL);
849 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
855 * The main function to conversation.
857 * @param argc number of arguments from the command line
858 * @param argv command line arguments
859 * @return 0 ok, 1 on error
862 main (int argc, char *const *argv)
864 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
866 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
867 1, &GNUNET_GETOPT_set_string, &ego_name},
868 {'p', "phone", "LINE",
869 gettext_noop ("sets the LINE to use for the phone"),
870 1, &GNUNET_GETOPT_set_uint, &line},
871 GNUNET_GETOPT_OPTION_END
876 flags = fcntl (0, F_GETFL, 0);
878 fcntl (0, F_SETFL, flags);
879 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
880 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
882 ret = GNUNET_PROGRAM_run (argc, argv,
883 "gnunet-conversation",
884 gettext_noop ("Enables having a conversation with other GNUnet users."),
885 options, &run, NULL);
886 GNUNET_free ((void *) argv);
887 return (GNUNET_OK == ret) ? 0 : 1;
890 /* end of gnunet-conversation.c */