6c3db21849655ceef259706b7e1242840247eff2
[oweals/gnunet.git] / src / conversation / gnunet-conversation.c
1 /*
2   This file is part of GNUnet.
3   Copyright (C) 2013 GNUnet e.V.
4
5   GNUnet is free software: you can redistribute it and/or modify it
6   under the terms of the GNU Affero General Public License as published
7   by the Free Software Foundation, either version 3 of the License,
8   or (at your option) any later version.
9
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   Affero General Public License for more details.
14  
15   You should have received a copy of the GNU Affero General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 /**
21  * @file conversation/gnunet-conversation.c
22  * @brief conversation implementation
23  * @author Simon Dieterle
24  * @author Andreas Fuchs
25  */
26 #include "platform.h"
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"
32 #ifdef WINDOWS
33 #include "../util/gnunet-helper-w32-console.h"
34 #endif
35
36 /**
37  * Maximum length allowed for the command line input.
38  */
39 #define MAX_MESSAGE_LENGTH 1024
40
41 #define XSTRINGIFY(x) STRINGIFY(x)
42
43 #define STRINGIFY(x) (#x)
44
45 #ifdef WINDOWS
46 /**
47  * Helper that reads the console for us.
48  */
49 struct GNUNET_HELPER_Handle *stdin_hlp;
50 #endif
51
52 /**
53  * Possible states of the phone.
54  */
55 enum PhoneState
56 {
57   /**
58    * We're waiting for our own idenitty.
59    */
60   PS_LOOKUP_EGO,
61
62   /**
63    * We're listening for calls
64    */
65   PS_LISTEN,
66
67   /**
68    * We accepted an incoming phone call.
69    */
70   PS_ACCEPTED,
71
72   /**
73    * Internal error
74    */
75   PS_ERROR
76 };
77
78
79 /**
80  * States for current outgoing call.
81  */
82 enum CallState
83 {
84   /**
85    * We are looking up some other participant.
86    */
87   CS_RESOLVING,
88
89   /**
90    * We are now ringing the other participant.
91    */
92   CS_RINGING,
93
94   /**
95    * The other party accepted our call and we are now connected.
96    */
97   CS_CONNECTED,
98
99   /**
100    * The call is currently suspended (by us).
101    */
102   CS_SUSPENDED
103
104 };
105
106
107 /**
108  * List of incoming calls
109  */
110 struct CallList
111 {
112
113   /**
114    * A DLL.
115    */
116   struct CallList *prev;
117
118   /**
119    * A DLL.
120    */
121   struct CallList *next;
122
123   /**
124    * Handle to hang up or activate.
125    */
126   struct GNUNET_CONVERSATION_Caller *caller;
127
128   /**
129    * Public key identifying the caller.
130    */
131   struct GNUNET_CRYPTO_EcdsaPublicKey caller_id;
132
133   /**
134    * Unique number of the call.
135    */
136   unsigned int caller_num;
137
138 };
139
140
141
142 /**
143  * Phone handle
144  */
145 static struct GNUNET_CONVERSATION_Phone *phone;
146
147 /**
148  * Call handle (for active outgoing call).
149  */
150 static struct GNUNET_CONVERSATION_Call *call;
151
152 /**
153  * Caller handle (for active incoming call).
154  * This call handler is NOT in the #cl_head / #cl_tail list.
155  */
156 static struct CallList *cl_active;
157
158 /**
159  * Head of calls waiting to be accepted.
160  */
161 static struct CallList *cl_head;
162
163 /**
164  * Tail of calls waiting to be accepted.
165  */
166 static struct CallList *cl_tail;
167
168 /**
169  * Desired phone line (string to be converted to a hash).
170  */
171 static char *line;
172
173 /**
174  * Task which handles the commands
175  */
176 static struct GNUNET_SCHEDULER_Task * handle_cmd_task;
177
178 /**
179  * Our speaker.
180  */
181 static struct GNUNET_SPEAKER_Handle *speaker;
182
183 /**
184  * Our microphone.
185  */
186 static struct GNUNET_MICROPHONE_Handle *mic;
187
188 /**
189  * Our configuration.
190  */
191 static struct GNUNET_CONFIGURATION_Handle *cfg;
192
193 /**
194  * Our ego.
195  */
196 static struct GNUNET_IDENTITY_Ego *my_caller_id;
197
198 /**
199  * Handle to identity service.
200  */
201 static struct GNUNET_IDENTITY_Handle *id;
202
203 /**
204  * Name of our ego.
205  */
206 static char *ego_name;
207
208 /**
209  * Public key of active conversation partner (if any).
210  */
211 static struct GNUNET_CRYPTO_EcdsaPublicKey peer_key;
212
213 /**
214  * Name of active conversation partner (if any).
215  */
216 static char *peer_name;
217
218 /**
219  * File handle for stdin.
220  */
221 static struct GNUNET_DISK_FileHandle *stdin_fh;
222
223 /**
224  * Our phone's current state.
225  */
226 static enum PhoneState phone_state;
227
228 /**
229  * Our call's current state.
230  */
231 static enum CallState call_state;
232
233 /**
234  * Counts the number of incoming calls we have had so far.
235  */
236 static unsigned int caller_num_gen;
237
238 /**
239  * GNS address for this phone.
240  */
241 static char *address;
242
243 /**
244  * Be verbose.
245  */
246 static int verbose;
247
248
249 /**
250  * Function called with an event emitted by a phone.
251  *
252  * @param cls closure
253  * @param code type of the event
254  * @param caller handle for the caller
255  * @param caller_id public key of the caller (in GNS)
256  */
257 static void
258 phone_event_handler (void *cls,
259                      enum GNUNET_CONVERSATION_PhoneEventCode code,
260                      struct GNUNET_CONVERSATION_Caller *caller,
261                      const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
262 {
263   struct CallList *cl;
264
265   (void) cls;
266   switch (code)
267   {
268   case GNUNET_CONVERSATION_EC_PHONE_RING:
269     /*
270      * FIXME: we should be playing our ringtones from contrib/sounds now!
271      *
272     ring_my_bell();
273      *
274      * see https://gstreamer.freedesktop.org/documentation/application-development/highlevel/playback-components.html on how to play a wav using the gst framework being used here
275      */
276     FPRINTF (stdout,
277              _("Incoming call from `%s'. Please /accept %u or /cancel %u the call.\n"),
278              GNUNET_GNSRECORD_pkey_to_zkey (caller_id),
279              caller_num_gen,
280              caller_num_gen);
281     cl = GNUNET_new (struct CallList);
282     cl->caller = caller;
283     cl->caller_id = *caller_id;
284     cl->caller_num = caller_num_gen++;
285     GNUNET_CONTAINER_DLL_insert (cl_head,
286                                  cl_tail,
287                                  cl);
288     break;
289   case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
290     for (cl = cl_head; NULL != cl; cl = cl->next)
291       if (caller == cl->caller)
292         break;
293     if ( (NULL == cl) &&
294          (caller == cl_active->caller) )
295       cl = cl_active;
296     if (NULL == cl)
297     {
298       GNUNET_break (0);
299       return;
300     }
301     FPRINTF (stdout,
302              _("Call from `%s' terminated\n"),
303              GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
304     if (cl == cl_active)
305     {
306       cl_active = NULL;
307       phone_state = PS_LISTEN;
308     }
309     else
310     {
311       GNUNET_CONTAINER_DLL_remove (cl_head,
312                                    cl_tail,
313                                    cl);
314     }
315     GNUNET_free (cl);
316     break;
317   }
318 }
319
320
321 /**
322  * Function called with an event emitted by a caller.
323  *
324  * @param cls closure with the `struct CallList` of the caller
325  * @param code type of the event issued by the caller
326  */
327 static void
328 caller_event_handler (void *cls,
329                       enum GNUNET_CONVERSATION_CallerEventCode code)
330 {
331   struct CallList *cl = cls;
332
333   switch (code)
334   {
335   case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
336     FPRINTF (stdout,
337              _("Call from `%s' suspended by other user\n"),
338              GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
339     break;
340   case GNUNET_CONVERSATION_EC_CALLER_RESUME:
341     FPRINTF (stdout,
342              _("Call from `%s' resumed by other user\n"),
343              GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
344     break;
345   }
346 }
347
348
349 /**
350  * Start our phone.
351  */
352 static void
353 start_phone ()
354 {
355   struct GNUNET_GNSRECORD_Data rd;
356
357   if (NULL == my_caller_id)
358   {
359     FPRINTF (stderr,
360              _("Ego `%s' no longer available, phone is now down.\n"),
361              ego_name);
362     phone_state = PS_LOOKUP_EGO;
363     return;
364   }
365   GNUNET_assert (NULL == phone);
366   phone = GNUNET_CONVERSATION_phone_create (cfg,
367                                             my_caller_id,
368                                             &phone_event_handler,
369                                             NULL);
370   /* FIXME: get record and print full GNS record info later here... */
371   if (NULL == phone)
372   {
373     FPRINTF (stderr,
374              "%s",
375              _("Failed to setup phone (internal error)\n"));
376     phone_state = PS_ERROR;
377   }
378   else
379   {
380     GNUNET_CONVERSATION_phone_get_record (phone,
381                                           &rd);
382     GNUNET_free_non_null (address);
383     address = GNUNET_GNSRECORD_value_to_string (rd.record_type,
384                                                 rd.data,
385                                                 rd.data_size);
386     FPRINTF (stdout,
387              _("Phone active at `%s'.  Type `/help' for a list of available commands\n"),
388              address);
389     phone_state = PS_LISTEN;
390   }
391 }
392
393
394 /**
395  * Function called with an event emitted by a call.
396  *
397  * @param cls closure, NULL
398  * @param code type of the event on the call
399  */
400 static void
401 call_event_handler (void *cls,
402                     enum GNUNET_CONVERSATION_CallEventCode code)
403 {
404   (void) cls;
405   
406   switch (code)
407   {
408   case GNUNET_CONVERSATION_EC_CALL_RINGING:
409     GNUNET_break (CS_RESOLVING == call_state);
410     FPRINTF (stdout,
411              _("Resolved address of `%s'. Now ringing other party.\n"),
412              peer_name);
413     call_state = CS_RINGING;
414     break;
415   case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
416     GNUNET_break (CS_RINGING == call_state);
417     FPRINTF (stdout,
418              _("Connection established to `%s'\n"),
419              peer_name);
420     call_state = CS_CONNECTED;
421     break;
422   case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
423     GNUNET_break (CS_RESOLVING == call_state);
424     FPRINTF (stdout,
425              _("Failed to resolve `%s'\n"),
426              peer_name);
427     GNUNET_free (peer_name);
428     peer_name = NULL;
429     call = NULL;
430     break;
431   case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
432     FPRINTF (stdout,
433              _("Call to `%s' terminated\n"),
434              peer_name);
435     GNUNET_free (peer_name);
436     peer_name = NULL;
437     call = NULL;
438     break;
439   case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
440     GNUNET_break (CS_CONNECTED == call_state);
441     FPRINTF (stdout,
442              _("Connection to `%s' suspended (by other user)\n"),
443              peer_name);
444     break;
445   case GNUNET_CONVERSATION_EC_CALL_RESUMED:
446     GNUNET_break (CS_CONNECTED == call_state);
447     FPRINTF (stdout,
448              _("Connection to `%s' resumed (by other user)\n"),
449              peer_name);
450     break;
451   case GNUNET_CONVERSATION_EC_CALL_ERROR:
452     FPRINTF (stdout,
453              _("Error with the call, restarting it\n"));
454     GNUNET_free (peer_name);
455     peer_name = NULL;
456     call = NULL;
457     break;
458   }
459 }
460
461
462 /**
463  * Function declareation for executing a action
464  *
465  * @param arguments arguments given to the function
466  */
467 typedef void
468 (*ActionFunction) (const char *arguments);
469
470
471 /**
472  * Structure which defines a command
473  */
474 struct VoipCommand
475 {
476   /**
477    * Command the user needs to enter.
478    */
479   const char *command;
480
481   /**
482    * Function to call on command.
483    */
484   ActionFunction Action;
485
486   /**
487    * Help text for the command.
488    */
489   const char *helptext;
490 };
491
492
493 /**
494  * Action function to print help for the command shell.
495  *
496  * @param args arguments given to the command
497  */
498 static void
499 do_help (const char *args);
500
501
502 /**
503  * Terminate the client
504  *
505  * @param args arguments given to the command
506  */
507 static void
508 do_quit (const char *args)
509 {
510   (void) args;
511   GNUNET_SCHEDULER_shutdown ();
512 }
513
514
515 /**
516  * Handler for unknown command.
517  *
518  * @param msg arguments given to the command
519  */
520 static void
521 do_unknown (const char *msg)
522 {
523   FPRINTF (stderr,
524            _("Unknown command `%s'\n"),
525            msg);
526 }
527
528
529 /**
530  * Initiating a new call
531  *
532  * @param arg arguments given to the command
533  */
534 static void
535 do_call (const char *arg)
536 {
537   if (NULL == my_caller_id)
538   {
539     FPRINTF (stderr,
540              _("Ego `%s' not available\n"),
541              ego_name);
542     return;
543   }
544   if (NULL != call)
545   {
546     FPRINTF (stderr,
547              _("You are calling someone else already, hang up first!\n"));
548     return;
549   }
550   switch (phone_state)
551   {
552   case PS_LOOKUP_EGO:
553     FPRINTF (stderr,
554              _("Ego `%s' not available\n"),
555              ego_name);
556     return;
557   case PS_LISTEN:
558     /* ok to call! */
559     break;
560   case PS_ACCEPTED:
561     FPRINTF (stderr,
562              _("You are answering call from `%s', hang up or suspend that call first!\n"),
563              GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
564     return;
565   case PS_ERROR:
566     /* ok to call */
567     break;
568   }
569   if (NULL == arg)
570   {
571     FPRINTF (stderr,
572              _("Call recipient missing.\n"));
573     do_help ("/call");
574     return;
575   }
576   peer_name = GNUNET_strdup (arg);
577   call_state = CS_RESOLVING;
578   GNUNET_assert (NULL == call);
579   call = GNUNET_CONVERSATION_call_start (cfg,
580                                          my_caller_id,
581                                          arg,
582                                          speaker,
583                                          mic,
584                                          &call_event_handler, NULL);
585 }
586
587
588 /**
589  * Accepting an incoming call
590  *
591  * @param args arguments given to the command
592  */
593 static void
594 do_accept (const char *args)
595 {
596   struct CallList *cl;
597   char buf[32];
598
599   if ( (NULL != call) &&
600        (CS_SUSPENDED != call_state) )
601   {
602     FPRINTF (stderr,
603              _("You are calling someone else already, hang up first!\n"));
604     return;
605   }
606   switch (phone_state)
607   {
608   case PS_LOOKUP_EGO:
609     GNUNET_break (0);
610     break;
611   case PS_LISTEN:
612     /* this is the expected state */
613     break;
614   case PS_ACCEPTED:
615     FPRINTF (stderr,
616              _("You are answering call from `%s', hang up or suspend that call first!\n"),
617              GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
618     return;
619   case PS_ERROR:
620     GNUNET_break (0);
621     break;
622   }
623   cl = cl_head;
624   if (NULL == cl)
625   {
626     FPRINTF (stderr,
627              _("There is no incoming call to accept here!\n"));
628     return;
629   }
630   if ( (NULL != cl->next) || (NULL != args) )
631   {
632     for (cl = cl_head; NULL != cl; cl = cl->next)
633     {
634       GNUNET_snprintf (buf, sizeof (buf),
635                        "%u",
636                        cl->caller_num);
637       if (0 == strcmp (buf, args))
638         break;
639     }
640   }
641   if (NULL == cl)
642   {
643     FPRINTF (stderr,
644              _("There is no incoming call `%s' to accept right now!\n"),
645              args);
646     return;
647   }
648   GNUNET_CONTAINER_DLL_remove (cl_head,
649                                cl_tail,
650                                cl);
651   cl_active = cl;
652   peer_key = cl->caller_id;
653   phone_state = PS_ACCEPTED;
654   GNUNET_CONVERSATION_caller_pick_up (cl->caller,
655                                       &caller_event_handler,
656                                       cl,
657                                       speaker,
658                                       mic);
659 }
660
661
662 /**
663  * Print address information for this phone.
664  *
665  * @param args arguments given to the command
666  */
667 static void
668 do_address (const char *args)
669 {
670   (void) args;
671   if (NULL == address)
672   {
673     FPRINTF (stdout,
674              "%s",
675              _("We currently do not have an address.\n"));
676     return;
677   }
678   FPRINTF (stdout,
679            "%s\n",
680            address);
681 }
682
683
684 /**
685  * Accepting an incoming call
686  *
687  * @param args arguments given to the command
688  */
689 static void
690 do_status (const char *args)
691 {
692   struct CallList *cl;
693
694   (void) args;
695   switch (phone_state)
696   {
697   case PS_LOOKUP_EGO:
698     FPRINTF (stdout,
699              _("We are currently trying to locate the private key for the ego `%s'.\n"),
700              ego_name);
701     break;
702   case PS_LISTEN:
703     FPRINTF (stdout,
704              _("We are listening for incoming calls for ego `%s' on line `%s'.\n"),
705              ego_name,
706              line);
707     break;
708   case PS_ACCEPTED:
709     FPRINTF (stdout,
710              _("You are having a conversation with `%s'.\n"),
711              GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));;
712     break;
713   case PS_ERROR:
714     FPRINTF (stdout,
715              _("We had an internal error setting up our phone line. You can still make calls.\n"));
716     break;
717   }
718   if (NULL != call)
719   {
720     switch (call_state)
721     {
722     case CS_RESOLVING:
723       FPRINTF (stdout,
724                _("We are trying to find the network address to call `%s'.\n"),
725                peer_name);
726       break;
727     case CS_RINGING:
728       FPRINTF (stdout,
729                _("We are calling `%s', their phone should be ringing.\n"),
730                peer_name);
731       break;
732     case CS_CONNECTED:
733       FPRINTF (stdout,
734                _("You are having a conversation with `%s'.\n"),
735                peer_name);
736       break;
737     case CS_SUSPENDED:
738       /* ok to accept incoming call right now */
739       break;
740     }
741   }
742   if ( (NULL != cl_head) &&
743        ( (cl_head != cl_active) ||
744          (cl_head != cl_tail) ) )
745   {
746     FPRINTF (stdout,
747              "%s",
748              _("Calls waiting:\n"));
749     for (cl = cl_head; NULL != cl; cl = cl->next)
750     {
751       if (cl == cl_active)
752         continue;
753       FPRINTF (stdout,
754                _("#%u: `%s'\n"),
755                cl->caller_num,
756                GNUNET_GNSRECORD_pkey_to_zkey (&cl->caller_id));
757     }
758     FPRINTF (stdout,
759              "%s",
760              "\n");
761   }
762 }
763
764
765 /**
766  * Suspending a call
767  *
768  * @param args arguments given to the command
769  */
770 static void
771 do_suspend (const char *args)
772 {
773   (void) args;
774   if (NULL != call)
775   {
776     switch (call_state)
777     {
778     case CS_RESOLVING:
779     case CS_RINGING:
780     case CS_SUSPENDED:
781       FPRINTF (stderr,
782                "%s",
783                _("There is no call that could be suspended right now.\n"));
784       return;
785     case CS_CONNECTED:
786       call_state = CS_SUSPENDED;
787       GNUNET_CONVERSATION_call_suspend (call);
788       return;
789     }
790   }
791   switch (phone_state)
792   {
793   case PS_LOOKUP_EGO:
794   case PS_LISTEN:
795   case PS_ERROR:
796     FPRINTF (stderr,
797              "%s",
798              _("There is no call that could be suspended right now.\n"));
799     return;
800   case PS_ACCEPTED:
801     /* expected state, do rejection logic */
802     break;
803   }
804   GNUNET_assert (NULL != cl_active);
805   GNUNET_CONVERSATION_caller_suspend (cl_active->caller);
806   cl_active = NULL;
807   phone_state = PS_LISTEN;
808 }
809
810
811 /**
812  * Resuming a call
813  *
814  * @param args arguments given to the command
815  */
816 static void
817 do_resume (const char *args)
818 {
819   struct CallList *cl;
820   char buf[32];
821
822   if (NULL != call)
823   {
824     switch (call_state)
825     {
826     case CS_RESOLVING:
827     case CS_RINGING:
828     case CS_CONNECTED:
829       FPRINTF (stderr,
830                "%s",
831                _("There is no call that could be resumed right now.\n"));
832       return;
833     case CS_SUSPENDED:
834       call_state = CS_CONNECTED;
835       GNUNET_CONVERSATION_call_resume (call,
836                                        speaker,
837                                        mic);
838       return;
839     }
840   }
841   switch (phone_state)
842   {
843   case PS_LOOKUP_EGO:
844   case PS_ERROR:
845     FPRINTF (stderr,
846              "%s",
847              _("There is no call that could be resumed right now.\n"));
848     return;
849   case PS_LISTEN:
850     /* expected state, do resume logic */
851     break;
852   case PS_ACCEPTED:
853     FPRINTF (stderr,
854              _("Already talking with `%s', cannot resume a call right now.\n"),
855              GNUNET_GNSRECORD_pkey_to_zkey (&peer_key));
856     return;
857   }
858   GNUNET_assert (NULL == cl_active);
859   cl = cl_head;
860   if (NULL == cl)
861   {
862     FPRINTF (stderr,
863              _("There is no incoming call to resume here!\n"));
864     return;
865   }
866   if ( (NULL != cl->next) || (NULL != args) )
867   {
868     for (cl = cl_head; NULL != cl; cl = cl->next)
869     {
870       GNUNET_snprintf (buf, sizeof (buf),
871                        "%u",
872                        cl->caller_num);
873       if (0 == strcmp (buf, args))
874         break;
875     }
876   }
877   if (NULL == cl)
878   {
879     FPRINTF (stderr,
880              _("There is no incoming call `%s' to resume right now!\n"),
881              args);
882     return;
883   }
884   cl_active = cl;
885   GNUNET_CONVERSATION_caller_resume (cl_active->caller,
886                                      speaker,
887                                      mic);
888   phone_state = PS_ACCEPTED;
889 }
890
891
892 /**
893  * Rejecting a call
894  *
895  * @param args arguments given to the command
896  */
897 static void
898 do_reject (const char *args)
899 {
900   struct CallList *cl;
901   char buf[32];
902
903   if (NULL != call)
904   {
905     GNUNET_CONVERSATION_call_stop (call);
906     call = NULL;
907     return;
908   }
909   switch (phone_state)
910   {
911   case PS_LOOKUP_EGO:
912   case PS_ERROR:
913     FPRINTF (stderr,
914              "%s",
915              _("There is no call that could be cancelled right now.\n"));
916     return;
917   case PS_LISTEN:
918     /* look for active incoming calls to refuse */
919     cl = cl_head;
920     if (NULL == cl)
921     {
922       FPRINTF (stderr,
923                _("There is no incoming call to refuse here!\n"));
924       return;
925     }
926     if ( (NULL != cl->next) || (NULL != args) )
927     {
928       for (cl = cl_head; NULL != cl; cl = cl->next)
929       {
930         GNUNET_snprintf (buf, sizeof (buf),
931                          "%u",
932                          cl->caller_num);
933         if (0 == strcmp (buf, args))
934           break;
935       }
936     }
937     if (NULL == cl)
938     {
939       FPRINTF (stderr,
940                _("There is no incoming call `%s' to refuse right now!\n"),
941                args);
942       return;
943     }
944     GNUNET_CONVERSATION_caller_hang_up (cl->caller);
945     GNUNET_CONTAINER_DLL_remove (cl_head,
946                                  cl_tail,
947                                  cl);
948     GNUNET_free (cl);
949     break;
950   case PS_ACCEPTED:
951     /* expected state, do rejection logic */
952     GNUNET_assert (NULL != cl_active);
953     GNUNET_CONVERSATION_caller_hang_up (cl_active->caller);
954     cl_active = NULL;
955     phone_state = PS_LISTEN;
956     break;
957   }
958 }
959
960
961 /**
962  * List of supported commands.
963  */
964 static struct VoipCommand commands[] = {
965   {"/address", &do_address,
966    gettext_noop ("Use `/address' to find out which address this phone should have in GNS")},
967   {"/call", &do_call,
968    gettext_noop ("Use `/call USER.gnu' to call USER")},
969   {"/accept", &do_accept,
970    gettext_noop ("Use `/accept #NUM' to accept incoming call #NUM")},
971   {"/suspend", &do_suspend,
972    gettext_noop ("Use `/suspend' to suspend the active call")},
973   {"/resume", &do_resume,
974    gettext_noop ("Use `/resume [#NUM]' to resume a call, #NUM is needed to resume incoming calls, no argument is needed to resume the current outgoing call.")},
975   {"/cancel", &do_reject,
976    gettext_noop ("Use `/cancel' to reject or terminate a call")},
977   {"/status", &do_status,
978    gettext_noop ("Use `/status' to print status information")},
979   {"/quit", &do_quit,
980    gettext_noop ("Use `/quit' to terminate gnunet-conversation")},
981   {"/help", &do_help,
982    gettext_noop ("Use `/help command' to get help for a specific command")},
983   {"", &do_unknown,
984    NULL},
985   {NULL, NULL, NULL},
986 };
987
988
989 /**
990  * Action function to print help for the command shell.
991  *
992  * @param args arguments given to the command
993  */
994 static void
995 do_help (const char *args)
996 {
997   unsigned int i;
998
999   i = 0;
1000   while ( (NULL != args) &&
1001           (0 != strlen (args)) &&
1002           (commands[i].Action != &do_help))
1003   {
1004     if (0 ==
1005         strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
1006     {
1007       FPRINTF (stdout,
1008                "%s\n",
1009                gettext (commands[i].helptext));
1010       return;
1011     }
1012     i++;
1013   }
1014   i = 0;
1015   FPRINTF (stdout,
1016            "%s",
1017            "Available commands:\n");
1018   while (commands[i].Action != &do_help)
1019   {
1020     FPRINTF (stdout,
1021              "%s\n",
1022              gettext (commands[i].command));
1023     i++;
1024   }
1025   FPRINTF (stdout,
1026            "%s",
1027            "\n");
1028   FPRINTF (stdout,
1029            "%s\n",
1030            gettext (commands[i].helptext));
1031 }
1032
1033
1034 /**
1035  * Task run during shutdown.
1036  *
1037  * @param cls NULL
1038  */
1039 static void
1040 do_stop_task (void *cls)
1041 {
1042   (void) cls;
1043 #ifdef WINDOWS
1044   if (NULL != stdin_hlp)
1045   {
1046     GNUNET_HELPER_stop (stdin_hlp, GNUNET_NO);
1047     stdin_hlp = NULL;
1048   }
1049 #endif
1050   if (NULL != call)
1051   {
1052     GNUNET_CONVERSATION_call_stop (call);
1053     call = NULL;
1054   }
1055   if (NULL != phone)
1056   {
1057     GNUNET_CONVERSATION_phone_destroy (phone);
1058     phone = NULL;
1059   }
1060   if (NULL != handle_cmd_task)
1061   {
1062     GNUNET_SCHEDULER_cancel (handle_cmd_task);
1063     handle_cmd_task = NULL;
1064   }
1065   if (NULL != id)
1066   {
1067     GNUNET_IDENTITY_disconnect (id);
1068     id = NULL;
1069   }
1070   GNUNET_SPEAKER_destroy (speaker);
1071   speaker = NULL;
1072   GNUNET_MICROPHONE_destroy (mic);
1073   mic = NULL;
1074   GNUNET_free (ego_name);
1075   ego_name = NULL;
1076   GNUNET_free_non_null (peer_name);
1077   peer_name = NULL;
1078   phone_state = PS_ERROR;
1079 }
1080
1081
1082 /**
1083  * Handle user command.
1084  *
1085  * @param message command the user typed in
1086  * @param str_len number of bytes to process in @a message
1087  */
1088 static void
1089 handle_command_string (char *message,
1090                        size_t str_len)
1091 {
1092   size_t i;
1093   const char *ptr;
1094
1095   if (0 == str_len)
1096     return;
1097   if (message[str_len - 1] == '\n')
1098     message[str_len - 1] = '\0';
1099   if (message[str_len - 2] == '\r')
1100     message[str_len - 2] = '\0';
1101   if (0 == strlen (message))
1102     return;
1103   i = 0;
1104   while ((NULL != commands[i].command) &&
1105          (0 != strncasecmp (commands[i].command, message,
1106                             strlen (commands[i].command))))
1107     i++;
1108   ptr = &message[strlen (commands[i].command)];
1109   while (isspace ((unsigned char) *ptr))
1110     ptr++;
1111   if ('\0' == *ptr)
1112     ptr = NULL;
1113   commands[i].Action (ptr);
1114 }
1115
1116
1117 #ifdef WINDOWS
1118 static int
1119 console_reader_chars (void *cls,
1120                       void *client,
1121                       const struct GNUNET_MessageHeader *message)
1122 {
1123   char *chars;
1124   size_t str_size;
1125
1126   (void) cls;
1127   switch (ntohs (message->type))
1128   {
1129   case GNUNET_MESSAGE_TYPE_W32_CONSOLE_HELPER_CHARS:
1130     chars = (char *) &message[1];
1131     str_size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
1132     if (chars[str_size - 1] != '\0')
1133       return GNUNET_SYSERR;
1134     /* FIXME: is it ok that we pass part of a const struct to
1135      * this function that may mangle the contents?
1136      */
1137     handle_command_string (chars, str_size - 1);
1138     break;
1139   default:
1140     GNUNET_break (0);
1141     break;
1142   }
1143   return GNUNET_OK;
1144 }
1145 #endif
1146
1147
1148 /**
1149  * Task to handle commands from the terminal.
1150  *
1151  * @param cls NULL
1152  */
1153 static void
1154 handle_command (void *cls)
1155 {
1156   char message[MAX_MESSAGE_LENGTH + 1];
1157
1158   (void) cls;
1159   handle_cmd_task =
1160     GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1161                                     stdin_fh,
1162                                     &handle_command, NULL);
1163   /* read message from command line and handle it */
1164   memset (message,
1165           0,
1166           MAX_MESSAGE_LENGTH + 1);
1167   if (NULL == fgets (message,
1168                      MAX_MESSAGE_LENGTH,
1169                      stdin))
1170     return;
1171   handle_command_string (message,
1172                          strlen (message));
1173 }
1174
1175
1176 /**
1177  * Function called by identity service with information about egos.
1178  *
1179  * @param cls NULL
1180  * @param ego ego handle
1181  * @param ctx unused
1182  * @param name name of the ego
1183  */
1184 static void
1185 identity_cb (void *cls,
1186              struct GNUNET_IDENTITY_Ego *ego,
1187              void **ctx,
1188              const char *name)
1189 {
1190   (void) cls;
1191   (void) ctx;
1192   if (NULL == name)
1193     return;
1194   if (ego == my_caller_id)
1195   {
1196     if (verbose)
1197       FPRINTF (stdout,
1198                _("Name of our ego changed to `%s'\n"),
1199                name);
1200     GNUNET_free (ego_name);
1201     ego_name = GNUNET_strdup (name);
1202     return;
1203   }
1204   if (0 != strcmp (name,
1205                    ego_name))
1206     return;
1207   if (NULL == ego)
1208   {
1209     if (verbose)
1210       FPRINTF (stdout,
1211                _("Our ego `%s' was deleted!\n"),
1212                ego_name);
1213     my_caller_id = NULL;
1214     return;
1215   }
1216   my_caller_id = ego;
1217   GNUNET_CONFIGURATION_set_value_string (cfg,
1218                                          "CONVERSATION",
1219                                          "LINE",
1220                                          line);
1221   start_phone ();
1222 }
1223
1224
1225 /**
1226  * Main function that will be run by the scheduler.
1227  *
1228  * @param cls closure
1229  * @param args remaining command-line arguments
1230  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1231  * @param c configuration
1232  */
1233 static void
1234 run (void *cls,
1235      char *const *args,
1236      const char *cfgfile,
1237      const struct GNUNET_CONFIGURATION_Handle *c)
1238 {
1239   (void) cls;
1240   (void) args;
1241   (void) cfgfile;
1242   cfg = GNUNET_CONFIGURATION_dup (c);
1243   speaker = GNUNET_SPEAKER_create_from_hardware (cfg);
1244   mic = GNUNET_MICROPHONE_create_from_hardware (cfg);
1245   if (NULL == ego_name)
1246   {
1247     FPRINTF (stderr,
1248              "%s",
1249              _("You must specify the NAME of an ego to use\n"));
1250     return;
1251   }
1252   id = GNUNET_IDENTITY_connect (cfg,
1253                                 &identity_cb,
1254                                 NULL);
1255 #ifdef WINDOWS
1256   if (stdin_fh == NULL)
1257   {
1258     static char cpid[64];
1259     static char *args[] = {"gnunet-helper-w32-console.exe", "chars",
1260         XSTRINGIFY (MAX_MESSAGE_LENGTH), cpid, NULL};
1261     snprintf (cpid, 64, "%d", GetCurrentProcessId ());
1262     stdin_hlp = GNUNET_HELPER_start (
1263         GNUNET_NO,
1264         "gnunet-helper-w32-console",
1265         args,
1266         console_reader_chars,
1267         NULL,
1268         NULL);
1269     if (NULL == stdin_hlp)
1270     {
1271       FPRINTF (stderr,
1272                "%s",
1273                _("Failed to start gnunet-helper-w32-console\n"));
1274       return;
1275     }
1276   }
1277   else
1278 #endif
1279   handle_cmd_task =
1280     GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
1281                                         &handle_command, NULL);
1282   GNUNET_SCHEDULER_add_shutdown (&do_stop_task,
1283                                  NULL);
1284 }
1285
1286
1287 /**
1288  * The main function to conversation.
1289  *
1290  * @param argc number of arguments from the command line
1291  * @param argv command line arguments
1292  * @return 0 ok, 1 on error
1293  */
1294 int
1295 main (int argc,
1296       char *const *argv)
1297 {
1298   struct GNUNET_GETOPT_CommandLineOption options[] = {
1299     GNUNET_GETOPT_option_string ('e',
1300                                  "ego",
1301                                  "NAME",
1302                                  gettext_noop ("sets the NAME of the ego to use for the caller ID"),
1303                                  &ego_name),
1304     GNUNET_GETOPT_option_string ('p',
1305                                  "phone",
1306                                  "LINE",
1307                                  gettext_noop ("sets the LINE to use for the phone"),
1308                                  &line),
1309     GNUNET_GETOPT_OPTION_END
1310   };
1311   int ret;
1312 #ifndef WINDOWS
1313   int flags;
1314   flags = fcntl (0, F_GETFL, 0);
1315   flags |= O_NONBLOCK;
1316   if (0 != fcntl (0, F_SETFL, flags))
1317     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1318                          "fcntl");
1319   stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1320 #else
1321   if (FILE_TYPE_CHAR == GetFileType ((HANDLE) _get_osfhandle (0)))
1322   {
1323     stdin_fh = NULL;
1324   }
1325   else
1326     stdin_fh = GNUNET_DISK_get_handle_from_int_fd (0);
1327 #endif
1328
1329   if (GNUNET_OK !=
1330       GNUNET_STRINGS_get_utf8_args (argc, argv,
1331                                     &argc, &argv))
1332     return 2;
1333   ret = GNUNET_PROGRAM_run (argc,
1334                             argv,
1335                             "gnunet-conversation",
1336                             gettext_noop ("Enables having a conversation with other GNUnet users."),
1337                             options,
1338                             &run, NULL);
1339   GNUNET_free ((void *) argv);
1340   if (NULL != cfg)
1341   {
1342     GNUNET_CONFIGURATION_destroy (cfg);
1343     cfg = NULL;
1344   }
1345   return (GNUNET_OK == ret) ? 0 : 1;
1346 }
1347
1348 /* end of gnunet-conversation.c */