1 /****************************************************************************
3 * $Id: commands.c,v 1.39 2008/07/03 15:08:34 smorlat Exp $
5 * Copyright (C) 2006-2009 Sandro Santilli <strk@keybit.net>
6 * Copyright (C) 2004 Simon MORLAT <simon.morlat@linphone.org>
8 ****************************************************************************
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.
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.
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.
24 ****************************************************************************/
35 #include <linphonecore.h>
36 #include "linphonec.h"
44 /***************************************************************************
46 * Forward declarations
48 ***************************************************************************/
50 extern char *lpc_strip_blanks(char *input);
52 /* Command handlers */
53 static int lpc_cmd_help(LinphoneCore *, char *);
54 static int lpc_cmd_proxy(LinphoneCore *, char *);
55 static int lpc_cmd_call(LinphoneCore *, char *);
56 static int lpc_cmd_answer(LinphoneCore *, char *);
57 static int lpc_cmd_autoanswer(LinphoneCore *, char *);
58 static int lpc_cmd_terminate(LinphoneCore *, char *);
59 static int lpc_cmd_call_logs(LinphoneCore *, char *);
60 static int lpc_cmd_ipv6(LinphoneCore *, char *);
61 static int lpc_cmd_refer(LinphoneCore *, char *);
62 static int lpc_cmd_quit(LinphoneCore *, char *);
63 static int lpc_cmd_nat(LinphoneCore *, char *);
64 static int lpc_cmd_stun(LinphoneCore *, char *);
65 static int lpc_cmd_firewall(LinphoneCore *, char *);
66 static int lpc_cmd_friend(LinphoneCore *, char*);
67 static int lpc_cmd_soundcard(LinphoneCore *, char *);
68 static int lpc_cmd_play(LinphoneCore *, char *);
69 static int lpc_cmd_record(LinphoneCore *, char *);
70 static int lpc_cmd_register(LinphoneCore *, char *);
71 static int lpc_cmd_unregister(LinphoneCore *, char *);
72 static int lpc_cmd_duration(LinphoneCore *lc, char *args);
73 static int lpc_cmd_status(LinphoneCore *lc, char *args);
74 static int lpc_cmd_ports(LinphoneCore *lc, char *args);
75 static int lpc_cmd_speak(LinphoneCore *lc, char *args);
76 static int lpc_cmd_codec(LinphoneCore *lc, char *args);
77 static int lpc_cmd_echocancellation(LinphoneCore *lc, char *args);
79 /* Command handler helpers */
80 static void linphonec_proxy_add(LinphoneCore *lc);
81 static void linphonec_proxy_display(LinphoneProxyConfig *lc);
82 static void linphonec_proxy_list(LinphoneCore *lc);
83 static void linphonec_proxy_remove(LinphoneCore *lc, int index);
84 static int linphonec_proxy_use(LinphoneCore *lc, int index);
85 static void linphonec_proxy_show(LinphoneCore *lc,int index);
86 static void linphonec_friend_display(LinphoneFriend *fr);
87 static int linphonec_friend_list(LinphoneCore *lc, char *arg);
88 static void linphonec_display_command_help(LPC_COMMAND *cmd);
89 static int linphonec_friend_call(LinphoneCore *lc, unsigned int num);
91 static int linphonec_friend_add(LinphoneCore *lc, const char *name, const char *addr);
93 static int linphonec_friend_delete(LinphoneCore *lc, int num);
94 static int linphonec_friend_delete(LinphoneCore *lc, int num);
95 static void linphonec_codec_list(LinphoneCore *lc);
96 static void linphonec_codec_enable(LinphoneCore *lc, int index);
97 static void linphonec_codec_disable(LinphoneCore *lc, int index);
101 /* Command table management */
102 static LPC_COMMAND *lpc_find_command(const char *name);
104 void linphonec_out(const char *fmt,...);
108 /***************************************************************************
112 ***************************************************************************/
117 LPC_COMMAND commands[] = {
118 { "help", lpc_cmd_help, "Print commands help", NULL },
119 { "call", lpc_cmd_call, "Call a SIP uri",
121 ": initiate a call to the specified destination."
123 { "terminate", lpc_cmd_terminate, "Terminate the current call",
125 { "answer", lpc_cmd_answer, "Answer a call",
126 "Accept an incoming call."
128 { "autoanswer", lpc_cmd_autoanswer, "Show/set auto-answer mode",
129 "'autoanswer' \t: show current autoanswer mode\n"
130 "'autoanswer enable'\t: enable autoanswer mode\n"
131 "'autoanswer disable'\t: disable autoanswer modeĀ \n"},
132 { "proxy", lpc_cmd_proxy, "Manage proxies",
133 "'proxy list' : list all proxy setups.\n"
134 "'proxy add' : add a new proxy setup.\n"
135 "'proxy remove <index>' : remove proxy setup with number index.\n"
136 "'proxy use <index>' : use proxy with number index as default proxy.\n"
137 "'proxy unuse' : don't use a default proxy.\n"
138 "'proxy show <index>' : show configuration and status of the proxy numbered by index.\n"
139 "'proxy show default' : show configuration and status of the default proxy.\n"
141 { "soundcard", lpc_cmd_soundcard, "Manage soundcards",
142 "'soundcard list' : list all sound devices.\n"
143 "'soundcard show' : show current sound devices configuration.\n"
144 "'soundcard use <index>' : select a sound device.\n"
145 "'soundcard use files' : use .wav files instead of soundcard\n"
147 { "ipv6", lpc_cmd_ipv6, "Use IPV6",
148 "'ipv6 status' : show ipv6 usage status.\n"
149 "'ipv6 enable' : enable the use of the ipv6 network.\n"
150 "'ipv6 disable' : do not use ipv6 network."
152 { "refer", lpc_cmd_refer,
153 "Refer the current call to the specified destination.",
154 "'refer <sip-url>' or 'r <sip-url>' "
155 ": refer the current call to the specified destination."
157 { "nat", lpc_cmd_nat, "Set nat address",
158 "'nat' : show nat settings.\n"
159 "'nat <addr>' : set nat address.\n"
161 { "stun", lpc_cmd_stun, "Set stun server address",
162 "'stun' : show stun settings.\n"
163 "'stun <addr>' : set stun server address.\n"
165 { "firewall", lpc_cmd_firewall, "Set firewall policy",
166 "'firewall' : show current firewall policy.\n"
167 "'firewall none' : use direct connection.\n"
168 "'firewall nat' : use nat address given with the 'nat' command.\n"
169 "'firewall stun' : use stun server given with the 'stun' command.\n"
171 { "call-logs", lpc_cmd_call_logs, "Calls history", NULL },
172 { "friend", lpc_cmd_friend, "Manage friends",
173 "'friend list [<pattern>]' : list friends.\n"
174 "'friend call <index>' : call a friend.\n"
175 "'friend add <name> <addr>' : add friend, <name> must be quoted to include\n"
176 " spaces, <addr> has \"sip:\" added if it isn't\n"
177 " there. Don't use '<' '>' around <addr>.\n"
178 "'friend delete <index>' : remove friend, 'all' removes all\n"
180 { "play", lpc_cmd_play, "play from a wav file",
181 "This feature is available only in file mode (see 'help soundcard')\n"
182 "'play <wav file>' : play a wav file."
184 { "record", lpc_cmd_record, "record to a wav file",
185 "This feature is available only in file mode (see 'help soundcard')\n"
186 "'record <wav file>' : record into wav file."
188 { "quit", lpc_cmd_quit, "Exit linphonec", NULL },
189 { "register", lpc_cmd_register, "Register in one line to a proxy" , "register <sip identity> <sip proxy> <password>"},
190 { "unregister", lpc_cmd_unregister, "Unregister from default proxy", NULL },
191 { "duration", lpc_cmd_duration, "Print duration in seconds of the last call.", NULL },
192 { "status", lpc_cmd_status, "Print various status information",
193 "'status register' \t: print status concerning registration\n"
194 "'status autoanswer'\t: tell whether autoanswer mode is enabled\n"
195 "'status hook' \t: print hook status\n" },
196 { "ports", lpc_cmd_ports, "Network ports configuration",
197 "'ports' \t: prints current used ports.\n"
198 "'ports sip <port number>'\t: Sets the sip port.\n" },
199 { "speak", lpc_cmd_speak, "Speak a sentence using espeak TTS engine",
200 "This feature is available only in file mode. (see 'help soundcard')\n"
201 "'speak <voice name> <sentence>' : speak a text using the specified espeak voice.\n"
202 "Example for english voice: 'speak default Hello my friend !'"
204 { "codec", lpc_cmd_codec, "Codec configuration",
205 "'codec list' : list codecs\n"
206 "'codec enable <index>' : enable available codec\n"
207 "'codec disable <index>' : disable codecs" },
208 { "ec", lpc_cmd_echocancellation, "Echo cancellation",
209 "'ec on [<delay>] [<tail>] [<framesize>]' : turn EC on with given delay, tail length and framesize\n"
210 "'ec off' : turn echo cancellation (EC) off\n"
211 "'ec show' : show EC status" },
212 { (char *)NULL, (lpc_cmd_handler)NULL, (char *)NULL, (char *)NULL }
215 /***************************************************************************
219 ***************************************************************************/
222 * Main command dispatcher.
223 * WARNING: modifies second argument!
225 * Always return 1 currently.
228 linphonec_parse_command_line(LinphoneCore *lc, char *cl)
234 /* Isolate first word and args */
235 while(*ptr && !isspace(*ptr)) ++ptr;
239 /* set args to first nonblank */
241 while(*args && isspace(*args)) ++args;
245 if ( isdigit(*cl) || *cl == '#' || *cl == '*' )
247 while ( isdigit(*cl) || *cl == '#' || *cl == '*' )
249 linphone_core_send_dtmf(lc, *cl);
250 ms_sleep(1); // be nice
254 // discard spurious trailing chars
258 /* Handle other kind of commands */
259 cmd=lpc_find_command(cl);
262 linphonec_out("'%s': Cannot understand this.\n", cl);
266 if ( ! cmd->func(lc, args) )
268 linphonec_out("Syntax error.\n");
269 linphonec_display_command_help(cmd);
276 * Generator function for command completion.
277 * STATE let us know whether to start from scratch;
278 * without any state (STATE==0), then we start at the
282 linphonec_command_generator(const char *text, int state)
284 static int index, len;
294 * Return the next name which partially matches
295 * from the commands list
297 while ((name=commands[index].name))
299 ++index; /* so next call get next command */
301 if (strncmp(name, text, len) == 0)
303 return ortp_strdup(name);
311 /***************************************************************************
315 ***************************************************************************/
318 lpc_cmd_help(LinphoneCore *lc, char *arg)
325 linphonec_out("Commands are:\n");
326 linphonec_out("---------------------------\n");
328 while (commands[i].help)
330 linphonec_out("%10.10s\t%s\n", commands[i].name,
335 linphonec_out("---------------------------\n");
336 linphonec_out("Type 'help <command>' for more details.\n");
341 cmd=lpc_find_command(arg);
344 linphonec_out("No such command.\n");
348 linphonec_display_command_help(cmd);
353 static char callee_name[256]={0};
354 static char caller_name[256]={0};
357 lpc_cmd_call(LinphoneCore *lc, char *args)
359 if ( ! args || ! *args )
364 if ( lc->call != NULL )
366 linphonec_out("Terminate current call first.\n");
370 if ( -1 == linphone_core_invite(lc, args) )
372 linphonec_out("Error from linphone_core_invite.\n");
376 snprintf(callee_name,sizeof(callee_name),"%s",args);
382 const char *linphonec_get_callee(){
386 const char *linphonec_get_caller(){
390 void linphonec_set_caller(const char *caller){
391 snprintf(caller_name,sizeof(caller_name)-1,"%s",caller);
395 lpc_cmd_refer(LinphoneCore *lc, char *args)
398 linphone_core_refer(lc, args);
400 linphonec_out("refer needs an argument\n");
406 lpc_cmd_terminate(LinphoneCore *lc, char *args)
408 if ( -1 == linphone_core_terminate_call(lc, NULL) )
410 linphonec_out("No active call.\n");
416 lpc_cmd_answer(LinphoneCore *lc, char *args)
418 if ( -1 == linphone_core_accept_call(lc, NULL) )
420 linphonec_out("No incoming call.\n");
426 lpc_cmd_autoanswer(LinphoneCore *lc, char *args)
430 if ( linphonec_get_autoanswer() ) {
431 linphonec_out("Auto answer is enabled. Use 'autoanswer disable' to disable.\n");
433 linphonec_out("Auto answer is disabled. Use 'autoanswer enable' to enable.\n");
438 if (strstr(args,"enable")){
439 linphonec_set_autoanswer(TRUE);
440 linphonec_out("Auto answer enabled.\n");
441 }else if (strstr(args,"disable")){
442 linphonec_set_autoanswer(FALSE);
443 linphonec_out("Auto answer disabled.\n");
449 lpc_cmd_quit(LinphoneCore *lc, char *args)
451 linphonec_main_loop_exit();
456 lpc_cmd_nat(LinphoneCore *lc, char *args)
461 if ( args ) args=lpc_strip_blanks(args);
465 linphone_core_set_nat_address(lc, args);
466 /* linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_NAT_ADDRESS); */
469 nat = linphone_core_get_nat_address(lc);
470 use = linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_NAT_ADDRESS;
471 linphonec_out("Nat address: %s%s\n", nat ? nat : "unspecified" , use ? "" : " (disabled - use 'firewall nat' to enable)");
477 lpc_cmd_stun(LinphoneCore *lc, char *args)
482 if ( args ) args=lpc_strip_blanks(args);
486 linphone_core_set_stun_server(lc, args);
487 /* linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_STUN); */
490 stun = linphone_core_get_stun_server(lc);
491 use = linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_STUN;
492 linphonec_out("Stun server: %s%s\n", stun ? stun : "unspecified" , use? "" : " (disabled - use 'firewall stun' to enable)");
498 lpc_cmd_firewall(LinphoneCore *lc, char *args)
500 const char* setting=NULL;
502 if ( args ) args=lpc_strip_blanks(args);
506 if (strcmp(args,"none")==0)
508 linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_NO_FIREWALL);
510 else if (strcmp(args,"stun")==0)
512 setting = linphone_core_get_stun_server(lc);
515 linphonec_out("No stun server address is defined, use 'stun <address>' first\n");
518 linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_STUN);
520 else if (strcmp(args,"nat")==0)
522 setting = linphone_core_get_nat_address(lc);
525 linphonec_out("No nat address is defined, use 'nat <address>' first");
528 linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_NAT_ADDRESS);
532 switch(linphone_core_get_firewall_policy(lc))
534 case LINPHONE_POLICY_NO_FIREWALL:
535 linphonec_out("No firewall\n");
537 case LINPHONE_POLICY_USE_STUN:
538 linphonec_out("Using stun server %s to discover firewall address\n", setting ? setting : linphone_core_get_stun_server(lc));
540 case LINPHONE_POLICY_USE_NAT_ADDRESS:
541 linphonec_out("Using supplied nat address %s.\n", setting ? setting : linphone_core_get_nat_address(lc));
548 /* Helper function for processing freind names */
550 lpc_friend_name(char **args, char **name)
552 /* Use space as a terminator unless quoted */
553 if (('"' == **args) || ('\'' == **args)){
558 while ((delim != *end) && ('\0' != *end)) end++;
560 fprintf(stderr, "Mismatched quotes\n");
567 *name = strsep(args, " ");
569 if (NULL == *args) { /* Means there was no separator */
570 fprintf(stderr, "Either name or address is missing\n");
573 if (NULL == *name) return 0;
580 lpc_cmd_friend(LinphoneCore *lc, char *args)
584 if ( args ) args=lpc_strip_blanks(args);
586 if ( ! args || ! *args ) return 0;
588 if ( !strncmp(args, "list", 4) )
590 return linphonec_friend_list(lc, args+4);
593 else if ( !strncmp(args, "call", 4) )
596 if ( ! *args ) return 0;
597 friend_num = strtol(args, NULL, 10);
599 if ( errno == ERANGE ) {
600 linphonec_out("Invalid friend number\n");
603 #endif /*_WIN32_WCE*/
604 linphonec_friend_call(lc, friend_num);
607 else if ( !strncmp(args, "delete", 6) )
610 if ( ! *args ) return 0;
611 while (*args == ' ') args++;
612 if ( ! *args ) return 0;
613 if (!strncmp(args, "all", 3))
619 friend_num = strtol(args, NULL, 10);
621 if ( errno == ERANGE ) {
622 linphonec_out("Invalid friend number\n");
625 #endif /*_WIN32_WCE*/
627 linphonec_friend_delete(lc, friend_num);
630 else if ( !strncmp(args, "add", 3) )
639 if ( ! *args ) return 0;
640 while (*args == ' ') args++;
641 if ( ! *args ) return 0;
643 if (!lpc_friend_name(&args, &name)) return 0;
645 while (*args == ' ') args++;
646 if ( ! *args ) return 0;
647 if (isdigit(*args)) {
648 strcpy (addr, "sip:");
649 addr_p = addr + strlen("sip:");
651 addr_orig = strsep(&args, " ");
652 if (1 >= strlen(addr_orig)) {
653 fprintf(stderr, "A single-digit address is not valid\n");
656 strcpy(addr_p, addr_orig);
657 linphonec_friend_add(lc, name, addr);
659 LinphoneFriend *new_friend;
660 new_friend = linphone_friend_new_with_addr(args);
661 linphone_core_add_friend(lc, new_friend);
668 static int lpc_cmd_play(LinphoneCore *lc, char *args){
669 if ( args ) args=lpc_strip_blanks(args);
670 if ( ! args || ! *args ) return 0;
671 linphone_core_set_play_file(lc,args);
675 static int lpc_cmd_record(LinphoneCore *lc, char *args){
676 if ( args ) args=lpc_strip_blanks(args);
677 if ( ! args || ! *args ) return 0;
678 linphone_core_set_record_file(lc,args);
686 lpc_cmd_proxy(LinphoneCore *lc, char *args)
693 if ( ! arg1 ) return 0;
695 /* Isolate first and second arg */
696 while(*ptr && !isspace(*ptr)) ++ptr;
701 while(*arg2 && isspace(*arg2)) ++arg2;
704 if (strcmp(arg1,"add")==0)
707 rl_inhibit_completion=1;
709 linphonec_proxy_add(lc);
711 rl_inhibit_completion=0;
714 else if (strcmp(arg1,"list")==0)
716 linphonec_proxy_list(lc);
718 else if (strcmp(arg1,"remove")==0)
720 linphonec_proxy_remove(lc,atoi(arg2));
722 else if (strcmp(arg1,"use")==0)
727 if ( linphonec_proxy_use(lc, proxynum) )
728 linphonec_out("Default proxy set to %d.\n", proxynum);
732 proxynum=linphone_core_get_default_proxy(lc, NULL);
733 if ( proxynum == -1 ) linphonec_out("No default proxy.\n");
734 else linphonec_out("Current default proxy is %d.\n", proxynum);
736 }else if (strcmp(arg1, "unuse")==0){
737 linphone_core_set_default_proxy(lc, NULL);
738 linphonec_out("Use no proxy.\n");
741 else if (strcmp(arg1, "show")==0)
745 if (strstr(arg2,"default"))
747 proxynum=linphone_core_get_default_proxy(lc, NULL);
748 if ( proxynum < 0 ) {
749 linphonec_out("No default proxy defined\n");
752 linphonec_proxy_show(lc,proxynum);
756 linphonec_proxy_show(lc, atoi(arg2));
759 else return 0; /* syntax error */
764 return 0; /* syntax error */
771 lpc_cmd_call_logs(LinphoneCore *lc, char *args)
773 const MSList *elem=linphone_core_get_call_logs(lc);
774 for (;elem!=NULL;elem=ms_list_next(elem))
776 LinphoneCallLog *cl=(LinphoneCallLog*)elem->data;
777 char *str=linphone_call_log_to_str(cl);
778 linphonec_out("%s\n",str);
785 lpc_cmd_ipv6(LinphoneCore *lc, char *arg1)
789 return 0; /* syntax error */
792 if (strcmp(arg1,"status")==0)
794 linphonec_out("ipv6 use enabled: %s\n",linphone_core_ipv6_enabled(lc) ? "true":"false");
796 else if (strcmp(arg1,"enable")==0)
798 linphone_core_enable_ipv6(lc,TRUE);
799 linphonec_out("ipv6 use enabled.\n");
801 else if (strcmp(arg1,"disable")==0)
803 linphone_core_enable_ipv6(lc,FALSE);
804 linphonec_out("ipv6 use disabled.\n");
808 return 0; /* syntax error */
813 static int devname_to_index(LinphoneCore *lc, const char *devname){
816 for(i=0,p=linphone_core_get_sound_devices(lc);*p!=NULL;++p,++i){
817 if (strcmp(devname,*p)==0) return i;
822 static const char *index_to_devname(LinphoneCore *lc, int index){
825 for(i=0,p=linphone_core_get_sound_devices(lc);*p!=NULL;++p,++i){
826 if (i==index) return *p;
831 static int lpc_cmd_soundcard(LinphoneCore *lc, char *args)
839 if (!args) return 0; /* syntax error */
841 /* Isolate first and second arg */
842 while(*ptr && !isspace(*ptr)) ++ptr;
847 while(*arg2 && isspace(*arg2)) ++arg2;
850 if (strcmp(arg1, "list")==0)
852 dev=linphone_core_get_sound_devices(lc);
853 for(i=0; dev[i]!=NULL; ++i){
854 linphonec_out("%i: %s\n",i,dev[i]);
859 if (strcmp(arg1, "show")==0)
861 linphonec_out("Ringer device: %s\n",
862 linphone_core_get_ringer_device(lc));
863 linphonec_out("Playback device: %s\n",
864 linphone_core_get_playback_device(lc));
865 linphonec_out("Capture device: %s\n",
866 linphone_core_get_capture_device(lc));
870 if (strcmp(arg1, "use")==0 && arg2)
872 if (strcmp(arg2, "files")==0)
874 linphonec_out("Using wav files instead of soundcard.\n");
875 linphone_core_use_files(lc,TRUE);
879 dev=linphone_core_get_sound_devices(lc);
880 index=atoi(arg2); /* FIXME: handle not-a-number */
881 for(i=0;dev[i]!=NULL;i++)
883 if (i!=index) continue;
885 linphone_core_set_ringer_device(lc,dev[i]);
886 linphone_core_set_playback_device(lc,dev[i]);
887 linphone_core_set_capture_device(lc,dev[i]);
888 linphonec_out("Using sound device %s\n",dev[i]);
891 linphonec_out("No such sound device\n");
894 if (strcmp(arg1, "capture")==0)
896 const char *devname=linphone_core_get_capture_device(lc);
898 linphonec_out("Using capture device #%i (%s)\n",
899 devname_to_index(lc,devname),devname);
901 index=atoi(arg2); /* FIXME: handle not-a-number */
902 devname=index_to_devname(lc,index);
904 linphone_core_set_capture_device(lc,devname);
905 linphonec_out("Using capture sound device %s\n",devname);
908 linphonec_out("No such sound device\n");
912 if (strcmp(arg1, "playback")==0)
914 const char *devname=linphone_core_get_playback_device(lc);
916 linphonec_out("Using playback device #%i (%s)\n",
917 devname_to_index(lc,devname),devname);
919 index=atoi(arg2); /* FIXME: handle not-a-number */
920 devname=index_to_devname(lc,index);
922 linphone_core_set_playback_device(lc,devname);
923 linphonec_out("Using playback sound device %s\n",devname);
926 linphonec_out("No such sound device\n");
930 if (strcmp(arg1, "ring")==0)
932 const char *devname=linphone_core_get_ringer_device(lc);
934 linphonec_out("Using ring device #%i (%s)\n",
935 devname_to_index(lc,devname),devname);
937 index=atoi(arg2); /* FIXME: handle not-a-number */
938 devname=index_to_devname(lc,index);
940 linphone_core_set_ringer_device(lc,devname);
941 linphonec_out("Using ring sound device %s\n",devname);
944 linphonec_out("No such sound device\n");
948 return 0; /* syntax error */
951 /***************************************************************************
953 * Commands helper functions
955 ***************************************************************************/
959 linphonec_proxy_add(LinphoneCore *lc)
961 bool_t enable_register=FALSE;
962 LinphoneProxyConfig *cfg;
964 linphonec_out("Adding new proxy setup. Hit ^D to abort.\n");
971 char *input=linphonec_readline("Enter proxy sip address: ");
975 linphonec_out("Aborted.\n");
980 clean=lpc_strip_blanks(input);
986 cfg=linphone_proxy_config_new();
987 if (linphone_proxy_config_set_server_addr(cfg,clean)<0)
989 linphonec_out("Invalid sip address (sip:sip.domain.tld).\n");
991 linphone_proxy_config_destroy(cfg);
1003 char *input=linphonec_readline("Your identity for this proxy: ");
1007 linphonec_out("Aborted.\n");
1008 linphone_proxy_config_destroy(cfg);
1013 clean=lpc_strip_blanks(input);
1019 linphone_proxy_config_set_identity(cfg, clean);
1020 if ( ! cfg->reg_identity )
1022 linphonec_out("Invalid identity (sip:name@sip.domain.tld).\n");
1031 * SIP Proxy enable register
1035 char *input=linphonec_readline("Do you want to register on this proxy (yes/no): ");
1039 linphonec_out("Aborted.\n");
1040 linphone_proxy_config_destroy(cfg);
1045 clean=lpc_strip_blanks(input);
1051 if ( ! strcmp(clean, "yes") ) enable_register=TRUE;
1052 else if ( ! strcmp(clean, "no") ) enable_register=FALSE;
1054 linphonec_out("Please answer with 'yes' or 'no'\n");
1058 linphone_proxy_config_enableregister(cfg, enable_register);
1064 * SIP Proxy registration expiration
1066 if ( enable_register==TRUE )
1071 char *input=linphonec_readline("Specify register expiration time"
1072 " in seconds (default is 600): ");
1075 linphonec_out("Aborted.\n");
1076 linphone_proxy_config_destroy(cfg);
1080 expires=strtol(input, (char **)NULL, 10);
1081 if ( expires == LONG_MIN || expires == LONG_MAX )
1083 linphonec_out("Invalid value: %s\n", strerror(errno));
1088 linphone_proxy_config_expires(cfg, expires);
1089 linphonec_out("Expiration: %d seconds\n", cfg->expires);
1101 char *input=linphonec_readline("Specify route if needed: ");
1105 linphonec_out("Aborted.\n");
1106 linphone_proxy_config_destroy(cfg);
1111 clean=lpc_strip_blanks(input);
1114 linphonec_out("No route specified.\n");
1118 linphone_proxy_config_set_route(cfg, clean);
1119 if ( ! cfg->reg_route )
1121 linphonec_out("Invalid route.\n");
1131 * Final confirmation
1138 linphonec_out("--------------------------------------------\n");
1139 linphonec_proxy_display(cfg);
1140 linphonec_out("--------------------------------------------\n");
1141 input=linphonec_readline("Accept the above proxy configuration (yes/no) ?: ");
1145 linphonec_out("Aborted.\n");
1146 linphone_proxy_config_destroy(cfg);
1151 clean=lpc_strip_blanks(input);
1157 if ( ! strcmp(clean, "yes") ) break;
1158 else if ( ! strcmp(clean, "no") )
1160 linphonec_out("Declined.\n");
1161 linphone_proxy_config_destroy(cfg);
1166 linphonec_out("Please answer with 'yes' or 'no'\n");
1172 linphone_core_add_proxy_config(lc,cfg);
1174 /* automatically set the last entered proxy as the default one */
1175 linphone_core_set_default_proxy(lc,cfg);
1177 linphonec_out("Proxy added.\n");
1181 linphonec_proxy_display(LinphoneProxyConfig *cfg)
1183 linphonec_out("sip address: %s\nroute: %s\nidentity: %s\nregister: %s\nexpires: %i\nregistered: %s\n",
1185 (cfg->reg_route!=NULL)?cfg->reg_route:"",
1186 (cfg->reg_identity!=NULL)?cfg->reg_identity:"",
1187 (cfg->reg_sendregister)?"yes":"no",
1189 linphone_proxy_config_is_registered(cfg) ? "yes" : "no");
1192 static void linphonec_proxy_show(LinphoneCore *lc, int index)
1196 for(elem=linphone_core_get_proxy_config_list(lc),i=0;elem!=NULL;elem=elem->next,++i){
1198 LinphoneProxyConfig *cfg=(LinphoneProxyConfig *)elem->data;
1199 linphonec_proxy_display(cfg);
1203 linphonec_out("No proxy with index %i\n", index);
1207 linphonec_proxy_list(LinphoneCore *lc)
1209 const MSList *proxies;
1211 int def=linphone_core_get_default_proxy(lc,NULL);
1213 proxies=linphone_core_get_proxy_config_list(lc);
1214 for(n=0;proxies!=NULL;proxies=ms_list_next(proxies),n++){
1216 linphonec_out("****** Proxy %i - this is the default one - *******\n",n);
1218 linphonec_out("****** Proxy %i *******\n",n);
1219 linphonec_proxy_display((LinphoneProxyConfig*)proxies->data);
1221 if ( ! n ) linphonec_out("No proxies defined\n");
1225 linphonec_proxy_remove(LinphoneCore *lc, int index)
1227 const MSList *proxies;
1228 LinphoneProxyConfig *cfg;
1229 proxies=linphone_core_get_proxy_config_list(lc);
1230 cfg=(LinphoneProxyConfig*)ms_list_nth_data(proxies,index);
1232 linphonec_out("No such proxy.\n");
1235 linphone_core_remove_proxy_config(lc,cfg);
1236 linphonec_out("Proxy %s removed.\n", cfg->reg_proxy);
1237 linphone_proxy_config_destroy(cfg);
1241 linphonec_proxy_use(LinphoneCore *lc, int index)
1243 const MSList *proxies;
1244 LinphoneProxyConfig *cfg;
1245 proxies=linphone_core_get_proxy_config_list(lc);
1246 cfg=(LinphoneProxyConfig*)ms_list_nth_data(proxies,index);
1248 linphonec_out("No such proxy (try 'proxy list').");
1251 linphone_core_set_default_proxy(lc,cfg);
1256 linphonec_friend_display(LinphoneFriend *fr)
1258 LinphoneAddress *uri=linphone_address_clone(linphone_friend_get_uri(fr));
1261 linphonec_out("name: %s\n", linphone_address_get_display_name(uri));
1262 linphone_address_set_display_name(uri,NULL);
1263 str=linphone_address_as_string(uri);
1264 linphonec_out("address: %s\n", str);
1268 linphonec_friend_list(LinphoneCore *lc, char *pat)
1270 const MSList *friend;
1274 pat=lpc_strip_blanks(pat);
1275 if (!*pat) pat = NULL;
1278 friend = linphone_core_get_friend_list(lc);
1279 for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
1282 const char *name = linphone_address_get_display_name(
1283 linphone_friend_get_uri((LinphoneFriend*)friend->data));
1284 if (name && ! strstr(name, pat) ) continue;
1286 linphonec_out("****** Friend %i *******\n",n);
1287 linphonec_friend_display((LinphoneFriend*)friend->data);
1294 linphonec_friend_call(LinphoneCore *lc, unsigned int num)
1296 const MSList *friend = linphone_core_get_friend_list(lc);
1300 for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
1305 addr = linphone_address_as_string(linphone_friend_get_uri((LinphoneFriend*)friend->data));
1306 ret=lpc_cmd_call(lc, addr);
1311 linphonec_out("No such friend %u\n", num);
1317 linphonec_friend_add(LinphoneCore *lc, const char *name, const char *addr)
1319 LinphoneFriend *newFriend;
1323 snprintf(url, PATH_MAX, "%s <%s>", name, addr);
1324 newFriend = linphone_friend_new_with_addr(url);
1325 linphone_core_add_friend(lc, newFriend);
1331 linphonec_friend_delete(LinphoneCore *lc, int num)
1333 const MSList *friend = linphone_core_get_friend_list(lc);
1336 for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
1340 linphone_core_remove_friend(lc, friend->data);
1348 for (i = 0 ; i < n ; i++)
1349 linphonec_friend_delete(lc, 0);
1353 linphonec_out("No such friend %u\n", num);
1358 linphonec_display_command_help(LPC_COMMAND *cmd)
1360 if ( cmd->doc ) linphonec_out ("%s\n", cmd->doc);
1361 else linphonec_out("%s\n", cmd->help);
1365 static int lpc_cmd_register(LinphoneCore *lc, char *args){
1369 LinphoneProxyConfig *cfg;
1371 if (!args) return 0;
1372 passwd[0]=proxy[0]=identity[0]='\0';
1373 sscanf(args,"%s %s %s",identity,proxy,passwd);
1374 if (proxy[0]=='\0' || identity[0]=='\0'){
1375 linphonec_out("Missing parameters, see help register\n");
1378 if (passwd[0]!='\0'){
1380 LinphoneAuthInfo *info;
1381 osip_from_init(&from);
1382 if (osip_from_parse(from,identity)==0){
1384 snprintf(realm,sizeof(realm)-1,"\"%s\"",from->url->host);
1385 info=linphone_auth_info_new(from->url->username,NULL,passwd,NULL,NULL);
1386 linphone_core_add_auth_info(lc,info);
1388 osip_from_free(from);
1390 elem=linphone_core_get_proxy_config_list(lc);
1392 cfg=(LinphoneProxyConfig*)elem->data;
1393 linphone_proxy_config_edit(cfg);
1395 else cfg=linphone_proxy_config_new();
1396 linphone_proxy_config_set_identity(cfg,identity);
1397 linphone_proxy_config_set_server_addr(cfg,proxy);
1398 linphone_proxy_config_enable_register(cfg,TRUE);
1399 if (elem) linphone_proxy_config_done(cfg);
1400 else linphone_core_add_proxy_config(lc,cfg);
1401 linphone_core_set_default_proxy(lc,cfg);
1405 static int lpc_cmd_unregister(LinphoneCore *lc, char *args){
1406 LinphoneProxyConfig *cfg=NULL;
1407 linphone_core_get_default_proxy(lc,&cfg);
1408 if (cfg && linphone_proxy_config_is_registered(cfg)) {
1409 linphone_proxy_config_edit(cfg);
1410 linphone_proxy_config_enable_register(cfg,FALSE);
1411 linphone_proxy_config_done(cfg);
1413 linphonec_out("unregistered\n");
1418 static int lpc_cmd_duration(LinphoneCore *lc, char *args){
1419 LinphoneCallLog *cl;
1420 const MSList *elem=linphone_core_get_call_logs(lc);
1421 for(;elem!=NULL;elem=elem->next){
1422 if (elem->next==NULL){
1423 cl=(LinphoneCallLog*)elem->data;
1424 linphonec_out("%i seconds\n",cl->duration);
1430 static int lpc_cmd_status(LinphoneCore *lc, char *args)
1432 LinphoneProxyConfig *cfg;
1434 if ( ! args ) return 0;
1435 linphone_core_get_default_proxy(lc,&cfg);
1436 if (strstr(args,"register"))
1440 if (linphone_proxy_config_is_registered(cfg)){
1441 linphonec_out("registered, identity=%s duration=%i\n",
1442 linphone_proxy_config_get_identity(cfg),
1443 linphone_proxy_config_get_expires(cfg));
1444 }else if (linphone_proxy_config_register_enabled(cfg)){
1445 linphonec_out("registered=-1\n");
1446 }else linphonec_out("registered=0\n");
1448 else linphonec_out("registered=0\n");
1450 else if (strstr(args,"autoanswer"))
1452 if (cfg && linphone_proxy_config_is_registered(cfg))
1453 linphonec_out("autoanswer=%i\n",linphonec_get_autoanswer());
1454 else linphonec_out("unregistered\n");
1456 else if (strstr(args,"hook"))
1458 gstate_t call_state=linphone_core_get_state(lc,GSTATE_GROUP_CALL);
1460 if (!cfg || !linphone_proxy_config_is_registered(cfg)){
1461 linphonec_out("unregistered\n");
1466 case GSTATE_CALL_OUT_INVITE:
1467 linphonec_out("hook=dialing\n");
1469 case GSTATE_CALL_IDLE:
1470 linphonec_out("hook=offhook\n");
1472 case GSTATE_CALL_OUT_CONNECTED:
1473 linphonec_out("Call out, hook=%s duration=%i\n", linphonec_get_callee(),
1474 linphone_core_get_current_call_duration(lc));
1476 case GSTATE_CALL_IN_CONNECTED:
1477 linphonec_out("hook=answered duration=%i\n" ,
1478 linphone_core_get_current_call_duration(lc));
1480 case GSTATE_CALL_IN_INVITE:
1481 linphonec_out("Incoming call from %s\n",linphonec_get_caller());
1493 static int lpc_cmd_ports(LinphoneCore *lc, char *args)
1497 linphonec_out("sip port = %i\naudio rtp port = %i\nvideo rtp port = %i\n",
1498 linphone_core_get_sip_port(lc),
1499 linphone_core_get_audio_port(lc),
1500 linphone_core_get_video_port(lc));
1503 if (sscanf(args,"sip %i",&port)==1){
1504 linphonec_out("Setting sip port to %i\n",port);
1505 linphone_core_set_sip_port(lc,port);
1511 static int lpc_cmd_speak(LinphoneCore *lc, char *args){
1519 if (!args) return 0;
1520 memset(voice,0,sizeof(voice));
1521 sscanf(args,"%s63",voice);
1522 sentence=args+strlen(voice);
1523 wavfile=tempnam("/tmp/","linphonec-espeak-");
1524 snprintf(cl,sizeof(cl),"espeak -v %s -s 100 -w %s --stdin",voice,wavfile);
1527 ms_error("Could not open pipe to espeak !");
1530 fprintf(file,"%s",sentence);
1531 status=pclose(file);
1532 if (WEXITSTATUS(status)==0){
1533 linphone_core_set_play_file(lc,wavfile);
1535 linphonec_out("espeak command failed.");
1538 linphonec_out("Sorry, this command is not implemented in windows version.");
1543 static int lpc_cmd_codec(LinphoneCore *lc, char *args){
1548 if (!args) return 0;
1550 /* Isolate first and second arg */
1551 while(*ptr && !isspace(*ptr)) ++ptr;
1556 while(*arg2 && isspace(*arg2)) ++arg2;
1559 if (strcmp(arg1,"enable")==0)
1561 #ifdef HAVE_READLINE
1562 rl_inhibit_completion=1;
1564 if (!strcmp(arg2,"all")) linphonec_codec_enable(lc,-1);
1565 else linphonec_codec_enable(lc,atoi(arg2));
1566 #ifdef HAVE_READLINE
1567 rl_inhibit_completion=0;
1570 else if (strcmp(arg1,"list")==0)
1572 linphonec_codec_list(lc);
1574 else if (strcmp(arg1,"disable")==0)
1576 if (!strcmp(arg2,"all")) linphonec_codec_disable(lc,-1);
1577 else linphonec_codec_disable(lc,atoi(arg2));
1581 return 0; /* syntax error */
1587 static void linphonec_codec_list(LinphoneCore *lc){
1589 codecs_config_t *config=&lc->codecs_conf;
1592 for(node=config->audio_codecs;node!=NULL;node=ms_list_next(node)){
1593 pt=(PayloadType*)(node->data);
1594 linphonec_out("%2d: %s (%d) %s\n", index, pt->mime_type, pt->clock_rate, payload_type_enabled(pt) ? "enabled" : "disabled");
1599 static void linphonec_codec_enable(LinphoneCore *lc, int sel_index){
1601 codecs_config_t *config=&lc->codecs_conf;
1604 for(node=config->audio_codecs;node!=NULL;node=ms_list_next(node)){
1605 if (index == sel_index || sel_index == -1) {
1606 pt=(PayloadType*)(node->data);
1607 pt->flags|=PAYLOAD_TYPE_ENABLED;
1608 linphonec_out("%2d: %s (%d) %s\n", index, pt->mime_type, pt->clock_rate, "enabled");
1614 static void linphonec_codec_disable(LinphoneCore *lc, int sel_index){
1616 codecs_config_t *config=&lc->codecs_conf;
1619 for(node=config->audio_codecs;node!=NULL;node=ms_list_next(node)){
1620 if (index == sel_index || sel_index == -1) {
1621 pt=(PayloadType*)(node->data);
1622 pt->flags&=~PAYLOAD_TYPE_ENABLED;
1623 linphonec_out("%2d: %s (%d) %s\n", index, pt->mime_type, pt->clock_rate, "disabled");
1629 static int lpc_cmd_echocancellation(LinphoneCore *lc, char *args){
1634 if (!args) return 0;
1636 /* Isolate first and second arg */
1637 while(*ptr && !isspace(*ptr)) ++ptr;
1642 while(*arg2 && isspace(*arg2)) ++arg2;
1645 if (strcmp(arg1,"on")==0){
1646 int delay, tail_len, frame_size;
1649 linphone_core_enable_echo_cancellation(lc,1);
1652 n = sscanf(arg2, "%d %d %d", &delay, &tail_len, &frame_size);
1655 lp_config_set_int(lc->config,"sound","ec_delay",delay);
1658 lp_config_set_int(lc->config,"sound","ec_delay",delay);
1659 lp_config_set_int(lc->config,"sound","ec_tail_len",tail_len);
1662 lp_config_set_int(lc->config,"sound","ec_delay",delay);
1663 lp_config_set_int(lc->config,"sound","ec_tail_len",tail_len);
1664 lp_config_set_int(lc->config,"sound","ec_framesize",frame_size);
1668 else if (strcmp(arg1,"off")==0){
1669 linphone_core_enable_echo_cancellation(lc,0);
1671 else if (strcmp(arg1,"show")==0){
1672 linphonec_out("echo cancellation is %s; delay %d, tail length %d, frame size %d\n",
1673 linphone_core_echo_cancellation_enabled(lc) ? "on" : "off",
1674 lp_config_get_int(lc->config,"sound","ec_delay",0),
1675 lp_config_get_int(lc->config,"sound","ec_tail_len",0),
1676 lp_config_get_int(lc->config,"sound","ec_framesize",0));
1685 /***************************************************************************
1687 * Command table management funx
1689 ***************************************************************************/
1692 * Find a command given its name
1694 static LPC_COMMAND *
1695 lpc_find_command(const char *name)
1699 for (i=0; commands[i].name; ++i)
1701 if (strcmp(name, commands[i].name) == 0)
1702 return &commands[i];
1705 return (LPC_COMMAND *)NULL;
1709 /****************************************************************************
1711 * $Log: commands.c,v $
1712 * Revision 1.39 2008/07/03 15:08:34 smorlat
1713 * api cleanups, interface in progress.
1715 * Revision 1.38 2008/06/17 20:38:59 smorlat
1716 * added missing file.
1718 * Revision 1.37 2008/04/09 09:26:00 smorlat
1719 * merge various patches
1722 * Revision 1.36 2007/08/01 14:47:53 strk
1723 * * console/commands.c: Clean up commands 'nat', 'stun'
1724 * and 'firewall' to be more intuitive.
1726 * Revision 1.35 2007/06/27 09:01:25 smorlat
1727 * logging improvements.
1729 * Revision 1.34 2007/02/20 10:17:13 smorlat
1730 * linphonec friends patch2
1732 * Revision 1.31 2006/09/22 07:22:47 smorlat
1733 * linphonecore api changes.
1735 * Revision 1.30 2006/09/08 15:32:57 smorlat
1736 * support for using files instead of soundcard (used by linphonec only)
1738 * Revision 1.29 2006/08/28 14:29:07 smorlat
1741 * Revision 1.28 2006/08/21 12:49:59 smorlat
1742 * merged several little patches.
1744 * Revision 1.27 2006/07/17 18:45:00 smorlat
1745 * support for several event queues in ortp.
1746 * glib dependency removed from coreapi/ and console/
1748 * Revision 1.26 2006/04/14 15:16:36 smorlat
1749 * soundcard use did nothing !
1751 * Revision 1.25 2006/04/06 20:09:33 smorlat
1752 * add linphonec command to see and select sound devices.
1754 * Revision 1.24 2006/03/04 11:17:10 smorlat
1755 * mediastreamer2 in progress.
1757 * Revision 1.23 2006/02/20 21:14:01 strk
1758 * Handled syntax errors with 'friend' command
1760 * Revision 1.22 2006/02/20 10:20:29 strk
1761 * Added substring-based filter support for command 'friend list'
1763 * Revision 1.21 2006/02/02 15:39:18 strk
1764 * - Added 'friend list' and 'friend call' commands
1765 * - Allowed for multiple DTFM send in a single line
1766 * - Added status-specific callback (bare version)
1768 * Revision 1.20 2006/01/26 11:54:34 strk
1769 * More robust 'nat' command handler (strip blanks in args)
1771 * Revision 1.19 2006/01/26 09:48:05 strk
1772 * Added limits.h include
1774 * Revision 1.18 2006/01/26 02:18:05 strk
1775 * Added new commands 'nat use' and 'nat unuse'.
1776 * These will required a pending patch to linphonecore.c
1779 * Revision 1.17 2006/01/20 14:12:33 strk
1780 * Added linphonec_init() and linphonec_finish() functions.
1781 * Handled SIGINT and SIGTERM to invoke linphonec_finish().
1782 * Handling of auto-termination (-t) moved to linphonec_finish().
1783 * Reworked main (input read) loop to not rely on 'terminate'
1784 * and 'run' variable (dropped). configfile_name allocated on stack
1785 * using PATH_MAX limit. Changed print_usage signature to allow
1786 * for an exit_status specification.
1788 * Revision 1.16 2006/01/18 09:25:32 strk
1789 * Command completion inhibited in proxy addition and auth request prompts.
1790 * Avoided use of linphonec_readline's internal filename completion.
1792 * Revision 1.15 2006/01/14 13:29:32 strk
1793 * Reworked commands interface to use a table structure,
1794 * used by command line parser and help function.
1795 * Implemented first level of completion (commands).
1796 * Added notification of invalid "answer" and "terminate"
1797 * commands (no incoming call, no active call).
1798 * Forbidden "call" intialization when a call is already active.
1799 * Cleaned up all commands, adding more feedback and error checks.
1801 * Revision 1.14 2006/01/13 13:00:29 strk
1802 * Added linphonec.h. Code layout change (added comments, forward decl,
1803 * globals on top, copyright notices and Logs). Handled out-of-memory
1804 * condition on history management. Removed assumption on sizeof(char).
1805 * Fixed bug in authentication prompt (introduced by linphonec_readline).
1806 * Added support for multiple authentication requests (up to MAX_PENDING_AUTH).
1809 ****************************************************************************/