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