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