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_gnsrecord_lib.h"
30 #include "gnunet_conversation_service.h"
31 #include "gnunet_namestore_service.h"
35 * Maximum length allowed for the command line input.
37 #define MAX_MESSAGE_LENGTH 1024
41 * Possible states of the program.
43 enum ConversationState
46 * We're waiting for our own idenitty.
51 * We're listening for calls
56 * Our phone is ringing.
61 * We accepted an incoming phone call.
66 * We are looking up some other participant.
71 * We are now ringing the other participant.
76 * The other party accepted our call and we are now connected.
91 static struct GNUNET_CONVERSATION_Phone *phone;
96 static struct GNUNET_CONVERSATION_Call *call;
101 static unsigned int line;
104 * Task which handles the commands
106 static GNUNET_SCHEDULER_TaskIdentifier handle_cmd_task;
111 static struct GNUNET_SPEAKER_Handle *speaker;
116 static struct GNUNET_MICROPHONE_Handle *mic;
121 static struct GNUNET_CONFIGURATION_Handle *cfg;
126 static struct GNUNET_IDENTITY_Ego *caller_id;
129 * Handle to identity service.
131 static struct GNUNET_IDENTITY_Handle *id;
136 static char *ego_name;
139 * Name of conversation partner (if any).
141 static char *peer_name;
144 * File handle for stdin.
146 static struct GNUNET_DISK_FileHandle *stdin_fh;
151 static enum ConversationState state;
154 * GNS address for this phone.
156 static char *address;
165 * Function called with an event emitted by a phone.
168 * @param code type of the event on the phone
169 * @param ... additional information, depends on @a code
172 phone_event_handler (void *cls,
173 enum GNUNET_CONVERSATION_EventCode code,
181 case GNUNET_CONVERSATION_EC_RING:
182 GNUNET_break (CS_LISTEN == state);
183 GNUNET_free_non_null (peer_name);
184 peer_name = GNUNET_strdup (va_arg (va, const char *));
186 _("Incoming call from `%s'.\nPlease /accept or /cancel the call.\n"),
190 case GNUNET_CONVERSATION_EC_RINGING:
193 case GNUNET_CONVERSATION_EC_READY:
196 case GNUNET_CONVERSATION_EC_GNS_FAIL:
199 case GNUNET_CONVERSATION_EC_BUSY:
202 case GNUNET_CONVERSATION_EC_TERMINATED:
203 GNUNET_break ( (CS_RING == state) ||
204 (CS_ACCEPTED == state) );
206 _("Call terminated: %s\n"),
207 va_arg (va, const char *));
221 struct GNUNET_GNSRECORD_Data rd;
223 if (NULL == caller_id)
226 _("Ego `%s' no longer available, phone is now down.\n"),
228 state = CS_LOOKUP_EGO;
231 phone = GNUNET_CONVERSATION_phone_create (cfg,
233 &phone_event_handler, NULL);
234 /* FIXME: get record and print full GNS record info later here... */
239 _("Failed to setup phone (internal error)\n"));
244 GNUNET_CONVERSATION_phone_get_record (phone,
246 GNUNET_free_non_null (address);
247 address = GNUNET_GNSRECORD_value_to_string (rd.record_type,
252 _("Phone active on line %u\n"),
253 (unsigned int) line);
260 * Function called with an event emitted by a phone.
263 * @param code type of the event on the phone
264 * @param ... additional information, depends on @a code
267 call_event_handler (void *cls,
268 enum GNUNET_CONVERSATION_EventCode code,
276 case GNUNET_CONVERSATION_EC_RING:
279 case GNUNET_CONVERSATION_EC_RINGING:
280 GNUNET_break (CS_RESOLVING == state);
284 _("Resolved address. Now ringing other party.\n"));
287 case GNUNET_CONVERSATION_EC_READY:
288 GNUNET_break (CS_RINGING == state);
290 _("Connection established to `%s': %s\n"),
292 va_arg (va, const char *));
293 state = CS_CONNECTED;
295 case GNUNET_CONVERSATION_EC_GNS_FAIL:
296 GNUNET_break (CS_RESOLVING == state);
298 _("Failed to resolve `%s'\n"),
303 case GNUNET_CONVERSATION_EC_BUSY:
304 GNUNET_break (CS_RINGING == state);
311 case GNUNET_CONVERSATION_EC_TERMINATED:
312 GNUNET_break ( (CS_RINGING == state) ||
313 (CS_CONNECTED == state) );
315 _("Call terminated: %s\n"),
316 va_arg (va, const char *));
326 * Function declareation for executing a action
328 * @param arguments arguments given to the function
330 typedef void (*ActionFunction) (const char *arguments);
334 * Structure which defines a command
339 * Command the user needs to enter.
344 * Function to call on command.
346 ActionFunction Action;
349 * Help text for the command.
351 const char *helptext;
356 * Action function to print help for the command shell.
358 * @param args arguments given to the command
361 do_help (const char *args);
365 * Terminate the client
367 * @param args arguments given to the command
370 do_quit (const char *args)
372 GNUNET_SCHEDULER_shutdown ();
377 * Handler for unknown command.
379 * @param msg arguments given to the command
382 do_unknown (const char *msg)
385 _("Unknown command `%s'\n"),
391 * Initiating a new call
393 * @param arg arguments given to the command
396 do_call (const char *arg)
398 if (NULL == caller_id)
401 _("Ego `%s' not available\n"),
409 _("Ego `%s' not available\n"),
417 _("Hanging up on incoming phone call from `%s' to call `%s'.\n"),
420 GNUNET_CONVERSATION_phone_hang_up (phone, NULL);
424 _("You are already in a conversation with `%s', refusing to call `%s'.\n"),
431 _("Aborting call to `%s'\n"),
433 GNUNET_CONVERSATION_call_stop (call, NULL);
438 _("You are already in a conversation with `%s', refusing to call `%s'.\n"),
446 GNUNET_assert (NULL == call);
449 GNUNET_CONVERSATION_phone_destroy (phone);
452 GNUNET_free_non_null (peer_name);
453 peer_name = GNUNET_strdup (arg);
454 call = GNUNET_CONVERSATION_call_start (cfg,
459 &call_event_handler, NULL);
460 state = CS_RESOLVING;
465 * Accepting an incoming call
467 * @param args arguments given to the command
470 do_accept (const char *args)
478 _("There is no incoming call to be accepted!\n"));
481 /* this is the expected state */
485 _("You are already in a conversation with `%s'.\n"),
491 _("You are trying to call `%s', cannot accept incoming calls right now.\n"),
496 _("You are already in a conversation with `%s'.\n"),
500 GNUNET_assert (NULL != phone);
501 GNUNET_CONVERSATION_phone_pick_up (phone,
510 * Print address information for this phone.
512 * @param args arguments given to the command
515 do_address (const char *args)
521 _("We currently do not have an address.\n"));
531 * Accepting an incoming call
533 * @param args arguments given to the command
536 do_status (const char *args)
542 _("We are currently trying to locate the private key for the ego `%s'.\n"),
547 _("We are listening for incoming calls for ego `%s' on line %u.\n"),
553 _("The phone is rining. `%s' is trying to call us.\n"),
559 _("You are having a conversation with `%s'.\n"),
564 _("We are trying to find the network address to call `%s'.\n"),
569 _("We are calling `%s', his phone should be ringing.\n"),
574 _("We had an internal error setting up our phone line. You can still make calls.\n"));
583 * @param args arguments given to the command
586 do_reject (const char *args)
595 _("There is no call that could be cancelled right now.\n"));
602 /* expected state, do rejection logic */
607 GNUNET_assert (NULL != phone);
608 GNUNET_CONVERSATION_phone_hang_up (phone,
614 GNUNET_CONVERSATION_call_stop (call, args);
622 * List of supported commands.
624 static struct VoipCommand commands[] = {
625 {"/address", &do_address,
626 gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
628 gettext_noop ("Use `/call USER.gnu' to call USER")},
629 {"/accept", &do_accept,
630 gettext_noop ("Use `/accept MESSAGE' to accept an incoming call")},
631 {"/cancel", &do_reject,
632 gettext_noop ("Use `/cancel MESSAGE' to reject or terminate a call")},
633 {"/status", &do_status,
634 gettext_noop ("Use `/status' to print status information")},
636 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
638 gettext_noop ("Use `/help command' to get help for a specific command")},
646 * Action function to print help for the command shell.
648 * @param args arguments given to the command
651 do_help (const char *args)
656 while ( (NULL != args) &&
657 (0 != strlen (args)) &&
658 (commands[i].Action != &do_help))
661 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
665 gettext (commands[i].helptext));
673 "Available commands:\n");
674 while (commands[i].Action != &do_help)
678 gettext (commands[i].command));
686 gettext (commands[i].helptext));
691 * Task run during shutdown.
697 do_stop_task (void *cls,
698 const struct GNUNET_SCHEDULER_TaskContext *tc)
702 GNUNET_CONVERSATION_call_stop (call, NULL);
707 GNUNET_CONVERSATION_phone_destroy (phone);
710 if (GNUNET_SCHEDULER_NO_TASK != handle_cmd_task)
712 GNUNET_SCHEDULER_cancel (handle_cmd_task);
713 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
717 GNUNET_IDENTITY_disconnect (id);
720 GNUNET_SPEAKER_destroy (speaker);
722 GNUNET_MICROPHONE_destroy (mic);
724 GNUNET_free (ego_name);
726 GNUNET_CONFIGURATION_destroy (cfg);
728 GNUNET_free_non_null (peer_name);
734 * Task to handle commands from the terminal.
737 * @param tc scheduler context
740 handle_command (void *cls,
741 const struct GNUNET_SCHEDULER_TaskContext *tc)
743 char message[MAX_MESSAGE_LENGTH + 1];
748 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
750 &handle_command, NULL);
751 /* read message from command line and handle it */
752 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
753 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
755 if (0 == strlen (message))
757 if (message[strlen (message) - 1] == '\n')
758 message[strlen (message) - 1] = '\0';
759 if (0 == strlen (message))
762 while ((NULL != commands[i].command) &&
763 (0 != strncasecmp (commands[i].command, message,
764 strlen (commands[i].command))))
766 ptr = &message[strlen (commands[i].command)];
767 while (isspace ((int) *ptr))
769 commands[i].Action (ptr);
774 * Function called by identity service with information about egos.
777 * @param ego ego handle
779 * @param name name of the ego
782 identity_cb (void *cls,
783 struct GNUNET_IDENTITY_Ego *ego,
789 if (ego == caller_id)
793 _("Name of our ego changed to `%s'\n"),
795 GNUNET_free (ego_name);
796 ego_name = GNUNET_strdup (name);
799 if (0 != strcmp (name,
806 _("Our ego `%s' was deleted!\n"),
812 GNUNET_CONFIGURATION_set_value_number (cfg,
821 * Main function that will be run by the scheduler.
824 * @param args remaining command-line arguments
825 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
826 * @param c configuration
832 const struct GNUNET_CONFIGURATION_Handle *c)
834 cfg = GNUNET_CONFIGURATION_dup (c);
835 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
836 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
837 if (NULL == ego_name)
841 _("You must specify the NAME of an ego to use\n"));
844 id = GNUNET_IDENTITY_connect (cfg,
848 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
849 &handle_command, NULL);
850 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
856 * The main function to conversation.
858 * @param argc number of arguments from the command line
859 * @param argv command line arguments
860 * @return 0 ok, 1 on error
863 main (int argc, char *const *argv)
865 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
867 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
868 1, &GNUNET_GETOPT_set_string, &ego_name},
869 {'p', "phone", "LINE",
870 gettext_noop ("sets the LINE to use for the phone"),
871 1, &GNUNET_GETOPT_set_uint, &line},
872 GNUNET_GETOPT_OPTION_END
877 flags = fcntl (0, F_GETFL, 0);
879 fcntl (0, F_SETFL, flags);
880 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
881 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
883 ret = GNUNET_PROGRAM_run (argc, argv,
884 "gnunet-conversation",
885 gettext_noop ("Enables having a conversation with other GNUnet users."),
886 options, &run, NULL);
887 GNUNET_free ((void *) argv);
888 return (GNUNET_OK == ret) ? 0 : 1;
891 /* end of gnunet-conversation.c */