commands.c 76.4 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4
/****************************************************************************
 *
 *  $Id: commands.c,v 1.39 2008/07/03 15:08:34 smorlat Exp $
 *
5
 *  Copyright (C) 2006-2009  Sandro Santilli <strk@keybit.net>
aymeric's avatar
aymeric committed
6 7
 *  Copyright (C) 2004  Simon MORLAT <simon.morlat@linphone.org>
 *
8
****************************************************************************
aymeric's avatar
aymeric committed
9
 *
10 11 12 13
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
aymeric's avatar
aymeric committed
14
 *
15 16 17 18
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
aymeric's avatar
aymeric committed
19
 *
20 21
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
aymeric's avatar
aymeric committed
23 24 25 26 27 28
 *
 ****************************************************************************/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
Jehan Monnier's avatar
Jehan Monnier committed
29
#ifndef _WIN32_WCE
aymeric's avatar
aymeric committed
30
#include <errno.h>
Jehan Monnier's avatar
Jehan Monnier committed
31
#endif /*_WIN32_WCE*/
aymeric's avatar
aymeric committed
32 33
#include <limits.h>
#include <ctype.h>
34
#include <linphone/core.h>
aymeric's avatar
aymeric committed
35
#include "linphonec.h"
36
#include <linphone/lpconfig.h>
aymeric's avatar
aymeric committed
37

38
#ifndef _WIN32
39
#include <sys/wait.h>
40
#include <unistd.h>
41 42
#endif

43 44 45
#define AUDIO 0
#define VIDEO 1

aymeric's avatar
aymeric committed
46 47
/***************************************************************************
 *
48
 *  Forward declarations
aymeric's avatar
aymeric committed
49 50 51 52 53 54 55 56 57
 *
 ***************************************************************************/

extern char *lpc_strip_blanks(char *input);

/* Command handlers */
static int lpc_cmd_help(LinphoneCore *, char *);
static int lpc_cmd_proxy(LinphoneCore *, char *);
static int lpc_cmd_call(LinphoneCore *, char *);
58
static int lpc_cmd_calls(LinphoneCore *, char *);
59
static int lpc_cmd_chat(LinphoneCore *, char *);
aymeric's avatar
aymeric committed
60
static int lpc_cmd_answer(LinphoneCore *, char *);
smorlat's avatar
smorlat committed
61
static int lpc_cmd_autoanswer(LinphoneCore *, char *);
aymeric's avatar
aymeric committed
62
static int lpc_cmd_terminate(LinphoneCore *, char *);
63
static int lpc_cmd_redirect(LinphoneCore *, char *);
aymeric's avatar
aymeric committed
64 65
static int lpc_cmd_call_logs(LinphoneCore *, char *);
static int lpc_cmd_ipv6(LinphoneCore *, char *);
66
static int lpc_cmd_transfer(LinphoneCore *, char *);
aymeric's avatar
aymeric committed
67 68 69 70 71 72
static int lpc_cmd_quit(LinphoneCore *, char *);
static int lpc_cmd_nat(LinphoneCore *, char *);
static int lpc_cmd_stun(LinphoneCore *, char *);
static int lpc_cmd_firewall(LinphoneCore *, char *);
static int lpc_cmd_friend(LinphoneCore *, char*);
static int lpc_cmd_soundcard(LinphoneCore *, char *);
73 74
static int lpc_cmd_webcam(LinphoneCore *, char *);
static int lpc_cmd_staticpic(LinphoneCore *, char *);
aymeric's avatar
aymeric committed
75 76
static int lpc_cmd_play(LinphoneCore *, char *);
static int lpc_cmd_record(LinphoneCore *, char *);
smorlat's avatar
smorlat committed
77
static int lpc_cmd_register(LinphoneCore *, char *);
smorlat's avatar
smorlat committed
78
static int lpc_cmd_unregister(LinphoneCore *, char *);
smorlat's avatar
smorlat committed
79
static int lpc_cmd_duration(LinphoneCore *lc, char *args);
smorlat's avatar
smorlat committed
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 85 86
static int lpc_cmd_acodec(LinphoneCore *lc, char *args);
static int lpc_cmd_vcodec(LinphoneCore *lc, char *args);
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 90
static int lpc_cmd_pause(LinphoneCore *lc, char *args);
static int lpc_cmd_resume(LinphoneCore *lc, char *args);
Simon Morlat's avatar
Simon Morlat committed
91 92
static int lpc_cmd_mute_mic(LinphoneCore *lc, char *args);
static int lpc_cmd_unmute_mic(LinphoneCore *lc, char *args);
93
static int lpc_cmd_playback_gain(LinphoneCore *lc, char *args);
Simon Morlat's avatar
Simon Morlat committed
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);
Simon Morlat's avatar
Simon Morlat committed
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);
smorlat's avatar
smorlat committed
109

aymeric's avatar
aymeric committed
110 111 112 113 114 115
/* Command handler helpers */
static void linphonec_proxy_add(LinphoneCore *lc);
static void linphonec_proxy_display(LinphoneProxyConfig *lc);
static void linphonec_proxy_list(LinphoneCore *lc);
static void linphonec_proxy_remove(LinphoneCore *lc, int index);
static  int linphonec_proxy_use(LinphoneCore *lc, int index);
smorlat's avatar
smorlat committed
116
static void linphonec_proxy_show(LinphoneCore *lc,int index);
aymeric's avatar
aymeric committed
117 118 119 120
static void linphonec_friend_display(LinphoneFriend *fr);
static int linphonec_friend_list(LinphoneCore *lc, char *arg);
static void linphonec_display_command_help(LPC_COMMAND *cmd);
static int linphonec_friend_call(LinphoneCore *lc, unsigned int num);
121
#ifndef _WIN32
aymeric's avatar
aymeric committed
122
static int linphonec_friend_add(LinphoneCore *lc, const char *name, const char *addr);
123
#endif
aymeric's avatar
aymeric committed
124
static int linphonec_friend_delete(LinphoneCore *lc, int num);
125
static int linphonec_friend_delete(LinphoneCore *lc, int num);
126 127 128
static void linphonec_codec_list(int type, LinphoneCore *lc);
static void linphonec_codec_enable(int type, LinphoneCore *lc, int index);
static void linphonec_codec_disable(int type, LinphoneCore *lc, int index);
129
static void lpc_display_call_states(LinphoneCore *lc);
smorlat's avatar
smorlat committed
130

aymeric's avatar
aymeric committed
131 132 133
/* Command table management */
static LPC_COMMAND *lpc_find_command(const char *name);

134 135
void linphonec_out(const char *fmt,...);

136 137
VideoParams lpc_video_params={-1,-1,-1,-1,NULL,TRUE,FALSE};
VideoParams lpc_preview_params={-1,-1,-1,-1,NULL,TRUE,FALSE};
138

aymeric's avatar
aymeric committed
139 140 141 142 143 144 145 146 147
/***************************************************************************
 *
 *  Global variables
 *
 ***************************************************************************/

/*
 * Commands table.
 */
