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