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