148
static LPC_COMMAND commands[] = {
149
	{ "help", lpc_cmd_help, "Print commands help.",
150 151 152
		"'help <command>'\t: displays specific help for command.\n"
		"'help advanced'\t: shows advanced commands.\n"
	},
153 154 155 156 157 158 159 160 161
	{ "answer", lpc_cmd_answer, "Answer a call",
		"'answer' : Answer the current incoming call\n"
		"'answer <call id>' : Answer the call with given id\n"
	},
	{ "autoanswer", lpc_cmd_autoanswer, "Show/set auto-answer mode",
		"'autoanswer'       \t: show current autoanswer mode\n"
		"'autoanswer enable'\t: enable autoanswer mode\n"
		"'autoanswer disable'\t: disable autoanswer mode\n"
	},
162 163
	{ "call", lpc_cmd_call, "Call a SIP uri or number",
#ifdef VIDEO_ENABLED
164 165 166 167
		"'call <sip-url or number>  [options]' \t: initiate a call to the specified destination.\n"
		"Options can be:\n"
		"--audio-only : initiate the call without video.\n"
		"--early-media : sends audio and video stream immediately when remote proposes early media.\n"
168 169 170
#else
		"'call <sip-url or number>' \t: initiate a call to the specified destination.\n"
#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
Simon Morlat's avatar
Simon Morlat committed
176
	},
177 178 179
#ifdef VIDEO_ENABLED
	{ "camera", lpc_cmd_camera, "Send camera output for current call.",
		"'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"
aymeric's avatar
aymeric committed
181
	},
182 183 184 185
#endif
	{ "chat", lpc_cmd_chat, "Chat with a SIP uri",
		"'chat <sip-url> \"message\"' "
		": send a chat message \"message\" to the specified destination."
aymeric's avatar
aymeric committed
186
	},
187 188 189
	{ "conference", lpc_cmd_conference, "Create and manage an audio conference.",
		"'conference add <call id> : join the call with id 'call id' into the audio conference."
		"'conference rm <call id> : remove the call with id 'call id' from the audio conference."
aymeric's avatar
aymeric committed
190
	},
191
	{ "duration", lpc_cmd_duration, "Print duration in seconds of the last call.", NULL
aymeric's avatar
aymeric committed
192
	},
strk's avatar
strk committed
193
	{ "firewall", lpc_cmd_firewall, "Set firewall policy",
aymeric's avatar
aymeric committed
194 195 196
		"'firewall'        : show current firewall policy.\n"
		"'firewall none'   : use direct connection.\n"
		"'firewall nat'    : use nat address given with the 'nat' command.\n"
strk's avatar
strk committed
197
		"'firewall stun'   : use stun server given with the 'stun' command.\n"
Yann Diorcet's avatar
Yann Diorcet committed
198 199
		"'firewall ice'    : use ice.\n"
		"'firewall upnp'   : use uPnP IGD.\n"
aymeric's avatar
aymeric committed
200 201 202 203 204 205 206 207 208
	},
	{ "friend", lpc_cmd_friend, "Manage friends",
		"'friend list [<pattern>]'    : list friends.\n"
		"'friend call <index>'        : call a friend.\n"
		"'friend add <name> <addr>'   : add friend, <name> must be quoted to include\n"
	    "                               spaces, <addr> has \"sip:\" added if it isn't\n"
	    "                               there.  Don't use '<' '>' around <addr>.\n"
		"'friend delete <index>'      : remove friend, 'all' removes all\n"
	},
209 210 211 212 213 214
	{ "ipv6", lpc_cmd_ipv6, "Use IPV6",
		"'ipv6 status' : show ipv6 usage status.\n"
		"'ipv6 enable' : enable the use of the ipv6 network.\n"
		"'ipv6 disable' : do not use ipv6 network."
	},
	{ "mute", lpc_cmd_mute_mic,
215 216
		"Mute microphone and suspend voice transmission.",
		NULL
217 218 219 220 221 222 223 224
	},
	{ "nat", lpc_cmd_nat, "Set nat address",
		"'nat'        : show nat settings.\n"
		"'nat <addr>' : set nat address.\n"
	},
	{ "pause", lpc_cmd_pause, "pause a call",
		"'pause' : pause the current call\n"
	},
225 226 227 228
	{ "play", lpc_cmd_play, "play a wav file",
		"This command has two roles:\n"
		"Plays a file instead of capturing from soundcard - only available in file mode (see 'help soundcard')\n"
		"Specifies a wav file to be played to play music to far end when putting it on hold (pause)\n"
aymeric's avatar
aymeric committed
229 230
		"'play <wav file>'    : play a wav file."
	},
231
	{ "playbackgain", lpc_cmd_playback_gain,
232 233
		"Adjust playback gain.",
		NULL
234 235 236 237 238 239 240 241 242 243
	},
	{ "proxy", lpc_cmd_proxy, "Manage proxies",
		"'proxy list' : list all proxy setups.\n"
		"'proxy add' : add a new proxy setup.\n"
		"'proxy remove <index>' : remove proxy setup with number index.\n"
		"'proxy use <index>' : use proxy with number index as default proxy.\n"
		"'proxy unuse' : don't use a default proxy.\n"
		"'proxy show <index>' : show configuration and status of the proxy numbered by index.\n"
		"'proxy show default' : show configuration and status of the default proxy.\n"
	},
aymeric's avatar
aymeric committed
244 245 246 247
	{ "record", lpc_cmd_record, "record to a wav file",
		"This feature is available only in file mode (see 'help soundcard')\n"
		"'record <wav file>'    : record into wav file."
	},
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	{ "resume", lpc_cmd_resume, "resume a call",
		"'resume' : resume the unique call\n"
		"'resume <call id>' : hold off the call with given id\n"
	},
	{ "soundcard", lpc_cmd_soundcard, "Manage soundcards",
		"'soundcard list' : list all sound devices.\n"
		"'soundcard show' : show current sound devices configuration.\n"
		"'soundcard use <index>' : select a sound device.\n"
		"'soundcard use files' : use .wav files instead of soundcard\n"
	},
	{ "stun", lpc_cmd_stun, "Set stun server address",
		"'stun'        : show stun settings.\n"
		"'stun <addr>' : set stun server address.\n"
	},
	{ "terminate", lpc_cmd_terminate, "Terminate a call",
		"'terminate' : Terminate the current call\n"
		"'terminate <call id>' : Terminate the call with supplied id\n"
		"'terminate <all>' : Terminate all the current calls\n"
	},
	{ "transfer", lpc_cmd_transfer,
		"Transfer a call to a specified destination.",
		"'transfer <sip-uri>' : transfers the current active call to the destination sip-uri\n"
		"'transfer <call id> <sip-uri>': transfers the call with 'id' to the destination sip-uri\n"
		"'transfer <call id1> --to-call <call id2>': transfers the call with 'id1' to the destination of call 'id2' (attended transfer)\n"
	},
	{ "unmute", lpc_cmd_unmute_mic,
274 275
		"Unmute microphone and resume voice transmission.",
		NULL
276 277 278 279 280 281 282 283 284
	},
	{ "webcam", lpc_cmd_webcam, "Manage webcams",
		"'webcam list' : list all known devices.\n"
		"'webcam use <index>' : select a video device.\n"
	},
	{ "quit", lpc_cmd_quit, "Exit linphonec", NULL
	},
	{ (char *)NULL, (lpc_cmd_handler)NULL, (char *)NULL, (char *)NULL
	}
285 286 287 288 289
};


