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