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"
46 /***************************************************************************
48 * Forward declarations
50 ***************************************************************************/
52 extern char *lpc_strip_blanks(char *input);
54 /* Command handlers */
55 static int lpc_cmd_help(LinphoneCore *, char *);
56 static int lpc_cmd_proxy(LinphoneCore *, char *);
57 static int lpc_cmd_call(LinphoneCore *, char *);
58 static int lpc_cmd_calls(LinphoneCore *, char *);
59 static int lpc_cmd_chat(LinphoneCore *, char *);
60 static int lpc_cmd_answer(LinphoneCore *, char *);
61 static int lpc_cmd_autoanswer(LinphoneCore *, char *);
62 static int lpc_cmd_terminate(LinphoneCore *, char *);
63 static int lpc_cmd_call_logs(LinphoneCore *, char *);
64 static int lpc_cmd_ipv6(LinphoneCore *, char *);
65 static int lpc_cmd_transfer(LinphoneCore *, char *);
66 static int lpc_cmd_quit(LinphoneCore *, char *);
67 static int lpc_cmd_nat(LinphoneCore *, char *);
68 static int lpc_cmd_stun(LinphoneCore *, char *);
69 static int lpc_cmd_firewall(LinphoneCore *, char *);
70 static int lpc_cmd_friend(LinphoneCore *, char*);
71 static int lpc_cmd_soundcard(LinphoneCore *, char *);
72 static int lpc_cmd_webcam(LinphoneCore *, char *);
73 static int lpc_cmd_staticpic(LinphoneCore *, char *);
74 static int lpc_cmd_play(LinphoneCore *, char *);
75 static int lpc_cmd_record(LinphoneCore *, char *);
76 static int lpc_cmd_register(LinphoneCore *, char *);
77 static int lpc_cmd_unregister(LinphoneCore *, char *);
78 static int lpc_cmd_duration(LinphoneCore *lc, char *args);
79 static int lpc_cmd_status(LinphoneCore *lc, char *args);
80 static int lpc_cmd_ports(LinphoneCore *lc, char *args);
81 static int lpc_cmd_speak(LinphoneCore *lc, char *args);
82 static int lpc_cmd_acodec(LinphoneCore *lc, char *args);
83 static int lpc_cmd_vcodec(LinphoneCore *lc, char *args);
84 static int lpc_cmd_codec(int type, LinphoneCore *lc, char *args);
85 static int lpc_cmd_echocancellation(LinphoneCore *lc, char *args);
86 static int lpc_cmd_pause(LinphoneCore *lc, char *args);
87 static int lpc_cmd_resume(LinphoneCore *lc, char *args);
88 static int lpc_cmd_mute_mic(LinphoneCore *lc, char *args);
89 static int lpc_cmd_unmute_mic(LinphoneCore *lc, char *args);
90 static int lpc_cmd_rtp_no_xmit_on_audio_mute(LinphoneCore *lc, char *args);
92 static int lpc_cmd_camera(LinphoneCore *lc, char *args);
93 static int lpc_cmd_video_window(LinphoneCore *lc, char *args);
94 static int lpc_cmd_preview_window(LinphoneCore *lc, char *args);
95 static int lpc_cmd_snapshot(LinphoneCore *lc, char *args);
97 static int lpc_cmd_states(LinphoneCore *lc, char *args);
98 static int lpc_cmd_identify(LinphoneCore *lc, char *args);
99 static int lpc_cmd_ringback(LinphoneCore *lc, char *args);
101 /* Command handler helpers */
102 static void linphonec_proxy_add(LinphoneCore *lc);
103 static void linphonec_proxy_display(LinphoneProxyConfig *lc);
104 static void linphonec_proxy_list(LinphoneCore *lc);
105 static void linphonec_proxy_remove(LinphoneCore *lc, int index);
106 static int linphonec_proxy_use(LinphoneCore *lc, int index);
107 static void linphonec_proxy_show(LinphoneCore *lc,int index);
108 static void linphonec_friend_display(LinphoneFriend *fr);
109 static int linphonec_friend_list(LinphoneCore *lc, char *arg);
110 static void linphonec_display_command_help(LPC_COMMAND *cmd);
111 static int linphonec_friend_call(LinphoneCore *lc, unsigned int num);
113 static int linphonec_friend_add(LinphoneCore *lc, const char *name, const char *addr);
115 static int linphonec_friend_delete(LinphoneCore *lc, int num);
116 static int linphonec_friend_delete(LinphoneCore *lc, int num);
117 static void linphonec_codec_list(int type, LinphoneCore *lc);
118 static void linphonec_codec_enable(int type, LinphoneCore *lc, int index);
119 static void linphonec_codec_disable(int type, LinphoneCore *lc, int index);
123 /* Command table management */
124 static LPC_COMMAND *lpc_find_command(const char *name);
126 void linphonec_out(const char *fmt,...);
128 VideoParams lpc_video_params={-1,-1,-1,-1,0,TRUE};
129 VideoParams lpc_preview_params={-1,-1,-1,-1,0,TRUE};
131 /***************************************************************************
135 ***************************************************************************/
140 static LPC_COMMAND commands[] = {
141 { "help", lpc_cmd_help, "Print commands help.",
142 "'help <command>'\t: displays specific help for command.\n"
143 "'help advanced'\t: shows advanced commands.\n"
145 { "call", lpc_cmd_call, "Call a SIP uri or number",
147 "'call <sip-url or number> [options]' \t: initiate a call to the specified destination.\n"
149 "--audio-only : initiate the call without video.\n"
150 "--early-media : sends audio and video stream immediately when remote proposes early media.\n"
152 "'call <sip-url or number>' \t: initiate a call to the specified destination.\n"
155 { "calls", lpc_cmd_calls, "Show all the current calls with their id and status.",
158 { "chat", lpc_cmd_chat, "Chat with a SIP uri",
159 "'chat <sip-url> \"message\"' "
160 ": send a chat message \"message\" to the specified destination."
162 { "terminate", lpc_cmd_terminate, "Terminate a call",
163 "'terminate' : Terminate the current call\n"
164 "'terminate <call id>' : Terminate the call with supplied id\n"
165 "'terminate <all>' : Terminate all the current calls\n"
167 { "answer", lpc_cmd_answer, "Answer a call",
168 "'answer' : Answer the current incoming call\n"
169 "'answer <call id>' : Answer the call with given id\n"
171 { "pause", lpc_cmd_pause, "pause a call",
172 "'pause' : pause the current call\n"},
173 { "resume", lpc_cmd_resume, "resume a call",
174 "'resume' : resume the unique call\n"
175 "'resume <call id>' : hold off the call with given id\n"},
176 { "transfer", lpc_cmd_transfer,
177 "Transfer a call to a specified destination.",
178 "'transfer <sip-uri>' : transfers the current active call to the destination sip-uri\n"
179 "'transfer <call id> <sip-uri>': transfers the call with 'id' to the destination sip-uri\n"
180 "'transfer <call id1> --to-call <call id2>': transfers the call with 'id1' to the destination of call 'id2' (attended transfer)\n"
182 { "mute", lpc_cmd_mute_mic,
183 "Mute microphone and suspend voice transmission."},
185 { "camera", lpc_cmd_camera, "Send camera output for current call.",
186 "'camera on'\t: allow sending of local camera video to remote end.\n"
187 "'camera off'\t: disable sending of local camera's video to remote end.\n"},
189 { "unmute", lpc_cmd_unmute_mic,
190 "Unmute microphone and resume voice transmission."},
191 { "duration", lpc_cmd_duration, "Print duration in seconds of the last call.", NULL },
193 { "autoanswer", lpc_cmd_autoanswer, "Show/set auto-answer mode",
194 "'autoanswer' \t: show current autoanswer mode\n"
195 "'autoanswer enable'\t: enable autoanswer mode\n"
196 "'autoanswer disable'\t: disable autoanswer modeĀ \n"},
197 { "proxy", lpc_cmd_proxy, "Manage proxies",
198 "'proxy list' : list all proxy setups.\n"
199 "'proxy add' : add a new proxy setup.\n"
200 "'proxy remove <index>' : remove proxy setup with number index.\n"
201 "'proxy use <index>' : use proxy with number index as default proxy.\n"
202 "'proxy unuse' : don't use a default proxy.\n"
203 "'proxy show <index>' : show configuration and status of the proxy numbered by index.\n"
204 "'proxy show default' : show configuration and status of the default proxy.\n"
206 { "soundcard", lpc_cmd_soundcard, "Manage soundcards",
207 "'soundcard list' : list all sound devices.\n"
208 "'soundcard show' : show current sound devices configuration.\n"
209 "'soundcard use <index>' : select a sound device.\n"
210 "'soundcard use files' : use .wav files instead of soundcard\n"
212 { "webcam", lpc_cmd_webcam, "Manage webcams",
213 "'webcam list' : list all known devices.\n"
214 "'webcam use <index>' : select a video device.\n"
216 { "ipv6", lpc_cmd_ipv6, "Use IPV6",
217 "'ipv6 status' : show ipv6 usage status.\n"
218 "'ipv6 enable' : enable the use of the ipv6 network.\n"
219 "'ipv6 disable' : do not use ipv6 network."
221 { "nat", lpc_cmd_nat, "Set nat address",
222 "'nat' : show nat settings.\n"
223 "'nat <addr>' : set nat address.\n"
225 { "stun", lpc_cmd_stun, "Set stun server address",
226 "'stun' : show stun settings.\n"
227 "'stun <addr>' : set stun server address.\n"
229 { "firewall", lpc_cmd_firewall, "Set firewall policy",
230 "'firewall' : show current firewall policy.\n"
231 "'firewall none' : use direct connection.\n"
232 "'firewall nat' : use nat address given with the 'nat' command.\n"
233 "'firewall stun' : use stun server given with the 'stun' command.\n"
235 { "call-logs", lpc_cmd_call_logs, "Calls history", NULL },
236 { "friend", lpc_cmd_friend, "Manage friends",
237 "'friend list [<pattern>]' : list friends.\n"
238 "'friend call <index>' : call a friend.\n"
239 "'friend add <name> <addr>' : add friend, <name> must be quoted to include\n"
240 " spaces, <addr> has \"sip:\" added if it isn't\n"
241 " there. Don't use '<' '>' around <addr>.\n"
242 "'friend delete <index>' : remove friend, 'all' removes all\n"
244 { "play", lpc_cmd_play, "play a wav file",
245 "This command has two roles:\n"
246 "Plays a file instead of capturing from soundcard - only available in file mode (see 'help soundcard')\n"
247 "Specifies a wav file to be played to play music to far end when putting it on hold (pause)\n"
248 "'play <wav file>' : play a wav file."
250 { "record", lpc_cmd_record, "record to a wav file",
251 "This feature is available only in file mode (see 'help soundcard')\n"
252 "'record <wav file>' : record into wav file."
254 { "quit", lpc_cmd_quit, "Exit linphonec", NULL },
255 { (char *)NULL, (lpc_cmd_handler)NULL, (char *)NULL, (char *)NULL }
259 static LPC_COMMAND advanced_commands[] = {
260 { "codec", lpc_cmd_acodec, "Audio codec configuration",
261 "'codec list' : list audio codecs\n"
262 "'codec enable <index>' : enable available audio codec\n"
263 "'codec disable <index>' : disable audio codec" },
264 { "vcodec", lpc_cmd_vcodec, "Video codec configuration",
265 "'vcodec list' : list video codecs\n"
266 "'vcodec enable <index>' : enable available video codec\n"
267 "'vcodec disable <index>' : disable video codec" },
268 { "ec", lpc_cmd_echocancellation, "Echo cancellation",
269 "'ec on [<delay>] [<tail>] [<framesize>]' : turn EC on with given delay, tail length and framesize\n"
270 "'ec off' : turn echo cancellation (EC) off\n"
271 "'ec show' : show EC status" },
272 { "nortp-on-audio-mute", lpc_cmd_rtp_no_xmit_on_audio_mute,
273 "Set the rtp_no_xmit_on_audio_mute configuration parameter",
274 " If set to 1 then rtp transmission will be muted when\n"
275 " audio is muted , otherwise rtp is always sent."},
277 { "vwindow", lpc_cmd_video_window, "Control video display window",
278 "'vwindow show': shows video window\n"
279 "'vwindow hide': hides video window\n"
280 "'vwindow pos <x> <y>': Moves video window to x,y pixel coordinates\n"
281 "'vwindow size <width> <height>': Resizes video window\n"
282 "'vwindow id <window id>': embeds video display into supplied window id."
284 { "pwindow", lpc_cmd_preview_window, "Control local camera video display (preview window)",
285 "'pwindow show': shows the local camera video display\n"
286 "'pwindow hide': hides the local camera video display\n"
287 "'pwindow pos <x> <y>': Moves preview window to x,y pixel coordinates\n"
288 "'pwindow size <width> <height>': Resizes preview window\n"
289 "'pwindow id <window id>': embeds preview display into supplied window id.\n"
290 "'pwindow integrated': integrate preview display within the video window of current call.\n"
291 "'pwindow standalone': use standalone window for preview display."
293 { "snapshot", lpc_cmd_snapshot, "Take a snapshot of currently received video stream",
294 "'snapshot <file path>': take a snapshot and records it in jpeg format into the supplied path\n"
297 { "states", lpc_cmd_states, "Show internal states of liblinphone, registrations and calls, according to linphonecore.h definitions",
298 "'states global': shows global state of liblinphone \n"
299 "'states calls': shows state of calls\n"
300 "'states proxies': shows state of proxy configurations"
302 { "register", lpc_cmd_register, "Register in one line to a proxy" , "register <sip identity> <sip proxy> <password>"},
303 { "unregister", lpc_cmd_unregister, "Unregister from default proxy", NULL },
304 { "status", lpc_cmd_status, "Print various status information",
305 "'status register' \t: print status concerning registration\n"
306 "'status autoanswer'\t: tell whether autoanswer mode is enabled\n"
307 "'status hook' \t: print hook status\n" },
308 { "ports", lpc_cmd_ports, "Network ports configuration",
309 "'ports' \t: prints current used ports.\n"
310 "'ports sip <port number>'\t: Sets the sip port.\n" },
311 { "speak", lpc_cmd_speak, "Speak a sentence using espeak TTS engine",
312 "This feature is available only in file mode. (see 'help soundcard')\n"
313 "'speak <voice name> <sentence>' : speak a text using the specified espeak voice.\n"
314 "Example for english voice: 'speak default Hello my friend !'"
316 { "staticpic", lpc_cmd_staticpic, "Manage static pictures when nowebcam",
317 "'staticpic set' : Set path to picture that should be used.\n"
318 "'staticpic fps' : Get/set frames per seconds for picture emission.\n"
320 { "identify", lpc_cmd_identify, "Returns the user-agent string of far end",
321 "'identify' \t: returns remote user-agent string for current call.\n"
322 "'identify <id>' \t: returns remote user-agent string for call with supplied id.\n"
324 { "ringback", lpc_cmd_ringback, "Specifies a ringback tone to be played to remote end during incoming calls",
325 "'ringback <path of mono .wav file>'\t: Specifies a ringback tone to be played to remote end during incoming calls\n"
326 "'ringback disable'\t: Disable playing of ringback tone to callers\n"
328 { NULL,NULL,NULL,NULL}
333 /***************************************************************************
337 ***************************************************************************/
340 * Main command dispatcher.
341 * WARNING: modifies second argument!
343 * Always return 1 currently.
346 linphonec_parse_command_line(LinphoneCore *lc, char *cl)
352 /* Isolate first word and args */
353 while(*ptr && !isspace(*ptr)) ++ptr;
357 /* set args to first nonblank */
359 while(*args && isspace(*args)) ++args;
363 if ( isdigit(*cl) || *cl == '#' || *cl == '*' )
365 while ( isdigit(*cl) || *cl == '#' || *cl == '*' )
367 linphone_core_send_dtmf(lc, *cl);
368 linphone_core_play_dtmf (lc,*cl,100);
369 ms_sleep(1); // be nice
373 // discard spurious trailing chars
377 /* Handle other kind of commands */
378 cmd=lpc_find_command(cl);
381 linphonec_out("'%s': Cannot understand this.\n", cl);
385 if ( ! cmd->func(lc, args) )
387 linphonec_out("Syntax error.\n");
388 linphonec_display_command_help(cmd);
395 * Generator function for command completion.
396 * STATE let us know whether to start from scratch;
397 * without any state (STATE==0), then we start at the
401 linphonec_command_generator(const char *text, int state)
403 static int index, len, adv;
413 * Return the next name which partially matches
414 * from the commands list
417 while ((name=commands[index].name))
419 ++index; /* so next call get next command */
421 if (strncmp(name, text, len) == 0)
423 return ortp_strdup(name);
430 while ((name=advanced_commands[index].name))
432 ++index; /* so next call get next command */
434 if (strncmp(name, text, len) == 0)
436 return ortp_strdup(name);
444 /***************************************************************************
448 ***************************************************************************/
451 lpc_cmd_help(LinphoneCore *lc, char *arg)
458 linphonec_out("Commands are:\n");
459 linphonec_out("---------------------------\n");
461 while (commands[i].help)
463 linphonec_out("%10.10s\t%s\n", commands[i].name,
468 linphonec_out("---------------------------\n");
469 linphonec_out("Type 'help <command>' for more details or 'help advanced' to list additional commands.\n");
474 if (strcmp(arg,"advanced")==0){
475 linphonec_out("Advanced commands are:\n");
476 linphonec_out("---------------------------\n");
478 while (advanced_commands[i].help)
480 linphonec_out("%10.10s\t%s\n", advanced_commands[i].name,
481 advanced_commands[i].help);
485 linphonec_out("---------------------------\n");
486 linphonec_out("Type 'help <command>' for more details.\n");
491 cmd=lpc_find_command(arg);
494 linphonec_out("No such command.\n");
498 linphonec_display_command_help(cmd);
503 static char callee_name[256]={0};
504 static char caller_name[256]={0};
506 static const char *get_call_status(LinphoneCall *call){
507 switch(linphone_call_get_state(call)){
508 case LinphoneCallPaused:
509 if (linphone_call_get_refer_to (call)!=NULL){
510 return "Paused (transfered)";
515 case LinphoneCallPausedByRemote:
516 return "Paused by remote";
518 case LinphoneCallIncomingReceived:
521 case LinphoneCallOutgoingInit:
522 case LinphoneCallOutgoingProgress:
523 return "Dialing out";
525 case LinphoneCallOutgoingEarlyMedia:
526 case LinphoneCallOutgoingRinging:
527 return "Remote ringing";
530 if (linphone_call_has_transfer_pending(call)){
531 return "Running (transfer pending)";
539 lpc_cmd_call(LinphoneCore *lc, char *args)
541 if ( ! args || ! *args )
547 LinphoneCallParams *cp=linphone_core_create_default_call_parameters (lc);
549 if ( linphone_core_in_call(lc) )
551 linphonec_out("Terminate or hold on the current call first.\n");
554 opt1=strstr(args,"--audio-only");
555 opt2=strstr(args,"--early-media");
558 linphone_call_params_enable_video (cp,FALSE);
562 linphone_call_params_enable_early_media_sending(cp,TRUE);
564 if ( NULL == (call=linphone_core_invite_with_params(lc, args,cp)) )
566 linphonec_out("Error from linphone_core_invite.\n");
570 snprintf(callee_name,sizeof(callee_name),"%s",args);
572 linphone_call_params_destroy(cp);
578 lpc_cmd_calls(LinphoneCore *lc, char *args){
579 const MSList *calls = linphone_core_get_calls(lc);
582 const MSList *p_calls = calls;
583 linphonec_out("ID\t\tDestination\t\t\t\tStatus\n---------------------------------------------------------------------\n");
584 while(p_calls != NULL)
586 LinphoneCall *call=(LinphoneCall*)p_calls->data;
587 char *tmp=linphone_call_get_remote_address_as_string(call);
588 linphonec_out("%li\t%s\t\t\t%s\r\n",
589 (long)linphone_call_get_user_pointer (call),
591 get_call_status(call));
592 p_calls = p_calls->next;
597 linphonec_out("No active call.\n");
604 lpc_cmd_chat(LinphoneCore *lc, char *args)
612 /* Isolate first and second arg */
613 while(*ptr && !isspace(*ptr)) ++ptr;
618 while(*arg2 && isspace(*arg2)) ++arg2;
622 /* missing one parameter */
625 LinphoneChatRoom *cr = linphone_core_create_chat_room(lc,arg1);
626 linphone_chat_room_send_message(cr,arg2);
627 linphone_chat_room_destroy(cr);
632 const char *linphonec_get_callee(){
636 const char *linphonec_get_caller(){
640 void linphonec_set_caller(const char *caller){
641 snprintf(caller_name,sizeof(caller_name)-1,"%s",caller);
645 lpc_cmd_transfer(LinphoneCore *lc, char *args)
650 const char *refer_to=NULL;
654 int n=sscanf(args,"%s %s %li",arg1,arg2,&id2);
655 if (n==1 || isalpha(*arg1)){
656 call=linphone_core_get_current_call(lc);
657 if (call==NULL && ms_list_size(linphone_core_get_calls(lc))==1){
658 call=(LinphoneCall*)linphone_core_get_calls(lc)->data;
662 linphonec_out("No active call, please specify a call id among the ones listed by 'calls' command.\n");
665 linphone_core_transfer_call(lc, call, refer_to);
668 refer_to=args+strlen(arg1)+1;
669 call=linphonec_get_call(id);
670 if (call==NULL) return 0;
671 linphone_core_transfer_call(lc, call, refer_to);
674 call=linphonec_get_call(id);
675 call2=linphonec_get_call(id2);
676 if (call==NULL || call2==NULL) return 0;
677 if (strcmp(arg2,"--to-call")!=0){
680 linphonec_out("Performing attended transfer of call %i to call %i",id,id2);
681 linphone_core_transfer_call_to_another (lc,call,call2);
684 linphonec_out("Transfer command requires at least one argument\n");
691 lpc_cmd_terminate(LinphoneCore *lc, char *args)
693 if (linphone_core_get_calls(lc)==NULL){
694 linphonec_out("No active calls\n");
699 if ( -1 == linphone_core_terminate_call(lc, NULL) ){
700 linphonec_out("Could not stop the active call.\n");
705 if(strcmp(args,"all")==0){
706 linphonec_out("We are going to stop all the calls.\n");
707 linphone_core_terminate_all_calls(lc);
710 /*the argument is a linphonec call id */
712 LinphoneCall *call=linphonec_get_call(id);
714 if (linphone_core_terminate_call(lc,call)==-1){
715 linphonec_out("Could not stop the call with id %li\n",id);
725 lpc_cmd_answer(LinphoneCore *lc, char *args){
728 int nb=ms_list_size(linphone_core_get_calls(lc));
730 //if just one call is present answer the only one in passing NULL to the linphone_core_accept_call ...
731 if ( -1 == linphone_core_accept_call(lc, NULL) )
733 linphonec_out("Fail to accept incoming call\n");
736 linphonec_out("There are no calls to answer.\n");
738 linphonec_out("Multiple calls in progress, please specify call id.\n");
744 if (sscanf(args,"%li",&id)==1){
745 LinphoneCall *call=linphonec_get_call (id);
746 if (linphone_core_accept_call (lc,call)==-1){
747 linphonec_out("Fail to accept call %i\n",id);
756 lpc_cmd_autoanswer(LinphoneCore *lc, char *args)
760 if ( linphonec_get_autoanswer() ) {
761 linphonec_out("Auto answer is enabled. Use 'autoanswer disable' to disable.\n");
763 linphonec_out("Auto answer is disabled. Use 'autoanswer enable' to enable.\n");
768 if (strstr(args,"enable")){
769 linphonec_set_autoanswer(TRUE);
770 linphonec_out("Auto answer enabled.\n");
771 }else if (strstr(args,"disable")){
772 linphonec_set_autoanswer(FALSE);
773 linphonec_out("Auto answer disabled.\n");
779 lpc_cmd_quit(LinphoneCore *lc, char *args)
781 linphonec_main_loop_exit();
786 lpc_cmd_nat(LinphoneCore *lc, char *args)
791 if ( args ) args=lpc_strip_blanks(args);
795 linphone_core_set_nat_address(lc, args);
796 /* linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_NAT_ADDRESS); */
799 nat = linphone_core_get_nat_address(lc);
800 use = linphone_core_get_firewall_policy(lc)==LinphonePolicyUseNatAddress;
801 linphonec_out("Nat address: %s%s\n", nat ? nat : "unspecified" , use ? "" : " (disabled - use 'firewall nat' to enable)");
807 lpc_cmd_stun(LinphoneCore *lc, char *args)
812 if ( args ) args=lpc_strip_blanks(args);
816 linphone_core_set_stun_server(lc, args);
817 /* linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_STUN); */
820 stun = linphone_core_get_stun_server(lc);
821 use = linphone_core_get_firewall_policy(lc)==LinphonePolicyUseStun;
822 linphonec_out("Stun server: %s%s\n", stun ? stun : "unspecified" , use? "" : " (disabled - use 'firewall stun' to enable)");
828 lpc_cmd_firewall(LinphoneCore *lc, char *args)
830 const char* setting=NULL;
832 if ( args ) args=lpc_strip_blanks(args);
836 if (strcmp(args,"none")==0)
838 linphone_core_set_firewall_policy(lc,LinphonePolicyNoFirewall);
840 else if (strcmp(args,"stun")==0)
842 setting = linphone_core_get_stun_server(lc);
845 linphonec_out("No stun server address is defined, use 'stun <address>' first\n");
848 linphone_core_set_firewall_policy(lc,LinphonePolicyUseStun);
850 else if (strcmp(args,"nat")==0)
852 setting = linphone_core_get_nat_address(lc);
855 linphonec_out("No nat address is defined, use 'nat <address>' first");
858 linphone_core_set_firewall_policy(lc,LinphonePolicyUseNatAddress);
862 switch(linphone_core_get_firewall_policy(lc))
864 case LinphonePolicyNoFirewall:
865 linphonec_out("No firewall\n");
867 case LinphonePolicyUseStun:
868 linphonec_out("Using stun server %s to discover firewall address\n", setting ? setting : linphone_core_get_stun_server(lc));
870 case LinphonePolicyUseNatAddress:
871 linphonec_out("Using supplied nat address %s.\n", setting ? setting : linphone_core_get_nat_address(lc));
878 /* Helper function for processing freind names */
880 lpc_friend_name(char **args, char **name)
882 /* Use space as a terminator unless quoted */
883 if (('"' == **args) || ('\'' == **args)){
888 while ((delim != *end) && ('\0' != *end)) end++;
890 fprintf(stderr, "Mismatched quotes\n");
897 *name = strsep(args, " ");
899 if (NULL == *args) { /* Means there was no separator */
900 fprintf(stderr, "Either name or address is missing\n");
903 if (NULL == *name) return 0;
910 lpc_cmd_friend(LinphoneCore *lc, char *args)
914 if ( args ) args=lpc_strip_blanks(args);
916 if ( ! args || ! *args ) return 0;
918 if ( !strncmp(args, "list", 4) )
920 return linphonec_friend_list(lc, args+4);
923 else if ( !strncmp(args, "call", 4) )
926 if ( ! *args ) return 0;
927 friend_num = strtol(args, NULL, 10);
929 if ( errno == ERANGE ) {
930 linphonec_out("Invalid friend number\n");
933 #endif /*_WIN32_WCE*/
934 linphonec_friend_call(lc, friend_num);
937 else if ( !strncmp(args, "delete", 6) )
940 if ( ! *args ) return 0;
941 while (*args == ' ') args++;
942 if ( ! *args ) return 0;
943 if (!strncmp(args, "all", 3))
949 friend_num = strtol(args, NULL, 10);
951 if ( errno == ERANGE ) {
952 linphonec_out("Invalid friend number\n");
955 #endif /*_WIN32_WCE*/
957 linphonec_friend_delete(lc, friend_num);
960 else if ( !strncmp(args, "add", 3) )
969 if ( ! *args ) return 0;
970 while (*args == ' ') args++;
971 if ( ! *args ) return 0;
973 if (!lpc_friend_name(&args, &name)) return 0;
975 while (*args == ' ') args++;
976 if ( ! *args ) return 0;
977 if (isdigit(*args)) {
978 strcpy (addr, "sip:");
979 addr_p = addr + strlen("sip:");
981 addr_orig = strsep(&args, " ");
982 if (1 >= strlen(addr_orig)) {
983 fprintf(stderr, "A single-digit address is not valid\n");
986 strcpy(addr_p, addr_orig);
987 linphonec_friend_add(lc, name, addr);
989 LinphoneFriend *new_friend;
990 new_friend = linphone_friend_new_with_addr(args);
991 linphone_core_add_friend(lc, new_friend);
998 static int lpc_cmd_play(LinphoneCore *lc, char *args){
999 if ( args ) args=lpc_strip_blanks(args);
1000 if ( ! args || ! *args ) return 0;
1001 linphone_core_set_play_file(lc,args);
1005 static int lpc_cmd_record(LinphoneCore *lc, char *args){
1006 if ( args ) args=lpc_strip_blanks(args);
1007 if ( ! args || ! *args ) return 0;
1008 linphone_core_set_record_file(lc,args);
1016 lpc_cmd_proxy(LinphoneCore *lc, char *args)
1023 if ( ! arg1 ) return 0;
1025 /* Isolate first and second arg */
1026 while(*ptr && !isspace(*ptr)) ++ptr;
1031 while(*arg2 && isspace(*arg2)) ++arg2;
1034 if (strcmp(arg1,"add")==0)
1036 #ifdef HAVE_READLINE
1037 rl_inhibit_completion=1;
1039 linphonec_proxy_add(lc);
1040 #ifdef HAVE_READLINE
1041 rl_inhibit_completion=0;
1044 else if (strcmp(arg1,"list")==0)
1046 linphonec_proxy_list(lc);
1048 else if (strcmp(arg1,"remove")==0)
1050 linphonec_proxy_remove(lc,atoi(arg2));
1052 else if (strcmp(arg1,"use")==0)
1054 if ( arg2 && *arg2 )
1056 proxynum=atoi(arg2);
1057 if ( linphonec_proxy_use(lc, proxynum) )
1058 linphonec_out("Default proxy set to %d.\n", proxynum);
1062 proxynum=linphone_core_get_default_proxy(lc, NULL);
1063 if ( proxynum == -1 ) linphonec_out("No default proxy.\n");
1064 else linphonec_out("Current default proxy is %d.\n", proxynum);
1066 }else if (strcmp(arg1, "unuse")==0){
1067 linphone_core_set_default_proxy(lc, NULL);
1068 linphonec_out("Use no proxy.\n");
1071 else if (strcmp(arg1, "show")==0)
1075 if (strstr(arg2,"default"))
1077 proxynum=linphone_core_get_default_proxy(lc, NULL);
1078 if ( proxynum < 0 ) {
1079 linphonec_out("No default proxy defined\n");
1082 linphonec_proxy_show(lc,proxynum);
1086 linphonec_proxy_show(lc, atoi(arg2));
1089 else return 0; /* syntax error */
1094 return 0; /* syntax error */
1101 lpc_cmd_call_logs(LinphoneCore *lc, char *args)
1103 const MSList *elem=linphone_core_get_call_logs(lc);
1104 for (;elem!=NULL;elem=ms_list_next(elem))
1106 LinphoneCallLog *cl=(LinphoneCallLog*)elem->data;
1107 char *str=linphone_call_log_to_str(cl);
1108 linphonec_out("%s\n",str);
1115 lpc_cmd_ipv6(LinphoneCore *lc, char *arg1)
1119 return 0; /* syntax error */
1122 if (strcmp(arg1,"status")==0)
1124 linphonec_out("ipv6 use enabled: %s\n",linphone_core_ipv6_enabled(lc) ? "true":"false");
1126 else if (strcmp(arg1,"enable")==0)
1128 linphone_core_enable_ipv6(lc,TRUE);
1129 linphonec_out("ipv6 use enabled.\n");
1131 else if (strcmp(arg1,"disable")==0)
1133 linphone_core_enable_ipv6(lc,FALSE);
1134 linphonec_out("ipv6 use disabled.\n");
1138 return 0; /* syntax error */
1143 static int devname_to_index(LinphoneCore *lc, const char *devname){
1146 for(i=0,p=linphone_core_get_sound_devices(lc);*p!=NULL;++p,++i){
1147 if (strcmp(devname,*p)==0) return i;
1152 static const char *index_to_devname(LinphoneCore *lc, int index){
1155 for(i=0,p=linphone_core_get_sound_devices(lc);*p!=NULL;++p,++i){
1156 if (i==index) return *p;
1161 static int lpc_cmd_soundcard(LinphoneCore *lc, char *args)
1169 if (!args) return 0; /* syntax error */
1171 /* Isolate first and second arg */
1172 while(*ptr && !isspace(*ptr)) ++ptr;
1177 while(*arg2 && isspace(*arg2)) ++arg2;
1180 if (strcmp(arg1, "list")==0)
1182 dev=linphone_core_get_sound_devices(lc);
1183 for(i=0; dev[i]!=NULL; ++i){
1184 linphonec_out("%i: %s\n",i,dev[i]);
1189 if (strcmp(arg1, "show")==0)
1191 linphonec_out("Ringer device: %s\n",
1192 linphone_core_get_ringer_device(lc));
1193 linphonec_out("Playback device: %s\n",
1194 linphone_core_get_playback_device(lc));
1195 linphonec_out("Capture device: %s\n",
1196 linphone_core_get_capture_device(lc));
1200 if (strcmp(arg1, "use")==0 && arg2)
1202 if (strcmp(arg2, "files")==0)
1204 linphonec_out("Using wav files instead of soundcard.\n");
1205 linphone_core_use_files(lc,TRUE);
1209 dev=linphone_core_get_sound_devices(lc);
1210 index=atoi(arg2); /* FIXME: handle not-a-number */
1211 for(i=0;dev[i]!=NULL;i++)
1213 if (i!=index) continue;
1215 linphone_core_set_ringer_device(lc,dev[i]);
1216 linphone_core_set_playback_device(lc,dev[i]);
1217 linphone_core_set_capture_device(lc,dev[i]);
1218 linphonec_out("Using sound device %s\n",dev[i]);
1221 linphonec_out("No such sound device\n");
1224 if (strcmp(arg1, "capture")==0)
1226 const char *devname=linphone_core_get_capture_device(lc);
1228 linphonec_out("Using capture device #%i (%s)\n",
1229 devname_to_index(lc,devname),devname);
1231 index=atoi(arg2); /* FIXME: handle not-a-number */
1232 devname=index_to_devname(lc,index);
1234 linphone_core_set_capture_device(lc,devname);
1235 linphonec_out("Using capture sound device %s\n",devname);
1238 linphonec_out("No such sound device\n");
1242 if (strcmp(arg1, "playback")==0)
1244 const char *devname=linphone_core_get_playback_device(lc);
1246 linphonec_out("Using playback device #%i (%s)\n",
1247 devname_to_index(lc,devname),devname);
1249 index=atoi(arg2); /* FIXME: handle not-a-number */
1250 devname=index_to_devname(lc,index);
1252 linphone_core_set_playback_device(lc,devname);
1253 linphonec_out("Using playback sound device %s\n",devname);
1256 linphonec_out("No such sound device\n");
1260 if (strcmp(arg1, "ring")==0)
1262 const char *devname=linphone_core_get_ringer_device(lc);
1264 linphonec_out("Using ring device #%i (%s)\n",
1265 devname_to_index(lc,devname),devname);
1267 index=atoi(arg2); /* FIXME: handle not-a-number */
1268 devname=index_to_devname(lc,index);
1270 linphone_core_set_ringer_device(lc,devname);
1271 linphonec_out("Using ring sound device %s\n",devname);
1274 linphonec_out("No such sound device\n");
1278 return 0; /* syntax error */
1281 static int lpc_cmd_webcam(LinphoneCore *lc, char *args)
1289 if (!args) return 0; /* syntax error */
1291 /* Isolate first and second arg */
1292 while(*ptr && !isspace(*ptr)) ++ptr;
1297 while(*arg2 && isspace(*arg2)) ++arg2;
1300 if (strcmp(arg1, "list")==0)
1302 dev=linphone_core_get_video_devices(lc);
1303 for(i=0; dev[i]!=NULL; ++i){
1304 linphonec_out("%i: %s\n",i,dev[i]);
1309 if (strcmp(arg1, "use")==0 && arg2)
1311 dev=linphone_core_get_video_devices(lc);
1312 index=atoi(arg2); /* FIXME: handle not-a-number */
1313 for(i=0;dev[i]!=NULL;i++)
1315 if (i!=index) continue;
1317 linphone_core_set_video_device(lc, dev[i]);
1318 linphonec_out("Using video device %s\n",dev[i]);
1321 linphonec_out("No such video device\n");
1324 return 0; /* syntax error */
1328 lpc_cmd_staticpic(LinphoneCore *lc, char *args)
1334 if (!args) return 0; /* Syntax error */
1336 /* Isolate first and second arg */
1337 while(*ptr && !isspace(*ptr)) ++ptr;
1342 while(*arg2 && isspace(*arg2)) ++arg2;
1345 if (strcmp(arg1, "set")==0 && arg2) {
1346 linphone_core_set_static_picture(lc, arg2);
1350 if (strcmp(arg1, "fps")==0) {
1352 float fps = atof(arg2); /* FIXME: Handle not-a-float */
1353 linphone_core_set_static_picture_fps(lc, fps);
1357 fps = linphone_core_get_static_picture_fps(lc);
1358 linphonec_out("Current FPS %f\n", fps);
1363 return 0; /* Syntax error */
1366 static int lpc_cmd_pause(LinphoneCore *lc, char *args){
1368 if(linphone_core_in_call(lc))
1370 linphone_core_pause_call(lc,linphone_core_get_current_call(lc));
1373 linphonec_out("you can only pause when a call is in process\n");
1377 static int lpc_cmd_resume(LinphoneCore *lc, char *args){
1379 if(linphone_core_in_call(lc))
1381 linphonec_out("There is already a call in process pause or stop it first");
1387 int n = sscanf(args, "%li", &id);
1389 LinphoneCall *call=linphonec_get_call (id);
1391 if(linphone_core_resume_call(lc,call)==-1){
1392 linphonec_out("There was a problem to resume the call check the remote address you gave %s\n",args);
1400 const MSList *calls = linphone_core_get_calls(lc);
1401 int nbcalls=ms_list_size(calls);
1404 if(linphone_core_resume_call(lc,calls->data) < 0)
1406 linphonec_out("There was a problem to resume the unique call.\n");
1409 }else if (nbcalls==0){
1410 linphonec_out("There is no calls at this time.\n");
1413 linphonec_out("There are %i calls at this time, please specify call id as given with 'calls' command.\n");
1420 /***************************************************************************
1422 * Commands helper functions
1424 ***************************************************************************/
1428 linphonec_proxy_add(LinphoneCore *lc)
1430 bool_t enable_register=FALSE;
1431 LinphoneProxyConfig *cfg;
1433 linphonec_out("Adding new proxy setup. Hit ^D to abort.\n");
1440 char *input=linphonec_readline("Enter proxy sip address: ");
1444 linphonec_out("Aborted.\n");
1449 clean=lpc_strip_blanks(input);
1455 cfg=linphone_proxy_config_new();
1456 if (linphone_proxy_config_set_server_addr(cfg,clean)<0)
1458 linphonec_out("Invalid sip address (sip:sip.domain.tld).\n");
1460 linphone_proxy_config_destroy(cfg);
1468 * SIP Proxy identity
1472 char *input=linphonec_readline("Your identity for this proxy: ");
1476 linphonec_out("Aborted.\n");
1477 linphone_proxy_config_destroy(cfg);
1482 clean=lpc_strip_blanks(input);
1488 linphone_proxy_config_set_identity(cfg, clean);
1489 if ( ! linphone_proxy_config_get_identity (cfg))
1491 linphonec_out("Invalid identity (sip:name@sip.domain.tld).\n");
1500 * SIP Proxy enable register
1504 char *input=linphonec_readline("Do you want to register on this proxy (yes/no): ");
1508 linphonec_out("Aborted.\n");
1509 linphone_proxy_config_destroy(cfg);
1514 clean=lpc_strip_blanks(input);
1520 if ( ! strcmp(clean, "yes") ) enable_register=TRUE;
1521 else if ( ! strcmp(clean, "no") ) enable_register=FALSE;
1523 linphonec_out("Please answer with 'yes' or 'no'\n");
1527 linphone_proxy_config_enableregister(cfg, enable_register);
1533 * SIP Proxy registration expiration
1535 if ( enable_register==TRUE )
1540 char *input=linphonec_readline("Specify register expiration time"
1541 " in seconds (default is 600): ");
1544 linphonec_out("Aborted.\n");
1545 linphone_proxy_config_destroy(cfg);
1549 expires=strtol(input, (char **)NULL, 10);
1550 if ( expires == LONG_MIN || expires == LONG_MAX )
1552 linphonec_out("Invalid value: %s\n", strerror(errno));
1557 linphone_proxy_config_expires(cfg, expires);
1558 linphonec_out("Expiration: %d seconds\n", linphone_proxy_config_get_expires (cfg));
1570 char *input=linphonec_readline("Specify route if needed: ");
1574 linphonec_out("Aborted.\n");
1575 linphone_proxy_config_destroy(cfg);
1580 clean=lpc_strip_blanks(input);
1583 linphonec_out("No route specified.\n");
1587 linphone_proxy_config_set_route(cfg, clean);
1588 if ( ! linphone_proxy_config_get_route(cfg) )
1590 linphonec_out("Invalid route.\n");
1600 * Final confirmation
1607 linphonec_out("--------------------------------------------\n");
1608 linphonec_proxy_display(cfg);
1609 linphonec_out("--------------------------------------------\n");
1610 input=linphonec_readline("Accept the above proxy configuration (yes/no) ?: ");
1614 linphonec_out("Aborted.\n");
1615 linphone_proxy_config_destroy(cfg);
1620 clean=lpc_strip_blanks(input);
1626 if ( ! strcmp(clean, "yes") ) break;
1627 else if ( ! strcmp(clean, "no") )
1629 linphonec_out("Declined.\n");
1630 linphone_proxy_config_destroy(cfg);
1635 linphonec_out("Please answer with 'yes' or 'no'\n");
1641 linphone_core_add_proxy_config(lc,cfg);
1643 /* automatically set the last entered proxy as the default one */
1644 linphone_core_set_default_proxy(lc,cfg);
1646 linphonec_out("Proxy added.\n");
1650 linphonec_proxy_display(LinphoneProxyConfig *cfg)
1652 const char *route=linphone_proxy_config_get_route(cfg);
1653 const char *identity=linphone_proxy_config_get_identity(cfg);
1654 linphonec_out("sip address: %s\nroute: %s\nidentity: %s\nregister: %s\nexpires: %i\nregistered: %s\n",
1655 linphone_proxy_config_get_addr(cfg),
1656 (route!=NULL)? route:"",
1657 (identity!=NULL)?identity:"",
1658 linphone_proxy_config_register_enabled (cfg)?"yes":"no",
1659 linphone_proxy_config_get_expires (cfg),
1660 linphone_proxy_config_is_registered(cfg) ? "yes" : "no");
1663 static void linphonec_proxy_show(LinphoneCore *lc, int index)
1667 for(elem=linphone_core_get_proxy_config_list(lc),i=0;elem!=NULL;elem=elem->next,++i){
1669 LinphoneProxyConfig *cfg=(LinphoneProxyConfig *)elem->data;
1670 linphonec_proxy_display(cfg);
1674 linphonec_out("No proxy with index %i\n", index);
1678 linphonec_proxy_list(LinphoneCore *lc)
1680 const MSList *proxies;
1682 int def=linphone_core_get_default_proxy(lc,NULL);
1684 proxies=linphone_core_get_proxy_config_list(lc);
1685 for(n=0;proxies!=NULL;proxies=ms_list_next(proxies),n++){
1687 linphonec_out("****** Proxy %i - this is the default one - *******\n",n);
1689 linphonec_out("****** Proxy %i *******\n",n);
1690 linphonec_proxy_display((LinphoneProxyConfig*)proxies->data);
1692 if ( ! n ) linphonec_out("No proxies defined\n");
1696 linphonec_proxy_remove(LinphoneCore *lc, int index)
1698 const MSList *proxies;
1699 LinphoneProxyConfig *cfg;
1700 proxies=linphone_core_get_proxy_config_list(lc);
1701 cfg=(LinphoneProxyConfig*)ms_list_nth_data(proxies,index);
1703 linphonec_out("No such proxy.\n");
1706 linphone_core_remove_proxy_config(lc,cfg);
1707 linphonec_out("Proxy %s removed.\n", linphone_proxy_config_get_addr(cfg));
1711 linphonec_proxy_use(LinphoneCore *lc, int index)
1713 const MSList *proxies;
1714 LinphoneProxyConfig *cfg;
1715 proxies=linphone_core_get_proxy_config_list(lc);
1716 cfg=(LinphoneProxyConfig*)ms_list_nth_data(proxies,index);
1718 linphonec_out("No such proxy (try 'proxy list').");
1721 linphone_core_set_default_proxy(lc,cfg);
1726 linphonec_friend_display(LinphoneFriend *fr)
1728 LinphoneAddress *uri=linphone_address_clone(linphone_friend_get_address(fr));
1731 linphonec_out("name: %s\n", linphone_address_get_display_name(uri));
1732 linphone_address_set_display_name(uri,NULL);
1733 str=linphone_address_as_string(uri);
1734 linphonec_out("address: %s\n", str);
1738 linphonec_friend_list(LinphoneCore *lc, char *pat)
1740 const MSList *friend;
1744 pat=lpc_strip_blanks(pat);
1745 if (!*pat) pat = NULL;
1748 friend = linphone_core_get_friend_list(lc);
1749 for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
1752 const char *name = linphone_address_get_display_name(
1753 linphone_friend_get_address((LinphoneFriend*)friend->data));
1754 if (name && ! strstr(name, pat) ) continue;
1756 linphonec_out("****** Friend %i *******\n",n);
1757 linphonec_friend_display((LinphoneFriend*)friend->data);
1764 linphonec_friend_call(LinphoneCore *lc, unsigned int num)
1766 const MSList *friend = linphone_core_get_friend_list(lc);
1770 for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
1775 addr = linphone_address_as_string(linphone_friend_get_address((LinphoneFriend*)friend->data));
1776 ret=lpc_cmd_call(lc, addr);
1781 linphonec_out("No such friend %u\n", num);
1787 linphonec_friend_add(LinphoneCore *lc, const char *name, const char *addr)
1789 LinphoneFriend *newFriend;
1793 snprintf(url, PATH_MAX, "%s <%s>", name, addr);
1794 newFriend = linphone_friend_new_with_addr(url);
1795 linphone_core_add_friend(lc, newFriend);
1801 linphonec_friend_delete(LinphoneCore *lc, int num)
1803 const MSList *friend = linphone_core_get_friend_list(lc);
1806 for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
1810 linphone_core_remove_friend(lc, friend->data);
1818 for (i = 0 ; i < n ; i++)
1819 linphonec_friend_delete(lc, 0);
1823 linphonec_out("No such friend %u\n", num);
1828 linphonec_display_command_help(LPC_COMMAND *cmd)
1830 if ( cmd->doc ) linphonec_out ("%s\n", cmd->doc);
1831 else linphonec_out("%s\n", cmd->help);
1835 static int lpc_cmd_register(LinphoneCore *lc, char *args){
1839 LinphoneProxyConfig *cfg;
1844 /* it means that you want to register the default proxy */
1845 LinphoneProxyConfig *cfg=NULL;
1846 linphone_core_get_default_proxy(lc,&cfg);
1849 if(!linphone_proxy_config_is_registered(cfg)) {
1850 linphone_proxy_config_enable_register(cfg,TRUE);
1851 linphone_proxy_config_done(cfg);
1853 linphonec_out("default proxy already registered\n");
1856 linphonec_out("we do not have a default proxy\n");
1861 passwd[0]=proxy[0]=identity[0]='\0';
1862 sscanf(args,"%s %s %s",identity,proxy,passwd);
1863 if (proxy[0]=='\0' || identity[0]=='\0'){
1864 linphonec_out("Missing parameters, see help register\n");
1867 if (passwd[0]!='\0'){
1868 LinphoneAddress *from;
1869 LinphoneAuthInfo *info;
1870 if ((from=linphone_address_new(identity))!=NULL){
1872 snprintf(realm,sizeof(realm)-1,"\"%s\"",linphone_address_get_domain(from));
1873 info=linphone_auth_info_new(linphone_address_get_username(from),NULL,passwd,NULL,NULL);
1874 linphone_core_add_auth_info(lc,info);
1875 linphone_address_destroy(from);
1876 linphone_auth_info_destroy(info);
1879 elem=linphone_core_get_proxy_config_list(lc);
1881 cfg=(LinphoneProxyConfig*)elem->data;
1882 linphone_proxy_config_edit(cfg);
1884 else cfg=linphone_proxy_config_new();
1885 linphone_proxy_config_set_identity(cfg,identity);
1886 linphone_proxy_config_set_server_addr(cfg,proxy);
1887 linphone_proxy_config_enable_register(cfg,TRUE);
1888 if (elem) linphone_proxy_config_done(cfg);
1889 else linphone_core_add_proxy_config(lc,cfg);
1890 linphone_core_set_default_proxy(lc,cfg);
1894 static int lpc_cmd_unregister(LinphoneCore *lc, char *args){
1895 LinphoneProxyConfig *cfg=NULL;
1896 linphone_core_get_default_proxy(lc,&cfg);
1897 if (cfg && linphone_proxy_config_is_registered(cfg)) {
1898 linphone_proxy_config_edit(cfg);
1899 linphone_proxy_config_enable_register(cfg,FALSE);
1900 linphone_proxy_config_done(cfg);
1902 linphonec_out("unregistered\n");
1907 static int lpc_cmd_duration(LinphoneCore *lc, char *args){
1908 LinphoneCallLog *cl;
1909 const MSList *elem=linphone_core_get_call_logs(lc);
1910 for(;elem!=NULL;elem=elem->next){
1911 if (elem->next==NULL){
1912 cl=(LinphoneCallLog*)elem->data;
1913 linphonec_out("%i seconds\n",cl->duration);
1919 static int lpc_cmd_status(LinphoneCore *lc, char *args)
1921 LinphoneProxyConfig *cfg;
1923 if ( ! args ) return 0;
1924 linphone_core_get_default_proxy(lc,&cfg);
1925 if (strstr(args,"register"))
1929 if (linphone_proxy_config_is_registered(cfg)){
1930 linphonec_out("registered, identity=%s duration=%i\n",
1931 linphone_proxy_config_get_identity(cfg),
1932 linphone_proxy_config_get_expires(cfg));
1933 }else if (linphone_proxy_config_register_enabled(cfg)){
1934 linphonec_out("registered=-1\n");
1935 }else linphonec_out("registered=0\n");
1937 else linphonec_out("registered=0\n");
1939 else if (strstr(args,"autoanswer"))
1941 if (cfg && linphone_proxy_config_is_registered(cfg))
1942 linphonec_out("autoanswer=%i\n",linphonec_get_autoanswer());
1943 else linphonec_out("unregistered\n");
1945 else if (strstr(args,"hook"))
1947 LinphoneCall *call=linphone_core_get_current_call (lc);
1948 LinphoneCallState call_state=LinphoneCallIdle;
1949 if (call) call_state=linphone_call_get_state(call);
1952 case LinphoneCallOutgoingInit:
1953 case LinphoneCallOutgoingProgress:
1954 linphonec_out("hook=dialing\n");
1956 case LinphoneCallIdle:
1957 linphonec_out("hook=offhook\n");
1959 case LinphoneCallStreamsRunning:
1960 case LinphoneCallConnected:
1961 if (linphone_call_get_dir(call)==LinphoneCallOutgoing){
1962 linphonec_out("Call out, hook=%s duration=%i, muted=%s rtp-xmit-muted=%s\n", linphonec_get_callee(),
1963 linphone_core_get_current_call_duration(lc),
1964 linphone_core_is_mic_muted (lc) ? "yes" : "no",
1965 linphone_core_is_rtp_muted(lc) ? "yes" : "no");
1967 linphonec_out("hook=answered duration=%i\n" ,
1968 linphone_core_get_current_call_duration(lc));
1971 case LinphoneCallIncomingReceived:
1972 linphonec_out("Incoming call from %s\n",linphonec_get_caller());
1984 static int lpc_cmd_ports(LinphoneCore *lc, char *args)
1988 linphonec_out("sip port = %i\naudio rtp port = %i\nvideo rtp port = %i\n",
1989 linphone_core_get_sip_port(lc),
1990 linphone_core_get_audio_port(lc),
1991 linphone_core_get_video_port(lc));
1994 if (sscanf(args,"sip %i",&port)==1){
1995 linphonec_out("Setting sip port to %i\n",port);
1996 linphone_core_set_sip_port(lc,port);
2002 static int lpc_cmd_speak(LinphoneCore *lc, char *args){
2011 if (!args) return 0;
2012 memset(voice,0,sizeof(voice));
2013 sscanf(args,"%s63",voice);
2014 sentence=args+strlen(voice);
2015 wavfile=tempnam("/tmp/","linphonec-espeak-");
2016 snprintf(cl,sizeof(cl),"espeak -v %s -s 100 -w %s --stdin",voice,wavfile);
2019 ms_error("Could not open pipe to espeak !");
2022 fprintf(file,"%s",sentence);
2023 status=pclose(file);
2024 if (WEXITSTATUS(status)==0){
2025 linphone_core_set_play_file(lc,wavfile);
2027 linphonec_out("espeak command failed.");
2030 linphonec_out("Sorry, this command is not implemented in windows version.");
2035 static int lpc_cmd_acodec(LinphoneCore *lc, char *args){
2036 return lpc_cmd_codec(AUDIO, lc, args);
2039 static int lpc_cmd_vcodec(LinphoneCore *lc, char *args){
2040 return lpc_cmd_codec(VIDEO, lc, args);
2043 static int lpc_cmd_codec(int type, LinphoneCore *lc, char *args){
2048 if (!args) return 0;
2050 /* Isolate first and second arg */
2051 while(*ptr && !isspace(*ptr)) ++ptr;
2056 while(*arg2 && isspace(*arg2)) ++arg2;
2059 if (strcmp(arg1,"enable")==0)
2061 #ifdef HAVE_READLINE
2062 rl_inhibit_completion=1;
2064 if (!strcmp(arg2,"all")) linphonec_codec_enable(type,lc,-1);
2065 else linphonec_codec_enable(type,lc,atoi(arg2));
2066 #ifdef HAVE_READLINE
2067 rl_inhibit_completion=0;
2070 else if (strcmp(arg1,"list")==0)
2072 linphonec_codec_list(type,lc);
2074 else if (strcmp(arg1,"disable")==0)
2076 if (!strcmp(arg2,"all")) linphonec_codec_disable(type,lc,-1);
2077 else linphonec_codec_disable(type,lc,atoi(arg2));
2081 return 0; /* syntax error */
2087 static void linphonec_codec_list(int type, LinphoneCore *lc){
2090 const MSList *node=NULL;
2092 if (type == AUDIO) {
2093 node=linphone_core_get_audio_codecs(lc);
2094 } else if(type==VIDEO) {
2095 node=linphone_core_get_video_codecs(lc);
2098 for(;node!=NULL;node=ms_list_next(node)){
2099 pt=(PayloadType*)(node->data);
2100 linphonec_out("%2d: %s (%d) %s\n", index, pt->mime_type, pt->clock_rate,
2101 linphone_core_payload_type_enabled(lc,pt) ? "enabled" : "disabled");
2106 static void linphonec_codec_enable(int type, LinphoneCore *lc, int sel_index){
2109 const MSList *node=NULL;
2111 if (type == AUDIO) {
2112 node=linphone_core_get_audio_codecs(lc);
2113 } else if(type==VIDEO) {
2114 node=linphone_core_get_video_codecs(lc);
2117 for(;node!=NULL;node=ms_list_next(node)){
2118 if (index == sel_index || sel_index == -1) {
2119 pt=(PayloadType*)(node->data);
2120 linphone_core_enable_payload_type (lc,pt,TRUE);
2121 linphonec_out("%2d: %s (%d) %s\n", index, pt->mime_type, pt->clock_rate, "enabled");
2127 static void linphonec_codec_disable(int type, LinphoneCore *lc, int sel_index){
2130 const MSList *node=NULL;
2132 if (type == AUDIO) {
2133 node=linphone_core_get_audio_codecs(lc);
2134 } else if(type==VIDEO) {
2135 node=linphone_core_get_video_codecs(lc);
2138 for(;node!=NULL;node=ms_list_next(node)){
2139 if (index == sel_index || sel_index == -1) {
2140 pt=(PayloadType*)(node->data);
2141 linphone_core_enable_payload_type (lc,pt,FALSE);
2142 linphonec_out("%2d: %s (%d) %s\n", index, pt->mime_type, pt->clock_rate, "disabled");
2148 static int lpc_cmd_echocancellation(LinphoneCore *lc, char *args){
2152 LpConfig *config=linphone_core_get_config(lc);
2154 if (!args) return 0;
2156 /* Isolate first and second arg */
2157 while(*ptr && !isspace(*ptr)) ++ptr;
2162 while(*arg2 && isspace(*arg2)) ++arg2;
2165 if (strcmp(arg1,"on")==0){
2166 int delay, tail_len, frame_size;
2169 linphone_core_enable_echo_cancellation(lc,1);
2172 n = sscanf(arg2, "%d %d %d", &delay, &tail_len, &frame_size);
2175 lp_config_set_int(config,"sound","ec_delay",delay);
2178 lp_config_set_int(config,"sound","ec_delay",delay);
2179 lp_config_set_int(config,"sound","ec_tail_len",tail_len);
2182 lp_config_set_int(config,"sound","ec_delay",delay);
2183 lp_config_set_int(config,"sound","ec_tail_len",tail_len);
2184 lp_config_set_int(config,"sound","ec_framesize",frame_size);
2188 else if (strcmp(arg1,"off")==0){
2189 linphone_core_enable_echo_cancellation(lc,0);
2191 else if (strcmp(arg1,"show")==0){
2192 linphonec_out("echo cancellation is %s; delay %d, tail length %d, frame size %d\n",
2193 linphone_core_echo_cancellation_enabled(lc) ? "on" : "off",
2194 lp_config_get_int(config,"sound","ec_delay",0),
2195 lp_config_get_int(config,"sound","ec_tail_len",0),
2196 lp_config_get_int(config,"sound","ec_framesize",0));
2205 static int lpc_cmd_mute_mic(LinphoneCore *lc, char *args)
2207 linphone_core_mute_mic(lc, 1);
2211 static int lpc_cmd_unmute_mic(LinphoneCore *lc, char *args){
2212 linphone_core_mute_mic(lc, 0);
2216 static int lpc_cmd_rtp_no_xmit_on_audio_mute(LinphoneCore *lc, char *args)
2218 bool_t rtp_xmit_off=FALSE;
2222 if(strstr(args,"1"))rtp_xmit_off=TRUE;
2223 if(linphone_core_get_current_call (lc)==NULL)
2224 linphone_core_set_rtp_no_xmit_on_audio_mute(lc,rtp_xmit_off);
2226 linphonec_out("nortp-on-audio-mute: call in progress - cannot change state\n");
2228 rtp_xmit_off=linphone_core_get_rtp_no_xmit_on_audio_mute(lc);
2229 if (rtp_xmit_off) status="off";
2231 linphonec_out("rtp transmit %s when audio muted\n",status);
2235 #ifdef VIDEO_ENABLED
2236 static int _lpc_cmd_video_window(LinphoneCore *lc, char *args, bool_t is_preview){
2237 char subcommand[64];
2240 VideoParams *params=is_preview ? &lpc_preview_params : &lpc_video_params;
2242 if (!args) return 0;
2243 err=sscanf(args,"%s %i %i",subcommand,&a,&b);
2245 if (strcmp(subcommand,"pos")==0){
2246 if (err<3) return 0;
2249 params->refresh=TRUE;
2250 }else if (strcmp(subcommand,"size")==0){
2251 if (err<3) return 0;
2254 params->refresh=TRUE;
2255 }else if (strcmp(subcommand,"show")==0){
2257 params->refresh=TRUE;
2258 if (is_preview) linphone_core_enable_video_preview (lc,TRUE);
2259 }else if (strcmp(subcommand,"hide")==0){
2261 params->refresh=TRUE;
2262 if (is_preview) linphone_core_enable_video_preview (lc,FALSE);
2263 }else if (strcmp(subcommand,"id")==0){
2265 linphonec_out("vwindow id: 0x%x\n",is_preview ? linphone_core_get_native_preview_window_id (lc) :
2266 linphone_core_get_native_video_window_id (lc));
2268 } else if (err != 2) return 0;
2271 linphone_core_set_native_preview_window_id (lc,a);
2273 linphone_core_set_native_video_window_id(lc,a);
2274 }else if (is_preview==TRUE){
2275 if (strcmp(subcommand,"integrated")==0){
2276 linphone_core_use_preview_window (lc,FALSE);
2277 }else if (strcmp(subcommand,"standalone")==0){
2278 linphone_core_use_preview_window(lc,TRUE);
2285 static int lpc_cmd_video_window(LinphoneCore *lc, char *args){
2286 return _lpc_cmd_video_window(lc, args, FALSE);
2289 static int lpc_cmd_preview_window(LinphoneCore *lc, char *args){
2290 return _lpc_cmd_video_window(lc, args, TRUE);
2294 static void lpc_display_global_state(LinphoneCore *lc){
2295 linphonec_out("Global liblinphone state\n%s\n",
2296 linphone_global_state_to_string(linphone_core_get_global_state(lc)));
2299 static void lpc_display_call_states(LinphoneCore *lc){
2303 linphonec_out("Call states\n"
2304 "Id | Destination | State\n"
2305 "---------------------------------------------------------------\n");
2306 elem=linphone_core_get_calls(lc);
2308 linphonec_out("(empty)\n");
2310 for(;elem!=NULL;elem=elem->next){
2311 call=(LinphoneCall*)elem->data;
2312 tmp=linphone_call_get_remote_address_as_string (call);
2313 linphonec_out("%-2i | %-35s | %s\n",(int)(long)linphone_call_get_user_pointer(call),
2314 tmp,linphone_call_state_to_string(linphone_call_get_state(call)));
2320 static void lpc_display_proxy_states(LinphoneCore *lc){
2322 linphonec_out("Proxy registration states\n"
2323 " Identity | State\n"
2324 "------------------------------------------------------------\n");
2325 elem=linphone_core_get_proxy_config_list (lc);
2326 if (elem==NULL) linphonec_out("(empty)\n");
2328 for(;elem!=NULL;elem=elem->next){
2329 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
2330 linphonec_out("%-40s | %s\n",linphone_proxy_config_get_identity (cfg),
2331 linphone_registration_state_to_string(linphone_proxy_config_get_state(cfg)));
2336 static int lpc_cmd_states(LinphoneCore *lc, char *args){
2338 lpc_display_global_state(lc);
2339 lpc_display_call_states(lc);
2340 lpc_display_proxy_states(lc);
2343 if (strcmp(args,"global")==0){
2344 lpc_display_global_state(lc);
2347 if (strcmp(args,"proxies")==0){
2348 lpc_display_proxy_states(lc);
2351 if (strcmp(args,"calls")==0){
2352 lpc_display_call_states(lc);
2358 #ifdef VIDEO_ENABLED
2359 static int lpc_cmd_camera(LinphoneCore *lc, char *args){
2360 LinphoneCall *call=linphone_core_get_current_call(lc);
2361 bool_t activated=FALSE;
2363 if (linphone_core_video_enabled (lc)==FALSE){
2364 linphonec_out("Video is disabled, re-run linphonec with -V option.");
2369 if (strcmp(args,"on")==0)
2371 else if (strcmp(args,"off")==0)
2379 linphonec_camera_enabled=activated;
2381 if (linphonec_camera_enabled){
2382 linphonec_out("Camera is enabled. Video stream will be setup immediately for outgoing and incoming calls.\n");
2384 linphonec_out("Camera is disabled. Calls will be established with audio-only, with the possibility to later add video using 'camera on'.\n");
2387 const LinphoneCallParams *cp=linphone_call_get_current_params (call);
2389 linphone_call_enable_camera(call,activated);
2390 if ((activated && !linphone_call_params_video_enabled (cp))){
2391 /*update the call to add the video stream*/
2392 LinphoneCallParams *ncp=linphone_call_params_copy(cp);
2393 linphone_call_params_enable_video(ncp,TRUE);
2394 linphone_core_update_call(lc,call,ncp);
2395 linphone_call_params_destroy (ncp);
2396 linphonec_out("Trying to bring up video stream...\n");
2399 if (linphone_call_camera_enabled (call))
2400 linphonec_out("Camera is allowed for current call.\n");
2401 else linphonec_out("Camera is dis-allowed for current call.\n");
2406 static int lpc_cmd_snapshot(LinphoneCore *lc, char *args){
2408 if (!args) return 0;
2409 call=linphone_core_get_current_call(lc);
2411 linphone_call_take_video_snapshot (call,args);
2412 linphonec_out("Taking video snaphot in file %s\n", args);
2413 }else linphonec_out("There is no active call.\n");
2419 static int lpc_cmd_identify(LinphoneCore *lc, char *args){
2421 const char *remote_ua;
2423 call=linphone_core_get_current_call(lc);
2425 linphonec_out("There is currently running call. Specify call id.\n");
2429 call=linphonec_get_call(atoi(args));
2434 remote_ua=linphone_call_get_remote_user_agent(call);
2436 linphonec_out("Remote user agent string is: %s\n",remote_ua);
2441 static int lpc_cmd_ringback(LinphoneCore *lc, char *args){
2442 if (!args) return 0;
2443 if (strcmp(args,"disable")==0){
2444 linphone_core_set_remote_ringback_tone(lc,NULL);
2445 linphonec_out("Disabling ringback tone.\n");
2448 linphone_core_set_remote_ringback_tone (lc,args);
2449 linphonec_out("Using %s as ringback tone to be played to callers.",args);
2453 /***************************************************************************
2455 * Command table management funx
2457 ***************************************************************************/
2460 * Find a command given its name
2462 static LPC_COMMAND *
2463 lpc_find_command(const char *name)
2467 for (i=0; commands[i].name; ++i)
2469 if (strcmp(name, commands[i].name) == 0)
2470 return &commands[i];
2473 for (i=0; advanced_commands[i].name; ++i)
2475 if (strcmp(name, advanced_commands[i].name) == 0)
2476 return &advanced_commands[i];
2479 return (LPC_COMMAND *)NULL;
2483 /****************************************************************************
2485 * $Log: commands.c,v $
2486 * Revision 1.39 2008/07/03 15:08:34 smorlat
2487 * api cleanups, interface in progress.
2489 * Revision 1.38 2008/06/17 20:38:59 smorlat
2490 * added missing file.
2492 * Revision 1.37 2008/04/09 09:26:00 smorlat
2493 * merge various patches
2496 * Revision 1.36 2007/08/01 14:47:53 strk
2497 * * console/commands.c: Clean up commands 'nat', 'stun'
2498 * and 'firewall' to be more intuitive.
2500 * Revision 1.35 2007/06/27 09:01:25 smorlat
2501 * logging improvements.
2503 * Revision 1.34 2007/02/20 10:17:13 smorlat
2504 * linphonec friends patch2
2506 * Revision 1.31 2006/09/22 07:22:47 smorlat
2507 * linphonecore api changes.
2509 * Revision 1.30 2006/09/08 15:32:57 smorlat
2510 * support for using files instead of soundcard (used by linphonec only)
2512 * Revision 1.29 2006/08/28 14:29:07 smorlat
2515 * Revision 1.28 2006/08/21 12:49:59 smorlat
2516 * merged several little patches.
2518 * Revision 1.27 2006/07/17 18:45:00 smorlat
2519 * support for several event queues in ortp.
2520 * glib dependency removed from coreapi/ and console/
2522 * Revision 1.26 2006/04/14 15:16:36 smorlat
2523 * soundcard use did nothing !
2525 * Revision 1.25 2006/04/06 20:09:33 smorlat
2526 * add linphonec command to see and select sound devices.
2528 * Revision 1.24 2006/03/04 11:17:10 smorlat
2529 * mediastreamer2 in progress.
2531 * Revision 1.23 2006/02/20 21:14:01 strk
2532 * Handled syntax errors with 'friend' command
2534 * Revision 1.22 2006/02/20 10:20:29 strk
2535 * Added substring-based filter support for command 'friend list'
2537 * Revision 1.21 2006/02/02 15:39:18 strk
2538 * - Added 'friend list' and 'friend call' commands
2539 * - Allowed for multiple DTFM send in a single line
2540 * - Added status-specific callback (bare version)
2542 * Revision 1.20 2006/01/26 11:54:34 strk
2543 * More robust 'nat' command handler (strip blanks in args)
2545 * Revision 1.19 2006/01/26 09:48:05 strk
2546 * Added limits.h include
2548 * Revision 1.18 2006/01/26 02:18:05 strk
2549 * Added new commands 'nat use' and 'nat unuse'.
2550 * These will required a pending patch to linphonecore.c
2553 * Revision 1.17 2006/01/20 14:12:33 strk
2554 * Added linphonec_init() and linphonec_finish() functions.
2555 * Handled SIGINT and SIGTERM to invoke linphonec_finish().
2556 * Handling of auto-termination (-t) moved to linphonec_finish().
2557 * Reworked main (input read) loop to not rely on 'terminate'
2558 * and 'run' variable (dropped). configfile_name allocated on stack
2559 * using PATH_MAX limit. Changed print_usage signature to allow
2560 * for an exit_status specification.
2562 * Revision 1.16 2006/01/18 09:25:32 strk
2563 * Command completion inhibited in proxy addition and auth request prompts.
2564 * Avoided use of linphonec_readline's internal filename completion.
2566 * Revision 1.15 2006/01/14 13:29:32 strk
2567 * Reworked commands interface to use a table structure,
2568 * used by command line parser and help function.
2569 * Implemented first level of completion (commands).
2570 * Added notification of invalid "answer" and "terminate"
2571 * commands (no incoming call, no active call).
2572 * Forbidden "call" intialization when a call is already active.
2573 * Cleaned up all commands, adding more feedback and error checks.
2575 * Revision 1.14 2006/01/13 13:00:29 strk
2576 * Added linphonec.h. Code layout change (added comments, forward decl,
2577 * globals on top, copyright notices and Logs). Handled out-of-memory
2578 * condition on history management. Removed assumption on sizeof(char).
2579 * Fixed bug in authentication prompt (introduced by linphonec_readline).
2580 * Added support for multiple authentication requests (up to MAX_PENDING_AUTH).
2583 ****************************************************************************/