static LPC_COMMAND advanced_commands[] = {
	 { "codec", lpc_cmd_acodec, "Audio codec configuration",
290 291 292
            "'codec list' : list audio codecs\n"
            "'codec enable <index>' : enable available audio codec\n"
            "'codec disable <index>' : disable audio codec" },
Simon Morlat's avatar
Simon Morlat committed
293 294 295 296
	{ "vcodec", lpc_cmd_vcodec, "Video codec configuration",
		"'vcodec list' : list video codecs\n"
		"'vcodec enable <index>' : enable available video codec\n"
		"'vcodec disable <index>' : disable video codec" },
297 298 299 300
	{ "ec", lpc_cmd_echocancellation, "Echo cancellation",
	    "'ec on [<delay>] [<tail>] [<framesize>]' : turn EC on with given delay, tail length and framesize\n"
	    "'ec off' : turn echo cancellation (EC) off\n"
	    "'ec show' : show EC status" },
301 302 303 304
	{ "el", lpc_cmd_echolimiter, "Echo limiter",
	    "'el on turns on echo limiter (automatic half duplex, for cases where echo canceller cannot work)\n"
	    "'el off' : turn echo limiter off\n"
	    "'el show' : show echo limiter status" },
Simon Morlat's avatar
Simon Morlat committed
305
	{ "nortp-on-audio-mute", lpc_cmd_rtp_no_xmit_on_audio_mute,
306 307
		  "Set the rtp_no_xmit_on_audio_mute configuration parameter",
		  "   If set to 1 then rtp transmission will be muted when\n"
308 309
		  "   audio is muted , otherwise rtp is always sent."
		},
310
#ifdef VIDEO_ENABLED
311 312 313 314
	{ "vwindow", lpc_cmd_video_window, "Control video display window",
		"'vwindow show': shows video window\n"
		"'vwindow hide': hides video window\n"
		"'vwindow pos <x> <y>': Moves video window to x,y pixel coordinates\n"
315 316 317 318 319 320 321 322 323 324 325
		"'vwindow size <width> <height>': Resizes video window\n"
		"'vwindow id <window id>': embeds video display into supplied window id."
	},
	{ "pwindow", lpc_cmd_preview_window, "Control local camera video display (preview window)",
		"'pwindow show': shows the local camera video display\n"
		"'pwindow hide': hides the local camera video display\n"
		"'pwindow pos <x> <y>': Moves preview window to x,y pixel coordinates\n"
		"'pwindow size <width> <height>': Resizes preview window\n"
		"'pwindow id <window id>': embeds preview display into supplied window id.\n"
		"'pwindow integrated': integrate preview display within the video window of current call.\n"
		"'pwindow standalone': use standalone window for preview display."
326
	},
327 328 329
	{ "snapshot", lpc_cmd_snapshot, "Take a snapshot of currently received video stream",
		"'snapshot <file path>': take a snapshot and records it in jpeg format into the supplied path\n"
	},
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 334
	{ "vfureq", lpc_cmd_vfureq, "Request the other side to send VFU for the current call",
		NULL
335
	},
336
#endif
337 338 339 340 341
	{ "states", lpc_cmd_states, "Show internal states of liblinphone, registrations and calls, according to linphonecore.h definitions",
		"'states global': shows global state of liblinphone \n"
		"'states calls': shows state of calls\n"
		"'states proxies': shows state of proxy configurations"
	},
342 343
	{ "register", lpc_cmd_register, "Register in one line to a proxy" , "register <sip identity> <sip proxy> <password>"
},
344
	{ "unregister", lpc_cmd_unregister, "Unregister from default proxy", NULL	},
345
	{ "status", lpc_cmd_status, "Print various status information",
346 347 348
			"'status register'  \t: print status concerning registration\n"
			"'status autoanswer'\t: tell whether autoanswer mode is enabled\n"
			"'status hook'      \t: print hook status\n" },
349
	{ "ports", lpc_cmd_ports, "Network ports configuration",
350 351
			"'ports'  \t: prints current used ports.\n"
			"'ports sip <port number>'\t: Sets the sip port.\n" },
352 353 354 355
	{ "param", lpc_cmd_param, "parameter set or read as normally given in .linphonerc",
			"'param <section> <parameter> [<value>]'  \t: reads [sets] given parameter.\n"
			"NOTES: - changes may become effective after (re)establishing a sip connection.\n"
			"       - upon exit, .linphonerc will reflect the updated state.\n" },
356 357 358 359 360 361 362 363 364
	{ "speak", lpc_cmd_speak, "Speak a sentence using espeak TTS engine",
			"This feature is available only in file mode. (see 'help soundcard')\n"
			"'speak <voice name> <sentence>'	: speak a text using the specified espeak voice.\n"
			"Example for english voice: 'speak default Hello my friend !'"
	},
	{ "staticpic", lpc_cmd_staticpic, "Manage static pictures when nowebcam",
		"'staticpic set' : Set path to picture that should be used.\n"
		"'staticpic fps' : Get/set frames per seconds for picture emission.\n"
	},
365 366 367 368
	{ "identify", lpc_cmd_identify, "Returns the user-agent string of far end",
		"'identify' \t: returns remote user-agent string for current call.\n"
		"'identify <id>' \t: returns remote user-agent string for call with supplied id.\n"
	},
369 370 371 372
	{ "ringback", lpc_cmd_ringback, "Specifies a ringback tone to be played to remote end during incoming calls",
		"'ringback <path of mono .wav file>'\t: Specifies a ringback tone to be played to remote end during incoming calls\n"
		"'ringback disable'\t: Disable playing of ringback tone to callers\n"
	},
373
	{ "redirect", lpc_cmd_redirect, "Redirect an incoming call",
374 375
		"'redirect <id> <redirect-uri>'\t: Redirect the specified call to the <redirect-uri>\n"
		"'redirect all <redirect-uri>'\t: Redirect all pending incoming calls to the <redirect-uri>\n"
376
	},
377 378 379
	{ "zrtp-set-verified", lpc_cmd_zrtp_verified,"Set ZRTP SAS verified.",
		"'Set ZRTP SAS verified'\n"
	},
380 381 382
	{ "zrtp-set-unverified", lpc_cmd_zrtp_unverified,"Set ZRTP SAS not verified.",
		"'Set ZRTP SAS not verified'\n"
	},
383
	{	NULL,NULL,NULL,NULL}
aymeric's avatar
aymeric committed
384 385
};

386 387


aymeric's avatar
aymeric committed
388 389
/***************************************************************************
 *
390
 *  Public interface
aymeric's avatar
aymeric committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
 *
 ***************************************************************************/

/*
 * Main command dispatcher.
 * WARNING: modifies second argument!
 *
 * Always return 1 currently.
 */
int
linphonec_parse_command_line(LinphoneCore *lc, char *cl)
{
	char *ptr=cl;
	char *args=NULL;
	LPC_COMMAND *cmd;

	/* Isolate first word and args */
	while(*ptr && !isspace(*ptr)) ++ptr;
	if (*ptr)
	{
		*ptr='\0';
 		/* set args to first nonblank */
		args=ptr+1;
		while(*args && isspace(*args)) ++args;
	}

	/* Handle DTMF */
	if ( isdigit(*cl) || *cl == '#' || *cl == '*' )
	{
		while ( isdigit(*cl) || *cl == '#' || *cl == '*' )
		{
422 423
			if (linphone_core_get_current_call(lc))
				linphone_call_send_dtmf(linphone_core_get_current_call(lc), *cl);
424
			linphone_core_play_dtmf (lc,*cl,100);
smorlat's avatar
smorlat committed
425
			ms_sleep(1); // be nice
aymeric's avatar
aymeric committed
426 427 428 429 430 431 432 433 434 435 436
			++cl;
		}

		// discard spurious trailing chars
		return 1;
	}

	/* Handle other kind of commands */
	cmd=lpc_find_command(cl);
	if ( !cmd )
	{
437
		linphonec_out("'%s': Cannot understand this.\n", cl);
aymeric's avatar
aymeric committed
438 439 440 441 442
		return 1;
	}

	if ( ! cmd->func(lc, args) )
	{
443
		linphonec_out("Syntax error.\n");
aymeric's avatar
aymeric committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
		linphonec_display_command_help(cmd);
	}

	return 1;
}

