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"
33 * Maximum length allowed for the command line input.
35 #define MAX_MESSAGE_LENGTH 1024
39 * Possible states of the program.
41 enum ConversationState
44 * We're waiting for our own idenitty.
49 * We're listening for calls
54 * Our phone is ringing.
59 * We accepted an incoming phone call.
64 * We are looking up some other participant.
69 * We are now ringing the other participant.
74 * The other party accepted our call and we are now connected.
89 static struct GNUNET_CONVERSATION_Phone *phone;
94 static struct GNUNET_CONVERSATION_Call *call;
99 static unsigned int line;
102 * Task which handles the commands
104 static GNUNET_SCHEDULER_TaskIdentifier handle_cmd_task;
109 static struct GNUNET_SPEAKER_Handle *speaker;
114 static struct GNUNET_MICROPHONE_Handle *mic;
119 static struct GNUNET_CONFIGURATION_Handle *cfg;
124 static struct GNUNET_IDENTITY_Ego *caller_id;
127 * Handle to identity service.
129 static struct GNUNET_IDENTITY_Handle *id;
134 static char *ego_name;
137 * Name of conversation partner (if any).
139 static char *peer_name;
142 * File handle for stdin.
144 static struct GNUNET_DISK_FileHandle *stdin_fh;
149 static enum ConversationState state;
158 * Function called with an event emitted by a phone.
161 * @param code type of the event on the phone
162 * @param ... additional information, depends on @a code
165 phone_event_handler (void *cls,
166 enum GNUNET_CONVERSATION_EventCode code,
174 case GNUNET_CONVERSATION_EC_RING:
175 GNUNET_break (CS_LISTEN == state);
176 GNUNET_free_non_null (peer_name);
177 peer_name = GNUNET_strdup (va_arg (va, const char *));
179 _("Incoming call from `%s'.\nPlease /accept or /cancel the call.\n"),
183 case GNUNET_CONVERSATION_EC_RINGING:
186 case GNUNET_CONVERSATION_EC_READY:
189 case GNUNET_CONVERSATION_EC_GNS_FAIL:
192 case GNUNET_CONVERSATION_EC_BUSY:
195 case GNUNET_CONVERSATION_EC_TERMINATED:
196 GNUNET_break ( (CS_RING == state) ||
197 (CS_ACCEPTED == state) );
199 _("Call terminated: %s\n"),
200 va_arg (va, const char *));
214 if (NULL == caller_id)
217 _("Ego `%s' no longer available, phone is now down.\n"),
219 state = CS_LOOKUP_EGO;
222 phone = GNUNET_CONVERSATION_phone_create (cfg,
224 &phone_event_handler, NULL);
225 /* FIXME: get record and print full GNS record info later here... */
230 _("Failed to setup phone (internal error)\n"));
237 _("Phone active on line %u\n"),
238 (unsigned int) line);
245 * Function called with an event emitted by a phone.
248 * @param code type of the event on the phone
249 * @param ... additional information, depends on @a code
252 call_event_handler (void *cls,
253 enum GNUNET_CONVERSATION_EventCode code,
261 case GNUNET_CONVERSATION_EC_RING:
264 case GNUNET_CONVERSATION_EC_RINGING:
265 GNUNET_break (CS_RESOLVING == state);
269 _("Resolved address. Now ringing other party.\n"));
272 case GNUNET_CONVERSATION_EC_READY:
273 GNUNET_break (CS_RINGING == state);
275 _("Connection established: %s\n"),
276 va_arg (va, const char *));
277 state = CS_CONNECTED;
279 case GNUNET_CONVERSATION_EC_GNS_FAIL:
280 GNUNET_break (CS_RESOLVING == state);
283 _("Failed to resolve name\n"));
284 GNUNET_CONVERSATION_call_stop (call, NULL);
288 case GNUNET_CONVERSATION_EC_BUSY:
289 GNUNET_break (CS_RINGING == state);
293 GNUNET_CONVERSATION_call_stop (call, NULL);
297 case GNUNET_CONVERSATION_EC_TERMINATED:
298 GNUNET_break ( (CS_RINGING == state) ||
299 (CS_CONNECTED == state) );
301 _("Call terminated: %s\n"),
302 va_arg (va, const char *));
303 GNUNET_CONVERSATION_call_stop (call, NULL);
313 * Function declareation for executing a action
315 * @param arguments arguments given to the function
317 typedef void (*ActionFunction) (const char *arguments);
321 * Structure which defines a command
326 * Command the user needs to enter.
331 * Function to call on command.
333 ActionFunction Action;
336 * Help text for the command.
338 const char *helptext;
343 * Action function to print help for the command shell.
345 * @param arguments arguments given to the command
348 do_help (const char *args);
352 * Terminate the client
354 * @param args arguments given to the command
357 do_quit (const char *args)
359 GNUNET_SCHEDULER_shutdown ();
364 * Handler for unknown command.
366 * @param args arguments given to the command
369 do_unknown (const char *msg)
372 _("Unknown command `%s'\n"),
378 * Initiating a new call
380 * @param args arguments given to the command
383 do_call (const char *arg)
385 if (NULL == caller_id)
388 _("Ego `%s' not available\n"),
396 _("Ego `%s' not available\n"),
404 _("Hanging up on incoming phone call from `%s' to call `%s'.\n"),
407 GNUNET_CONVERSATION_phone_hang_up (phone, NULL);
411 _("You are already in a conversation with `%s', refusing to call `%s'.\n"),
418 _("Aborting call to `%s'\n"),
420 GNUNET_CONVERSATION_call_stop (call, NULL);
425 _("You are already in a conversation with `%s', refusing to call `%s'.\n"),
433 GNUNET_assert (NULL == call);
436 GNUNET_CONVERSATION_phone_destroy (phone);
439 GNUNET_free_non_null (peer_name);
440 peer_name = GNUNET_strdup (arg);
441 call = GNUNET_CONVERSATION_call_start (cfg,
446 &call_event_handler, NULL);
447 state = CS_RESOLVING;
452 * Accepting an incoming call
454 * @param args arguments given to the command
457 do_accept (const char *args)
465 _("There is no incoming call to be accepted!\n"));
468 /* this is the expected state */
472 _("You are already in a conversation with `%s'.\n"),
478 _("You are trying to call `%s', cannot accept incoming calls right now.\n"),
483 _("You are already in a conversation with `%s'.\n"),
487 GNUNET_assert (NULL != phone);
488 GNUNET_CONVERSATION_phone_pick_up (phone,
497 * Accepting an incoming call
499 * @param args arguments given to the command
502 do_status (const char *args)
508 _("We are currently trying to locate the private key for the ego `%s'.\n"),
513 _("We are listening for incoming calls for ego `%s' on line %u.\n"),
519 _("The phone is rining. `%s' is trying to call us.\n"),
525 _("You are having a conversation with `%s'.\n"),
530 _("We are trying to find the network address to call `%s'.\n"),
535 _("We are calling `%s', his phone should be ringing.\n"),
540 _("We had an internal error setting up our phone line. You can still make calls.\n"));
549 * @param args arguments given to the command
552 do_reject (const char *args)
561 _("There is no call that could be cancelled right now.\n"));
568 /* expected state, do rejection logic */
573 GNUNET_assert (NULL != phone);
574 GNUNET_CONVERSATION_phone_hang_up (phone,
580 GNUNET_CONVERSATION_call_stop (call, args);
588 * List of supported commands.
590 static struct VoipCommand commands[] = {
592 gettext_noop ("Use `/call USER.gnu'")},
593 {"/accept", &do_accept,
594 gettext_noop ("Use `/accept MESSAGE' to accept an incoming call")},
595 {"/cancel", &do_reject,
596 gettext_noop ("Use `/cancel MESSAGE' to reject or terminate a call")},
597 {"/status", &do_status,
598 gettext_noop ("Use `/status to print status information")},
600 gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
602 gettext_noop ("Use `/help command' to get help for a specific command")},
610 * Action function to print help for the command shell.
612 * @param arguments arguments given to the command
615 do_help (const char *args)
620 while ( (NULL != args) &&
621 (0 != strlen (args)) &&
622 (commands[i].Action != &do_help))
625 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
629 gettext (commands[i].helptext));
637 "Available commands:\n");
638 while (commands[i].Action != &do_help)
642 gettext (commands[i].command));
650 gettext (commands[i].helptext));
655 * Task run during shutdown.
661 do_stop_task (void *cls,
662 const struct GNUNET_SCHEDULER_TaskContext *tc)
666 GNUNET_CONVERSATION_call_stop (call, NULL);
671 GNUNET_CONVERSATION_phone_destroy (phone);
674 if (GNUNET_SCHEDULER_NO_TASK != handle_cmd_task)
676 GNUNET_SCHEDULER_cancel (handle_cmd_task);
677 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
681 GNUNET_IDENTITY_disconnect (id);
684 GNUNET_SPEAKER_destroy (speaker);
686 GNUNET_MICROPHONE_destroy (mic);
688 GNUNET_free (ego_name);
690 GNUNET_CONFIGURATION_destroy (cfg);
692 GNUNET_free_non_null (peer_name);
698 * Task to handle commands from the terminal.
701 * @param tc scheduler context
704 handle_command (void *cls,
705 const struct GNUNET_SCHEDULER_TaskContext *tc)
707 char message[MAX_MESSAGE_LENGTH + 1];
712 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
714 &handle_command, NULL);
715 /* read message from command line and handle it */
716 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
717 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
719 if (0 == strlen (message))
721 if (message[strlen (message) - 1] == '\n')
722 message[strlen (message) - 1] = '\0';
723 if (0 == strlen (message))
726 while ((NULL != commands[i].command) &&
727 (0 != strncasecmp (commands[i].command, message,
728 strlen (commands[i].command))))
730 ptr = &message[strlen (commands[i].command)];
731 while (isspace ((int) *ptr))
733 commands[i].Action (ptr);
738 * Function called by identity service with information about egos.
741 * @param ego ego handle
743 * @param name name of the ego
746 identity_cb (void *cls,
747 struct GNUNET_IDENTITY_Ego *ego,
753 if (ego == caller_id)
757 _("Name of our ego changed to `%s'\n"),
759 GNUNET_free (ego_name);
760 ego_name = GNUNET_strdup (name);
763 if (0 != strcmp (name,
770 _("Our ego `%s' was deleted!\n"),
776 GNUNET_CONFIGURATION_set_value_number (cfg,
785 * Main function that will be run by the scheduler.
788 * @param args remaining command-line arguments
789 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
790 * @param c configuration
796 const struct GNUNET_CONFIGURATION_Handle *c)
798 cfg = GNUNET_CONFIGURATION_dup (c);
799 speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
800 mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
801 if (NULL == ego_name)
805 _("You must specify the NAME of an ego to use\n"));
808 id = GNUNET_IDENTITY_connect (cfg,
812 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
813 &handle_command, NULL);
814 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
820 * The main function to conversation.
822 * @param argc number of arguments from the command line
823 * @param argv command line arguments
824 * @return 0 ok, 1 on error
827 main (int argc, char *const *argv)
829 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
831 gettext_noop ("sets the NAME of the ego to use for the phone (and name resolution)"),
832 1, &GNUNET_GETOPT_set_string, &ego_name},
833 {'p', "phone", "LINE",
834 gettext_noop ("sets the LINE to use for the phone"),
835 1, &GNUNET_GETOPT_set_uint, &line},
836 GNUNET_GETOPT_OPTION_END
841 flags = fcntl (0, F_GETFL, 0);
843 fcntl (0, F_SETFL, flags);
844 stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
845 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
847 ret = GNUNET_PROGRAM_run (argc, argv,
848 "gnunet-conversation",
849 gettext_noop ("Enables having a conversation with other GNUnet users."),
850 options, &run, NULL);
851 GNUNET_free ((void *) argv);
852 return (GNUNET_OK == ret) ? 0 : 1;
855 /* end of gnunet-conversation.c */