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