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