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