/*
 * Generator function for command completion.
 * STATE let us know whether to start from scratch;
 * without any state (STATE==0), then we start at the
 * top of the list.
 */
char *
linphonec_command_generator(const char *text, int state)
{
459
	static int index, len, adv;
aymeric's avatar
aymeric committed
460 461 462 463 464
	char *name;

	if ( ! state )
	{
		index=0;
465
		adv=0;
aymeric's avatar
aymeric committed
466 467 468 469 470 471
		len=strlen(text);
	}
	/*
 	 * Return the next name which partially matches
	 * from the commands list
	 */
472 473
	if (adv==0){
		while ((name=commands[index].name))
aymeric's avatar
aymeric committed
474
		{
475 476 477 478 479 480
			++index; /* so next call get next command */

			if (strncmp(name, text, len) == 0)
			{
				return ortp_strdup(name);
			}
aymeric's avatar
aymeric committed
481
		}
482 483
		adv=1;
		index=0;
aymeric's avatar
aymeric committed
484
	}
485 486 487 488
	if (adv==1){
		while ((name=advanced_commands[index].name))
		{
			++index; /* so next call get next command */
aymeric's avatar
aymeric committed
489

490 491 492 493 494 495
			if (strncmp(name, text, len) == 0)
			{
				return ortp_strdup(name);
			}
		}
	}
aymeric's avatar
aymeric committed
496 497 498 499 500 501
	return NULL;
}


/***************************************************************************
 *
502
 *  Command handlers
aymeric's avatar
aymeric committed
503 504 505 506 507 508 509 510 511 512 513
 *
 ***************************************************************************/

static int
lpc_cmd_help(LinphoneCore *lc, char *arg)
{
	int i=0;
	LPC_COMMAND *cmd;

	if (!arg || !*arg)
	{
514 515
		linphonec_out("Commands are:\n");
		linphonec_out("---------------------------\n");
aymeric's avatar
aymeric committed
516 517 518

		while (commands[i].help)
		{
519
			linphonec_out("%10.10s\t%s\n", commands[i].name,
aymeric's avatar
aymeric committed
520 521 522
				commands[i].help);
			i++;
		}
523

524
		linphonec_out("---------------------------\n");
525 526
		linphonec_out("Type 'help <command>' for more details or\n");
		linphonec_out("     'help advanced' to list additional commands.\n");
aymeric's avatar
aymeric committed
527 528 529 530

		return 1;
	}

531 532 533 534 535 536
	if (strcmp(arg,"advanced")==0){
		linphonec_out("Advanced commands are:\n");
		linphonec_out("---------------------------\n");
		i=0;
		while (advanced_commands[i].help)
		{
jehan's avatar
jehan committed
537
			linphonec_out("%20.20s\t%s\n", advanced_commands[i].name,
538 539 540
				advanced_commands[i].help);
			i++;
		}
541

542 543 544 545 546
		linphonec_out("---------------------------\n");
		linphonec_out("Type 'help <command>' for more details.\n");

		return 1;
	}
547

aymeric's avatar
aymeric committed
548 549 550
	cmd=lpc_find_command(arg);
	if ( !cmd )
	{
551
		linphonec_out("No such command.\n");
aymeric's avatar
aymeric committed
552 553 554 555 556 557 558 559
		return 1;
	}

	linphonec_display_command_help(cmd);
	return 1;

}

smorlat's avatar
smorlat committed
560
static char callee_name[256]={0};
561
static char caller_name[256]={0};
smorlat's avatar
smorlat committed
562

563

aymeric's avatar
aymeric committed
564 565 566 567 568 569 570 571
static int
lpc_cmd_call(LinphoneCore *lc, char *args)
{
	if ( ! args || ! *args )
	{
		return 0;
	}
	{
572
		LinphoneCall *call;
573
		LinphoneCallParams *cp=linphone_core_create_call_params (lc, NULL);
574
		char *opt1,*opt2;
575 576 577 578 579
		if ( linphone_core_in_call(lc) )
		{
			linphonec_out("Terminate or hold on the current call first.\n");
			return 1;
		}
580 581 582 583
		opt1=strstr(args,"--audio-only");
		opt2=strstr(args,"--early-media");
		if (opt1){
			opt1[0]='\0';
584
			while(--opt1 > args && opt1[0]==' ') opt1[0]='\0';
585 586
			linphone_call_params_enable_video (cp,FALSE);
		}
587 588
		if (opt2){
			opt2[0]='\0';
589
			while(--opt2 > args && opt2[0]==' ') opt2[0]='\0';
590 591
			linphone_call_params_enable_early_media_sending(cp,TRUE);
		}
592
		if ( NULL == (call=linphone_core_invite_with_params(lc, args,cp)) )
aymeric's avatar
aymeric committed
593
		{
594
			linphonec_out("Error from linphone_core_invite.\n");
aymeric's avatar
aymeric committed
595 596 597
		}
		else
		{
smorlat's avatar
smorlat committed
598
			snprintf(callee_name,sizeof(callee_name),"%s",args);
aymeric's avatar
aymeric committed
599
		}
Simon Morlat's avatar
Simon Morlat committed
600
		linphone_call_params_unref(cp);
aymeric's avatar
aymeric committed
601 602 603 604
	}
	return 1;
}

605
static int
606
lpc_cmd_calls(LinphoneCore *lc, char *args){
607
	const bctbx_list_t *calls = linphone_core_get_calls(lc);
608 609
	if(calls)
	{
610
		lpc_display_call_states(lc);
611 612 613 614 615 616 617 618
	}else
	{
		linphonec_out("No active call.\n");
	}
	return 1;
}


619 620 621 622 623 624
static int
lpc_cmd_chat(LinphoneCore *lc, char *args)
{
	char *arg1 = args;
	char *arg2 = NULL;
	char *ptr = args;
625
	LinphoneChatRoom *cr;
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641

	if (!args) return 0;

	/* Isolate first and second arg */
	while(*ptr && !isspace(*ptr)) ++ptr;
	if ( *ptr )
	{
		*ptr='\0';
		arg2=ptr+1;
		while(*arg2 && isspace(*arg2)) ++arg2;
	}
	else
	{
		/* missing one parameter */
		return 0;
	}
Simon Morlat's avatar
Simon Morlat committed
642
	cr = linphone_core_get_chat_room_from_uri(lc,arg1);
643 644 645 646
	linphone_chat_room_send_message(cr,arg2);
	return 1;
}

647
const char *linphonec_get_callee(void){
smorlat's avatar
smorlat committed
648 649 650
	return callee_name;
}

651
const char *linphonec_get_caller(void){
652 653 654 655 656 657 658
	return caller_name;
}

void linphonec_set_caller(const char *caller){
	snprintf(caller_name,sizeof(caller_name)-1,"%s",caller);
}

