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> [--audio-only]' \t: initiate a call to the specified destination.\n"
149 "'call <sip-url or number>' \t: initiate a call to the specified destination.\n"
152 { "calls", lpc_cmd_calls, "Show all the current calls with their id and status.",
155 { "chat", lpc_cmd_chat, "Chat with a SIP uri",
156 "'chat <sip-url> \"message\"' "
157 ": send a chat message \"message\" to the specified destination."
159 { "terminate", lpc_cmd_terminate, "Terminate a call",
160 "'terminate' : Terminate the current call\n"
161 "'terminate <call id>' : Terminate the call with supplied id\n"
162 "'terminate <all>' : Terminate all the current calls\n"
164 { "answer", lpc_cmd_answer, "Answer a call",
165 "'answer' : Answer the current incoming call\n"
166 "'answer <call id>' : Answer the call with given id\n"
168 { "pause", lpc_cmd_pause, "pause a call",
169 "'pause' : pause the current call\n"},
170 { "resume", lpc_cmd_resume, "resume a call",
171 "'resume' : resume the unique call\n"
172 "'resume <call id>' : hold off the call with given id\n"},
173 { "transfer", lpc_cmd_transfer,
174 "Transfer a call to a specified destination.",
175 "'transfer <sip-uri>' : transfers the current active call to the destination sip-uri\n"
176 "'transfer <call id> <sip-uri>': transfers the call with 'id' to the destination sip-uri\n"
177 "'transfer <call id1> --to-call <call id2>': transfers the call with 'id1' to the destination of call 'id2' (attended transfer)\n"
179 { "mute", lpc_cmd_mute_mic,
180 "Mute microphone and suspend voice transmission."},
182 { "camera", lpc_cmd_camera, "Send camera output for current call.",
183 "'camera on'\t: allow sending of local camera video to remote end.\n"
184 "'camera off'\t: disable sending of local camera's video to remote end.\n"},
186 { "unmute", lpc_cmd_unmute_mic,
187 "Unmute microphone and resume voice transmission."},
188 { "duration", lpc_cmd_duration, "Print duration in seconds of the last call.", NULL },
190 { "autoanswer", lpc_cmd_autoanswer, "Show/set auto-answer mode",
191 "'autoanswer' \t: show current autoanswer mode\n"
192 "'autoanswer enable'\t: enable autoanswer mode\n"
193 "'autoanswer disable'\t: disable autoanswer modeĀ \n"},
194 { "proxy", lpc_cmd_proxy, "Manage proxies",
195 "'proxy list' : list all proxy setups.\n"
196 "'proxy add' : add a new proxy setup.\n"
197 "'proxy remove <index>' : remove proxy setup with number index.\n"
198 "'proxy use <index>' : use proxy with number index as default proxy.\n"
199 "'proxy unuse' : don't use a default proxy.\n"
200 "'proxy show <index>' : show configuration and status of the proxy numbered by index.\n"
201 "'proxy show default' : show configuration and status of the default proxy.\n"
203 { "soundcard", lpc_cmd_soundcard, "Manage soundcards",
204 "'soundcard list' : list all sound devices.\n"
205 "'soundcard show' : show current sound devices configuration.\n"
206 "'soundcard use <index>' : select a sound device.\n"
207 "'soundcard use files' : use .wav files instead of soundcard\n"
209 { "webcam", lpc_cmd_webcam, "Manage webcams",
210 "'webcam list' : list all known devices.\n"
211 "'webcam use <index>' : select a video device.\n"
213 { "ipv6", lpc_cmd_ipv6, "Use IPV6",
214 "'ipv6 status' : show ipv6 usage status.\n"
215 "'ipv6 enable' : enable the use of the ipv6 network.\n"
216 "'ipv6 disable' : do not use ipv6 network."
218 { "nat", lpc_cmd_nat, "Set nat address",
219 "'nat' : show nat settings.\n"
220 "'nat <addr>' : set nat address.\n"
222 { "stun", lpc_cmd_stun, "Set stun server address",
223 "'stun' : show stun settings.\n"
224 "'stun <addr>' : set stun server address.\n"
226 { "firewall", lpc_cmd_firewall, "Set firewall policy",
227 "'firewall' : show current firewall policy.\n"
228 "'firewall none' : use direct connection.\n"
229 "'firewall nat' : use nat address given with the 'nat' command.\n"
230 "'firewall stun' : use stun server given with the 'stun' command.\n"
232 { "call-logs", lpc_cmd_call_logs, "Calls history", NULL },
233 { "friend", lpc_cmd_friend, "Manage friends",
234 "'friend list [<pattern>]' : list friends.\n"
235 "'friend call <index>' : call a friend.\n"
236 "'friend add <name> <addr>' : add friend, <name> must be quoted to include\n"
237 " spaces, <addr> has \"sip:\" added if it isn't\n"
238 " there. Don't use '<' '>' around <addr>.\n"
239 "'friend delete <index>' : remove friend, 'all' removes all\n"
241 { "play", lpc_cmd_play, "play a wav file",
242 "This command has two roles:\n"
243 "Plays a file instead of capturing from soundcard - only available in file mode (see 'help soundcard')\n"
244 "Specifies a wav file to be played to play music to far end when putting it on hold (pause)\n"
245 "'play <wav file>' : play a wav file."
247 { "record", lpc_cmd_record, "record to a wav file",
248 "This feature is available only in file mode (see 'help soundcard')\n"
249 "'record <wav file>' : record into wav file."
251 { "quit", lpc_cmd_quit, "Exit linphonec", NULL },
252 { (char *)NULL, (lpc_cmd_handler)NULL, (char *)NULL, (char *)NULL }
256 static LPC_COMMAND advanced_commands[] = {
257 { "codec", lpc_cmd_acodec, "Audio codec configuration",
258 "'codec list' : list audio codecs\n"
259 "'codec enable <index>' : enable available audio codec\n"
260 "'codec disable <index>' : disable audio codec" },
261 { "vcodec", lpc_cmd_vcodec, "Video codec configuration",
262 "'vcodec list' : list video codecs\n"
263 "'vcodec enable <index>' : enable available video codec\n"
264 "'vcodec disable <index>' : disable video codec" },
265 { "ec", lpc_cmd_echocancellation, "Echo cancellation",
266 "'ec on [<delay>] [<tail>] [<framesize>]' : turn EC on with given delay, tail length and framesize\n"
267 "'ec off' : turn echo cancellation (EC) off\n"
268 "'ec show' : show EC status" },
269 { "nortp-on-audio-mute", lpc_cmd_rtp_no_xmit_on_audio_mute,
270 "Set the rtp_no_xmit_on_audio_mute configuration parameter",
271 " If set to 1 then rtp transmission will be muted when\n"
272 " audio is muted , otherwise rtp is always sent."},
274 { "vwindow", lpc_cmd_video_window, "Control video display window",
275 "'vwindow show': shows video window\n"
276 "'vwindow hide': hides video window\n"
277 "'vwindow pos <x> <y>': Moves video window to x,y pixel coordinates\n"
278 "'vwindow size <width> <height>': Resizes video window\n"
279 "'vwindow id <window id>': embeds video display into supplied window id."
281 { "pwindow", lpc_cmd_preview_window, "Control local camera video display (preview window)",
282 "'pwindow show': shows the local camera video display\n"
283 "'pwindow hide': hides the local camera video display\n"
284 "'pwindow pos <x> <y>': Moves preview window to x,y pixel coordinates\n"
285 "'pwindow size <width> <height>': Resizes preview window\n"
286 "'pwindow id <window id>': embeds preview display into supplied window id.\n"
287 "'pwindow integrated': integrate preview display within the video window of current call.\n"
288 "'pwindow standalone': use standalone window for preview display."
290 { "snapshot", lpc_cmd_snapshot, "Take a snapshot of currently received video stream",
291 "'snapshot <file path>': take a snapshot and records it in jpeg format into the supplied path\n"
294 { "states", lpc_cmd_states, "Show internal states of liblinphone, registrations and calls, according to linphonecore.h definitions",
295 "'states global': shows global state of liblinphone \n"
296 "'states calls': shows state of calls\n"
297 "'states proxies': shows state of proxy configurations"
299 { "register", lpc_cmd_register, "Register in one line to a proxy" , "register <sip identity> <sip proxy> <password>"},
300 { "unregister", lpc_cmd_unregister, "Unregister from default proxy", NULL },
301 { "status", lpc_cmd_status, "Print various status information",
302 "'status register' \t: print status concerning registration\n"
303 "'status autoanswer'\t: tell whether autoanswer mode is enabled\n"
304 "'status hook' \t: print hook status\n" },
305 { "ports", lpc_cmd_ports, "Network ports configuration",
306 "'ports' \t: prints current used ports.\n"
307 "'ports sip <port number>'\t: Sets the sip port.\n" },
308 { "speak", lpc_cmd_speak, "Speak a sentence using espeak TTS engine",
309 "This feature is available only in file mode. (see 'help soundcard')\n"
310 "'speak <voice name> <sentence>' : speak a text using the specified espeak voice.\n"
311 "Example for english voice: 'speak default Hello my friend !'"
313 { "staticpic", lpc_cmd_staticpic, "Manage static pictures when nowebcam",
314 "'staticpic set' : Set path to picture that should be used.\n"
315 "'staticpic fps' : Get/set frames per seconds for picture emission.\n"
317 { "identify", lpc_cmd_identify, "Returns the user-agent string of far end",
318 "'identify' \t: returns remote user-agent string for current call.\n"
319 "'identify <id>' \t: returns remote user-agent string for call with supplied id.\n"
321 { "ringback", lpc_cmd_ringback, "Specifies a ringback tone to be played to remote end during incoming calls",
322 "'ringback <path of mono .wav file>'\t: Specifies a ringback tone to be played to remote end during incoming calls\n"
323 "'ringback disable'\t: Disable playing of ringback tone to callers\n"
325 { NULL,NULL,NULL,NULL}
330 /***************************************************************************
334 ***************************************************************************/
337 * Main command dispatcher.
338 * WARNING: modifies second argument!
340 * Always return 1 currently.
343 linphonec_parse_command_line(LinphoneCore *lc, char *cl)
349 /* Isolate first word and args */
350 while(*ptr && !isspace(*ptr)) ++ptr;
354 /* set args to first nonblank */
356 while(*args && isspace(*args)) ++args;
360 if ( isdigit(*cl) || *cl == '#' || *cl == '*' )
362 while ( isdigit(*cl) || *cl == '#' || *cl == '*' )
364 linphone_core_send_dtmf(lc, *cl);
365 linphone_core_play_dtmf (lc,*cl,100);
366 ms_sleep(1); // be nice
370 // discard spurious trailing chars
374 /* Handle other kind of commands */
375 cmd=lpc_find_command(cl);
378 linphonec_out("'%s': Cannot understand this.\n", cl);
382 if ( ! cmd->func(lc, args) )
384 linphonec_out("Syntax error.\n");
385 linphonec_display_command_help(cmd);
392 * Generator function for command completion.
393 * STATE let us know whether to start from scratch;
394 * without any state (STATE==0), then we start at the
398 linphonec_command_generator(const char *text, int state)
400 static int index, len, adv;
410 * Return the next name which partially matches
411 * from the commands list
414 while ((name=commands[index].name))
416 ++index; /* so next call get next command */
418 if (strncmp(name, text, len) == 0)
420 return ortp_strdup(name);
427 while ((name=advanced_commands[index].name))
429 ++index; /* so next call get next command */
431 if (strncmp(name, text, len) == 0)
433 return ortp_strdup(name);
441 /***************************************************************************
445 ***************************************************************************/
448 lpc_cmd_help(LinphoneCore *lc, char *arg)
455 linphonec_out("Commands are:\n");
456 linphonec_out("---------------------------\n");
458 while (commands[i].help)
460 linphonec_out("%10.10s\t%s\n", commands[i].name,
465 linphonec_out("---------------------------\n");
466 linphonec_out("Type 'help <command>' for more details or 'help advanced' to list additional commands.\n");
471 if (strcmp(arg,"advanced")==0){
472 linphonec_out("Advanced commands are:\n");
473 linphonec_out("---------------------------\n");
475 while (advanced_commands[i].help)
477 linphonec_out("%10.10s\t%s\n", advanced_commands[i].name,
478 advanced_commands[i].help);
482 linphonec_out("---------------------------\n");
483 linphonec_out("Type 'help <command>' for more details.\n");
488 cmd=lpc_find_command(arg);
491 linphonec_out("No such command.\n");
495 linphonec_display_command_help(cmd);
500 static char callee_name[256]={0};
501 static char caller_name[256]={0};
503 static const char *get_call_status(LinphoneCall *call){
504 switch(linphone_call_get_state(call)){
505 case LinphoneCallPaused:
506 if (linphone_call_get_refer_to (call)!=NULL){
507 return "Paused (transfered)";
512 case LinphoneCallPausedByRemote:
513 return "Paused by remote";
515 case LinphoneCallIncomingReceived:
518 case LinphoneCallOutgoingInit:
519 case LinphoneCallOutgoingProgress:
520 return "Dialing out";
522 case LinphoneCallOutgoingEarlyMedia:
523 case LinphoneCallOutgoingRinging:
524 return "Remote ringing";
527 if (linphone_call_has_transfer_pending(call)){
528 return "Running (transfer pending)";
536 lpc_cmd_call(LinphoneCore *lc, char *args)
538 if ( ! args || ! *args )
544 LinphoneCallParams *cp=linphone_core_create_default_call_parameters (lc);
546 if ( linphone_core_in_call(lc) )
548 linphonec_out("Terminate or hold on the current call first.\n");
551 opt=strstr(args,"--audio-only");
554 linphone_call_params_enable_video (cp,FALSE);
556 if ( NULL == (call=linphone_core_invite_with_params(lc, args,cp)) )
558 linphonec_out("Error from linphone_core_invite.\n");
562 snprintf(callee_name,sizeof(callee_name),"%s",args);
564 linphone_call_params_destroy(cp);
570 lpc_cmd_calls(LinphoneCore *lc, char *args){
571 const MSList *calls = linphone_core_get_calls(lc);
574 const MSList *p_calls = calls;
575 linphonec_out("ID\t\tDestination\t\t\t\tStatus\n---------------------------------------------------------------------\n");
576 while(p_calls != NULL)
578 LinphoneCall *call=(LinphoneCall*)p_calls->data;
579 char *tmp=linphone_call_get_remote_address_as_string(call);
580 linphonec_out("%li\t%s\t\t\t%s\r\n",
581 (long)linphone_call_get_user_pointer (call),
583 get_call_status(call));
584 p_calls = p_calls->next;
589 linphonec_out("No active call.\n");
596 lpc_cmd_chat(LinphoneCore *lc, char *args)
604 /* Isolate first and second arg */
605 while(*ptr && !isspace(*ptr)) ++ptr;
610 while(*arg2 && isspace(*arg2)) ++arg2;
614 /* missing one parameter */
617 LinphoneChatRoom *cr = linphone_core_create_chat_room(lc,arg1);
618 linphone_chat_room_send_message(cr,arg2);
619 linphone_chat_room_destroy(cr);
624 const char *linphonec_get_callee(){
628 const char *linphonec_get_caller(){
632 void linphonec_set_caller(const char *caller){
633 snprintf(caller_name,sizeof(caller_name)-1,"%s",caller);
637 lpc_cmd_transfer(LinphoneCore *lc, char *args)
642 const char *refer_to=NULL;
646 int n=sscanf(args,"%s %s %li",arg1,arg2,&id2);
647 if (n==1 || isalpha(*arg1)){
648 call=linphone_core_get_current_call(lc);
649 if (call==NULL && ms_list_size(linphone_core_get_calls(lc))==1){
650 call=(LinphoneCall*)linphone_core_get_calls(lc)->data;
654 linphonec_out("No active call, please specify a call id among the ones listed by 'calls' command.\n");
657 linphone_core_transfer_call(lc, call, refer_to);
660 refer_to=args+strlen(arg1)+1;
661 call=linphonec_get_call(id);
662 if (call==NULL) return 0;
663 linphone_core_transfer_call(lc, call, refer_to);
666 call=linphonec_get_call(id);
667 call2=linphonec_get_call(id2);
668 if (call==NULL || call2==NULL) return 0;
669 if (strcmp(arg2,"--to-call")!=0){
672 linphonec_out("Performing attended transfer of call %i to call %i",id,id2);
673 linphone_core_transfer_call_to_another (lc,call,call2);
676 linphonec_out("Transfer command requires at least one argument\n");
683 lpc_cmd_terminate(LinphoneCore *lc, char *args)
685 if (linphone_core_get_calls(lc)==NULL){
686 linphonec_out("No active calls\n");
691 if ( -1 == linphone_core_terminate_call(lc, NULL) ){
692 linphonec_out("Could not stop the active call.\n");
697 if(strcmp(args,"all")==0){
698 linphonec_out("We are going to stop all the calls.\n");
699 linphone_core_terminate_all_calls(lc);
702 /*the argument is a linphonec call id */
704 LinphoneCall *call=linphonec_get_call(id);
706 if (linphone_core_terminate_call(lc,call)==-1){
707 linphonec_out("Could not stop the call with id %li\n",id);
717 lpc_cmd_answer(LinphoneCore *lc, char *args){
720 int nb=ms_list_size(linphone_core_get_calls(lc));
722 //if just one call is present answer the only one in passing NULL to the linphone_core_accept_call ...
723 if ( -1 == linphone_core_accept_call(lc, NULL) )
725 linphonec_out("Fail to accept incoming call\n");
728 linphonec_out("There are no calls to answer.\n");
730 linphonec_out("Multiple calls in progress, please specify call id.\n");
736 if (sscanf(args,"%li",&id)==1){
737 LinphoneCall *call=linphonec_get_call (id);
738 if (linphone_core_accept_call (lc,call)==-1){
739 linphonec_out("Fail to accept call %i\n",id);
748 lpc_cmd_autoanswer(LinphoneCore *lc, char *args)
752 if ( linphonec_get_autoanswer() ) {
753 linphonec_out("Auto answer is enabled. Use 'autoanswer disable' to disable.\n");
755 linphonec_out("Auto answer is disabled. Use 'autoanswer enable' to enable.\n");
760 if (strstr(args,"enable")){
761 linphonec_set_autoanswer(TRUE);
762 linphonec_out("Auto answer enabled.\n");
763 }else if (strstr(args,"disable")){
764 linphonec_set_autoanswer(FALSE);
765 linphonec_out("Auto answer disabled.\n");
771 lpc_cmd_quit(LinphoneCore *lc, char *args)
773 linphonec_main_loop_exit();
778 lpc_cmd_nat(LinphoneCore *lc, char *args)
783 if ( args ) args=lpc_strip_blanks(args);
787 linphone_core_set_nat_address(lc, args);
788 /* linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_NAT_ADDRESS); */
791 nat = linphone_core_get_nat_address(lc);
792 use = linphone_core_get_firewall_policy(lc)==LinphonePolicyUseNatAddress;
793 linphonec_out("Nat address: %s%s\n", nat ? nat : "unspecified" , use ? "" : " (disabled - use 'firewall nat' to enable)");
799 lpc_cmd_stun(LinphoneCore *lc, char *args)
804 if ( args ) args=lpc_strip_blanks(args);
808 linphone_core_set_stun_server(lc, args);
809 /* linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_STUN); */
812 stun = linphone_core_get_stun_server(lc);
813 use = linphone_core_get_firewall_policy(lc)==LinphonePolicyUseStun;
814 linphonec_out("Stun server: %s%s\n", stun ? stun : "unspecified" , use? "" : " (disabled - use 'firewall stun' to enable)");
820 lpc_cmd_firewall(LinphoneCore *lc, char *args)
822 const char* setting=NULL;
824 if ( args ) args=lpc_strip_blanks(args);
828 if (strcmp(args,"none")==0)
830 linphone_core_set_firewall_policy(lc,LinphonePolicyNoFirewall);
832 else if (strcmp(args,"stun")==0)
834 setting = linphone_core_get_stun_server(lc);
837 linphonec_out("No stun server address is defined, use 'stun <address>' first\n");
840 linphone_core_set_firewall_policy(lc,LinphonePolicyUseStun);
842 else if (strcmp(args,"nat")==0)
844 setting = linphone_core_get_nat_address(lc);
847 linphonec_out("No nat address is defined, use 'nat <address>' first");
850 linphone_core_set_firewall_policy(lc,LinphonePolicyUseNatAddress);
854 switch(linphone_core_get_firewall_policy(lc))
856 case LinphonePolicyNoFirewall:
857 linphonec_out("No firewall\n");
859 case LinphonePolicyUseStun:
860 linphonec_out("Using stun server %s to discover firewall address\n", setting ? setting : linphone_core_get_stun_server(lc));
862 case LinphonePolicyUseNatAddress:
863 linphonec_out("Using supplied nat address %s.\n", setting ? setting : linphone_core_get_nat_address(lc));
870 /* Helper function for processing freind names */
872 lpc_friend_name(char **args, char **name)
874 /* Use space as a terminator unless quoted */
875 if (('"' == **args) || ('\'' == **args)){
880 while ((delim != *end) && ('\0' != *end)) end++;
882 fprintf(stderr, "Mismatched quotes\n");
889 *name = strsep(args, " ");
891 if (NULL == *args) { /* Means there was no separator */
892 fprintf(stderr, "Either name or address is missing\n");
895 if (NULL == *name) return 0;
902 lpc_cmd_friend(LinphoneCore *lc, char *args)
906 if ( args ) args=lpc_strip_blanks(args);
908 if ( ! args || ! *args ) return 0;
910 if ( !strncmp(args, "list", 4) )
912 return linphonec_friend_list(lc, args+4);
915 else if ( !strncmp(args, "call", 4) )
918 if ( ! *args ) return 0;
919 friend_num = strtol(args, NULL, 10);
921 if ( errno == ERANGE ) {
922 linphonec_out("Invalid friend number\n");
925 #endif /*_WIN32_WCE*/
926 linphonec_friend_call(lc, friend_num);
929 else if ( !strncmp(args, "delete", 6) )
932 if ( ! *args ) return 0;
933 while (*args == ' ') args++;
934 if ( ! *args ) return 0;
935 if (!strncmp(args, "all", 3))
941 friend_num = strtol(args, NULL, 10);
943 if ( errno == ERANGE ) {
944 linphonec_out("Invalid friend number\n");
947 #endif /*_WIN32_WCE*/
949 linphonec_friend_delete(lc, friend_num);
952 else if ( !strncmp(args, "add", 3) )
961 if ( ! *args ) return 0;
962 while (*args == ' ') args++;
963 if ( ! *args ) return 0;
965 if (!lpc_friend_name(&args, &name)) return 0;
967 while (*args == ' ') args++;
968 if ( ! *args ) return 0;
969 if (isdigit(*args)) {
970 strcpy (addr, "sip:");
971 addr_p = addr + strlen("sip:");
973 addr_orig = strsep(&args, " ");
974 if (1 >= strlen(addr_orig)) {
975 fprintf(stderr, "A single-digit address is not valid\n");
978 strcpy(addr_p, addr_orig);
979 linphonec_friend_add(lc, name, addr);
981 LinphoneFriend *new_friend;
982 new_friend = linphone_friend_new_with_addr(args);
983 linphone_core_add_friend(lc, new_friend);
990 static int lpc_cmd_play(LinphoneCore *lc, char *args){
991 if ( args ) args=lpc_strip_blanks(args);
992 if ( ! args || ! *args ) return 0;
993 linphone_core_set_play_file(lc,args);
997 static int lpc_cmd_record(LinphoneCore *lc, char *args){
998 if ( args ) args=lpc_strip_blanks(args);
999 if ( ! args || ! *args ) return 0;
1000 linphone_core_set_record_file(lc,args);
1008 lpc_cmd_proxy(LinphoneCore *lc, char *args)
1015 if ( ! arg1 ) return 0;
1017 /* Isolate first and second arg */
1018 while(*ptr && !isspace(*ptr)) ++ptr;
1023 while(*arg2 && isspace(*arg2)) ++arg2;
1026 if (strcmp(arg1,"add")==0)
1028 #ifdef HAVE_READLINE
1029 rl_inhibit_completion=1;
1031 linphonec_proxy_add(lc);
1032 #ifdef HAVE_READLINE
1033 rl_inhibit_completion=0;
1036 else if (strcmp(arg1,"list")==0)
1038 linphonec_proxy_list(lc);
1040 else if (strcmp(arg1,"remove")==0)
1042 linphonec_proxy_remove(lc,atoi(arg2));
1044 else if (strcmp(arg1,"use")==0)
1046 if ( arg2 && *arg2 )
1048 proxynum=atoi(arg2);
1049 if ( linphonec_proxy_use(lc, proxynum) )
1050 linphonec_out("Default proxy set to %d.\n", proxynum);
1054 proxynum=linphone_core_get_default_proxy(lc, NULL);
1055 if ( proxynum == -1 ) linphonec_out("No default proxy.\n");
1056 else linphonec_out("Current default proxy is %d.\n", proxynum);
1058 }else if (strcmp(arg1, "unuse")==0){
1059 linphone_core_set_default_proxy(lc, NULL);
1060 linphonec_out("Use no proxy.\n");
1063 else if (strcmp(arg1, "show")==0)
1067 if (strstr(arg2,"default"))
1069 proxynum=linphone_core_get_default_proxy(lc, NULL);
1070 if ( proxynum < 0 ) {
1071 linphonec_out("No default proxy defined\n");
1074 linphonec_proxy_show(lc,proxynum);
1078 linphonec_proxy_show(lc, atoi(arg2));
1081 else return 0; /* syntax error */
1086 return 0; /* syntax error */
1093 lpc_cmd_call_logs(LinphoneCore *lc, char *args)
1095 const MSList *elem=linphone_core_get_call_logs(lc);
1096 for (;elem!=NULL;elem=ms_list_next(elem))
1098 LinphoneCallLog *cl=(LinphoneCallLog*)elem->data;
1099 char *str=linphone_call_log_to_str(cl);
1100 linphonec_out("%s\n",str);
1107 lpc_cmd_ipv6(LinphoneCore *lc, char *arg1)
1111 return 0; /* syntax error */
1114 if (strcmp(arg1,"status")==0)
1116 linphonec_out("ipv6 use enabled: %s\n",linphone_core_ipv6_enabled(lc) ? "true":"false");
1118 else if (strcmp(arg1,"enable")==0)
1120 linphone_core_enable_ipv6(lc,TRUE);
1121 linphonec_out("ipv6 use enabled.\n");
1123 else if (strcmp(arg1,"disable")==0)
1125 linphone_core_enable_ipv6(lc,FALSE);
1126 linphonec_out("ipv6 use disabled.\n");
1130 return 0; /* syntax error */
1135 static int devname_to_index(LinphoneCore *lc, const char *devname){
1138 for(i=0,p=linphone_core_get_sound_devices(lc);*p!=NULL;++p,++i){
1139 if (strcmp(devname,*p)==0) return i;
1144 static const char *index_to_devname(LinphoneCore *lc, int index){
1147 for(i=0,p=linphone_core_get_sound_devices(lc);*p!=NULL;++p,++i){
1148 if (i==index) return *p;
1153 static int lpc_cmd_soundcard(LinphoneCore *lc, char *args)
1161 if (!args) return 0; /* syntax error */
1163 /* Isolate first and second arg */
1164 while(*ptr && !isspace(*ptr)) ++ptr;
1169 while(*arg2 && isspace(*arg2)) ++arg2;
1172 if (strcmp(arg1, "list")==0)
1174 dev=linphone_core_get_sound_devices(lc);
1175 for(i=0; dev[i]!=NULL; ++i){
1176 linphonec_out("%i: %s\n",i,dev[i]);
1181 if (strcmp(arg1, "show")==0)
1183 linphonec_out("Ringer device: %s\n",
1184 linphone_core_get_ringer_device(lc));
1185 linphonec_out("Playback device: %s\n",
1186 linphone_core_get_playback_device(lc));
1187 linphonec_out("Capture device: %s\n",
1188 linphone_core_get_capture_device(lc));
1192 if (strcmp(arg1, "use")==0 && arg2)
1194 if (strcmp(arg2, "files")==0)
1196 linphonec_out("Using wav files instead of soundcard.\n");
1197 linphone_core_use_files(lc,TRUE);
1201 dev=linphone_core_get_sound_devices(lc);
1202 index=atoi(arg2); /* FIXME: handle not-a-number */
1203 for(i=0;dev[i]!=NULL;i++)
1205 if (i!=index) continue;
1207 linphone_core_set_ringer_device(lc,dev[i]);
1208 linphone_core_set_playback_device(lc,dev[i]);
1209 linphone_core_set_capture_device(lc,dev[i]);
1210 linphonec_out("Using sound device %s\n",dev[i]);
1213 linphonec_out("No such sound device\n");
1216 if (strcmp(arg1, "capture")==0)
1218 const char *devname=linphone_core_get_capture_device(lc);
1220 linphonec_out("Using capture device #%i (%s)\n",
1221 devname_to_index(lc,devname),devname);
1223 index=atoi(arg2); /* FIXME: handle not-a-number */
1224 devname=index_to_devname(lc,index);
1226 linphone_core_set_capture_device(lc,devname);
1227 linphonec_out("Using capture sound device %s\n",devname);
1230 linphonec_out("No such sound device\n");
1234 if (strcmp(arg1, "playback")==0)
1236 const char *devname=linphone_core_get_playback_device(lc);
1238 linphonec_out("Using playback device #%i (%s)\n",
1239 devname_to_index(lc,devname),devname);
1241 index=atoi(arg2); /* FIXME: handle not-a-number */
1242 devname=index_to_devname(lc,index);
1244 linphone_core_set_playback_device(lc,devname);
1245 linphonec_out("Using playback sound device %s\n",devname);
1248 linphonec_out("No such sound device\n");
1252 if (strcmp(arg1, "ring")==0)
1254 const char *devname=linphone_core_get_ringer_device(lc);
1256 linphonec_out("Using ring device #%i (%s)\n",
1257 devname_to_index(lc,devname),devname);
1259 index=atoi(arg2); /* FIXME: handle not-a-number */
1260 devname=index_to_devname(lc,index);
1262 linphone_core_set_ringer_device(lc,devname);
1263 linphonec_out("Using ring sound device %s\n",devname);
1266 linphonec_out("No such sound device\n");
1270 return 0; /* syntax error */
1273 static int lpc_cmd_webcam(LinphoneCore *lc, char *args)
1281 if (!args) return 0; /* syntax error */
1283 /* Isolate first and second arg */
1284 while(*ptr && !isspace(*ptr)) ++ptr;
1289 while(*arg2 && isspace(*arg2)) ++arg2;
1292 if (strcmp(arg1, "list")==0)
1294 dev=linphone_core_get_video_devices(lc);
1295 for(i=0; dev[i]!=NULL; ++i){
1296 linphonec_out("%i: %s\n",i,dev[i]);
1301 if (strcmp(arg1, "use")==0 && arg2)
1303 dev=linphone_core_get_video_devices(lc);
1304 index=atoi(arg2); /* FIXME: handle not-a-number */
1305 for(i=0;dev[i]!=NULL;i++)
1307 if (i!=index) continue;
1309 linphone_core_set_video_device(lc, dev[i]);
1310 linphonec_out("Using video device %s\n",dev[i]);
1313 linphonec_out("No such video device\n");
1316 return 0; /* syntax error */
1320 lpc_cmd_staticpic(LinphoneCore *lc, char *args)
1326 if (!args) return 0; /* Syntax error */
1328 /* Isolate first and second arg */
1329 while(*ptr && !isspace(*ptr)) ++ptr;
1334 while(*arg2 && isspace(*arg2)) ++arg2;
1337 if (strcmp(arg1, "set")==0 && arg2) {
1338 linphone_core_set_static_picture(lc, arg2);
1342 if (strcmp(arg1, "fps")==0) {
1344 float fps = atof(arg2); /* FIXME: Handle not-a-float */
1345 linphone_core_set_static_picture_fps(lc, fps);
1349 fps = linphone_core_get_static_picture_fps(lc);
1350 linphonec_out("Current FPS %f\n", fps);
1355 return 0; /* Syntax error */
1358 static int lpc_cmd_pause(LinphoneCore *lc, char *args){
1360 if(linphone_core_in_call(lc))
1362 linphone_core_pause_call(lc,linphone_core_get_current_call(lc));
1365 linphonec_out("you can only pause when a call is in process\n");
1369 static int lpc_cmd_resume(LinphoneCore *lc, char *args){
1371 if(linphone_core_in_call(lc))
1373 linphonec_out("There is already a call in process pause or stop it first");
1379 int n = sscanf(args, "%li", &id);
1381 LinphoneCall *call=linphonec_get_call (id);
1383 if(linphone_core_resume_call(lc,call)==-1){
1384 linphonec_out("There was a problem to resume the call check the remote address you gave %s\n",args);
1392 const MSList *calls = linphone_core_get_calls(lc);
1393 int nbcalls=ms_list_size(calls);
1396 if(linphone_core_resume_call(lc,calls->data) < 0)
1398 linphonec_out("There was a problem to resume the unique call.\n");
1401 }else if (nbcalls==0){
1402 linphonec_out("There is no calls at this time.\n");
1405 linphonec_out("There are %i calls at this time, please specify call id as given with 'calls' command.\n");
1412 /***************************************************************************
1414 * Commands helper functions
1416 ***************************************************************************/
1420 linphonec_proxy_add(LinphoneCore *lc)
1422 bool_t enable_register=FALSE;
1423 LinphoneProxyConfig *cfg;
1425 linphonec_out("Adding new proxy setup. Hit ^D to abort.\n");
1432 char *input=linphonec_readline("Enter proxy sip address: ");
1436 linphonec_out("Aborted.\n");
1441 clean=lpc_strip_blanks(input);
1447 cfg=linphone_proxy_config_new();
1448 if (linphone_proxy_config_set_server_addr(cfg,clean)<0)
1450 linphonec_out("Invalid sip address (sip:sip.domain.tld).\n");
1452 linphone_proxy_config_destroy(cfg);
1460 * SIP Proxy identity
1464 char *input=linphonec_readline("Your identity for this proxy: ");
1468 linphonec_out("Aborted.\n");
1469 linphone_proxy_config_destroy(cfg);
1474 clean=lpc_strip_blanks(input);
1480 linphone_proxy_config_set_identity(cfg, clean);
1481 if ( ! linphone_proxy_config_get_identity (cfg))
1483 linphonec_out("Invalid identity (sip:name@sip.domain.tld).\n");
1492 * SIP Proxy enable register
1496 char *input=linphonec_readline("Do you want to register on this proxy (yes/no): ");
1500 linphonec_out("Aborted.\n");
1501 linphone_proxy_config_destroy(cfg);
1506 clean=lpc_strip_blanks(input);
1512 if ( ! strcmp(clean, "yes") ) enable_register=TRUE;
1513 else if ( ! strcmp(clean, "no") ) enable_register=FALSE;
1515 linphonec_out("Please answer with 'yes' or 'no'\n");
1519 linphone_proxy_config_enableregister(cfg, enable_register);
1525 * SIP Proxy registration expiration
1527 if ( enable_register==TRUE )
1532 char *input=linphonec_readline("Specify register expiration time"
1533 " in seconds (default is 600): ");
1536 linphonec_out("Aborted.\n");
1537 linphone_proxy_config_destroy(cfg);
1541 expires=strtol(input, (char **)NULL, 10);
1542 if ( expires == LONG_MIN || expires == LONG_MAX )
1544 linphonec_out("Invalid value: %s\n", strerror(errno));
1549 linphone_proxy_config_expires(cfg, expires);
1550 linphonec_out("Expiration: %d seconds\n", linphone_proxy_config_get_expires (cfg));
1562 char *input=linphonec_readline("Specify route if needed: ");
1566 linphonec_out("Aborted.\n");
1567 linphone_proxy_config_destroy(cfg);
1572 clean=lpc_strip_blanks(input);
1575 linphonec_out("No route specified.\n");
1579 linphone_proxy_config_set_route(cfg, clean);
1580 if ( ! linphone_proxy_config_get_route(cfg) )
1582 linphonec_out("Invalid route.\n");
1592 * Final confirmation
1599 linphonec_out("--------------------------------------------\n");
1600 linphonec_proxy_display(cfg);
1601 linphonec_out("--------------------------------------------\n");
1602 input=linphonec_readline("Accept the above proxy configuration (yes/no) ?: ");
1606 linphonec_out("Aborted.\n");
1607 linphone_proxy_config_destroy(cfg);
1612 clean=lpc_strip_blanks(input);
1618 if ( ! strcmp(clean, "yes") ) break;
1619 else if ( ! strcmp(clean, "no") )
1621 linphonec_out("Declined.\n");
1622 linphone_proxy_config_destroy(cfg);
1627 linphonec_out("Please answer with 'yes' or 'no'\n");
1633 linphone_core_add_proxy_config(lc,cfg);
1635 /* automatically set the last entered proxy as the default one */
1636 linphone_core_set_default_proxy(lc,cfg);
1638 linphonec_out("Proxy added.\n");
1642 linphonec_proxy_display(LinphoneProxyConfig *cfg)
1644 const char *route=linphone_proxy_config_get_route(cfg);
1645 const char *identity=linphone_proxy_config_get_identity(cfg);
1646 linphonec_out("sip address: %s\nroute: %s\nidentity: %s\nregister: %s\nexpires: %i\nregistered: %s\n",
1647 linphone_proxy_config_get_addr(cfg),
1648 (route!=NULL)? route:"",
1649 (identity!=NULL)?identity:"",
1650 linphone_proxy_config_register_enabled (cfg)?"yes":"no",
1651 linphone_proxy_config_get_expires (cfg),
1652 linphone_proxy_config_is_registered(cfg) ? "yes" : "no");
1655 static void linphonec_proxy_show(LinphoneCore *lc, int index)
1659 for(elem=linphone_core_get_proxy_config_list(lc),i=0;elem!=NULL;elem=elem->next,++i){
1661 LinphoneProxyConfig *cfg=(LinphoneProxyConfig *)elem->data;
1662 linphonec_proxy_display(cfg);
1666 linphonec_out("No proxy with index %i\n", index);
1670 linphonec_proxy_list(LinphoneCore *lc)
1672 const MSList *proxies;
1674 int def=linphone_core_get_default_proxy(lc,NULL);
1676 proxies=linphone_core_get_proxy_config_list(lc);
1677 for(n=0;proxies!=NULL;proxies=ms_list_next(proxies),n++){
1679 linphonec_out("****** Proxy %i - this is the default one - *******\n",n);
1681 linphonec_out("****** Proxy %i *******\n",n);
1682 linphonec_proxy_display((LinphoneProxyConfig*)proxies->data);
1684 if ( ! n ) linphonec_out("No proxies defined\n");
1688 linphonec_proxy_remove(LinphoneCore *lc, int index)
1690 const MSList *proxies;
1691 LinphoneProxyConfig *cfg;
1692 proxies=linphone_core_get_proxy_config_list(lc);
1693 cfg=(LinphoneProxyConfig*)ms_list_nth_data(proxies,index);
1695 linphonec_out("No such proxy.\n");
1698 linphone_core_remove_proxy_config(lc,cfg);
1699 linphonec_out("Proxy %s removed.\n", linphone_proxy_config_get_addr(cfg));
1703 linphonec_proxy_use(LinphoneCore *lc, int index)
1705 const MSList *proxies;
1706 LinphoneProxyConfig *cfg;
1707 proxies=linphone_core_get_proxy_config_list(lc);
1708 cfg=(LinphoneProxyConfig*)ms_list_nth_data(proxies,index);
1710 linphonec_out("No such proxy (try 'proxy list').");
1713 linphone_core_set_default_proxy(lc,cfg);
1718 linphonec_friend_display(LinphoneFriend *fr)
1720 LinphoneAddress *uri=linphone_address_clone(linphone_friend_get_address(fr));
1723 linphonec_out("name: %s\n", linphone_address_get_display_name(uri));
1724 linphone_address_set_display_name(uri,NULL);
1725 str=linphone_address_as_string(uri);
1726 linphonec_out("address: %s\n", str);
1730 linphonec_friend_list(LinphoneCore *lc, char *pat)
1732 const MSList *friend;
1736 pat=lpc_strip_blanks(pat);
1737 if (!*pat) pat = NULL;
1740 friend = linphone_core_get_friend_list(lc);
1741 for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
1744 const char *name = linphone_address_get_display_name(
1745 linphone_friend_get_address((LinphoneFriend*)friend->data));
1746 if (name && ! strstr(name, pat) ) continue;
1748 linphonec_out("****** Friend %i *******\n",n);
1749 linphonec_friend_display((LinphoneFriend*)friend->data);
1756 linphonec_friend_call(LinphoneCore *lc, unsigned int num)
1758 const MSList *friend = linphone_core_get_friend_list(lc);
1762 for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
1767 addr = linphone_address_as_string(linphone_friend_get_address((LinphoneFriend*)friend->data));
1768 ret=lpc_cmd_call(lc, addr);
1773 linphonec_out("No such friend %u\n", num);
1779 linphonec_friend_add(LinphoneCore *lc, const char *name, const char *addr)
1781 LinphoneFriend *newFriend;
1785 snprintf(url, PATH_MAX, "%s <%s>", name, addr);
1786 newFriend = linphone_friend_new_with_addr(url);
1787 linphone_core_add_friend(lc, newFriend);
1793 linphonec_friend_delete(LinphoneCore *lc, int num)
1795 const MSList *friend = linphone_core_get_friend_list(lc);
1798 for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
1802 linphone_core_remove_friend(lc, friend->data);
1810 for (i = 0 ; i < n ; i++)
1811 linphonec_friend_delete(lc, 0);
1815 linphonec_out("No such friend %u\n", num);
1820 linphonec_display_command_help(LPC_COMMAND *cmd)
1822 if ( cmd->doc ) linphonec_out ("%s\n", cmd->doc);
1823 else linphonec_out("%s\n", cmd->help);
1827 static int lpc_cmd_register(LinphoneCore *lc, char *args){
1831 LinphoneProxyConfig *cfg;
1836 /* it means that you want to register the default proxy */
1837 LinphoneProxyConfig *cfg=NULL;
1838 linphone_core_get_default_proxy(lc,&cfg);
1841 if(!linphone_proxy_config_is_registered(cfg)) {
1842 linphone_proxy_config_enable_register(cfg,TRUE);
1843 linphone_proxy_config_done(cfg);
1845 linphonec_out("default proxy already registered\n");
1848 linphonec_out("we do not have a default proxy\n");
1853 passwd[0]=proxy[0]=identity[0]='\0';
1854 sscanf(args,"%s %s %s",identity,proxy,passwd);
1855 if (proxy[0]=='\0' || identity[0]=='\0'){
1856 linphonec_out("Missing parameters, see help register\n");
1859 if (passwd[0]!='\0'){
1860 LinphoneAddress *from;
1861 LinphoneAuthInfo *info;
1862 if ((from=linphone_address_new(identity))!=NULL){
1864 snprintf(realm,sizeof(realm)-1,"\"%s\"",linphone_address_get_domain(from));
1865 info=linphone_auth_info_new(linphone_address_get_username(from),NULL,passwd,NULL,NULL);
1866 linphone_core_add_auth_info(lc,info);
1867 linphone_address_destroy(from);
1868 linphone_auth_info_destroy(info);
1871 elem=linphone_core_get_proxy_config_list(lc);
1873 cfg=(LinphoneProxyConfig*)elem->data;
1874 linphone_proxy_config_edit(cfg);
1876 else cfg=linphone_proxy_config_new();
1877 linphone_proxy_config_set_identity(cfg,identity);
1878 linphone_proxy_config_set_server_addr(cfg,proxy);
1879 linphone_proxy_config_enable_register(cfg,TRUE);
1880 if (elem) linphone_proxy_config_done(cfg);
1881 else linphone_core_add_proxy_config(lc,cfg);
1882 linphone_core_set_default_proxy(lc,cfg);
1886 static int lpc_cmd_unregister(LinphoneCore *lc, char *args){
1887 LinphoneProxyConfig *cfg=NULL;
1888 linphone_core_get_default_proxy(lc,&cfg);
1889 if (cfg && linphone_proxy_config_is_registered(cfg)) {
1890 linphone_proxy_config_edit(cfg);
1891 linphone_proxy_config_enable_register(cfg,FALSE);
1892 linphone_proxy_config_done(cfg);
1894 linphonec_out("unregistered\n");
1899 static int lpc_cmd_duration(LinphoneCore *lc, char *args){
1900 LinphoneCallLog *cl;
1901 const MSList *elem=linphone_core_get_call_logs(lc);
1902 for(;elem!=NULL;elem=elem->next){
1903 if (elem->next==NULL){
1904 cl=(LinphoneCallLog*)elem->data;
1905 linphonec_out("%i seconds\n",cl->duration);
1911 static int lpc_cmd_status(LinphoneCore *lc, char *args)
1913 LinphoneProxyConfig *cfg;
1915 if ( ! args ) return 0;
1916 linphone_core_get_default_proxy(lc,&cfg);
1917 if (strstr(args,"register"))
1921 if (linphone_proxy_config_is_registered(cfg)){
1922 linphonec_out("registered, identity=%s duration=%i\n",
1923 linphone_proxy_config_get_identity(cfg),
1924 linphone_proxy_config_get_expires(cfg));
1925 }else if (linphone_proxy_config_register_enabled(cfg)){
1926 linphonec_out("registered=-1\n");
1927 }else linphonec_out("registered=0\n");
1929 else linphonec_out("registered=0\n");
1931 else if (strstr(args,"autoanswer"))
1933 if (cfg && linphone_proxy_config_is_registered(cfg))
1934 linphonec_out("autoanswer=%i\n",linphonec_get_autoanswer());
1935 else linphonec_out("unregistered\n");
1937 else if (strstr(args,"hook"))
1939 LinphoneCall *call=linphone_core_get_current_call (lc);
1940 LinphoneCallState call_state=LinphoneCallIdle;
1941 if (call) call_state=linphone_call_get_state(call);
1944 case LinphoneCallOutgoingInit:
1945 case LinphoneCallOutgoingProgress:
1946 linphonec_out("hook=dialing\n");
1948 case LinphoneCallIdle:
1949 linphonec_out("hook=offhook\n");
1951 case LinphoneCallStreamsRunning:
1952 case LinphoneCallConnected:
1953 if (linphone_call_get_dir(call)==LinphoneCallOutgoing){
1954 linphonec_out("Call out, hook=%s duration=%i, muted=%s rtp-xmit-muted=%s\n", linphonec_get_callee(),
1955 linphone_core_get_current_call_duration(lc),
1956 linphone_core_is_mic_muted (lc) ? "yes" : "no",
1957 linphone_core_is_rtp_muted(lc) ? "yes" : "no");
1959 linphonec_out("hook=answered duration=%i\n" ,
1960 linphone_core_get_current_call_duration(lc));
1963 case LinphoneCallIncomingReceived:
1964 linphonec_out("Incoming call from %s\n",linphonec_get_caller());
1976 static int lpc_cmd_ports(LinphoneCore *lc, char *args)
1980 linphonec_out("sip port = %i\naudio rtp port = %i\nvideo rtp port = %i\n",
1981 linphone_core_get_sip_port(lc),
1982 linphone_core_get_audio_port(lc),
1983 linphone_core_get_video_port(lc));
1986 if (sscanf(args,"sip %i",&port)==1){
1987 linphonec_out("Setting sip port to %i\n",port);
1988 linphone_core_set_sip_port(lc,port);
1994 static int lpc_cmd_speak(LinphoneCore *lc, char *args){
2003 if (!args) return 0;
2004 memset(voice,0,sizeof(voice));
2005 sscanf(args,"%s63",voice);
2006 sentence=args+strlen(voice);
2007 wavfile=tempnam("/tmp/","linphonec-espeak-");
2008 snprintf(cl,sizeof(cl),"espeak -v %s -s 100 -w %s --stdin",voice,wavfile);
2011 ms_error("Could not open pipe to espeak !");
2014 fprintf(file,"%s",sentence);
2015 status=pclose(file);
2016 if (WEXITSTATUS(status)==0){
2017 linphone_core_set_play_file(lc,wavfile);
2019 linphonec_out("espeak command failed.");
2022 linphonec_out("Sorry, this command is not implemented in windows version.");
2027 static int lpc_cmd_acodec(LinphoneCore *lc, char *args){
2028 return lpc_cmd_codec(AUDIO, lc, args);
2031 static int lpc_cmd_vcodec(LinphoneCore *lc, char *args){
2032 return lpc_cmd_codec(VIDEO, lc, args);
2035 static int lpc_cmd_codec(int type, LinphoneCore *lc, char *args){
2040 if (!args) return 0;
2042 /* Isolate first and second arg */
2043 while(*ptr && !isspace(*ptr)) ++ptr;
2048 while(*arg2 && isspace(*arg2)) ++arg2;
2051 if (strcmp(arg1,"enable")==0)
2053 #ifdef HAVE_READLINE
2054 rl_inhibit_completion=1;
2056 if (!strcmp(arg2,"all")) linphonec_codec_enable(type,lc,-1);
2057 else linphonec_codec_enable(type,lc,atoi(arg2));
2058 #ifdef HAVE_READLINE
2059 rl_inhibit_completion=0;
2062 else if (strcmp(arg1,"list")==0)
2064 linphonec_codec_list(type,lc);
2066 else if (strcmp(arg1,"disable")==0)
2068 if (!strcmp(arg2,"all")) linphonec_codec_disable(type,lc,-1);
2069 else linphonec_codec_disable(type,lc,atoi(arg2));
2073 return 0; /* syntax error */
2079 static void linphonec_codec_list(int type, LinphoneCore *lc){
2082 const MSList *node=NULL;
2084 if (type == AUDIO) {
2085 node=linphone_core_get_audio_codecs(lc);
2086 } else if(type==VIDEO) {
2087 node=linphone_core_get_video_codecs(lc);
2090 for(;node!=NULL;node=ms_list_next(node)){
2091 pt=(PayloadType*)(node->data);
2092 linphonec_out("%2d: %s (%d) %s\n", index, pt->mime_type, pt->clock_rate,
2093 linphone_core_payload_type_enabled(lc,pt) ? "enabled" : "disabled");
2098 static void linphonec_codec_enable(int type, LinphoneCore *lc, int sel_index){
2101 const MSList *node=NULL;
2103 if (type == AUDIO) {
2104 node=linphone_core_get_audio_codecs(lc);
2105 } else if(type==VIDEO) {
2106 node=linphone_core_get_video_codecs(lc);
2109 for(;node!=NULL;node=ms_list_next(node)){
2110 if (index == sel_index || sel_index == -1) {
2111 pt=(PayloadType*)(node->data);
2112 linphone_core_enable_payload_type (lc,pt,TRUE);
2113 linphonec_out("%2d: %s (%d) %s\n", index, pt->mime_type, pt->clock_rate, "enabled");
2119 static void linphonec_codec_disable(int type, LinphoneCore *lc, int sel_index){
2122 const MSList *node=NULL;
2124 if (type == AUDIO) {
2125 node=linphone_core_get_audio_codecs(lc);
2126 } else if(type==VIDEO) {
2127 node=linphone_core_get_video_codecs(lc);
2130 for(;node!=NULL;node=ms_list_next(node)){
2131 if (index == sel_index || sel_index == -1) {
2132 pt=(PayloadType*)(node->data);
2133 linphone_core_enable_payload_type (lc,pt,FALSE);
2134 linphonec_out("%2d: %s (%d) %s\n", index, pt->mime_type, pt->clock_rate, "disabled");
2140 static int lpc_cmd_echocancellation(LinphoneCore *lc, char *args){
2144 LpConfig *config=linphone_core_get_config(lc);
2146 if (!args) return 0;
2148 /* Isolate first and second arg */
2149 while(*ptr && !isspace(*ptr)) ++ptr;
2154 while(*arg2 && isspace(*arg2)) ++arg2;
2157 if (strcmp(arg1,"on")==0){
2158 int delay, tail_len, frame_size;
2161 linphone_core_enable_echo_cancellation(lc,1);
2164 n = sscanf(arg2, "%d %d %d", &delay, &tail_len, &frame_size);
2167 lp_config_set_int(config,"sound","ec_delay",delay);
2170 lp_config_set_int(config,"sound","ec_delay",delay);
2171 lp_config_set_int(config,"sound","ec_tail_len",tail_len);
2174 lp_config_set_int(config,"sound","ec_delay",delay);
2175 lp_config_set_int(config,"sound","ec_tail_len",tail_len);
2176 lp_config_set_int(config,"sound","ec_framesize",frame_size);
2180 else if (strcmp(arg1,"off")==0){
2181 linphone_core_enable_echo_cancellation(lc,0);
2183 else if (strcmp(arg1,"show")==0){
2184 linphonec_out("echo cancellation is %s; delay %d, tail length %d, frame size %d\n",
2185 linphone_core_echo_cancellation_enabled(lc) ? "on" : "off",
2186 lp_config_get_int(config,"sound","ec_delay",0),
2187 lp_config_get_int(config,"sound","ec_tail_len",0),
2188 lp_config_get_int(config,"sound","ec_framesize",0));
2197 static int lpc_cmd_mute_mic(LinphoneCore *lc, char *args)
2199 linphone_core_mute_mic(lc, 1);
2203 static int lpc_cmd_unmute_mic(LinphoneCore *lc, char *args){
2204 linphone_core_mute_mic(lc, 0);
2208 static int lpc_cmd_rtp_no_xmit_on_audio_mute(LinphoneCore *lc, char *args)
2210 bool_t rtp_xmit_off=FALSE;
2214 if(strstr(args,"1"))rtp_xmit_off=TRUE;
2215 if(linphone_core_get_current_call (lc)==NULL)
2216 linphone_core_set_rtp_no_xmit_on_audio_mute(lc,rtp_xmit_off);
2218 linphonec_out("nortp-on-audio-mute: call in progress - cannot change state\n");
2220 rtp_xmit_off=linphone_core_get_rtp_no_xmit_on_audio_mute(lc);
2221 if (rtp_xmit_off) status="off";
2223 linphonec_out("rtp transmit %s when audio muted\n",status);
2227 #ifdef VIDEO_ENABLED
2228 static int _lpc_cmd_video_window(LinphoneCore *lc, char *args, bool_t is_preview){
2229 char subcommand[64];
2232 VideoParams *params=is_preview ? &lpc_preview_params : &lpc_video_params;
2234 if (!args) return 0;
2235 err=sscanf(args,"%s %i %i",subcommand,&a,&b);
2237 if (strcmp(subcommand,"pos")==0){
2238 if (err<3) return 0;
2241 params->refresh=TRUE;
2242 }else if (strcmp(subcommand,"size")==0){
2243 if (err<3) return 0;
2246 params->refresh=TRUE;
2247 }else if (strcmp(subcommand,"show")==0){
2249 params->refresh=TRUE;
2250 if (is_preview) linphone_core_enable_video_preview (lc,TRUE);
2251 }else if (strcmp(subcommand,"hide")==0){
2253 params->refresh=TRUE;
2254 if (is_preview) linphone_core_enable_video_preview (lc,FALSE);
2255 }else if (strcmp(subcommand,"id")==0){
2257 linphonec_out("vwindow id: 0x%x\n",is_preview ? linphone_core_get_native_preview_window_id (lc) :
2258 linphone_core_get_native_video_window_id (lc));
2260 } else if (err != 2) return 0;
2263 linphone_core_set_native_preview_window_id (lc,a);
2265 linphone_core_set_native_video_window_id(lc,a);
2266 }else if (is_preview==TRUE){
2267 if (strcmp(subcommand,"integrated")==0){
2268 linphone_core_use_preview_window (lc,FALSE);
2269 }else if (strcmp(subcommand,"standalone")==0){
2270 linphone_core_use_preview_window(lc,TRUE);
2277 static int lpc_cmd_video_window(LinphoneCore *lc, char *args){
2278 return _lpc_cmd_video_window(lc, args, FALSE);
2281 static int lpc_cmd_preview_window(LinphoneCore *lc, char *args){
2282 return _lpc_cmd_video_window(lc, args, TRUE);
2286 static void lpc_display_global_state(LinphoneCore *lc){
2287 linphonec_out("Global liblinphone state\n%s\n",
2288 linphone_global_state_to_string(linphone_core_get_global_state(lc)));
2291 static void lpc_display_call_states(LinphoneCore *lc){
2295 linphonec_out("Call states\n"
2296 "Id | Destination | State\n"
2297 "---------------------------------------------------------------\n");
2298 elem=linphone_core_get_calls(lc);
2300 linphonec_out("(empty)\n");
2302 for(;elem!=NULL;elem=elem->next){
2303 call=(LinphoneCall*)elem->data;
2304 tmp=linphone_call_get_remote_address_as_string (call);
2305 linphonec_out("%-2i | %-35s | %s\n",(int)(long)linphone_call_get_user_pointer(call),
2306 tmp,linphone_call_state_to_string(linphone_call_get_state(call)));
2312 static void lpc_display_proxy_states(LinphoneCore *lc){
2314 linphonec_out("Proxy registration states\n"
2315 " Identity | State\n"
2316 "------------------------------------------------------------\n");
2317 elem=linphone_core_get_proxy_config_list (lc);
2318 if (elem==NULL) linphonec_out("(empty)\n");
2320 for(;elem!=NULL;elem=elem->next){
2321 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
2322 linphonec_out("%-40s | %s\n",linphone_proxy_config_get_identity (cfg),
2323 linphone_registration_state_to_string(linphone_proxy_config_get_state(cfg)));
2328 static int lpc_cmd_states(LinphoneCore *lc, char *args){
2330 lpc_display_global_state(lc);
2331 lpc_display_call_states(lc);
2332 lpc_display_proxy_states(lc);
2335 if (strcmp(args,"global")==0){
2336 lpc_display_global_state(lc);
2339 if (strcmp(args,"proxies")==0){
2340 lpc_display_proxy_states(lc);
2343 if (strcmp(args,"calls")==0){
2344 lpc_display_call_states(lc);
2350 #ifdef VIDEO_ENABLED
2351 static int lpc_cmd_camera(LinphoneCore *lc, char *args){
2352 LinphoneCall *call=linphone_core_get_current_call(lc);
2353 bool_t activated=FALSE;
2355 if (linphone_core_video_enabled (lc)==FALSE){
2356 linphonec_out("Video is disabled, re-run linphonec with -V option.");
2361 if (strcmp(args,"on")==0)
2363 else if (strcmp(args,"off")==0)
2371 linphonec_camera_enabled=activated;
2373 if (linphonec_camera_enabled){
2374 linphonec_out("Camera is enabled. Video stream will be setup immediately for outgoing and incoming calls.\n");
2376 linphonec_out("Camera is disabled. Calls will be established with audio-only, with the possibility to later add video using 'camera on'.\n");
2379 const LinphoneCallParams *cp=linphone_call_get_current_params (call);
2381 linphone_call_enable_camera(call,activated);
2382 if ((activated && !linphone_call_params_video_enabled (cp))){
2383 /*update the call to add the video stream*/
2384 LinphoneCallParams *ncp=linphone_call_params_copy(cp);
2385 linphone_call_params_enable_video(ncp,TRUE);
2386 linphone_core_update_call(lc,call,ncp);
2387 linphone_call_params_destroy (ncp);
2388 linphonec_out("Trying to bring up video stream...\n");
2391 if (linphone_call_camera_enabled (call))
2392 linphonec_out("Camera is allowed for current call.\n");
2393 else linphonec_out("Camera is dis-allowed for current call.\n");
2398 static int lpc_cmd_snapshot(LinphoneCore *lc, char *args){
2400 if (!args) return 0;
2401 call=linphone_core_get_current_call(lc);
2403 linphone_call_take_video_snapshot (call,args);
2404 linphonec_out("Taking video snaphot in file %s\n", args);
2405 }else linphonec_out("There is no active call.\n");
2411 static int lpc_cmd_identify(LinphoneCore *lc, char *args){
2413 const char *remote_ua;
2415 call=linphone_core_get_current_call(lc);
2417 linphonec_out("There is currently running call. Specify call id.\n");
2421 call=linphonec_get_call(atoi(args));
2426 remote_ua=linphone_call_get_remote_user_agent(call);
2428 linphonec_out("Remote user agent string is: %s\n",remote_ua);
2433 static int lpc_cmd_ringback(LinphoneCore *lc, char *args){
2434 if (!args) return 0;
2435 if (strcmp(args,"disable")==0){
2436 linphone_core_set_remote_ringback_tone(lc,NULL);
2437 linphonec_out("Disabling ringback tone.\n");
2440 linphone_core_set_remote_ringback_tone (lc,args);
2441 linphonec_out("Using %s as ringback tone to be played to callers.",args);
2445 /***************************************************************************
2447 * Command table management funx
2449 ***************************************************************************/
2452 * Find a command given its name
2454 static LPC_COMMAND *
2455 lpc_find_command(const char *name)
2459 for (i=0; commands[i].name; ++i)
2461 if (strcmp(name, commands[i].name) == 0)
2462 return &commands[i];
2465 for (i=0; advanced_commands[i].name; ++i)
2467 if (strcmp(name, advanced_commands[i].name) == 0)
2468 return &advanced_commands[i];
2471 return (LPC_COMMAND *)NULL;
2475 /****************************************************************************
2477 * $Log: commands.c,v $
2478 * Revision 1.39 2008/07/03 15:08:34 smorlat
2479 * api cleanups, interface in progress.
2481 * Revision 1.38 2008/06/17 20:38:59 smorlat
2482 * added missing file.
2484 * Revision 1.37 2008/04/09 09:26:00 smorlat
2485 * merge various patches
2488 * Revision 1.36 2007/08/01 14:47:53 strk
2489 * * console/commands.c: Clean up commands 'nat', 'stun'
2490 * and 'firewall' to be more intuitive.
2492 * Revision 1.35 2007/06/27 09:01:25 smorlat
2493 * logging improvements.
2495 * Revision 1.34 2007/02/20 10:17:13 smorlat
2496 * linphonec friends patch2
2498 * Revision 1.31 2006/09/22 07:22:47 smorlat
2499 * linphonecore api changes.
2501 * Revision 1.30 2006/09/08 15:32:57 smorlat
2502 * support for using files instead of soundcard (used by linphonec only)
2504 * Revision 1.29 2006/08/28 14:29:07 smorlat
2507 * Revision 1.28 2006/08/21 12:49:59 smorlat
2508 * merged several little patches.
2510 * Revision 1.27 2006/07/17 18:45:00 smorlat
2511 * support for several event queues in ortp.
2512 * glib dependency removed from coreapi/ and console/
2514 * Revision 1.26 2006/04/14 15:16:36 smorlat
2515 * soundcard use did nothing !
2517 * Revision 1.25 2006/04/06 20:09:33 smorlat
2518 * add linphonec command to see and select sound devices.
2520 * Revision 1.24 2006/03/04 11:17:10 smorlat
2521 * mediastreamer2 in progress.
2523 * Revision 1.23 2006/02/20 21:14:01 strk
2524 * Handled syntax errors with 'friend' command
2526 * Revision 1.22 2006/02/20 10:20:29 strk
2527 * Added substring-based filter support for command 'friend list'
2529 * Revision 1.21 2006/02/02 15:39:18 strk
2530 * - Added 'friend list' and 'friend call' commands
2531 * - Allowed for multiple DTFM send in a single line
2532 * - Added status-specific callback (bare version)
2534 * Revision 1.20 2006/01/26 11:54:34 strk
2535 * More robust 'nat' command handler (strip blanks in args)
2537 * Revision 1.19 2006/01/26 09:48:05 strk
2538 * Added limits.h include
2540 * Revision 1.18 2006/01/26 02:18:05 strk
2541 * Added new commands 'nat use' and 'nat unuse'.
2542 * These will required a pending patch to linphonecore.c
2545 * Revision 1.17 2006/01/20 14:12:33 strk
2546 * Added linphonec_init() and linphonec_finish() functions.
2547 * Handled SIGINT and SIGTERM to invoke linphonec_finish().
2548 * Handling of auto-termination (-t) moved to linphonec_finish().
2549 * Reworked main (input read) loop to not rely on 'terminate'
2550 * and 'run' variable (dropped). configfile_name allocated on stack
2551 * using PATH_MAX limit. Changed print_usage signature to allow
2552 * for an exit_status specification.
2554 * Revision 1.16 2006/01/18 09:25:32 strk
2555 * Command completion inhibited in proxy addition and auth request prompts.
2556 * Avoided use of linphonec_readline's internal filename completion.
2558 * Revision 1.15 2006/01/14 13:29:32 strk
2559 * Reworked commands interface to use a table structure,
2560 * used by command line parser and help function.
2561 * Implemented first level of completion (commands).
2562 * Added notification of invalid "answer" and "terminate"
2563 * commands (no incoming call, no active call).
2564 * Forbidden "call" intialization when a call is already active.
2565 * Cleaned up all commands, adding more feedback and error checks.
2567 * Revision 1.14 2006/01/13 13:00:29 strk
2568 * Added linphonec.h. Code layout change (added comments, forward decl,
2569 * globals on top, copyright notices and Logs). Handled out-of-memory
2570 * condition on history management. Removed assumption on sizeof(char).
2571 * Fixed bug in authentication prompt (introduced by linphonec_readline).
2572 * Added support for multiple authentication requests (up to MAX_PENDING_AUTH).
2575 ****************************************************************************/