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