aymeric's avatar
aymeric committed
659
static int
660
lpc_cmd_transfer(LinphoneCore *lc, char *args)
aymeric's avatar
aymeric committed
661
{
662 663
	if (args){
		LinphoneCall *call;
664
		LinphoneCall *call2;
665 666 667
		const char *refer_to=NULL;
		char arg1[256]={0};
		char arg2[266]={0};
668
		long id2=0;
669
		int n=sscanf(args,"%255s %265s %li",arg1,arg2,&id2);
670 671
		if (n==1 || isalpha(*arg1)){
			call=linphone_core_get_current_call(lc);
672
			if (call==NULL && bctbx_list_size(linphone_core_get_calls(lc))==1){
673 674 675 676 677 678 679
				call=(LinphoneCall*)linphone_core_get_calls(lc)->data;
			}
			refer_to=args;
			if (call==NULL){
				linphonec_out("No active call, please specify a call id among the ones listed by 'calls' command.\n");
				return 0;
			}
680 681
			linphone_core_transfer_call(lc, call, refer_to);
		}else if (n==2){
682 683 684 685
			long id=atoi(arg1);
			refer_to=args+strlen(arg1)+1;
			call=linphonec_get_call(id);
			if (call==NULL) return 0;
686 687 688 689 690 691 692 693 694 695 696 697
			linphone_core_transfer_call(lc, call, refer_to);
		}else if (n==3){
			long id=atoi(arg1);
			call=linphonec_get_call(id);
			call2=linphonec_get_call(id2);
			if (call==NULL || call2==NULL) return 0;
			if (strcmp(arg2,"--to-call")!=0){
				return 0;
			}
			linphonec_out("Performing attended transfer of call %i to call %i",id,id2);
			linphone_core_transfer_call_to_another (lc,call,call2);
		}else return 0;
698 699 700
	}else{
		linphonec_out("Transfer command requires at least one argument\n");
		return 0;
aymeric's avatar
aymeric committed
701 702 703 704 705 706 707
	}
	return 1;
}

static int
lpc_cmd_terminate(LinphoneCore *lc, char *args)
{
708
	if (linphone_core_get_calls(lc)==NULL){
709
		linphonec_out("No active calls\n");
710 711
		return 1;
	}
712
	if (!args)
aymeric's avatar
aymeric committed
713
	{
714 715
		if ( -1 == linphone_core_terminate_call(lc, NULL) ){
			linphonec_out("Could not stop the active call.\n");
716 717
		}
		return 1;
aymeric's avatar
aymeric committed
718
	}
719

720 721 722 723 724 725 726 727 728 729
	if(strcmp(args,"all")==0){
		linphonec_out("We are going to stop all the calls.\n");
		linphone_core_terminate_all_calls(lc);
		return 1;
	}else{
		/*the argument is a linphonec call id */
		long id=atoi(args);
		LinphoneCall *call=linphonec_get_call(id);
		if (call){
			if (linphone_core_terminate_call(lc,call)==-1){
730
				linphonec_out("Could not stop the call with id %li\n",id);
731
			}
732 733
		}else return 0;
		return 1;
734 735
	}
	return 0;
736

aymeric's avatar
aymeric committed
737 738
}

739 740
static int
lpc_cmd_redirect(LinphoneCore *lc, char *args){
741
	const bctbx_list_t *elem;
742 743 744 745 746 747
	int didit=0;
	if (!args) return 0;
	if ((elem=linphone_core_get_calls(lc))==NULL){
		linphonec_out("No active calls.\n");
		return 1;
	}
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
	if (strncmp(args, "all ", 4) == 0) {
		while(elem!=NULL){
			LinphoneCall *call=(LinphoneCall*)elem->data;
			if (linphone_call_get_state(call)==LinphoneCallIncomingReceived){
				if (linphone_core_redirect_call(lc,call,args+4) != 0) {
					linphonec_out("Could not redirect call.\n");
					elem=elem->next;
				} else {
					didit=1;
					/*as the redirection closes the call, we need to re-check the call list that is invalidated.*/
					elem=linphone_core_get_calls(lc);
				}
			}else elem=elem->next;
		}
		if (didit==0){
			linphonec_out("There is no pending incoming call to redirect.\n");
		}
	} else {
		char space;
		long id;
		int charRead;
		if ( sscanf(args, "%li%c%n", &id, &space, &charRead) == 2 && space == ' ') {
			LinphoneCall * call = linphonec_get_call(id);
			if ( call != NULL ) {
				if (linphone_call_get_state(call)!=LinphoneCallIncomingReceived) {
					linphonec_out("The state of the call is not incoming, can't be redirected.\n");
				} else if (linphone_core_redirect_call(lc,call,args+charRead) != 0) {
					linphonec_out("Could not redirect call.\n");
				}
777
			}
778
		} else return 0;
779 780 781 782
	}
	return 1;
}

aymeric's avatar
aymeric committed
783
static int
784
lpc_cmd_answer(LinphoneCore *lc, char *args){
785
	if (!args)
aymeric's avatar
aymeric committed
786
	{
787
		int nb=bctbx_list_size(linphone_core_get_calls(lc));
788 789 790
		if (nb==1){
			//if just one call is present answer the only one in passing NULL to the linphone_core_accept_call ...
			if ( -1 == linphone_core_accept_call(lc, NULL) )
791
			{
792
				linphonec_out("Fail to accept incoming call\n");
793
			}
794 795 796 797 798
		}else if (nb==0){
			linphonec_out("There are no calls to answer.\n");
		}else{
			linphonec_out("Multiple calls in progress, please specify call id.\n");
			return 0;
799
		}
800 801 802 803 804 805 806 807 808 809
		return 1;
	}else{
		long id;
		if (sscanf(args,"%li",&id)==1){
			LinphoneCall *call=linphonec_get_call (id);
			if (linphone_core_accept_call (lc,call)==-1){
				linphonec_out("Fail to accept call %i\n",id);
			}
		}else return 0;
		return 1;
810 811
	}
	return 0;
aymeric's avatar
aymeric committed
812 813
}

smorlat's avatar
smorlat committed
814 815 816
static int
lpc_cmd_autoanswer(LinphoneCore *lc, char *args)
{
817 818 819 820 821 822 823 824 825 826
	if ( ! args )
	{
		if ( linphonec_get_autoanswer() ) {
			linphonec_out("Auto answer is enabled. Use 'autoanswer disable' to disable.\n");
		} else {
			linphonec_out("Auto answer is disabled. Use 'autoanswer enable' to enable.\n");
		}
		return 1;
	}

smorlat's avatar
smorlat committed
827 828
	if (strstr(args,"enable")){
		linphonec_set_autoanswer(TRUE);
smorlat's avatar
smorlat committed
829
		linphonec_out("Auto answer enabled.\n");
smorlat's avatar
smorlat committed
830 831
	}else if (strstr(args,"disable")){
		linphonec_set_autoanswer(FALSE);
smorlat's avatar
smorlat committed
832
		linphonec_out("Auto answer disabled.\n");
smorlat's avatar
smorlat committed
833 834 835 836
	}else return 0;
	return 1;
}

aymeric's avatar
aymeric committed
837 838 839
static int
lpc_cmd_quit(LinphoneCore *lc, char *args)
{
840
	linphonec_main_loop_exit();
aymeric's avatar
aymeric committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
	return 1;
}

static int
lpc_cmd_nat(LinphoneCore *lc, char *args)
{
	bool_t use;
	const char *nat;

	if ( args ) args=lpc_strip_blanks(args);

	if ( args && *args )
	{
		linphone_core_set_nat_address(lc, args);
		/* linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_NAT_ADDRESS); */
	}

	nat = linphone_core_get_nat_address(lc);
Simon Morlat's avatar
Simon Morlat committed
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)");
aymeric's avatar
aymeric committed
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

	return 1;
}

static int
lpc_cmd_stun(LinphoneCore *lc, char *args)
{
	bool_t use;
	const char *stun;

	if ( args ) args=lpc_strip_blanks(args);

	if ( args && *args )
	{
		linphone_core_set_stun_server(lc, args);
		/* linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_USE_STUN); */
	}

	stun = linphone_core_get_stun_server(lc);
Simon Morlat's avatar
Simon Morlat committed
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)");
aymeric's avatar
aymeric committed
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896

	return 1;
}

