]> sjero.net Git - linphone/blob - console/commands.c
Merge branch 'master' of belledonne-communications.com:linphone-private
[linphone] / console / commands.c
1 /****************************************************************************
2  *
3  *  $Id: commands.c,v 1.39 2008/07/03 15:08:34 smorlat Exp $
4  *
5  *  Copyright (C) 2006-2009  Sandro Santilli <strk@keybit.net>
6  *  Copyright (C) 2004  Simon MORLAT <simon.morlat@linphone.org>
7  *
8 ****************************************************************************
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  *
24  ****************************************************************************/
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #ifndef _WIN32_WCE
30 #include <errno.h>
31 #include <unistd.h>
32 #endif /*_WIN32_WCE*/
33 #include <limits.h>
34 #include <ctype.h>
35 #include <linphonecore.h>
36 #include "linphonec.h"
37 #include "private.h"
38 #include "lpconfig.h"
39
40 #ifndef WIN32
41 #include <sys/wait.h>
42 #endif
43
44 #define AUDIO 0
45 #define VIDEO 1
46
47 /***************************************************************************
48  *
49  *  Forward declarations 
50  *
51  ***************************************************************************/
52
53 extern char *lpc_strip_blanks(char *input);
54
55 /* Command handlers */
56 static int lpc_cmd_help(LinphoneCore *, char *);
57 static int lpc_cmd_proxy(LinphoneCore *, char *);
58 static int lpc_cmd_call(LinphoneCore *, char *);
59 static int lpc_cmd_chat(LinphoneCore *, char *);
60 static int lpc_cmd_answer(LinphoneCore *, char *);
61 static int lpc_cmd_autoanswer(LinphoneCore *, char *);
62 static int lpc_cmd_terminate(LinphoneCore *, char *);
63 static int lpc_cmd_call_logs(LinphoneCore *, char *);
64 static int lpc_cmd_ipv6(LinphoneCore *, char *);
65 static int lpc_cmd_refer(LinphoneCore *, char *);
66 static int lpc_cmd_quit(LinphoneCore *, char *);
67 static int lpc_cmd_nat(LinphoneCore *, char *);
68 static int lpc_cmd_stun(LinphoneCore *, char *);
69 static int lpc_cmd_firewall(LinphoneCore *, char *);
70 static int lpc_cmd_friend(LinphoneCore *, char*);
71 static int lpc_cmd_soundcard(LinphoneCore *, char *);
72 static int lpc_cmd_webcam(LinphoneCore *, char *);
73 static int lpc_cmd_staticpic(LinphoneCore *, char *);
74 static int lpc_cmd_play(LinphoneCore *, char *);
75 static int lpc_cmd_record(LinphoneCore *, char *);
76 static int lpc_cmd_register(LinphoneCore *, char *);
77 static int lpc_cmd_unregister(LinphoneCore *, char *);
78 static int lpc_cmd_duration(LinphoneCore *lc, char *args);
79 static int lpc_cmd_status(LinphoneCore *lc, char *args);
80 static int lpc_cmd_ports(LinphoneCore *lc, char *args);
81 static int lpc_cmd_speak(LinphoneCore *lc, char *args);
82 static int lpc_cmd_acodec(LinphoneCore *lc, char *args);
83 static int lpc_cmd_vcodec(LinphoneCore *lc, char *args);
84 static int lpc_cmd_codec(int type, LinphoneCore *lc, char *args);
85 static int lpc_cmd_echocancellation(LinphoneCore *lc, char *args);
86 static int lpc_cmd_mute_mic(LinphoneCore *lc, char *args);
87 static int lpc_cmd_unmute_mic(LinphoneCore *lc, char *args);
88 static int lpc_cmd_rtp_no_xmit_on_audio_mute(LinphoneCore *lc, char *args);
89
90 /* Command handler helpers */
91 static void linphonec_proxy_add(LinphoneCore *lc);
92 static void linphonec_proxy_display(LinphoneProxyConfig *lc);
93 static void linphonec_proxy_list(LinphoneCore *lc);
94 static void linphonec_proxy_remove(LinphoneCore *lc, int index);
95 static  int linphonec_proxy_use(LinphoneCore *lc, int index);
96 static void linphonec_proxy_show(LinphoneCore *lc,int index);
97 static void linphonec_friend_display(LinphoneFriend *fr);
98 static int linphonec_friend_list(LinphoneCore *lc, char *arg);
99 static void linphonec_display_command_help(LPC_COMMAND *cmd);
100 static int linphonec_friend_call(LinphoneCore *lc, unsigned int num);
101 #ifndef WIN32
102 static int linphonec_friend_add(LinphoneCore *lc, const char *name, const char *addr);
103 #endif
104 static int linphonec_friend_delete(LinphoneCore *lc, int num);
105 static int linphonec_friend_delete(LinphoneCore *lc, int num);
106 static void linphonec_codec_list(int type, LinphoneCore *lc);
107 static void linphonec_codec_enable(int type, LinphoneCore *lc, int index);
108 static void linphonec_codec_disable(int type, LinphoneCore *lc, int index);
109
110
111
112 /* Command table management */
113 static LPC_COMMAND *lpc_find_command(const char *name);
114
115 void linphonec_out(const char *fmt,...);
116
117
118
119 /***************************************************************************
120  *
121  *  Global variables
122  *
123  ***************************************************************************/
124
125 /*
126  * Commands table.
127  */
128 LPC_COMMAND commands[] = {
129         { "help", lpc_cmd_help, "Print commands help", NULL },
130         { "call", lpc_cmd_call, "Call a SIP uri",
131                 "'call <sip-url>' "
132                 ": initiate a call to the specified destination."
133                 },
134         { "chat", lpc_cmd_chat, "Chat with a SIP uri",
135                 "'chat <sip-url> \"message\"' "
136                 ": send a chat message \"message\" to the specified destination."
137                 },
138         { "terminate", lpc_cmd_terminate, "Terminate the current call",
139                 NULL },
140         { "answer", lpc_cmd_answer, "Answer a call",
141                 "Accept an incoming call."
142         },
143         { "autoanswer", lpc_cmd_autoanswer, "Show/set auto-answer mode",
144                 "'autoanswer'       \t: show current autoanswer mode\n"
145                 "'autoanswer enable'\t: enable autoanswer mode\n"
146                 "'autoanswer disable'\t: disable autoanswer modeĀ \n"},
147         { "proxy", lpc_cmd_proxy, "Manage proxies",
148                 "'proxy list' : list all proxy setups.\n"
149                 "'proxy add' : add a new proxy setup.\n"
150                 "'proxy remove <index>' : remove proxy setup with number index.\n"
151                 "'proxy use <index>' : use proxy with number index as default proxy.\n"
152                 "'proxy unuse' : don't use a default proxy.\n"
153                 "'proxy show <index>' : show configuration and status of the proxy numbered by index.\n"
154                 "'proxy show default' : show configuration and status of the default proxy.\n"
155         },
156         { "soundcard", lpc_cmd_soundcard, "Manage soundcards",
157                 "'soundcard list' : list all sound devices.\n"
158                 "'soundcard show' : show current sound devices configuration.\n"
159                 "'soundcard use <index>' : select a sound device.\n"
160                 "'soundcard use files' : use .wav files instead of soundcard\n"
161         },
162         { "webcam", lpc_cmd_webcam, "Manage webcams",
163                 "'webcam list' : list all known devices.\n"
164                 "'webcam use <index>' : select a video device.\n"
165         },
166         { "staticpic", lpc_cmd_staticpic, "Manage static pictures when nowebcam",
167                 "'staticpic set' : Set path to picture that should be used.\n"
168         },
169         { "ipv6", lpc_cmd_ipv6, "Use IPV6",
170                 "'ipv6 status' : show ipv6 usage status.\n"
171                 "'ipv6 enable' : enable the use of the ipv6 network.\n"
172                 "'ipv6 disable' : do not use ipv6 network."
173         },
174         { "refer", lpc_cmd_refer,
175                 "Refer the current call to the specified destination.",
176                 "'refer <sip-url>' or 'r <sip-url>' "
177                 ": refer the current call to the specified destination."
178         },
179         { "nat", lpc_cmd_nat, "Set nat address",
180                 "'nat'        : show nat settings.\n"
181                 "'nat <addr>' : set nat address.\n"
182         },
183         { "stun", lpc_cmd_stun, "Set stun server address",
184                 "'stun'        : show stun settings.\n"
185                 "'stun <addr>' : set stun server address.\n"
186         },
187         { "firewall", lpc_cmd_firewall, "Set firewall policy",
188                 "'firewall'        : show current firewall policy.\n"
189                 "'firewall none'   : use direct connection.\n"
190                 "'firewall nat'    : use nat address given with the 'nat' command.\n"
191                 "'firewall stun'   : use stun server given with the 'stun' command.\n"
192         },
193         { "call-logs", lpc_cmd_call_logs, "Calls history", NULL },
194         { "friend", lpc_cmd_friend, "Manage friends",
195                 "'friend list [<pattern>]'    : list friends.\n"
196                 "'friend call <index>'        : call a friend.\n"
197                 "'friend add <name> <addr>'   : add friend, <name> must be quoted to include\n"
198             "                               spaces, <addr> has \"sip:\" added if it isn't\n"
199             "                               there.  Don't use '<' '>' around <addr>.\n"
200                 "'friend delete <index>'      : remove friend, 'all' removes all\n"
201         },
202         { "play", lpc_cmd_play, "play from a wav file",
203                 "This feature is available only in file mode (see 'help soundcard')\n"
204                 "'play <wav file>'    : play a wav file."
205         },
206         { "record", lpc_cmd_record, "record to a wav file",
207                 "This feature is available only in file mode (see 'help soundcard')\n"
208                 "'record <wav file>'    : record into wav file."
209         },
210         { "quit", lpc_cmd_quit, "Exit linphonec", NULL },
211         { "register", lpc_cmd_register, "Register in one line to a proxy" , "register <sip identity> <sip proxy> <password>"},
212         { "unregister", lpc_cmd_unregister, "Unregister from default proxy", NULL       },
213         { "duration", lpc_cmd_duration, "Print duration in seconds of the last call.", NULL },
214         { "status", lpc_cmd_status, "Print various status information", 
215                         "'status register'  \t: print status concerning registration\n"
216                         "'status autoanswer'\t: tell whether autoanswer mode is enabled\n"
217                         "'status hook'      \t: print hook status\n" },
218         { "ports", lpc_cmd_ports, "Network ports configuration", 
219                         "'ports'  \t: prints current used ports.\n"
220                         "'ports sip <port number>'\t: Sets the sip port.\n" },
221         { "speak", lpc_cmd_speak, "Speak a sentence using espeak TTS engine",
222                         "This feature is available only in file mode. (see 'help soundcard')\n"
223                         "'speak <voice name> <sentence>'        : speak a text using the specified espeak voice.\n"
224                         "Example for english voice: 'speak default Hello my friend !'"
225         },
226     { "codec", lpc_cmd_acodec, "Audio codec configuration",
227             "'codec list' : list audio codecs\n"
228             "'codec enable <index>' : enable available audio codec\n"
229             "'codec disable <index>' : disable audio codec" },
230     { "vcodec", lpc_cmd_vcodec, "Video codec configuration",
231             "'vcodec list' : list video codecs\n"
232             "'vcodec enable <index>' : enable available video codec\n"
233             "'vcodec disable <index>' : disable video codec" },
234     { "ec", lpc_cmd_echocancellation, "Echo cancellation",
235             "'ec on [<delay>] [<tail>] [<framesize>]' : turn EC on with given delay, tail length and framesize\n"
236             "'ec off' : turn echo cancellation (EC) off\n"
237             "'ec show' : show EC status" },
238         { "mute", lpc_cmd_mute_mic, 
239           "Mute microphone and suspend voice transmission."},
240         { "unmute", lpc_cmd_unmute_mic, 
241           "Unmute microphone and resume voice transmission."},
242         { "nortp-on-audio-mute", lpc_cmd_rtp_no_xmit_on_audio_mute,
243           "Set the rtp_no_xmit_on_audio_mute configuration parameter",
244           "   If set to 1 then rtp transmission will be muted when\n"
245           "   audio is muted , otherwise rtp is always sent."}, 
246         { (char *)NULL, (lpc_cmd_handler)NULL, (char *)NULL, (char *)NULL }
247 };
248
249 /***************************************************************************
250  *
251  *  Public interface 
252  *
253  ***************************************************************************/
254
255 /*
256  * Main command dispatcher.
257  * WARNING: modifies second argument!
258  *
259  * Always return 1 currently.
260  */
261 int
262 linphonec_parse_command_line(LinphoneCore *lc, char *cl)
263 {
264         char *ptr=cl;
265         char *args=NULL;
266         LPC_COMMAND *cmd;
267
268         /* Isolate first word and args */
269         while(*ptr && !isspace(*ptr)) ++ptr;
270         if (*ptr)
271         {
272                 *ptr='\0';
273                 /* set args to first nonblank */
274                 args=ptr+1;
275                 while(*args && isspace(*args)) ++args;
276         }
277
278         /* Handle DTMF */
279         if ( isdigit(*cl) || *cl == '#' || *cl == '*' )
280         {
281                 while ( isdigit(*cl) || *cl == '#' || *cl == '*' )
282                 {
283                         linphone_core_send_dtmf(lc, *cl);
284                         linphone_core_play_dtmf (lc,*cl,100);
285                         ms_sleep(1); // be nice
286                         ++cl;
287                 }
288
289                 // discard spurious trailing chars
290                 return 1;
291         }
292
293         /* Handle other kind of commands */
294         cmd=lpc_find_command(cl);
295         if ( !cmd )
296         {
297                 linphonec_out("'%s': Cannot understand this.\n", cl);
298                 return 1;
299         }
300
301         if ( ! cmd->func(lc, args) )
302         {
303                 linphonec_out("Syntax error.\n");
304                 linphonec_display_command_help(cmd);
305         }
306
307         return 1;
308 }
309
310 /*
311  * Generator function for command completion.
312  * STATE let us know whether to start from scratch;
313  * without any state (STATE==0), then we start at the
314  * top of the list.
315  */
316 char *
317 linphonec_command_generator(const char *text, int state)
318 {
319         static int index, len;
320         char *name;
321
322         if ( ! state )
323         {
324                 index=0;
325                 len=strlen(text);
326         }
327
328         /*
329          * Return the next name which partially matches
330          * from the commands list
331          */
332         while ((name=commands[index].name))
333         {
334                 ++index; /* so next call get next command */
335
336                 if (strncmp(name, text, len) == 0)
337                 {
338                         return ortp_strdup(name);
339                 }
340         }
341
342         return NULL;
343 }
344
345
346 /***************************************************************************
347  *
348  *  Command handlers 
349  *
350  ***************************************************************************/
351
352 static int
353 lpc_cmd_help(LinphoneCore *lc, char *arg)
354 {
355         int i=0;
356         LPC_COMMAND *cmd;
357
358         if (!arg || !*arg)
359         {
360                 linphonec_out("Commands are:\n");
361                 linphonec_out("---------------------------\n");
362
363                 while (commands[i].help)
364                 {
365                         linphonec_out("%10.10s\t%s\n", commands[i].name,
366                                 commands[i].help);
367                         i++;
368                 }
369                 
370                 linphonec_out("---------------------------\n");
371                 linphonec_out("Type 'help <command>' for more details.\n");
372
373                 return 1;
374         }
375
376         cmd=lpc_find_command(arg);
377         if ( !cmd )
378         {
379                 linphonec_out("No such command.\n");
380                 return 1;
381         }
382
383         linphonec_display_command_help(cmd);
384         return 1;
385
386 }
387
388 static char callee_name[256]={0};
389 static char caller_name[256]={0};
390
391 static int
392 lpc_cmd_call(LinphoneCore *lc, char *args)
393 {
394         if ( ! args || ! *args )
395         {
396                 return 0;
397         }
398
399         if ( lc->call != NULL )
400         {
401                 linphonec_out("Terminate current call first.\n");
402         }
403         else
404         {
405                 if ( -1 == linphone_core_invite(lc, args) )
406                 {
407                         linphonec_out("Error from linphone_core_invite.\n");
408                 }
409                 else
410                 {
411                         snprintf(callee_name,sizeof(callee_name),"%s",args);
412                 }
413         }
414         return 1;
415 }
416
417 static int
418 lpc_cmd_chat(LinphoneCore *lc, char *args)
419 {
420         char *arg1 = args;
421         char *arg2 = NULL;
422         char *ptr = args;
423
424         if (!args) return 0;
425
426         /* Isolate first and second arg */
427         while(*ptr && !isspace(*ptr)) ++ptr;
428         if ( *ptr )
429         {
430                 *ptr='\0';
431                 arg2=ptr+1;
432                 while(*arg2 && isspace(*arg2)) ++arg2;
433         }
434         else
435         {
436                 /* missing one parameter */
437                 return 0;
438         }
439         LinphoneChatRoom *cr = linphone_core_create_chat_room(lc,arg1);
440         linphone_chat_room_send_message(cr,arg2);
441         linphone_chat_room_destroy(cr);
442
443         return 1;
444 }
445
446 const char *linphonec_get_callee(){
447         return callee_name;
448 }
449
450 const char *linphonec_get_caller(){
451         return caller_name;
452 }
453
454 void linphonec_set_caller(const char *caller){
455         snprintf(caller_name,sizeof(caller_name)-1,"%s",caller);
456 }
457
458 static int
459 lpc_cmd_refer(LinphoneCore *lc, char *args)
460 {
461         if (args)
462                 linphone_core_refer(lc, args);
463         else{
464                 linphonec_out("refer needs an argument\n");
465         }
466         return 1;
467 }
468
469 static int
470 lpc_cmd_terminate(LinphoneCore *lc, char *args)
471 {
472         if ( -1 == linphone_core_terminate_call(lc, NULL) )
473         {
474                 linphonec_out("No active call.\n");
475         }
476         return 1;
477 }
478
479 static int
480 lpc_cmd_answer(LinphoneCore *lc, char *args)
481 {
482         if ( -1 == linphone_core_accept_call(lc, NULL) )
483         {
484                 linphonec_out("No incoming call.\n");
485         }
486         return 1;
487 }
488
489 static int
490 lpc_cmd_autoanswer(LinphoneCore *lc, char *args)
491 {
492         if ( ! args )
493         {
494                 if ( linphonec_get_autoanswer() ) {
495                         linphonec_out("Auto answer is enabled. Use 'autoanswer disable' to disable.\n");
496                 } else {
497                         linphonec_out("Auto answer is disabled. Use 'autoanswer enable' to enable.\n");
498                 }
499                 return 1;
500         }
501
502         if (strstr(args,"enable")){
503                 linphonec_set_autoanswer(TRUE);
504                 linphonec_out("Auto answer enabled.\n");
505         }else if (strstr(args,"disable")){
506                 linphonec_set_autoanswer(FALSE);
507                 linphonec_out("Auto answer disabled.\n");
508         }else return 0;
509         return 1;
510 }
511
512 static int
513 lpc_cmd_quit(LinphoneCore *lc, char *args)
514 {
515         linphonec_main_loop_exit();
516         return 1;
517 }
518
519 static int
520 lpc_cmd_nat(LinphoneCore *lc, char *args)
521 {
522         bool_t use;
523         const char *nat;
524
525         if ( args ) args=lpc_strip_blanks(args);
526
527         if ( args && *args )
528         {
529                 linphone_core_set_nat_address(lc, args);
530                 /* linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_NAT_ADDRESS); */
531         }
532
533         nat = linphone_core_get_nat_address(lc);
534         use = linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_NAT_ADDRESS;
535         linphonec_out("Nat address: %s%s\n", nat ? nat : "unspecified" , use ? "" : " (disabled - use 'firewall nat' to enable)");
536
537         return 1;
538 }
539
540 static int
541 lpc_cmd_stun(LinphoneCore *lc, char *args)
542 {
543         bool_t use;
544         const char *stun;
545
546         if ( args ) args=lpc_strip_blanks(args);
547
548         if ( args && *args )
549         {
550                 linphone_core_set_stun_server(lc, args);
551                 /* linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_STUN); */
552         }
553
554         stun = linphone_core_get_stun_server(lc);
555         use = linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_STUN;
556         linphonec_out("Stun server: %s%s\n", stun ? stun : "unspecified" , use? "" : " (disabled - use 'firewall stun' to enable)");
557
558         return 1;
559 }
560
561 static int
562 lpc_cmd_firewall(LinphoneCore *lc, char *args)
563 {
564         const char* setting=NULL;
565
566         if ( args ) args=lpc_strip_blanks(args);
567
568         if ( args && *args )
569         {
570                 if (strcmp(args,"none")==0)
571                 {
572                         linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_NO_FIREWALL);
573                 }
574                 else if (strcmp(args,"stun")==0)
575                 {
576                         setting = linphone_core_get_stun_server(lc);
577                         if ( ! setting )
578                         {
579                                 linphonec_out("No stun server address is defined, use 'stun <address>' first\n");
580                                 return 1;
581                         }
582                         linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_STUN);
583                 }
584                 else if (strcmp(args,"nat")==0)
585                 {
586                         setting = linphone_core_get_nat_address(lc);
587                         if ( ! setting )
588                         {
589                                 linphonec_out("No nat address is defined, use 'nat <address>' first");
590                                 return 1;
591                         }
592                         linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_NAT_ADDRESS);
593                 }
594         }
595
596         switch(linphone_core_get_firewall_policy(lc))
597         {
598                 case LINPHONE_POLICY_NO_FIREWALL:
599                         linphonec_out("No firewall\n");
600                         break;
601                 case LINPHONE_POLICY_USE_STUN:
602                         linphonec_out("Using stun server %s to discover firewall address\n", setting ? setting : linphone_core_get_stun_server(lc));
603                         break;
604                 case LINPHONE_POLICY_USE_NAT_ADDRESS:
605                         linphonec_out("Using supplied nat address %s.\n", setting ? setting : linphone_core_get_nat_address(lc));
606                         break;
607         }
608         return 1;
609 }
610
611 #ifndef WIN32
612 /* Helper function for processing freind names */
613 static int
614 lpc_friend_name(char **args, char **name)
615 {
616         /* Use space as a terminator unless quoted */
617         if (('"' == **args) || ('\'' == **args)){
618                 char *end;
619                 char delim = **args;
620                 (*args)++;
621                 end = (*args);
622                 while ((delim != *end) && ('\0' != *end)) end++;
623                 if ('\0' == *end) {
624                         fprintf(stderr, "Mismatched quotes\n");
625                         return 0;
626                 }
627                 *name = *args;
628                 *end = '\0';
629                 *args = ++end;
630         } else {
631                 *name = strsep(args, " ");
632                 
633                 if (NULL == *args) { /* Means there was no separator */
634                         fprintf(stderr, "Either name or address is missing\n");
635                         return 0;
636                 }
637                 if (NULL == *name) return 0;
638         }
639         return 1;
640 }
641 #endif
642
643 static int
644 lpc_cmd_friend(LinphoneCore *lc, char *args)
645 {
646         int friend_num;
647
648         if ( args ) args=lpc_strip_blanks(args);
649
650         if ( ! args || ! *args ) return 0;
651
652         if ( !strncmp(args, "list", 4) )
653         {
654                 return linphonec_friend_list(lc, args+4);
655                 return 1;
656         }
657         else if ( !strncmp(args, "call", 4) )
658         {
659                 args+=4;
660                 if ( ! *args ) return 0;
661                 friend_num = strtol(args, NULL, 10);
662 #ifndef _WIN32_WCE              
663                 if ( errno == ERANGE ) {
664                         linphonec_out("Invalid friend number\n");
665                         return 0;
666                 }
667 #endif /*_WIN32_WCE*/
668                 linphonec_friend_call(lc, friend_num);
669                 return 1;
670         }
671         else if ( !strncmp(args, "delete", 6) )
672         {
673                 args+=6;
674                 if ( ! *args ) return 0;
675                 while (*args == ' ') args++;
676                 if ( ! *args ) return 0;
677                 if (!strncmp(args, "all", 3))
678                 {
679                         friend_num = -1;
680                 } 
681                 else
682                 {
683                         friend_num = strtol(args, NULL, 10);
684 #ifndef _WIN32_WCE              
685                         if ( errno == ERANGE ) {
686                                 linphonec_out("Invalid friend number\n");
687                                 return 0;
688                         }
689 #endif /*_WIN32_WCE*/
690                 }
691                 linphonec_friend_delete(lc, friend_num);
692                 return 1;
693         }
694         else if ( !strncmp(args, "add", 3) )
695         {
696 #ifndef WIN32
697                 char  *name;
698                 char  addr[80];
699                 char *addr_p = addr;
700                 char *addr_orig;
701
702                 args+=3;
703                 if ( ! *args ) return 0;
704                 while (*args == ' ') args++;
705                 if ( ! *args ) return 0;
706
707                 if (!lpc_friend_name(&args,  &name)) return 0;
708
709                 while (*args == ' ') args++;
710                 if ( ! *args ) return 0;
711                 if (isdigit(*args)) {
712                         strcpy (addr, "sip:");
713                         addr_p = addr + strlen("sip:");
714                 }
715                 addr_orig = strsep(&args, " ");
716                 if (1 >= strlen(addr_orig)) {
717                         fprintf(stderr, "A single-digit address is not valid\n");
718                         return 0;
719                 }
720                 strcpy(addr_p, addr_orig);
721                 linphonec_friend_add(lc, name, addr);
722 #else
723                 LinphoneFriend *new_friend;
724                 new_friend = linphone_friend_new_with_addr(args);
725                 linphone_core_add_friend(lc, new_friend);
726 #endif
727                 return 1;
728         }
729         return 0;
730 }
731
732 static int lpc_cmd_play(LinphoneCore *lc, char *args){
733         if ( args ) args=lpc_strip_blanks(args);
734         if ( ! args || ! *args ) return 0;
735         linphone_core_set_play_file(lc,args);
736         return 1;
737 }
738
739 static int lpc_cmd_record(LinphoneCore *lc, char *args){
740         if ( args ) args=lpc_strip_blanks(args);
741         if ( ! args || ! *args ) return 0;
742         linphone_core_set_record_file(lc,args);
743         return 1;
744 }
745
746 /*
747  * Modified input
748  */
749 static int
750 lpc_cmd_proxy(LinphoneCore *lc, char *args)
751 {
752         char *arg1 = args;
753         char *arg2 = NULL;
754         char *ptr = args;
755         int proxynum;
756
757         if ( ! arg1 ) return 0;
758
759         /* Isolate first and second arg */
760         while(*ptr && !isspace(*ptr)) ++ptr;
761         if ( *ptr )
762         {
763                 *ptr='\0';
764                 arg2=ptr+1;
765                 while(*arg2 && isspace(*arg2)) ++arg2;
766         }
767
768         if (strcmp(arg1,"add")==0)
769         {
770 #ifdef HAVE_READLINE
771                 rl_inhibit_completion=1;
772 #endif
773                 linphonec_proxy_add(lc);
774 #ifdef HAVE_READLINE
775                 rl_inhibit_completion=0;
776 #endif
777         }
778         else if (strcmp(arg1,"list")==0)
779         {
780                 linphonec_proxy_list(lc);
781         }
782         else if (strcmp(arg1,"remove")==0)
783         {
784                 linphonec_proxy_remove(lc,atoi(arg2));
785         }
786         else if (strcmp(arg1,"use")==0)
787         {
788                 if ( arg2 && *arg2 )
789                 {
790                         proxynum=atoi(arg2);
791                         if ( linphonec_proxy_use(lc, proxynum) )
792                                 linphonec_out("Default proxy set to %d.\n", proxynum);
793                 }
794                 else
795                 {
796                         proxynum=linphone_core_get_default_proxy(lc, NULL);
797                         if ( proxynum == -1 ) linphonec_out("No default proxy.\n");
798                         else linphonec_out("Current default proxy is %d.\n", proxynum);
799                 }
800         }else if (strcmp(arg1, "unuse")==0){
801                 linphone_core_set_default_proxy(lc, NULL);
802                 linphonec_out("Use no proxy.\n");
803         }
804
805         else if (strcmp(arg1, "show")==0)
806         {
807                 if (arg2 && *arg2)
808                 {
809                         if (strstr(arg2,"default"))
810                         {
811                 proxynum=linphone_core_get_default_proxy(lc, NULL);
812                 if ( proxynum < 0 ) {
813                         linphonec_out("No default proxy defined\n");
814                         return 1;
815                 }
816                 linphonec_proxy_show(lc,proxynum);
817                         }
818                         else
819                         {
820                 linphonec_proxy_show(lc, atoi(arg2));
821                         }
822                 }
823                 else return 0; /* syntax error */
824         }
825
826         else
827         {
828                 return 0; /* syntax error */
829         }
830
831         return 1;
832 }
833
834 static int
835 lpc_cmd_call_logs(LinphoneCore *lc, char *args)
836 {
837         const MSList *elem=linphone_core_get_call_logs(lc);
838         for (;elem!=NULL;elem=ms_list_next(elem))
839         {
840                 LinphoneCallLog *cl=(LinphoneCallLog*)elem->data;
841                 char *str=linphone_call_log_to_str(cl);
842                 linphonec_out("%s\n",str);
843                 ms_free(str);
844         }
845         return 1;
846 }
847
848 static int
849 lpc_cmd_ipv6(LinphoneCore *lc, char *arg1)
850 {
851         if ( ! arg1 )
852         {
853                 return 0; /* syntax error */
854         }
855
856         if (strcmp(arg1,"status")==0)
857         {
858                 linphonec_out("ipv6 use enabled: %s\n",linphone_core_ipv6_enabled(lc) ? "true":"false");
859         }
860         else if (strcmp(arg1,"enable")==0)
861         {
862                 linphone_core_enable_ipv6(lc,TRUE);
863                 linphonec_out("ipv6 use enabled.\n");
864         }
865         else if (strcmp(arg1,"disable")==0)
866         {
867                 linphone_core_enable_ipv6(lc,FALSE);
868                 linphonec_out("ipv6 use disabled.\n");
869         }
870         else
871         {
872                 return 0; /* syntax error */
873         }
874         return 1;
875 }
876
877 static int devname_to_index(LinphoneCore *lc, const char *devname){
878         const char **p;
879         int i;
880         for(i=0,p=linphone_core_get_sound_devices(lc);*p!=NULL;++p,++i){
881                 if (strcmp(devname,*p)==0) return i;
882         }
883         return -1;
884 }
885
886 static const char *index_to_devname(LinphoneCore *lc, int index){
887         const char **p;
888         int i;
889         for(i=0,p=linphone_core_get_sound_devices(lc);*p!=NULL;++p,++i){
890                 if (i==index) return *p;
891         }
892         return NULL;
893 }
894
895 static int lpc_cmd_soundcard(LinphoneCore *lc, char *args)
896 {
897         int i, index;
898         const char **dev;
899         char *arg1 = args;
900         char *arg2 = NULL;
901         char *ptr = args;
902
903         if (!args) return 0; /* syntax error */
904
905         /* Isolate first and second arg */
906         while(*ptr && !isspace(*ptr)) ++ptr;
907         if ( *ptr )
908         {
909                 *ptr='\0';
910                 arg2=ptr+1;
911                 while(*arg2 && isspace(*arg2)) ++arg2;
912         }
913
914         if (strcmp(arg1, "list")==0)
915         {
916                 dev=linphone_core_get_sound_devices(lc);
917                 for(i=0; dev[i]!=NULL; ++i){
918                         linphonec_out("%i: %s\n",i,dev[i]);
919                 }
920                 return 1;
921         }
922
923         if (strcmp(arg1, "show")==0)
924         {
925                 linphonec_out("Ringer device: %s\n",
926                         linphone_core_get_ringer_device(lc));
927                 linphonec_out("Playback device: %s\n",
928                         linphone_core_get_playback_device(lc));
929                 linphonec_out("Capture device: %s\n",
930                         linphone_core_get_capture_device(lc));
931                 return 1;
932         }
933
934         if (strcmp(arg1, "use")==0 && arg2)
935         {
936                 if (strcmp(arg2, "files")==0)
937                 {
938                         linphonec_out("Using wav files instead of soundcard.\n");
939                         linphone_core_use_files(lc,TRUE);
940                         return 1;
941                 }
942
943                 dev=linphone_core_get_sound_devices(lc);
944                 index=atoi(arg2); /* FIXME: handle not-a-number */
945                 for(i=0;dev[i]!=NULL;i++)
946                 {
947                         if (i!=index) continue;
948
949                         linphone_core_set_ringer_device(lc,dev[i]);
950                         linphone_core_set_playback_device(lc,dev[i]);
951                         linphone_core_set_capture_device(lc,dev[i]);
952                         linphonec_out("Using sound device %s\n",dev[i]);
953                         return 1;
954                 }
955                 linphonec_out("No such sound device\n");
956                 return 1;
957         }
958         if (strcmp(arg1, "capture")==0)
959         {
960                 const char *devname=linphone_core_get_capture_device(lc);
961                 if (!arg2){
962                         linphonec_out("Using capture device #%i (%s)\n",
963                                         devname_to_index(lc,devname),devname);
964                 }else{
965                         index=atoi(arg2); /* FIXME: handle not-a-number */
966                         devname=index_to_devname(lc,index);
967                         if (devname!=NULL){
968                                 linphone_core_set_capture_device(lc,devname);
969                                 linphonec_out("Using capture sound device %s\n",devname);
970                                 return 1;
971                         }
972                         linphonec_out("No such sound device\n");
973                 }
974                 return 1;
975         }
976         if (strcmp(arg1, "playback")==0)
977         {
978                 const char *devname=linphone_core_get_playback_device(lc);
979                 if (!arg2){
980                         linphonec_out("Using playback device #%i (%s)\n",
981                                         devname_to_index(lc,devname),devname);
982                 }else{
983                         index=atoi(arg2); /* FIXME: handle not-a-number */
984                         devname=index_to_devname(lc,index);
985                         if (devname!=NULL){
986                                 linphone_core_set_playback_device(lc,devname);
987                                 linphonec_out("Using playback sound device %s\n",devname);
988                                 return 1;
989                         }
990                         linphonec_out("No such sound device\n");
991                 }
992                 return 1;
993         }
994         if (strcmp(arg1, "ring")==0)
995         {
996                 const char *devname=linphone_core_get_ringer_device(lc);
997                 if (!arg2){
998                         linphonec_out("Using ring device #%i (%s)\n",
999                                         devname_to_index(lc,devname),devname);
1000                 }else{
1001                         index=atoi(arg2); /* FIXME: handle not-a-number */
1002                         devname=index_to_devname(lc,index);
1003                         if (devname!=NULL){
1004                                 linphone_core_set_ringer_device(lc,devname);
1005                                 linphonec_out("Using ring sound device %s\n",devname);
1006                                 return 1;
1007                         }
1008                         linphonec_out("No such sound device\n");
1009                 }
1010                 return 1;
1011         }
1012         return 0; /* syntax error */
1013 }
1014
1015 static int lpc_cmd_webcam(LinphoneCore *lc, char *args)
1016 {
1017         int i, index;
1018         const char **dev;
1019         char *arg1 = args;
1020         char *arg2 = NULL;
1021         char *ptr = args;
1022
1023         if (!args) return 0; /* syntax error */
1024
1025         /* Isolate first and second arg */
1026         while(*ptr && !isspace(*ptr)) ++ptr;
1027         if ( *ptr )
1028         {
1029                 *ptr='\0';
1030                 arg2=ptr+1;
1031                 while(*arg2 && isspace(*arg2)) ++arg2;
1032         }
1033
1034         if (strcmp(arg1, "list")==0)
1035         {
1036                 dev=linphone_core_get_video_devices(lc);
1037                 for(i=0; dev[i]!=NULL; ++i){
1038                         linphonec_out("%i: %s\n",i,dev[i]);
1039                 }
1040                 return 1;
1041         }
1042
1043         if (strcmp(arg1, "use")==0 && arg2)
1044         {
1045                 dev=linphone_core_get_video_devices(lc);
1046                 index=atoi(arg2); /* FIXME: handle not-a-number */
1047                 for(i=0;dev[i]!=NULL;i++)
1048                 {
1049                         if (i!=index) continue;
1050
1051                         linphone_core_set_video_device(lc, dev[i]);
1052                         linphonec_out("Using video device %s\n",dev[i]);
1053                         return 1;
1054                 }
1055                 linphonec_out("No such video device\n");
1056                 return 1;
1057         }
1058         return 0; /* syntax error */
1059 }
1060
1061 static int
1062 lpc_cmd_staticpic(LinphoneCore *lc, char *args)
1063 {
1064         char *arg1 = args;
1065         char *arg2 = NULL;
1066         char *ptr = args;
1067
1068         if (!args) return 0;  /* Syntax error */
1069
1070         /* Isolate first and second arg */
1071         while(*ptr && !isspace(*ptr)) ++ptr;
1072         if ( *ptr )
1073         {
1074                 *ptr='\0';
1075                 arg2=ptr+1;
1076                 while(*arg2 && isspace(*arg2)) ++arg2;
1077         }
1078
1079         if (strcmp(arg1, "set")==0 && arg2) {
1080                 linphone_core_set_static_picture(lc, arg2);
1081                 return 1;
1082         }
1083
1084         return 0; /* Syntax error */
1085 }
1086
1087
1088
1089 /***************************************************************************
1090  *
1091  *  Commands helper functions
1092  *
1093  ***************************************************************************/
1094
1095
1096 static void
1097 linphonec_proxy_add(LinphoneCore *lc)
1098 {
1099         bool_t enable_register=FALSE;
1100         LinphoneProxyConfig *cfg;
1101
1102         linphonec_out("Adding new proxy setup. Hit ^D to abort.\n");
1103
1104         /*
1105          * SIP Proxy address
1106          */
1107         while (1)
1108         {
1109                 char *input=linphonec_readline("Enter proxy sip address: ");
1110                 char *clean;
1111
1112                 if ( ! input ) {
1113                         linphonec_out("Aborted.\n");
1114                         return;
1115                 }
1116
1117                 /* Strip blanks */
1118                 clean=lpc_strip_blanks(input);
1119                 if ( ! *clean ) {
1120                         free(input);
1121                         continue;
1122                 }
1123
1124                 cfg=linphone_proxy_config_new();
1125                 if (linphone_proxy_config_set_server_addr(cfg,clean)<0)
1126                 {
1127                         linphonec_out("Invalid sip address (sip:sip.domain.tld).\n");
1128                         free(input);
1129                         linphone_proxy_config_destroy(cfg);
1130                         continue;
1131                 }
1132                 free(input);
1133                 break;
1134         }
1135
1136         /*
1137          * SIP Proxy identity
1138          */
1139         while (1)
1140         {
1141                 char *input=linphonec_readline("Your identity for this proxy: ");
1142                 char *clean;
1143
1144                 if ( ! input ) {
1145                         linphonec_out("Aborted.\n");
1146                         linphone_proxy_config_destroy(cfg);
1147                         return;
1148                 }
1149
1150                 /* Strip blanks */
1151                 clean=lpc_strip_blanks(input);
1152                 if ( ! *clean ) {
1153                         free(input);
1154                         continue;
1155                 }
1156
1157                 linphone_proxy_config_set_identity(cfg, clean);
1158                 if ( ! cfg->reg_identity )
1159                 {
1160                         linphonec_out("Invalid identity (sip:name@sip.domain.tld).\n");
1161                         free(input);
1162                         continue;
1163                 }
1164                 free(input);
1165                 break;
1166         }
1167
1168         /*
1169          * SIP Proxy enable register
1170          */
1171         while (1)
1172         {
1173                 char *input=linphonec_readline("Do you want to register on this proxy (yes/no): ");
1174                 char *clean;
1175
1176                 if ( ! input ) {
1177                         linphonec_out("Aborted.\n");
1178                         linphone_proxy_config_destroy(cfg);
1179                         return;
1180                 }
1181
1182                 /* Strip blanks */
1183                 clean=lpc_strip_blanks(input);
1184                 if ( ! *clean ) {
1185                         free(input);
1186                         continue;
1187                 }
1188
1189                 if ( ! strcmp(clean, "yes") ) enable_register=TRUE;
1190                 else if ( ! strcmp(clean, "no") ) enable_register=FALSE;
1191                 else {
1192                         linphonec_out("Please answer with 'yes' or 'no'\n");
1193                         free(input);
1194                         continue;
1195                 }
1196                 linphone_proxy_config_enableregister(cfg, enable_register);
1197                 free(input);
1198                 break;
1199         }
1200
1201         /*
1202          * SIP Proxy registration expiration
1203          */
1204         if ( enable_register==TRUE )
1205         {
1206                 long int expires=0;
1207                 while (1)
1208                 {
1209                         char *input=linphonec_readline("Specify register expiration time"
1210                                 " in seconds (default is 600): ");
1211
1212                         if ( ! input ) {
1213                                 linphonec_out("Aborted.\n");
1214                                 linphone_proxy_config_destroy(cfg);
1215                                 return;
1216                         }
1217
1218                         expires=strtol(input, (char **)NULL, 10);
1219                         if ( expires == LONG_MIN || expires == LONG_MAX )
1220                         {
1221                                 linphonec_out("Invalid value: %s\n", strerror(errno));
1222                                 free(input);
1223                                 continue;
1224                         }
1225
1226                         linphone_proxy_config_expires(cfg, expires);
1227                         linphonec_out("Expiration: %d seconds\n", cfg->expires);
1228
1229                         free(input);
1230                         break;
1231                 }
1232         }
1233
1234         /*
1235          * SIP proxy route
1236          */
1237         while (1)
1238         {
1239                 char *input=linphonec_readline("Specify route if needed: ");
1240                 char *clean;
1241
1242                 if ( ! input ) {
1243                         linphonec_out("Aborted.\n");
1244                         linphone_proxy_config_destroy(cfg);
1245                         return;
1246                 }
1247
1248                 /* Strip blanks */
1249                 clean=lpc_strip_blanks(input);
1250                 if ( ! *clean ) {
1251                         free(input);
1252                         linphonec_out("No route specified.\n");
1253                         break;
1254                 }
1255
1256                 linphone_proxy_config_set_route(cfg, clean);
1257                 if ( ! cfg->reg_route )
1258                 {
1259                         linphonec_out("Invalid route.\n");
1260                         free(input);
1261                         continue;
1262                 }
1263
1264                 free(input);
1265                 break;
1266         }
1267
1268         /*
1269          * Final confirmation 
1270          */
1271         while (1)
1272         {
1273                 char *input;
1274                 char *clean;
1275
1276                 linphonec_out("--------------------------------------------\n");
1277                 linphonec_proxy_display(cfg);
1278                 linphonec_out("--------------------------------------------\n");
1279                 input=linphonec_readline("Accept the above proxy configuration (yes/no) ?: ");
1280
1281
1282                 if ( ! input ) {
1283                         linphonec_out("Aborted.\n");
1284                         linphone_proxy_config_destroy(cfg);
1285                         return;
1286                 }
1287
1288                 /* Strip blanks */
1289                 clean=lpc_strip_blanks(input);
1290                 if ( ! *clean ) {
1291                         free(input);
1292                         continue;
1293                 }
1294
1295                 if ( ! strcmp(clean, "yes") ) break;
1296                 else if ( ! strcmp(clean, "no") )
1297                 {
1298                         linphonec_out("Declined.\n");
1299                         linphone_proxy_config_destroy(cfg);
1300                         free(input);
1301                         return;
1302                 }
1303
1304                 linphonec_out("Please answer with 'yes' or 'no'\n");
1305                 free(input);
1306                 continue;
1307         }
1308
1309
1310         linphone_core_add_proxy_config(lc,cfg);
1311
1312         /* automatically set the last entered proxy as the default one */
1313         linphone_core_set_default_proxy(lc,cfg);
1314
1315         linphonec_out("Proxy added.\n");
1316 }
1317
1318 static void
1319 linphonec_proxy_display(LinphoneProxyConfig *cfg)
1320 {
1321         linphonec_out("sip address: %s\nroute: %s\nidentity: %s\nregister: %s\nexpires: %i\nregistered: %s\n",
1322                         cfg->reg_proxy,
1323                         (cfg->reg_route!=NULL)?cfg->reg_route:"",
1324                         (cfg->reg_identity!=NULL)?cfg->reg_identity:"",
1325                         (cfg->reg_sendregister)?"yes":"no",
1326                         cfg->expires,
1327                         linphone_proxy_config_is_registered(cfg) ? "yes" : "no");
1328 }
1329
1330 static void linphonec_proxy_show(LinphoneCore *lc, int index)
1331 {
1332         const MSList *elem;
1333         int i;
1334         for(elem=linphone_core_get_proxy_config_list(lc),i=0;elem!=NULL;elem=elem->next,++i){
1335                 if (index==i){
1336                         LinphoneProxyConfig *cfg=(LinphoneProxyConfig *)elem->data;
1337                         linphonec_proxy_display(cfg);
1338                         return;
1339                 }
1340         }
1341         linphonec_out("No proxy with index %i\n", index);
1342 }
1343
1344 static void
1345 linphonec_proxy_list(LinphoneCore *lc)
1346 {
1347         const MSList *proxies;
1348         int n;
1349         int def=linphone_core_get_default_proxy(lc,NULL);
1350         
1351         proxies=linphone_core_get_proxy_config_list(lc);
1352         for(n=0;proxies!=NULL;proxies=ms_list_next(proxies),n++){
1353                 if (n==def)
1354                         linphonec_out("****** Proxy %i - this is the default one - *******\n",n);
1355                 else 
1356                         linphonec_out("****** Proxy %i *******\n",n);
1357                 linphonec_proxy_display((LinphoneProxyConfig*)proxies->data);
1358         }
1359         if ( ! n ) linphonec_out("No proxies defined\n");
1360 }
1361
1362 static void
1363 linphonec_proxy_remove(LinphoneCore *lc, int index)
1364 {
1365         const MSList *proxies;
1366         LinphoneProxyConfig *cfg;
1367         proxies=linphone_core_get_proxy_config_list(lc);
1368         cfg=(LinphoneProxyConfig*)ms_list_nth_data(proxies,index);
1369         if (cfg==NULL){
1370                 linphonec_out("No such proxy.\n");
1371                 return;
1372         }
1373         linphone_core_remove_proxy_config(lc,cfg);
1374         linphonec_out("Proxy %s removed.\n", cfg->reg_proxy);
1375         linphone_proxy_config_destroy(cfg);
1376 }
1377
1378 static int
1379 linphonec_proxy_use(LinphoneCore *lc, int index)
1380 {
1381         const MSList *proxies;
1382         LinphoneProxyConfig *cfg;
1383         proxies=linphone_core_get_proxy_config_list(lc);
1384         cfg=(LinphoneProxyConfig*)ms_list_nth_data(proxies,index);
1385         if (cfg==NULL){
1386                 linphonec_out("No such proxy (try 'proxy list').");
1387                 return 0;
1388         }
1389         linphone_core_set_default_proxy(lc,cfg);
1390         return 1;
1391 }
1392
1393 static void
1394 linphonec_friend_display(LinphoneFriend *fr)
1395 {
1396         LinphoneAddress *uri=linphone_address_clone(linphone_friend_get_address(fr));
1397         char *str;
1398         
1399         linphonec_out("name: %s\n", linphone_address_get_display_name(uri));
1400         linphone_address_set_display_name(uri,NULL);
1401         str=linphone_address_as_string(uri);
1402         linphonec_out("address: %s\n", str);
1403 }
1404
1405 static int
1406 linphonec_friend_list(LinphoneCore *lc, char *pat)
1407 {
1408         const MSList *friend;
1409         int n;
1410
1411         if (pat) {
1412                 pat=lpc_strip_blanks(pat);
1413                 if (!*pat) pat = NULL;
1414         }
1415
1416         friend = linphone_core_get_friend_list(lc);
1417         for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
1418         {
1419                 if ( pat ) {
1420                         const char *name = linphone_address_get_display_name(
1421                             linphone_friend_get_address((LinphoneFriend*)friend->data));
1422                         if (name && ! strstr(name, pat) ) continue;
1423                 }
1424                 linphonec_out("****** Friend %i *******\n",n);
1425                 linphonec_friend_display((LinphoneFriend*)friend->data);
1426         }
1427
1428         return 1;
1429 }
1430
1431 static int
1432 linphonec_friend_call(LinphoneCore *lc, unsigned int num)
1433 {
1434         const MSList *friend = linphone_core_get_friend_list(lc);
1435         unsigned int n;
1436         char *addr;
1437
1438         for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
1439         {
1440                 if ( n == num )
1441                 {
1442                         int ret;
1443                         addr = linphone_address_as_string(linphone_friend_get_address((LinphoneFriend*)friend->data));
1444                         ret=lpc_cmd_call(lc, addr);
1445                         ms_free(addr);
1446                         return ret;
1447                 }
1448         }
1449         linphonec_out("No such friend %u\n", num);
1450         return 1;
1451 }
1452
1453 #ifndef WIN32
1454 static int
1455 linphonec_friend_add(LinphoneCore *lc, const char *name, const char *addr)
1456 {
1457         LinphoneFriend *newFriend;
1458
1459         char url[PATH_MAX];
1460
1461         snprintf(url, PATH_MAX, "%s <%s>", name, addr);
1462         newFriend = linphone_friend_new_with_addr(url);
1463         linphone_core_add_friend(lc, newFriend);
1464         return 0;
1465 }
1466 #endif
1467
1468 static int
1469 linphonec_friend_delete(LinphoneCore *lc, int num)
1470 {
1471         const MSList *friend = linphone_core_get_friend_list(lc);
1472         unsigned int n;
1473
1474         for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
1475         {
1476                 if ( n == num )
1477                 {
1478                         linphone_core_remove_friend(lc, friend->data);
1479                         return 0;
1480                 }
1481         }
1482
1483         if (-1 == num) 
1484         {
1485                 unsigned int i;
1486                 for (i = 0 ; i < n ; i++)
1487                         linphonec_friend_delete(lc, 0);
1488                 return 0;
1489         }
1490
1491         linphonec_out("No such friend %u\n", num);
1492         return 1;
1493 }
1494
1495 static void
1496 linphonec_display_command_help(LPC_COMMAND *cmd)
1497 {
1498         if ( cmd->doc ) linphonec_out ("%s\n", cmd->doc);
1499         else linphonec_out("%s\n", cmd->help);
1500 }
1501
1502
1503 static int lpc_cmd_register(LinphoneCore *lc, char *args){
1504         char identity[512];
1505         char proxy[512];
1506         char passwd[512];
1507         LinphoneProxyConfig *cfg;
1508         const MSList *elem;
1509     
1510         if (!args)
1511         {
1512                 /* it means that you want to register the default proxy */
1513                 LinphoneProxyConfig *cfg=NULL;
1514                 linphone_core_get_default_proxy(lc,&cfg);
1515                 if (cfg)
1516                 {
1517                         if(!linphone_proxy_config_is_registered(cfg)) {
1518                                 linphone_proxy_config_enable_register(cfg,TRUE);
1519                                 linphone_proxy_config_done(cfg);
1520                         }else{
1521                                 linphonec_out("default proxy already registered\n");
1522                         }
1523                 }else{
1524                         linphonec_out("we do not have a default proxy\n");
1525                         return 0;
1526                 }
1527                 return 1;
1528         }
1529         passwd[0]=proxy[0]=identity[0]='\0';
1530         sscanf(args,"%s %s %s",identity,proxy,passwd);
1531         if (proxy[0]=='\0' || identity[0]=='\0'){
1532                 linphonec_out("Missing parameters, see help register\n");
1533                 return 1;
1534         }
1535         if (passwd[0]!='\0'){
1536                 LinphoneAddress *from;
1537                 LinphoneAuthInfo *info;
1538                 if ((from=linphone_address_new(identity))!=NULL){
1539                         char realm[128];
1540                         snprintf(realm,sizeof(realm)-1,"\"%s\"",linphone_address_get_domain(from));
1541                         info=linphone_auth_info_new(linphone_address_get_username(from),NULL,passwd,NULL,NULL);
1542                         linphone_core_add_auth_info(lc,info);
1543                         linphone_address_destroy(from);
1544                         linphone_auth_info_destroy(info);
1545                 }
1546         }
1547         elem=linphone_core_get_proxy_config_list(lc);
1548         if (elem) {
1549                 cfg=(LinphoneProxyConfig*)elem->data;
1550                 linphone_proxy_config_edit(cfg);
1551         }
1552         else cfg=linphone_proxy_config_new();
1553         linphone_proxy_config_set_identity(cfg,identity);
1554         linphone_proxy_config_set_server_addr(cfg,proxy);
1555         linphone_proxy_config_enable_register(cfg,TRUE);
1556         if (elem) linphone_proxy_config_done(cfg);
1557         else linphone_core_add_proxy_config(lc,cfg);
1558         linphone_core_set_default_proxy(lc,cfg);
1559         return 1;
1560 }
1561
1562 static int lpc_cmd_unregister(LinphoneCore *lc, char *args){
1563         LinphoneProxyConfig *cfg=NULL;
1564         linphone_core_get_default_proxy(lc,&cfg);
1565         if (cfg && linphone_proxy_config_is_registered(cfg)) {
1566                 linphone_proxy_config_edit(cfg);
1567                 linphone_proxy_config_enable_register(cfg,FALSE);
1568                 linphone_proxy_config_done(cfg);
1569         }else{
1570                 linphonec_out("unregistered\n");
1571         }
1572         return 1;
1573 }
1574
1575 static int lpc_cmd_duration(LinphoneCore *lc, char *args){
1576         LinphoneCallLog *cl;
1577         const MSList *elem=linphone_core_get_call_logs(lc);
1578         for(;elem!=NULL;elem=elem->next){
1579                 if (elem->next==NULL){
1580                         cl=(LinphoneCallLog*)elem->data;
1581                         linphonec_out("%i seconds\n",cl->duration);
1582                 }
1583         }
1584         return 1;
1585 }
1586
1587 static int lpc_cmd_status(LinphoneCore *lc, char *args)
1588 {
1589         LinphoneProxyConfig *cfg;
1590         
1591         if ( ! args ) return 0;
1592         linphone_core_get_default_proxy(lc,&cfg);
1593         if (strstr(args,"register"))
1594         {
1595                 if (cfg)
1596                 {
1597                         if (linphone_proxy_config_is_registered(cfg)){
1598                                 linphonec_out("registered, identity=%s duration=%i\n",
1599                                         linphone_proxy_config_get_identity(cfg),
1600                                         linphone_proxy_config_get_expires(cfg));
1601                         }else if (linphone_proxy_config_register_enabled(cfg)){
1602                                 linphonec_out("registered=-1\n");
1603                         }else linphonec_out("registered=0\n");
1604                 }
1605                 else linphonec_out("registered=0\n");
1606         }
1607         else if (strstr(args,"autoanswer"))
1608         {
1609                 if (cfg && linphone_proxy_config_is_registered(cfg))
1610                         linphonec_out("autoanswer=%i\n",linphonec_get_autoanswer());
1611                 else linphonec_out("unregistered\n");
1612         }
1613         else if (strstr(args,"hook"))
1614         {
1615                 gstate_t call_state=linphone_core_get_state(lc,GSTATE_GROUP_CALL);
1616 /*
1617                 if (!cfg || !linphone_proxy_config_is_registered(cfg)){
1618                         linphonec_out("unregistered\n");
1619                         return 1;
1620                 }
1621  */
1622                 switch(call_state){
1623                         case GSTATE_CALL_OUT_INVITE:
1624                                 linphonec_out("hook=dialing\n");
1625                         break;
1626                         case GSTATE_CALL_IDLE:
1627                                 linphonec_out("hook=offhook\n");
1628                         break;
1629                         case GSTATE_CALL_OUT_CONNECTED:
1630                                 linphonec_out("Call out, hook=%s duration=%i, muted=%s rtp-xmit-muted=%s\n", linphonec_get_callee(),
1631                                               linphone_core_get_current_call_duration(lc),
1632                                               lc->audio_muted ? "yes" : "no",
1633                                               linphone_core_is_rtp_muted(lc) ? "yes"  : "no");
1634                         break;
1635                         case GSTATE_CALL_IN_CONNECTED:
1636                                 linphonec_out("hook=answered duration=%i\n" ,
1637                                         linphone_core_get_current_call_duration(lc));
1638                                 break;
1639                         case GSTATE_CALL_IN_INVITE:
1640                                 linphonec_out("Incoming call from %s\n",linphonec_get_caller());
1641                                 break;
1642                         default:
1643                                 break;
1644                 }
1645                 
1646         }
1647         else return 0;
1648
1649         return 1;
1650 }
1651
1652 static int lpc_cmd_ports(LinphoneCore *lc, char *args)
1653 {
1654         int port;
1655         if ( ! args ){
1656                 linphonec_out("sip port = %i\naudio rtp port = %i\nvideo rtp port = %i\n",
1657                         linphone_core_get_sip_port(lc),
1658                         linphone_core_get_audio_port(lc),
1659                         linphone_core_get_video_port(lc));
1660                 return 1;
1661         }
1662         if (sscanf(args,"sip %i",&port)==1){
1663                 linphonec_out("Setting sip port to %i\n",port);
1664                 linphone_core_set_sip_port(lc,port);
1665         }else return 0;
1666
1667         return 1;
1668 }
1669
1670 static int lpc_cmd_speak(LinphoneCore *lc, char *args){
1671 #ifndef WIN32
1672         char voice[64];
1673         char *sentence;
1674         char cl[128];
1675         char *wavfile;
1676         int status;
1677         FILE *file;
1678         
1679     if (!args) return 0;
1680         memset(voice,0,sizeof(voice));
1681         sscanf(args,"%s63",voice);
1682         sentence=args+strlen(voice);
1683         wavfile=tempnam("/tmp/","linphonec-espeak-");
1684         snprintf(cl,sizeof(cl),"espeak -v %s -s 100 -w %s --stdin",voice,wavfile);
1685         file=popen(cl,"w");
1686         if (file==NULL){
1687                 ms_error("Could not open pipe to espeak !");
1688                 return 1;
1689         }
1690         fprintf(file,"%s",sentence);
1691         status=pclose(file);
1692         if (WEXITSTATUS(status)==0){
1693                 linphone_core_set_play_file(lc,wavfile);
1694         }else{
1695                 linphonec_out("espeak command failed.");
1696         }
1697 #else
1698         linphonec_out("Sorry, this command is not implemented in windows version.");
1699 #endif
1700         return 1;
1701 }
1702
1703 static int lpc_cmd_acodec(LinphoneCore *lc, char *args){
1704     return lpc_cmd_codec(AUDIO, lc, args);
1705 }
1706
1707 static int lpc_cmd_vcodec(LinphoneCore *lc, char *args){
1708     return lpc_cmd_codec(VIDEO, lc, args);
1709 }
1710
1711 static int lpc_cmd_codec(int type, LinphoneCore *lc, char *args){
1712         char *arg1 = args;
1713         char *arg2 = NULL;
1714         char *ptr = args;
1715
1716         if (!args) return 0;
1717
1718         /* Isolate first and second arg */
1719         while(*ptr && !isspace(*ptr)) ++ptr;
1720         if ( *ptr )
1721         {
1722                 *ptr='\0';
1723                 arg2=ptr+1;
1724                 while(*arg2 && isspace(*arg2)) ++arg2;
1725         }
1726
1727         if (strcmp(arg1,"enable")==0)
1728         {
1729 #ifdef HAVE_READLINE
1730                 rl_inhibit_completion=1;
1731 #endif
1732         if (!strcmp(arg2,"all")) linphonec_codec_enable(type,lc,-1);
1733         else linphonec_codec_enable(type,lc,atoi(arg2));
1734 #ifdef HAVE_READLINE
1735                 rl_inhibit_completion=0;
1736 #endif
1737         }
1738         else if (strcmp(arg1,"list")==0)
1739         {
1740                 linphonec_codec_list(type,lc);
1741         }
1742         else if (strcmp(arg1,"disable")==0)
1743         {
1744         if (!strcmp(arg2,"all")) linphonec_codec_disable(type,lc,-1);
1745         else linphonec_codec_disable(type,lc,atoi(arg2));
1746         }
1747         else
1748         {
1749                 return 0; /* syntax error */
1750         }
1751
1752         return 1;
1753 }
1754
1755 static void linphonec_codec_list(int type, LinphoneCore *lc){
1756         PayloadType *pt;
1757     codecs_config_t *config=&lc->codecs_conf;
1758         int index=0;
1759         MSList *node=NULL;
1760
1761     if (type == AUDIO) {
1762       node=config->audio_codecs;
1763     } else if(type==VIDEO) {
1764       node=config->video_codecs;
1765     }
1766
1767         for(;node!=NULL;node=ms_list_next(node)){
1768                 pt=(PayloadType*)(node->data);
1769         linphonec_out("%2d: %s (%d) %s\n", index, pt->mime_type, pt->clock_rate, 
1770                     linphone_core_payload_type_enabled(lc,pt) ? "enabled" : "disabled");
1771                 index++;
1772         }
1773 }
1774
1775 static void linphonec_codec_enable(int type, LinphoneCore *lc, int sel_index){
1776         PayloadType *pt;
1777     codecs_config_t *config=&lc->codecs_conf;
1778         int index=0;
1779         MSList *node=NULL;
1780
1781     if (type == AUDIO) {
1782       node=config->audio_codecs;
1783     } else if(type==VIDEO) {
1784       node=config->video_codecs;
1785     }
1786
1787     for(;node!=NULL;node=ms_list_next(node)){
1788         if (index == sel_index || sel_index == -1) {
1789                     pt=(PayloadType*)(node->data);
1790             pt->flags|=PAYLOAD_TYPE_ENABLED;
1791             linphonec_out("%2d: %s (%d) %s\n", index, pt->mime_type, pt->clock_rate, "enabled");
1792         }
1793                 index++;
1794         }
1795 }
1796
1797 static void linphonec_codec_disable(int type, LinphoneCore *lc, int sel_index){
1798         PayloadType *pt;
1799     codecs_config_t *config=&lc->codecs_conf;
1800         int index=0;
1801         MSList *node=NULL;
1802
1803     if (type == AUDIO) {
1804       node=config->audio_codecs;
1805     } else if(type==VIDEO) {
1806       node=config->video_codecs;
1807     }
1808
1809         for(;node!=NULL;node=ms_list_next(node)){
1810         if (index == sel_index || sel_index == -1) {
1811                 pt=(PayloadType*)(node->data);
1812             pt->flags&=~PAYLOAD_TYPE_ENABLED;
1813             linphonec_out("%2d: %s (%d) %s\n", index, pt->mime_type, pt->clock_rate, "disabled");
1814         }
1815                 index++;
1816         }
1817 }
1818
1819 static int lpc_cmd_echocancellation(LinphoneCore *lc, char *args){
1820         char *arg1 = args;
1821         char *arg2 = NULL;
1822         char *ptr = args;
1823
1824         if (!args) return 0;
1825
1826         /* Isolate first and second arg */
1827         while(*ptr && !isspace(*ptr)) ++ptr;
1828         if ( *ptr )
1829         {
1830                 *ptr='\0';
1831                 arg2=ptr+1;
1832                 while(*arg2 && isspace(*arg2)) ++arg2;
1833         }
1834
1835         if (strcmp(arg1,"on")==0){
1836         int delay, tail_len, frame_size;
1837         int n;
1838
1839         linphone_core_enable_echo_cancellation(lc,1);
1840
1841         if (arg2 != 0) {
1842             n = sscanf(arg2, "%d %d %d", &delay, &tail_len, &frame_size);
1843
1844             if (n == 1) {   
1845                 lp_config_set_int(lc->config,"sound","ec_delay",delay);
1846             }
1847             else if (n == 2) {
1848                 lp_config_set_int(lc->config,"sound","ec_delay",delay);
1849                 lp_config_set_int(lc->config,"sound","ec_tail_len",tail_len);
1850             }
1851             else if (n == 3) {
1852                 lp_config_set_int(lc->config,"sound","ec_delay",delay);
1853                 lp_config_set_int(lc->config,"sound","ec_tail_len",tail_len);
1854                 lp_config_set_int(lc->config,"sound","ec_framesize",frame_size);
1855             }
1856         }
1857     }
1858     else if (strcmp(arg1,"off")==0){
1859         linphone_core_enable_echo_cancellation(lc,0);
1860     }
1861     else if (strcmp(arg1,"show")==0){
1862         linphonec_out("echo cancellation is %s; delay %d, tail length %d, frame size %d\n", 
1863             linphone_core_echo_cancellation_enabled(lc) ? "on" : "off",
1864             lp_config_get_int(lc->config,"sound","ec_delay",0),
1865             lp_config_get_int(lc->config,"sound","ec_tail_len",0),
1866             lp_config_get_int(lc->config,"sound","ec_framesize",0));        
1867     }
1868     else {
1869         return 0;
1870     }
1871
1872     return 1;
1873 }
1874
1875 static int lpc_cmd_mute_mic(LinphoneCore *lc, char *args)
1876 {
1877   if ( lc->call != NULL )
1878     linphone_core_mute_mic(lc, 1);
1879   return 1;
1880 }
1881
1882 static int lpc_cmd_unmute_mic(LinphoneCore *lc, char *args)
1883 {
1884   if ( lc->call != NULL )
1885     linphone_core_mute_mic(lc, 0);
1886   return 1;
1887 }
1888
1889 static int lpc_cmd_rtp_no_xmit_on_audio_mute(LinphoneCore *lc, char *args)
1890 {
1891   bool_t rtp_xmit_off=FALSE;
1892   char *status;
1893   gstate_t call_state=linphone_core_get_state(lc,GSTATE_GROUP_CALL);
1894
1895   if(args){
1896     if(strstr(args,"1"))rtp_xmit_off=TRUE;
1897     if(call_state == GSTATE_CALL_IDLE)
1898       linphone_core_set_rtp_no_xmit_on_audio_mute(lc,rtp_xmit_off);
1899     else 
1900       linphonec_out("nortp-on-audio-mute: call in progress - cannot change state\n");
1901   }
1902   rtp_xmit_off=linphone_core_get_rtp_no_xmit_on_audio_mute(lc);
1903   if(rtp_xmit_off)status="off";
1904   else status="on";
1905   linphonec_out("rtp transmit %s when audio muted\n",status);
1906   return 1;
1907 }
1908
1909
1910 /***************************************************************************
1911  *
1912  *  Command table management funx
1913  *
1914  ***************************************************************************/
1915
1916 /*
1917  * Find a command given its name
1918  */
1919 static LPC_COMMAND *
1920 lpc_find_command(const char *name)
1921 {
1922         int i;
1923
1924         for (i=0; commands[i].name; ++i)
1925         {
1926                 if (strcmp(name, commands[i].name) == 0)
1927                         return &commands[i];
1928         }
1929
1930         return (LPC_COMMAND *)NULL;
1931 }
1932
1933
1934 /****************************************************************************
1935  *
1936  * $Log: commands.c,v $
1937  * Revision 1.39  2008/07/03 15:08:34  smorlat
1938  * api cleanups, interface in progress.
1939  *
1940  * Revision 1.38  2008/06/17 20:38:59  smorlat
1941  * added missing file.
1942  *
1943  * Revision 1.37  2008/04/09 09:26:00  smorlat
1944  * merge various patches
1945  * H264 support.
1946  *
1947  * Revision 1.36  2007/08/01 14:47:53  strk
1948  *         * console/commands.c: Clean up commands 'nat', 'stun'
1949  *           and 'firewall' to be more intuitive.
1950  *
1951  * Revision 1.35  2007/06/27 09:01:25  smorlat
1952  * logging improvements.
1953  *
1954  * Revision 1.34  2007/02/20 10:17:13  smorlat
1955  * linphonec friends patch2
1956  *
1957  * Revision 1.31  2006/09/22 07:22:47  smorlat
1958  * linphonecore api changes.
1959  *
1960  * Revision 1.30  2006/09/08 15:32:57  smorlat
1961  * support for using files instead of soundcard (used by linphonec only)
1962  *
1963  * Revision 1.29  2006/08/28 14:29:07  smorlat
1964  * fix bug.
1965  *
1966  * Revision 1.28  2006/08/21 12:49:59  smorlat
1967  * merged several little patches.
1968  *
1969  * Revision 1.27  2006/07/17 18:45:00  smorlat
1970  * support for several event queues in ortp.
1971  * glib dependency removed from coreapi/ and console/
1972  *
1973  * Revision 1.26  2006/04/14 15:16:36  smorlat
1974  * soundcard use did nothing !
1975  *
1976  * Revision 1.25  2006/04/06 20:09:33  smorlat
1977  * add linphonec command to see and select sound devices.
1978  *
1979  * Revision 1.24  2006/03/04 11:17:10  smorlat
1980  * mediastreamer2 in progress.
1981  *
1982  * Revision 1.23  2006/02/20 21:14:01  strk
1983  * Handled syntax errors with 'friend' command
1984  *
1985  * Revision 1.22  2006/02/20 10:20:29  strk
1986  * Added substring-based filter support for command 'friend list'
1987  *
1988  * Revision 1.21  2006/02/02 15:39:18  strk
1989  * - Added 'friend list' and 'friend call' commands
1990  * - Allowed for multiple DTFM send in a single line
1991  * - Added status-specific callback (bare version)
1992  *
1993  * Revision 1.20  2006/01/26 11:54:34  strk
1994  * More robust 'nat' command handler (strip blanks in args)
1995  *
1996  * Revision 1.19  2006/01/26 09:48:05  strk
1997  * Added limits.h include
1998  *
1999  * Revision 1.18  2006/01/26 02:18:05  strk
2000  * Added new commands 'nat use' and 'nat unuse'.
2001  * These will required a pending patch to linphonecore.c
2002  * in order to work.
2003  *
2004  * Revision 1.17  2006/01/20 14:12:33  strk
2005  * Added linphonec_init() and linphonec_finish() functions.
2006  * Handled SIGINT and SIGTERM to invoke linphonec_finish().
2007  * Handling of auto-termination (-t) moved to linphonec_finish().
2008  * Reworked main (input read) loop to not rely on 'terminate'
2009  * and 'run' variable (dropped). configfile_name allocated on stack
2010  * using PATH_MAX limit. Changed print_usage signature to allow
2011  * for an exit_status specification.
2012  *
2013  * Revision 1.16  2006/01/18 09:25:32  strk
2014  * Command completion inhibited in proxy addition and auth request prompts.
2015  * Avoided use of linphonec_readline's internal filename completion.
2016  *
2017  * Revision 1.15  2006/01/14 13:29:32  strk
2018  * Reworked commands interface to use a table structure,
2019  * used by command line parser and help function.
2020  * Implemented first level of completion (commands).
2021  * Added notification of invalid "answer" and "terminate"
2022  * commands (no incoming call, no active call).
2023  * Forbidden "call" intialization when a call is already active.
2024  * Cleaned up all commands, adding more feedback and error checks.
2025  *
2026  * Revision 1.14  2006/01/13 13:00:29  strk
2027  * Added linphonec.h. Code layout change (added comments, forward decl,
2028  * globals on top, copyright notices and Logs). Handled out-of-memory
2029  * condition on history management. Removed assumption on sizeof(char).
2030  * Fixed bug in authentication prompt (introduced by linphonec_readline).
2031  * Added support for multiple authentication requests (up to MAX_PENDING_AUTH).
2032  *
2033  *
2034  ****************************************************************************/