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