static int
lpc_cmd_firewall(LinphoneCore *lc, char *args)
{
	const char* setting=NULL;

	if ( args ) args=lpc_strip_blanks(args);

	if ( args && *args )
	{
		if (strcmp(args,"none")==0)
		{
Simon Morlat's avatar
Simon Morlat committed
897
			linphone_core_set_firewall_policy(lc,LinphonePolicyNoFirewall);
aymeric's avatar
aymeric committed
898
		}
899
		else if (strcmp(args,"upnp")==0)
Yann Diorcet's avatar
Yann Diorcet committed
900 901 902
		{
			linphone_core_set_firewall_policy(lc,LinphonePolicyUseUpnp);
		}
903 904 905 906 907 908 909 910 911 912
		else if (strcmp(args,"ice")==0)
		{
			setting = linphone_core_get_stun_server(lc);
			if ( ! setting )
			{
				linphonec_out("No stun server address is defined, use 'stun <address>' first\n");
				return 1;
			}
			linphone_core_set_firewall_policy(lc,LinphonePolicyUseIce);
		}
aymeric's avatar
aymeric committed
913 914 915 916 917
		else if (strcmp(args,"stun")==0)
		{
			setting = linphone_core_get_stun_server(lc);
			if ( ! setting )
			{
918
				linphonec_out("No stun server address is defined, use 'stun <address>' first\n");
aymeric's avatar
aymeric committed
919 920
				return 1;
			}
Simon Morlat's avatar
Simon Morlat committed
921
			linphone_core_set_firewall_policy(lc,LinphonePolicyUseStun);
aymeric's avatar
aymeric committed
922 923 924 925 926 927
		}
		else if (strcmp(args,"nat")==0)
		{
			setting = linphone_core_get_nat_address(lc);
			if ( ! setting )
			{
928
				linphonec_out("No nat address is defined, use 'nat <address>' first");
aymeric's avatar
aymeric committed
929 930
				return 1;
			}
Simon Morlat's avatar
Simon Morlat committed
931
			linphone_core_set_firewall_policy(lc,LinphonePolicyUseNatAddress);
aymeric's avatar
aymeric committed
932 933 934 935 936
		}
	}

	switch(linphone_core_get_firewall_policy(lc))
	{
Simon Morlat's avatar
Simon Morlat committed
937
		case LinphonePolicyNoFirewall:
938
			linphonec_out("No firewall\n");
aymeric's avatar
aymeric committed
939
			break;
Simon Morlat's avatar
Simon Morlat committed
940
		case LinphonePolicyUseStun:
941
			linphonec_out("Using stun server %s to discover firewall address\n", setting ? setting : linphone_core_get_stun_server(lc));
aymeric's avatar
aymeric committed
942
			break;
Simon Morlat's avatar
Simon Morlat committed
943
		case LinphonePolicyUseNatAddress:
944
			linphonec_out("Using supplied nat address %s.\n", setting ? setting : linphone_core_get_nat_address(lc));
aymeric's avatar
aymeric committed
945
			break;
946 947 948
		case LinphonePolicyUseIce:
			linphonec_out("Using ice with stun server %s to discover firewall address\n", setting ? setting : linphone_core_get_stun_server(lc));
			break;
Yann Diorcet's avatar
Yann Diorcet committed
949 950 951
		case LinphonePolicyUseUpnp:
			linphonec_out("Using uPnP IGD protocol\n");
			break;
aymeric's avatar
aymeric committed
952 953 954 955
	}
	return 1;
}

956
#ifndef _WIN32
aymeric's avatar
aymeric committed
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
/* Helper function for processing freind names */
static int
lpc_friend_name(char **args, char **name)
{
	/* Use space as a terminator unless quoted */
	if (('"' == **args) || ('\'' == **args)){
		char *end;
		char delim = **args;
		(*args)++;
		end = (*args);
		while ((delim != *end) && ('\0' != *end)) end++;
		if ('\0' == *end) {
			fprintf(stderr, "Mismatched quotes\n");
			return 0;
		}
		*name = *args;
		*end = '\0';
		*args = ++end;
	} else {
		*name = strsep(args, " ");
977

aymeric's avatar
aymeric committed
978 979 980 981 982 983 984 985
		if (NULL == *args) { /* Means there was no separator */
			fprintf(stderr, "Either name or address is missing\n");
			return 0;
		}
		if (NULL == *name) return 0;
	}
	return 1;
}
smorlat's avatar
smorlat committed
986
#endif
aymeric's avatar
aymeric committed
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

static int
lpc_cmd_friend(LinphoneCore *lc, char *args)
{
	int friend_num;

	if ( args ) args=lpc_strip_blanks(args);

	if ( ! args || ! *args ) return 0;

	if ( !strncmp(args, "list", 4) )
	{
		return linphonec_friend_list(lc, args+4);
		return 1;
	}
	else if ( !strncmp(args, "call", 4) )
	{
		args+=4;
		if ( ! *args ) return 0;
		friend_num = strtol(args, NULL, 10);
1007
#ifndef _WIN32_WCE
aymeric's avatar
aymeric committed
1008
		if ( errno == ERANGE ) {
1009
			linphonec_out("Invalid friend number\n");
aymeric's avatar
aymeric committed
1010 1011
			return 0;
		}
Jehan Monnier's avatar
Jehan Monnier committed
1012
#endif /*_WIN32_WCE*/
aymeric's avatar
aymeric committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
		linphonec_friend_call(lc, friend_num);
		return 1;
	}
	else if ( !strncmp(args, "delete", 6) )
	{
		args+=6;
		if ( ! *args ) return 0;
		while (*args == ' ') args++;
		if ( ! *args ) return 0;
		if (!strncmp(args, "all", 3))
		{
			friend_num = -1;
1025
		}
aymeric's avatar
aymeric committed
1026 1027 1028
		else
		{
			friend_num = strtol(args, NULL, 10);
1029
#ifndef _WIN32_WCE
aymeric's avatar
aymeric committed
1030
			if ( errno == ERANGE ) {
1031
				linphonec_out("Invalid friend number\n");
aymeric's avatar
aymeric committed
1032 1033
				return 0;
			}
Jehan Monnier's avatar
Jehan Monnier committed
1034
#endif /*_WIN32_WCE*/
aymeric's avatar
aymeric committed
1035 1036 1037 1038 1039 1040
		}
		linphonec_friend_delete(lc, friend_num);
		return 1;
	}
	else if ( !strncmp(args, "add", 3) )
	{
1041
#ifndef _WIN32
aymeric's avatar
aymeric committed
1042 1043 1044 1045 1046 1047 1048 1049 1050
		char  *name;
		char  addr[80];
		char *addr_p = addr;
		char *addr_orig;

		args+=3;
		if ( ! *args ) return 0;
		while (*args == ' ') args++;
		if ( ! *args ) return 0;
smorlat's avatar
smorlat committed
1051

aymeric's avatar
aymeric committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
		if (!lpc_friend_name(&args,  &name)) return 0;

		while (*args == ' ') args++;
		if ( ! *args ) return 0;
		if (isdigit(*args)) {
			strcpy (addr, "sip:");
			addr_p = addr + strlen("sip:");
		}
		addr_orig = strsep(&args, " ");
		if (1 >= strlen(addr_orig)) {
			fprintf(stderr, "A single-digit address is not valid\n");
			return 0;
		}
		strcpy(addr_p, addr_orig);
		linphonec_friend_add(lc, name, addr);
smorlat's avatar
smorlat committed
1067 1068
#else
		LinphoneFriend *new_friend;
1069
		new_friend = linphone_core_create_friend_with_address(lc, args);
smorlat's avatar
smorlat committed
1070 1071
		linphone_core_add_friend(lc, new_friend);
#endif
aymeric's avatar
aymeric committed
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
		return 1;
	}
	return 0;
}

