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