static int lpc_cmd_play(LinphoneCore *lc, char *args){
	if ( args ) args=lpc_strip_blanks(args);
	if ( ! args || ! *args ) return 0;
	linphone_core_set_play_file(lc,args);
	return 1;
}

static int lpc_cmd_record(LinphoneCore *lc, char *args){
	if ( args ) args=lpc_strip_blanks(args);
	if ( ! args || ! *args ) return 0;
	linphone_core_set_record_file(lc,args);
	return 1;
}

/*
 * Modified input
 */
static int
lpc_cmd_proxy(LinphoneCore *lc, char *args)
{
	char *arg1 = args;
	char *arg2 = NULL;
	char *ptr = args;
	int proxynum;

	if ( ! arg1 ) return 0;

	/* Isolate first and second arg */
	while(*ptr && !isspace(*ptr)) ++ptr;
	if ( *ptr )
	{
		*ptr='\0';
		arg2=ptr+1;
		while(*arg2 && isspace(*arg2)) ++arg2;
	}

	if (strcmp(arg1,"add")==0)
	{
1115
#ifdef HAVE_READLINE
aymeric's avatar
aymeric committed
1116
		rl_inhibit_completion=1;
1117
#endif
aymeric's avatar
aymeric committed
1118
		linphonec_proxy_add(lc);
1119
#ifdef HAVE_READLINE
aymeric's avatar
aymeric committed
1120
		rl_inhibit_completion=0;
1121
#endif
aymeric's avatar
aymeric committed
1122 1123 1124 1125 1126 1127 1128
	}
	else if (strcmp(arg1,"list")==0)
	{
		linphonec_proxy_list(lc);
	}
	else if (strcmp(arg1,"remove")==0)
	{
Simon Morlat's avatar
Simon Morlat committed
1129
		if (arg2==NULL) return 0;
aymeric's avatar
aymeric committed
1130 1131 1132 1133 1134 1135 1136 1137
		linphonec_proxy_remove(lc,atoi(arg2));
	}
	else if (strcmp(arg1,"use")==0)
	{
		if ( arg2 && *arg2 )
		{
			proxynum=atoi(arg2);
			if ( linphonec_proxy_use(lc, proxynum) )
1138
				linphonec_out("Default proxy set to %d.\n", proxynum);
aymeric's avatar
aymeric committed
1139 1140 1141 1142
		}
		else
		{
			proxynum=linphone_core_get_default_proxy(lc, NULL);
1143 1144
			if ( proxynum == -1 ) linphonec_out("No default proxy.\n");
			else linphonec_out("Current default proxy is %d.\n", proxynum);
aymeric's avatar
aymeric committed
1145
		}
smorlat's avatar
smorlat committed
1146
	}else if (strcmp(arg1, "unuse")==0){
aymeric's avatar
aymeric committed
1147
		linphone_core_set_default_proxy(lc, NULL);
1148
		linphonec_out("Use no proxy.\n");
1149 1150 1151 1152 1153 1154 1155 1156
	}

	else if (strcmp(arg1, "show")==0)
	{
		if (arg2 && *arg2)
		{
			if (strstr(arg2,"default"))
			{
Simon Morlat's avatar
Simon Morlat committed
1157 1158 1159 1160 1161 1162
				proxynum=linphone_core_get_default_proxy(lc, NULL);
				if ( proxynum < 0 ) {
					linphonec_out("No default proxy defined\n");
					return 1;
				}
				linphonec_proxy_show(lc,proxynum);
1163 1164 1165
			}
			else
			{
Simon Morlat's avatar
Simon Morlat committed
1166
				linphonec_proxy_show(lc, atoi(arg2));
1167 1168
			}
		}
1169
		else return 0; /* syntax error */
1170 1171 1172
	}

	else
aymeric's avatar
aymeric committed
1173
	{
1174
		return 0; /* syntax error */
aymeric's avatar
aymeric committed
1175 1176 1177 1178 1179 1180 1181 1182
	}

	return 1;
}

static int
lpc_cmd_call_logs(LinphoneCore *lc, char *args)
{
1183 1184
	const bctbx_list_t *elem=linphone_core_get_call_logs(lc);
	for (;elem!=NULL;elem=bctbx_list_next(elem))
aymeric's avatar
aymeric committed
1185 1186 1187
	{
		LinphoneCallLog *cl=(LinphoneCallLog*)elem->data;
		char *str=linphone_call_log_to_str(cl);
1188
		linphonec_out("%s\n",str);
aymeric's avatar
aymeric committed
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
		ms_free(str);
	}
	return 1;
}

static int
lpc_cmd_ipv6(LinphoneCore *lc, char *arg1)
{
	if ( ! arg1 )
	{
1199
		return 0; /* syntax error */
aymeric's avatar
aymeric committed
1200 1201 1202 1203
	}

	if (strcmp(arg1,"status")==0)
	{
1204
		linphonec_out("ipv6 use enabled: %s\n",linphone_core_ipv6_enabled(lc) ? "true":"false");
aymeric's avatar
aymeric committed
1205 1206 1207 1208
	}
	else if (strcmp(arg1,"enable")==0)
	{
		linphone_core_enable_ipv6(lc,TRUE);
1209
		linphonec_out("ipv6 use enabled.\n");
aymeric's avatar
aymeric committed
1210 1211 1212 1213
	}
	else if (strcmp(arg1,"disable")==0)
	{
		linphone_core_enable_ipv6(lc,FALSE);
1214
		linphonec_out("ipv6 use disabled.\n");
aymeric's avatar
aymeric committed
1215 1216 1217
	}
	else
	{
1218
		return 0; /* syntax error */
aymeric's avatar
aymeric committed
1219 1220 1221 1222
	}
	return 1;
}

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
static int devname_to_index(LinphoneCore *lc, const char *devname){
	const char **p;
	int i;
	for(i=0,p=linphone_core_get_sound_devices(lc);*p!=NULL;++p,++i){
		if (strcmp(devname,*p)==0) return i;
	}
	return -1;
}

static const char *index_to_devname(LinphoneCore *lc, int index){
	const char **p;
	int i;
	for(i=0,p=linphone_core_get_sound_devices(lc);*p!=NULL;++p,++i){
		if (i==index) return *p;
	}
	return NULL;
}

1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
static int lpc_cmd_soundcard(LinphoneCore *lc, char *args)
{
	int i, index;
	const char **dev;
	char *arg1 = args;
	char *arg2 = NULL;
	char *ptr = args;

	if (!args) return 0; /* syntax error */

	/* Isolate first and second arg */
	while(*ptr && !isspace(*ptr)) ++ptr;
	if ( *ptr )
	{
		*ptr='\0';
		arg2=ptr+1;
		while(*arg2 && isspace(*arg2)) ++arg2;
aymeric's avatar
aymeric committed
1258
	}
1259 1260 1261 1262 1263

	if (strcmp(arg1, "list")==0)
	{
		dev=linphone_core_get_sound_devices(lc);
		for(i=0; dev[i]!=NULL; ++i){
1264
			linphonec_out("%i: %s\n",i,dev[i]);
aymeric's avatar
aymeric committed
1265 1266
		}
		return 1;
1267 1268 1269 1270
	}

	if (strcmp(arg1, "show")==0)
	{
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
		if (linphone_core_get_use_files(lc)) {
			linphonec_out("Using files.\n");
		} else {
			linphonec_out("Ringer device: %s\n",
				linphone_core_get_ringer_device(lc));
			linphonec_out("Playback device: %s\n",
				linphone_core_get_playback_device(lc));
			linphonec_out("Capture device: %s\n",
				linphone_core_get_capture_device(lc));
		}
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
		return 1;
	}

	if (strcmp(arg1, "use")==0 && arg2)
	{
		if (strcmp(arg2, "files")==0)
		{
			linphonec_out("Using wav files instead of soundcard.\n");
			linphone_core_use_files(lc,TRUE);
			return 1;
aymeric's avatar
aymeric committed
1291
		}
1292

1293 1294
		linphone_core_use_files(lc,FALSE);

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
		dev=linphone_core_get_sound_devices(lc);
		index=atoi(arg2); /* FIXME: handle not-a-number */
		for(i=0;dev[i]!=NULL;i++)
		{
			if (i!=index) continue;

			linphone_core_set_ringer_device(lc,dev[i]);
			linphone_core_set_playback_device(lc,dev[i]);
			linphone_core_set_capture_device(lc,dev[i]);
			linphonec_out("Using sound device %s\n",dev[i]);
			return 1;
		}
		linphonec_out("No such sound device\n");
		return 1;
aymeric's avatar
aymeric committed
1309
	}
1310

1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
	if (strcmp(arg1, "capture")==0)
	{
		const char *devname=linphone_core_get_capture_device(lc);
		if (!arg2){
			linphonec_out("Using capture device #%i (%s)\n",
					devname_to_index(lc,devname),devname);
		}else{
			index=atoi(arg2); /* FIXME: handle not-a-number */
			devname=index_to_devname(lc,index);
			if (devname!=NULL){
				linphone_core_set_capture_device(lc,devname);
				linphonec_out("Using capture sound device %s\n",devname);
				return 1;
			}
			linphonec_out("No such sound device\n");
		}
		return 1;
	}
	if (strcmp(arg1, "playback")==0)
	{
		const char *devname=linphone_core_get_playback_device(lc);
		if (!arg2){
			linphonec_out("Using playback device #%i (%s)\n",
					devname_to_index(lc,devname),devname);
		}else{
			index=atoi(arg2); /* FIXME: handle not-a-number */
			devname=index_to_devname(lc,index);
			if (devname!=NULL){
				linphone_core_set_playback_device(lc,devname);
				linphonec_out("Using playback sound device %s\n",devname);
				return 1;
			}
			linphonec_out("No such sound device\n");
		}
		return 1;
	}
	if (strcmp(arg1, "ring")==0)
	{
		const char *devname=linphone_core_get_ringer_device(lc);
		if (!arg2){
			linphonec_out("Using ring device #%i (%s)\n",
					devname_to_index(lc,devname),devname);
		}else{
			index=atoi(arg2); /* FIXME: handle not-a-number */
			devname=index_to_devname(lc,index);
			if (devname!=NULL){
				linphone_core_set_ringer_device(lc,devname);
				linphonec_out("Using ring sound device %s\n",devname);
				return 1;
			}
			linphonec_out("No such sound device\n");
		}
		return 1;
	}
1365
	return 0; /* syntax error */
aymeric's avatar
aymeric committed
1366 1367
}

1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
static int lpc_cmd_webcam(LinphoneCore *lc, char *args)
{
	int i, index;
	const char **dev;
	char *arg1 = args;
	char *arg2 = NULL;
	char *ptr = args;

	if (!args) return 0; /* syntax error */

	/* Isolate first and second arg */
	while(*ptr && !isspace(*ptr)) ++ptr;
	if ( *ptr )
	{
		*ptr='\0';
		arg2=ptr+1;
		while(*arg2 && isspace(*arg2)) ++arg2;
	}

	if (strcmp(arg1, "list")==0)
	{
		dev=linphone_core_get_video_devices(lc);
		for(i=0; dev[i]!=NULL; ++i){
			linphonec_out("%i: %s\n",i,dev[i]);
		}
		return 1;
	}

	if (strcmp(arg1, "use")==0 && arg2)
	{
		dev=linphone_core_get_video_devices(lc);
		index=atoi(arg2); /* FIXME: handle not-a-number */
		for(i=0;dev[i]!=NULL;i++)
		{
			if (i!=index) continue;

			linphone_core_set_video_device(lc, dev[i]);
			linphonec_out("Using video device %s\n",dev[i]);
			return 1;
		}
		linphonec_out("No such video device\n");
		return 1;
	}
	return 0; /* syntax error */
}

static int
lpc_cmd_staticpic(LinphoneCore *lc, char *args)
{
	char *arg1 = args;
	char *arg2 = NULL;
	char *ptr = args;

	if (!args) return 0;  /* Syntax error */

	/* Isolate first and second arg */
	while(*ptr && !isspace(*ptr)) ++ptr;
	if ( *ptr )
	{
		*ptr='\0';
		arg2=ptr+1;
		while(*arg2 && isspace(*arg2)) ++arg2;
	}

	if (strcmp(arg1, "set")==0 && arg2) {
1433 1434
		linphone_core_set_static_picture(lc, arg2);
		return 1;
1435 1436
	}

1437 1438
	if (strcmp(arg1, "fps")==0) {
	  if (arg2) {
1439
	        float fps = (float)atof(arg2); /* FIXME: Handle not-a-float */
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
		linphone_core_set_static_picture_fps(lc, fps);
		return 1;
	  } else {
		float fps;
		fps = linphone_core_get_static_picture_fps(lc);
		linphonec_out("Current FPS %f\n", fps);
		return 1;
	  }
	}

1450 1451 1452
	return 0; /* Syntax error */
}

1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
static int lpc_cmd_pause(LinphoneCore *lc, char *args){

	if(linphone_core_in_call(lc))
	{
		linphone_core_pause_call(lc,linphone_core_get_current_call(lc));
		return 1;
	}
	linphonec_out("you can only pause when a call is in process\n");
    return 0;
}
1463

1464
static int lpc_cmd_resume(LinphoneCore *lc, char *args){
1465

1466 1467 1468
	if(linphone_core_in_call(lc))
	{
		linphonec_out("There is already a call in process pause or stop it first");
1469
		return 1;
1470 1471 1472
	}
	if (args)
	{
1473 1474 1475 1476 1477
		long id;
		int n = sscanf(args, "%li", &id);
		if (n == 1){
			LinphoneCall *call=linphonec_get_call (id);
			if (call){
1478
				if(linphone_core_resume_call(lc,call)==-1){
1479 1480
					linphonec_out("There was a problem to resume the call check the remote address you gave %s\n",args);
				}
1481
			}
1482
			return 1;
1483
		}else return 0;
1484 1485 1486
	}
	else
	{
1487 1488
		const bctbx_list_t *calls = linphone_core_get_calls(lc);
		int nbcalls=bctbx_list_size(calls);
1489
		if( nbcalls == 1)
1490 1491 1492
		{
			if(linphone_core_resume_call(lc,calls->data) < 0)
			{
1493
				linphonec_out("There was a problem to resume the unique call.\n");
1494
			}
1495 1496 1497 1498 1499 1500
			return 1;
		}else if (nbcalls==0){