commands.c 61.8 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 22
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 32
#include <unistd.h>
#endif /*_WIN32_WCE*/
aymeric's avatar
aymeric committed
33 34 35 36
#include <limits.h>
#include <ctype.h>
#include <linphonecore.h>
#include "linphonec.h"
37
#include "private.h"
38
#include "lpconfig.h"
aymeric's avatar
aymeric committed
39

40 41 42 43
#ifndef WIN32
#include <sys/wait.h>
#endif

44 45 46
#define AUDIO 0
#define VIDEO 1

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

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 *);
59
static int lpc_cmd_calls(LinphoneCore *, char *);
60
static int lpc_cmd_chat(LinphoneCore *, char *);
aymeric's avatar
aymeric committed
61
static int lpc_cmd_answer(LinphoneCore *, char *);
smorlat's avatar
smorlat committed
62
static int lpc_cmd_autoanswer(LinphoneCore *, char *);
aymeric's avatar
aymeric committed
63 64 65
static int lpc_cmd_terminate(LinphoneCore *, char *);
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_speak(LinphoneCore *lc, char *args);
83 84 85
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);
86
static int lpc_cmd_echocancellation(LinphoneCore *lc, char *args);
87 88
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
89 90 91
static int lpc_cmd_mute_mic(LinphoneCore *lc, char *args);
static int lpc_cmd_unmute_mic(LinphoneCore *lc, char *args);
static int lpc_cmd_rtp_no_xmit_on_audio_mute(LinphoneCore *lc, char *args);
92
static int lpc_cmd_video_window(LinphoneCore *lc, char *args);
93
static int lpc_cmd_states(LinphoneCore *lc, char *args);
smorlat's avatar
smorlat committed
94

aymeric's avatar
aymeric committed
95 96 97 98 99 100
/* 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
101
static void linphonec_proxy_show(LinphoneCore *lc,int index);
aymeric's avatar
aymeric committed
102 103 104 105
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);
106
#ifndef WIN32
aymeric's avatar
aymeric committed
107
static int linphonec_friend_add(LinphoneCore *lc, const char *name, const char *addr);
108
#endif
aymeric's avatar
aymeric committed
109
static int linphonec_friend_delete(LinphoneCore *lc, int num);
110
static int linphonec_friend_delete(LinphoneCore *lc, int num);
111 112 113
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);
aymeric's avatar
aymeric committed
114

smorlat's avatar
smorlat committed
115 116


aymeric's avatar
aymeric committed
117 118 119
/* Command table management */
static LPC_COMMAND *lpc_find_command(const char *name);

120 121
void linphonec_out(const char *fmt,...);

122
VideoParams lpc_video_params={-1,-1,-1,-1,TRUE};
123 124


aymeric's avatar
aymeric committed
125 126 127 128 129 130 131 132 133
/***************************************************************************
 *
 *  Global variables
 *
 ***************************************************************************/

/*
 * Commands table.
 */
134
static LPC_COMMAND commands[] = {
135
	{ "help", lpc_cmd_help, "Print commands help.",
136 137 138
		"'help <command>'\t: displays specific help for command.\n"
		"'help advanced'\t: shows advanced commands.\n"
	},
aymeric's avatar
aymeric committed
139
	{ "call", lpc_cmd_call, "Call a SIP uri",
140
		"'call <sip-url>' \t: initiate a call to the specified destination.\n"
141 142
		"'call show' \t: show all the current calls with their id and status.\n"
		},
143
	{ "calls", lpc_cmd_calls, "Show all the current calls with their id and status.",
144
		NULL
aymeric's avatar
aymeric committed
145
		},
146 147 148 149
	{ "chat", lpc_cmd_chat, "Chat with a SIP uri",
		"'chat <sip-url> \"message\"' "
		": send a chat message \"message\" to the specified destination."
		},
150 151
	{ "terminate", lpc_cmd_terminate, "Terminate a call",
		"'terminate' : Terminate the current call\n"
152
		"'terminate <call id>' : Terminate the call with supplied id\n"
153 154
		"'terminate <all>' : Terminate all the current calls\n"
		},
aymeric's avatar
aymeric committed
155
	{ "answer", lpc_cmd_answer, "Answer a call",
156
		"'answer' : Answer the current incoming call\n"
157
		"'answer <call id>' : Answer the call with given id\n"
aymeric's avatar
aymeric committed
158
	},
159 160 161 162 163 164 165 166 167 168 169
	{ "pause", lpc_cmd_pause, "pause a call",
		"'pause' : pause the current call\n"},
	{ "resume", lpc_cmd_resume, "resume a call",
		"'resume' : resume the unique call\n"
		"'resume <call id>' : hold off the call with given id\n"},
	{ "mute", lpc_cmd_mute_mic, 
	  "Mute microphone and suspend voice transmission."},
	{ "unmute", lpc_cmd_unmute_mic, 
		  "Unmute microphone and resume voice transmission."},
	{ "duration", lpc_cmd_duration, "Print duration in seconds of the last call.", NULL },
	
170 171
	{ "autoanswer", lpc_cmd_autoanswer, "Show/set auto-answer mode",
		"'autoanswer'       \t: show current autoanswer mode\n"
smorlat's avatar
smorlat committed
172 173
		"'autoanswer enable'\t: enable autoanswer mode\n"
		"'autoanswer disable'\t: disable autoanswer mode \n"},
aymeric's avatar
aymeric committed
174 175 176 177 178
	{ "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"
179
		"'proxy unuse' : don't use a default proxy.\n"
smorlat's avatar
smorlat committed
180 181
		"'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
182 183 184
	},
	{ "soundcard", lpc_cmd_soundcard, "Manage soundcards",
		"'soundcard list' : list all sound devices.\n"
185
		"'soundcard show' : show current sound devices configuration.\n"
aymeric's avatar
aymeric committed
186 187 188
		"'soundcard use <index>' : select a sound device.\n"
		"'soundcard use files' : use .wav files instead of soundcard\n"
	},
189 190 191 192
	{ "webcam", lpc_cmd_webcam, "Manage webcams",
		"'webcam list' : list all known devices.\n"
		"'webcam use <index>' : select a video device.\n"
	},
aymeric's avatar
aymeric committed
193 194 195 196 197
	{ "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."
	},
198 199 200 201
	{ "transfer", lpc_cmd_transfer,
		"Transfer a call to a specified destination.",
		"'transfer <sip-uri>' : transfers the current active call to the destination sip-uri"
		"'transfer <call id> <sip-uri>': transfers the call with 'id' to the destination sip-uri"
aymeric's avatar
aymeric committed
202 203 204 205 206 207 208
	},
	{ "nat", lpc_cmd_nat, "Set nat address",
		"'nat'        : show nat settings.\n"
		"'nat <addr>' : set nat address.\n"
	},
	{ "stun", lpc_cmd_stun, "Set stun server address",
		"'stun'        : show stun settings.\n"
strk's avatar
strk committed
209
		"'stun <addr>' : set stun server address.\n"
aymeric's avatar
aymeric committed
210
	},
strk's avatar
strk committed
211
	{ "firewall", lpc_cmd_firewall, "Set firewall policy",
aymeric's avatar
aymeric committed
212 213 214
		"'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
215
		"'firewall stun'   : use stun server given with the 'stun' command.\n"
aymeric's avatar
aymeric committed
216
	},
217
	{ "call-logs", lpc_cmd_call_logs, "Calls history", NULL },
aymeric's avatar
aymeric committed
218 219 220 221 222 223 224 225
	{ "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"
	},
226 227 228 229
	{ "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
230 231 232 233 234 235 236
		"'play <wav file>'    : play a wav file."
	},
	{ "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."
	},
	{ "quit", lpc_cmd_quit, "Exit linphonec", NULL },
237 238 239 240 241 242
	{ (char *)NULL, (lpc_cmd_handler)NULL, (char *)NULL, (char *)NULL }
};


static LPC_COMMAND advanced_commands[] = {
	 { "codec", lpc_cmd_acodec, "Audio codec configuration",
243 244 245 246 247 248 249
            "'codec list' : list audio codecs\n"
            "'codec enable <index>' : enable available audio codec\n"
            "'codec disable <index>' : disable audio codec" },
    { "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" },
250 251 252 253
	{ "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" },
Simon Morlat's avatar
Simon Morlat committed
254
	{ "nortp-on-audio-mute", lpc_cmd_rtp_no_xmit_on_audio_mute,
255 256 257
		  "Set the rtp_no_xmit_on_audio_mute configuration parameter",
		  "   If set to 1 then rtp transmission will be muted when\n"
		  "   audio is muted , otherwise rtp is always sent."}, 
258 259 260 261 262 263 264 265 266 267 268
	{ "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"
		"'vwindow size <width> <height>': Resizes video window"
	},
	{ "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"
	},
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	{ "register", lpc_cmd_register, "Register in one line to a proxy" , "register <sip identity> <sip proxy> <password>"},
	{ "unregister", lpc_cmd_unregister, "Unregister from default proxy", NULL	},
	{ "status", lpc_cmd_status, "Print various status information", 
			"'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" },
	{ "ports", lpc_cmd_ports, "Network ports configuration", 
			"'ports'  \t: prints current used ports.\n"
			"'ports sip <port number>'\t: Sets the sip port.\n" },
	{ "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"
	},
	{	NULL,NULL,NULL,NULL}
aymeric's avatar
aymeric committed
288 289
};

290 291


aymeric's avatar
aymeric committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
/***************************************************************************
 *
 *  Public interface 
 *
 ***************************************************************************/

/*
 * 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 == '*' )
		{
			linphone_core_send_dtmf(lc, *cl);
327
			linphone_core_play_dtmf (lc,*cl,100);
smorlat's avatar
smorlat committed
328
			ms_sleep(1); // be nice
aymeric's avatar
aymeric committed
329 330 331 332 333 334 335 336 337 338 339
			++cl;
		}

		// discard spurious trailing chars
		return 1;
	}

	/* Handle other kind of commands */
	cmd=lpc_find_command(cl);
	if ( !cmd )
	{
340
		linphonec_out("'%s': Cannot understand this.\n", cl);
aymeric's avatar
aymeric committed
341 342 343 344 345
		return 1;
	}

	if ( ! cmd->func(lc, args) )
	{
346
		linphonec_out("Syntax error.\n");
aymeric's avatar
aymeric committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
		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)
{
362
	static int index, len, adv;
aymeric's avatar
aymeric committed
363 364 365 366 367
	char *name;

	if ( ! state )
	{
		index=0;
368
		adv=0;
aymeric's avatar
aymeric committed
369 370 371 372 373 374
		len=strlen(text);
	}
	/*
 	 * Return the next name which partially matches
	 * from the commands list
	 */
375 376
	if (adv==0){
		while ((name=commands[index].name))
aymeric's avatar
aymeric committed
377
		{
378 379 380 381 382 383
			++index; /* so next call get next command */

			if (strncmp(name, text, len) == 0)
			{
				return ortp_strdup(name);
			}
aymeric's avatar
aymeric committed
384
		}
385 386
		adv=1;
		index=0;
aymeric's avatar
aymeric committed
387
	}
388 389 390 391
	if (adv==1){
		while ((name=advanced_commands[index].name))
		{
			++index; /* so next call get next command */
aymeric's avatar
aymeric committed
392

393 394 395 396 397 398
			if (strncmp(name, text, len) == 0)
			{
				return ortp_strdup(name);
			}
		}
	}
aymeric's avatar
aymeric committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
	return NULL;
}


/***************************************************************************
 *
 *  Command handlers 
 *
 ***************************************************************************/

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

	if (!arg || !*arg)
	{
417 418
		linphonec_out("Commands are:\n");
		linphonec_out("---------------------------\n");
aymeric's avatar
aymeric committed
419 420 421

		while (commands[i].help)
		{
422
			linphonec_out("%10.10s\t%s\n", commands[i].name,
aymeric's avatar
aymeric committed
423 424 425 426
				commands[i].help);
			i++;
		}
		
427
		linphonec_out("---------------------------\n");
428
		linphonec_out("Type 'help <command>' for more details or 'help advanced' to list additional commands.\n");
aymeric's avatar
aymeric committed
429 430 431 432

		return 1;
	}

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
	if (strcmp(arg,"advanced")==0){
		linphonec_out("Advanced commands are:\n");
		linphonec_out("---------------------------\n");
		i=0;
		while (advanced_commands[i].help)
		{
			linphonec_out("%10.10s\t%s\n", advanced_commands[i].name,
				advanced_commands[i].help);
			i++;
		}
		
		linphonec_out("---------------------------\n");
		linphonec_out("Type 'help <command>' for more details.\n");

		return 1;
	}
	
aymeric's avatar
aymeric committed
450 451 452
	cmd=lpc_find_command(arg);
	if ( !cmd )
	{
453
		linphonec_out("No such command.\n");
aymeric's avatar
aymeric committed
454 455 456 457 458 459 460 461
		return 1;
	}

	linphonec_display_command_help(cmd);
	return 1;

}

smorlat's avatar
smorlat committed
462
static char callee_name[256]={0};
463
static char caller_name[256]={0};
smorlat's avatar
smorlat committed
464

465 466 467 468 469 470 471 472 473
static const char *get_call_status(LinphoneCall *call){
	switch(linphone_call_get_state(call)){
		case LinphoneCallPaused:
			if (linphone_call_get_refer_to (call)!=NULL){
				return "Paused (transfered)";
			}else{
				return "Paused";
			}
		break;
474 475 476
		case LinphoneCallPausedByRemote:
			return "Paused by remote";
		break;
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
		case LinphoneCallIncomingReceived:
			return "Pending";
		break;
		case LinphoneCallOutgoingInit:
		case LinphoneCallOutgoingProgress:
			return "Dialing out";
		break;
		case LinphoneCallOutgoingEarlyMedia:
		case LinphoneCallOutgoingRinging:
			return "Remote ringing";
		break;
		default:
			if (linphone_call_has_transfer_pending(call)){
				return "Running (transfer pending)";
			}else
				return "Running";
	}
	return "";
}

aymeric's avatar
aymeric committed
497 498 499 500 501 502 503 504
static int
lpc_cmd_call(LinphoneCore *lc, char *args)
{
	if ( ! args || ! *args )
	{
		return 0;
	}
	{
505
		LinphoneCall *call;
506 507 508 509 510
		if ( linphone_core_in_call(lc) )
		{
			linphonec_out("Terminate or hold on the current call first.\n");
			return 1;
		}
511
		if ( NULL == (call=linphone_core_invite(lc, args)) )
aymeric's avatar
aymeric committed
512
		{
513
			linphonec_out("Error from linphone_core_invite.\n");
aymeric's avatar
aymeric committed
514 515 516
		}
		else
		{
smorlat's avatar
smorlat committed
517
			snprintf(callee_name,sizeof(callee_name),"%s",args);
aymeric's avatar
aymeric committed
518 519 520 521 522
		}
	}
	return 1;
}

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
static int 
lpc_cmd_calls(LinphoneCore *lc, char *args){
	const MSList *calls = linphone_core_get_calls(lc);
	if(calls)
	{
		const MSList *p_calls = calls;
		linphonec_out("ID\t\tDestination\t\t\t\tStatus\n---------------------------------------------------------------------\n");
		while(p_calls != NULL)			
		{
			LinphoneCall *call=(LinphoneCall*)p_calls->data;
			char *tmp=linphone_call_get_remote_address_as_string(call);
			linphonec_out("%li\t%s\t\t\t%s\r\n",
						  (long)linphone_call_get_user_pointer (call),
					tmp,
					get_call_status(call));
			p_calls = p_calls->next;
			ms_free(tmp);
		}
	}else
	{
		linphonec_out("No active call.\n");
	}
	return 1;
}


549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
static int
lpc_cmd_chat(LinphoneCore *lc, char *args)
{
	char *arg1 = args;
	char *arg2 = NULL;
	char *ptr = args;

	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;
	}
	LinphoneChatRoom *cr = linphone_core_create_chat_room(lc,arg1);
	linphone_chat_room_send_message(cr,arg2);
	linphone_chat_room_destroy(cr);

	return 1;
}

578
const char *linphonec_get_callee(){
smorlat's avatar
smorlat committed
579 580 581
	return callee_name;
}

582 583 584 585 586 587 588 589
const char *linphonec_get_caller(){
	return caller_name;
}

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

aymeric's avatar
aymeric committed
590
static int
591
lpc_cmd_transfer(LinphoneCore *lc, char *args)
aymeric's avatar
aymeric committed
592
{
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
	if (args){
		LinphoneCall *call;
		const char *refer_to=NULL;
		char arg1[256]={0};
		char arg2[266]={0};
		int n=sscanf(args,"%s %s",arg1,arg2);
		if (n==1 || isalpha(*arg1)){
			call=linphone_core_get_current_call(lc);
			if (call==NULL && linphone_core_get_calls_nb (lc)==1){
				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;
			}
		}else{
			long id=atoi(arg1);
			refer_to=args+strlen(arg1)+1;
			call=linphonec_get_call(id);
			if (call==NULL) return 0;
		}
		linphone_core_transfer_call(lc, call, refer_to);
	}else{
		linphonec_out("Transfer command requires at least one argument\n");
		return 0;
aymeric's avatar
aymeric committed
619 620 621 622 623 624 625
	}
	return 1;
}

static int
lpc_cmd_terminate(LinphoneCore *lc, char *args)
{
626 627 628 629
	if (linphone_core_get_calls(lc)==NULL){
		linphonec_out("No active calls");
		return 1;
	}
630
	if (!args)
aymeric's avatar
aymeric committed
631
	{
632 633
		if ( -1 == linphone_core_terminate_call(lc, NULL) ){
			linphonec_out("Could not stop the active call.\n");
634 635
		}
		return 1;
aymeric's avatar
aymeric committed
636
	}
637
	
638 639 640 641 642 643 644 645 646 647 648
	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){
				linphonec_out("Could not stop the call with id %li",id);
649
			}
650 651
		}else return 0;
		return 1;
652 653 654
	}
	return 0;
	
aymeric's avatar
aymeric committed
655 656 657
}

static int
658
lpc_cmd_answer(LinphoneCore *lc, char *args){
659
	if (!args)
aymeric's avatar
aymeric committed
660
	{
661 662 663 664
		int nb=ms_list_size(linphone_core_get_calls(lc));
		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) )
665
			{
666
				linphonec_out("Fail to accept incoming call\n");
667
			}
668 669 670 671 672
		}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;
673
		}
674 675 676 677 678 679 680 681 682 683
		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;
684 685
	}
	return 0;
aymeric's avatar
aymeric committed
686 687
}

smorlat's avatar
smorlat committed
688 689 690
static int
lpc_cmd_autoanswer(LinphoneCore *lc, char *args)
{
691 692 693 694 695 696 697 698 699 700
	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
701 702
	if (strstr(args,"enable")){
		linphonec_set_autoanswer(TRUE);
smorlat's avatar
smorlat committed
703
		linphonec_out("Auto answer enabled.\n");
smorlat's avatar
smorlat committed
704 705
	}else if (strstr(args,"disable")){
		linphonec_set_autoanswer(FALSE);
smorlat's avatar
smorlat committed
706
		linphonec_out("Auto answer disabled.\n");
smorlat's avatar
smorlat committed
707 708 709 710
	}else return 0;
	return 1;
}

aymeric's avatar
aymeric committed
711 712 713
static int
lpc_cmd_quit(LinphoneCore *lc, char *args)
{
714
	linphonec_main_loop_exit();
aymeric's avatar
aymeric committed
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
	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
733
	use = linphone_core_get_firewall_policy(lc)==LinphonePolicyUseNatAddress;
734
	linphonec_out("Nat address: %s%s\n", nat ? nat : "unspecified" , use ? "" : " (disabled - use 'firewall nat' to enable)");
aymeric's avatar
aymeric committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

	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
754
	use = linphone_core_get_firewall_policy(lc)==LinphonePolicyUseStun;
755
	linphonec_out("Stun server: %s%s\n", stun ? stun : "unspecified" , use? "" : " (disabled - use 'firewall stun' to enable)");
aymeric's avatar
aymeric committed
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

	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
771
			linphone_core_set_firewall_policy(lc,LinphonePolicyNoFirewall);
aymeric's avatar
aymeric committed
772 773 774 775 776 777
		}
		else if (strcmp(args,"stun")==0)
		{
			setting = linphone_core_get_stun_server(lc);
			if ( ! setting )
			{
778
				linphonec_out("No stun server address is defined, use 'stun <address>' first\n");
aymeric's avatar
aymeric committed
779 780
				return 1;
			}
Simon Morlat's avatar
Simon Morlat committed
781
			linphone_core_set_firewall_policy(lc,LinphonePolicyUseStun);
aymeric's avatar
aymeric committed
782 783 784 785 786 787
		}
		else if (strcmp(args,"nat")==0)
		{
			setting = linphone_core_get_nat_address(lc);
			if ( ! setting )
			{
788
				linphonec_out("No nat address is defined, use 'nat <address>' first");
aymeric's avatar
aymeric committed
789 790
				return 1;
			}
Simon Morlat's avatar
Simon Morlat committed
791
			linphone_core_set_firewall_policy(lc,LinphonePolicyUseNatAddress);
aymeric's avatar
aymeric committed
792 793 794 795 796
		}
	}

	switch(linphone_core_get_firewall_policy(lc))
	{
Simon Morlat's avatar
Simon Morlat committed
797
		case LinphonePolicyNoFirewall:
798
			linphonec_out("No firewall\n");
aymeric's avatar
aymeric committed
799
			break;
Simon Morlat's avatar
Simon Morlat committed
800
		case LinphonePolicyUseStun:
801
			linphonec_out("Using stun server %s to discover firewall address\n", setting ? setting : linphone_core_get_stun_server(lc));
aymeric's avatar
aymeric committed
802
			break;
Simon Morlat's avatar
Simon Morlat committed
803
		case LinphonePolicyUseNatAddress:
804
			linphonec_out("Using supplied nat address %s.\n", setting ? setting : linphone_core_get_nat_address(lc));
aymeric's avatar
aymeric committed
805 806 807 808 809
			break;
	}
	return 1;
}

smorlat's avatar
smorlat committed
810
#ifndef WIN32
aymeric's avatar
aymeric committed
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
/* 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, " ");
		
		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
840
#endif
aymeric's avatar
aymeric committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860

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);
Jehan Monnier's avatar
Jehan Monnier committed
861
#ifndef _WIN32_WCE		
aymeric's avatar
aymeric committed
862
		if ( errno == ERANGE ) {
863
			linphonec_out("Invalid friend number\n");
aymeric's avatar
aymeric committed
864 865
			return 0;
		}
Jehan Monnier's avatar
Jehan Monnier committed
866
#endif /*_WIN32_WCE*/
aymeric's avatar
aymeric committed
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
		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;
		} 
		else
		{
			friend_num = strtol(args, NULL, 10);
Jehan Monnier's avatar
Jehan Monnier committed
883
#ifndef _WIN32_WCE		
aymeric's avatar
aymeric committed
884
			if ( errno == ERANGE ) {
885
				linphonec_out("Invalid friend number\n");
aymeric's avatar
aymeric committed
886 887
				return 0;
			}
Jehan Monnier's avatar
Jehan Monnier committed
888
#endif /*_WIN32_WCE*/
aymeric's avatar
aymeric committed
889 890 891 892 893 894
		}
		linphonec_friend_delete(lc, friend_num);
		return 1;
	}
	else if ( !strncmp(args, "add", 3) )
	{
smorlat's avatar
smorlat committed
895
#ifndef WIN32
aymeric's avatar
aymeric committed
896 897 898 899 900 901 902 903 904
		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
905

aymeric's avatar
aymeric committed
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
		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
921 922 923 924 925
#else
		LinphoneFriend *new_friend;
		new_friend = linphone_friend_new_with_addr(args);
		linphone_core_add_friend(lc, new_friend);
#endif
aymeric's avatar
aymeric committed
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
		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)
	{
969
#ifdef HAVE_READLINE
aymeric's avatar
aymeric committed
970
		rl_inhibit_completion=1;
971
#endif
aymeric's avatar
aymeric committed
972
		linphonec_proxy_add(lc);
973
#ifdef HAVE_READLINE
aymeric's avatar
aymeric committed
974
		rl_inhibit_completion=0;
975
#endif
aymeric's avatar
aymeric committed
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
	}
	else if (strcmp(arg1,"list")==0)
	{
		linphonec_proxy_list(lc);
	}
	else if (strcmp(arg1,"remove")==0)
	{
		linphonec_proxy_remove(lc,atoi(arg2));
	}
	else if (strcmp(arg1,"use")==0)
	{
		if ( arg2 && *arg2 )
		{
			proxynum=atoi(arg2);
			if ( linphonec_proxy_use(lc, proxynum) )
991
				linphonec_out("Default proxy set to %d.\n", proxynum);
aymeric's avatar
aymeric committed
992 993 994 995
		}
		else
		{
			proxynum=linphone_core_get_default_proxy(lc, NULL);
996 997
			if ( proxynum == -1 ) linphonec_out("No default proxy.\n");
			else linphonec_out("Current default proxy is %d.\n", proxynum);
aymeric's avatar
aymeric committed
998
		}
smorlat's avatar
smorlat committed
999
	}else if (strcmp(arg1, "unuse")==0){
aymeric's avatar
aymeric committed
1000
		linphone_core_set_default_proxy(lc, NULL);
1001
		linphonec_out("Use no proxy.\n");
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
	}

	else if (strcmp(arg1, "show")==0)
	{
		if (arg2 && *arg2)
		{
			if (strstr(arg2,"default"))
			{
		proxynum=linphone_core_get_default_proxy(lc, NULL);
		if ( proxynum < 0 ) {
			linphonec_out("No default proxy defined\n");
			return 1;
smorlat's avatar
smorlat committed
1014
		}
1015 1016 1017 1018 1019 1020 1021
		linphonec_proxy_show(lc,proxynum);
			}
			else
			{
		linphonec_proxy_show(lc, atoi(arg2));
			}
		}
1022
		else return 0; /* syntax error */
1023 1024 1025
	}

	else
aymeric's avatar
aymeric committed
1026
	{
1027
		return 0; /* syntax error */
aymeric's avatar
aymeric committed
1028 1029 1030 1031 1032 1033 1034 1035
	}

	return 1;
}

static int
lpc_cmd_call_logs(LinphoneCore *lc, char *args)
{
1036
	const MSList *elem=linphone_core_get_call_logs(lc);
aymeric's avatar
aymeric committed
1037 1038 1039 1040
	for (;elem!=NULL;elem=ms_list_next(elem))
	{
		LinphoneCallLog *cl=(LinphoneCallLog*)elem->data;
		char *str=linphone_call_log_to_str(cl);
1041
		linphonec_out("%s\n",str);
aymeric's avatar
aymeric committed
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
		ms_free(str);
	}
	return 1;
}

static int
lpc_cmd_ipv6(LinphoneCore *lc, char *arg1)
{
	if ( ! arg1 )
	{
1052
		return 0; /* syntax error */
aymeric's avatar
aymeric committed
1053 1054 1055 1056
	}

	if (strcmp(arg1,"status")==0)
	{
1057
		linphonec_out("ipv6 use enabled: %s\n",linphone_core_ipv6_enabled(lc) ? "true":"false");
aymeric's avatar
aymeric committed
1058 1059 1060 1061
	}
	else if (strcmp(arg1,"enable")==0)
	{
		linphone_core_enable_ipv6(lc,TRUE);
1062
		linphonec_out("ipv6 use enabled.\n");
aymeric's avatar
aymeric committed
1063 1064 1065 1066
	}
	else if (strcmp(arg1,"disable")==0)
	{
		linphone_core_enable_ipv6(lc,FALSE);
1067
		linphonec_out("ipv6 use disabled.\n");
aymeric's avatar
aymeric committed
1068 1069 1070
	}
	else
	{
1071
		return 0; /* syntax error */
aymeric's avatar
aymeric committed
1072 1073 1074 1075
	}
	return 1;
}

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
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;
}

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
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
1111
	}
1112 1113 1114 1115 1116

	if (strcmp(arg1, "list")==0)
	{
		dev=linphone_core_get_sound_devices(lc);
		for(i=0; dev[i]!=NULL; ++i){
1117
			linphonec_out("%i: %s\n",i,dev[i]);
aymeric's avatar
aymeric committed
1118 1119
		}
		return 1;
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
	}

	if (strcmp(arg1, "show")==0)
	{
		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));
		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
1140
		}
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155

		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
1156
	}
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
	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;
	}
1211
	return 0; /* syntax error */
aymeric's avatar
aymeric committed
1212 1213
}

1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
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) {
1279 1280
		linphone_core_set_static_picture(lc, arg2);
		return 1;
1281 1282
	}

1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
	if (strcmp(arg1, "fps")==0) {
	  if (arg2) {
	        float fps = atof(arg2); /* FIXME: Handle not-a-float */
		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;
	  }
	}

1296 1297 1298
	return 0; /* Syntax error */
}

1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
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;
}
1309

1310 1311 1312 1313 1314
static int lpc_cmd_resume(LinphoneCore *lc, char *args){
	
	if(linphone_core_in_call(lc))
	{
		linphonec_out("There is already a call in process pause or stop it first");
1315
		return 1;
1316 1317 1318
	}
	if (args)
	{
1319 1320 1321 1322 1323
		long id;
		int n = sscanf(args, "%li", &id);
		if (n == 1){
			LinphoneCall *call=linphonec_get_call (id);
			if (call){
1324
				if(linphone_core_resume_call(lc,call)==-1){
1325 1326
					linphonec_out("There was a problem to resume the call check the remote address you gave %s\n",args);
				}
1327
			}
1328
			return 1;
1329
		}else return 0;
1330 1331 1332
	}
	else
	{
Simon Morlat's avatar
Simon Morlat committed
1333
		const MSList *calls = linphone_core_get_calls(lc);
1334 1335
		int nbcalls=ms_list_size(calls);
		if( nbcalls == 1)
1336 1337 1338
		{
			if(linphone_core_resume_call(lc,calls->data) < 0)
			{
1339
				linphonec_out("There was a problem to resume the unique call.\n");
1340
			}
1341 1342 1343 1344 1345 1346
			return 1;
		}else if (nbcalls==0){
			linphonec_out("There is no calls at this time.\n");
			return 1;
		}else{
			linphonec_out("There are %i calls at this time, please specify call id as given with 'calls' command.\n");
1347 1348 1349 1350 1351
		}
	}
	return 0;
    
}
1352

aymeric's avatar
aymeric committed
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
/***************************************************************************
 *
 *  Commands helper functions
 *
 ***************************************************************************/


static void
linphonec_proxy_add(LinphoneCore *lc)
{
	bool_t enable_register=FALSE;
	LinphoneProxyConfig *cfg;

1366
	linphonec_out("Adding new proxy setup. Hit ^D to abort.\n");
aymeric's avatar
aymeric committed
1367 1368 1369 1370 1371 1372

	/*
	 * SIP Proxy address
	 */
	while (1)
	{
1373
		char *input=linphonec_readline("Enter proxy sip address: ");
aymeric's avatar
aymeric committed
1374 1375 1376
		char *clean;

		if ( ! input ) {
1377
			linphonec_out("Aborted.\n");
aymeric's avatar
aymeric committed
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
			return;
		}

		/* Strip blanks */
		clean=lpc_strip_blanks(input);
		if ( ! *clean ) {
			free(input);
			continue;
		}

		cfg=linphone_proxy_config_new();
		if (linphone_proxy_config_set_server_addr(cfg,clean)<0)
		{
1391
			linphonec_out("Invalid sip address (sip:sip.domain.tld).\n");
aymeric's avatar
aymeric committed
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
			free(input);
			linphone_proxy_config_destroy(cfg);
			continue;
		}
		free(input);
		break;
	}

	/*
	 * SIP Proxy identity
	 */
	while (1)
	{
1405
		char *input=linphonec_readline("Your identity for this proxy: ");
aymeric's avatar
aymeric committed
1406 1407 1408
		char *clean;

		if ( ! input ) {
1409
			linphonec_out("Aborted.\n");
aymeric's avatar
aymeric committed
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
			linphone_proxy_config_destroy(cfg);
			return;
		}

		/* Strip blanks */
		clean=lpc_strip_blanks(input);
		if ( ! *clean ) {
			free(input);
			continue;
		}

		linphone_proxy_config_set_identity(cfg, clean);
		if ( ! cfg->reg_identity )
		{
1424
			linphonec_out("Invalid identity (sip:name@sip.domain.tld).\n");
aymeric's avatar
aymeric committed
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
			free(input);
			continue;
		}
		free(input);
		break;
	}

	/*
	 * SIP Proxy enable register
	 */
	while (1)
	{
1437
		char *input=linphonec_readline("Do you want to register on this proxy (yes/no): ");
aymeric's avatar
aymeric committed
1438 1439 1440
		char *clean;

		if ( ! input ) {
1441
			linphonec_out("Aborted.\n");
aymeric's avatar
aymeric committed
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
			linphone_proxy_config_destroy(cfg);
			return;
		}

		/* Strip blanks */
		clean=lpc_strip_blanks(input);
		if ( ! *clean ) {
			free(input);
			continue;
		}

		if ( ! strcmp(clean, "yes") ) enable_register=TRUE;
		else if ( ! strcmp(clean, "no") ) enable_register=FALSE;
		else {
1456
			linphonec_out("Please answer with 'yes' or 'no'\n");
aymeric's avatar
aymeric committed
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
			free(input);
			continue;
		}
		linphone_proxy_config_enableregister(cfg, enable_register);
		free(input);
		break;
	}

	/*
	 * SIP Proxy registration expiration
	 */
	if ( enable_register==TRUE )
	{
		long int expires=0;
		while (1)
		{
1473
			char *input=linphonec_readline("Specify register expiration time"
aymeric's avatar
aymeric committed
1474 1475 1476
				" in seconds (default is 600): ");

			if ( ! input ) {
1477
				linphonec_out("Aborted.\n");
aymeric's avatar
aymeric committed
1478 1479 1480 1481 1482 1483 1484
				linphone_proxy_config_destroy(cfg);
				return;
			}

			expires=strtol(input, (char **)NULL, 10);
			if ( expires == LONG_MIN || expires == LONG_MAX )
			{
1485
				linphonec_out("Invalid value: %s\n", strerror(errno));
aymeric's avatar
aymeric committed
1486 1487 1488 1489 1490
				free(input);
				continue;
			}

			linphone_proxy_config_expires(cfg, expires);
1491
			linphonec_out("Expiration: %d seconds\n", cfg->expires);
aymeric's avatar
aymeric committed
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502

			free(input);
			break;
		}
	}

	/*
	 * SIP proxy route
	 */
	while (1)
	{
1503
		char *input=linphonec_readline("Specify route if needed: ");
aymeric's avatar
aymeric committed
1504 1505 1506
		char *clean;

		if ( ! input ) {
1507
			linphonec_out("Aborted.\n");
aymeric's avatar
aymeric committed
1508 1509 1510 1511 1512 1513 1514 1515
			linphone_proxy_config_destroy(cfg);
			return;
		}

		/* Strip blanks */
		clean=lpc_strip_blanks(input);
		if ( ! *clean ) {
			free(input);
1516
			linphonec_out("No route specified.\n");
aymeric's avatar
aymeric committed
1517 1518 1519 1520 1521 1522
			break;
		}

		linphone_proxy_config_set_route(cfg, clean);
		if ( ! cfg->reg_route )
		{
1523
			linphonec_out("Invalid route.\n");
aymeric's avatar
aymeric committed
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
			free(input);
			continue;
		}

		free(input);
		break;
	}

	/*
	 * Final confirmation 
	 */
	while (1)
	{
		char *input;
		char *clean;

1540
		linphonec_out("--------------------------------------------\n");
aymeric's avatar
aymeric committed
1541
		linphonec_proxy_display(cfg);
1542
		linphonec_out("--------------------------------------------\n");
1543
		input=linphonec_readline("Accept the above proxy configuration (yes/no) ?: ");
aymeric's avatar
aymeric committed
1544 1545 1546


		if ( ! input ) {
1547
			linphonec_out("Aborted.\n");
aymeric's avatar
aymeric committed
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
			linphone_proxy_config_destroy(cfg);
			return;
		}

		/* Strip blanks */
		clean=lpc_strip_blanks(input);
		if ( ! *clean ) {
			free(input);
			continue;
		}

		if ( ! strcmp(clean, "yes") ) break;
		else if ( ! strcmp(clean, "no") )
		{
1562
			linphonec_out("Declined.\n");
aymeric's avatar
aymeric committed
1563 1564 1565 1566 1567
			linphone_proxy_config_destroy(cfg);
			free(input);
			return;
		}

1568
		linphonec_out("Please answer with 'yes' or 'no'\n");
aymeric's avatar
aymeric committed
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
		free(input);
		continue;
	}


	linphone_core_add_proxy_config(lc,cfg);

	/* automatically set the last entered proxy as the default one */
	linphone_core_set_default_proxy(lc,cfg);

1579
	linphonec_out("Proxy added.\n");
aymeric's avatar
aymeric committed
1580 1581 1582 1583 1584
}

static void
linphonec_proxy_display(LinphoneProxyConfig *cfg)
{
smorlat's avatar
smorlat committed
1585
	linphonec_out("sip address: %s\nroute: %s\nidentity: %s\nregister: %s\nexpires: %i\nregistered: %s\n",
aymeric's avatar
aymeric committed
1586 1587 1588 1589
			cfg->reg_proxy,
			(cfg->reg_route!=NULL)?cfg->reg_route:"",
			(cfg->reg_identity!=NULL)?cfg->reg_identity:"",
			(cfg->reg_sendregister)?"yes":"no",
smorlat's avatar
smorlat committed
1590 1591 1592 1593
			cfg->expires,
			linphone_proxy_config_is_registered(cfg) ? "yes" : "no");
}

1594 1595
static void linphonec_proxy_show(LinphoneCore *lc, int index)
{
smorlat's avatar
smorlat committed
1596 1597 1598 1599 1600 1601 1602 1603 1604
	const MSList *elem;
	int i;
	for(elem=linphone_core_get_proxy_config_list(lc),i=0;elem!=NULL;elem=elem->next,++i){
		if (index==i){
			LinphoneProxyConfig *cfg=(LinphoneProxyConfig *)elem->data;
			linphonec_proxy_display(cfg);
			return;
		}
	}
1605
	linphonec_out("No proxy with index %i\n", index);
aymeric's avatar
aymeric committed
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
}

static void
linphonec_proxy_list(LinphoneCore *lc)
{
	const MSList *proxies;
	int n;
	int def=linphone_core_get_default_proxy(lc,NULL);
	
	proxies=linphone_core_get_proxy_config_list(lc);
	for(n=0;proxies!=NULL;proxies=ms_list_next(proxies),n++){
		if (n==def)
1618
			linphonec_out("****** Proxy %i - this is the default one - *******\n",n);
aymeric's avatar
aymeric committed
1619
		else 
1620
			linphonec_out("****** Proxy %i *******\n",n);
aymeric's avatar
aymeric committed
1621 1622
		linphonec_proxy_display((LinphoneProxyConfig*)proxies->data);
	}
1623
	if ( ! n ) linphonec_out("No proxies defined\n");
aymeric's avatar
aymeric committed
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
}

static void
linphonec_proxy_remove(LinphoneCore *lc, int index)
{
	const MSList *proxies;
	LinphoneProxyConfig *cfg;
	proxies=linphone_core_get_proxy_config_list(lc);
	cfg=(LinphoneProxyConfig*)ms_list_nth_data(proxies,index);
	if (cfg==NULL){
1634
		linphonec_out("No such proxy.\n");
aymeric's avatar
aymeric committed
1635 1636 1637
		return;
	}
	linphone_core_remove_proxy_config(lc,cfg);
1638
	linphonec_out("Proxy %s removed.\n", cfg->reg_proxy);
aymeric's avatar
aymeric committed
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
	linphone_proxy_config_destroy(cfg);
}

static int
linphonec_proxy_use(LinphoneCore *lc, int index)
{
	const MSList *proxies;
	LinphoneProxyConfig *cfg;
	proxies=linphone_core_get_proxy_config_list(lc);
	cfg=(LinphoneProxyConfig*)ms_list_nth_data(proxies,index);
	if (cfg==NULL){
1650
		linphonec_out("No such proxy (try 'proxy list').");
aymeric's avatar
aymeric committed
1651 1652 1653 1654 1655 1656 1657 1658 1659
		return 0;
	}
	linphone_core_set_default_proxy(lc,cfg);
	return 1;
}

static void
linphonec_friend_display(LinphoneFriend *fr)
{
1660
	LinphoneAddress *uri=linphone_address_clone(linphone_friend_get_address(fr));
1661 1662
	char *str;
	
1663 1664 1665
	linphonec_out("name: %s\n", linphone_address_get_display_name(uri));
	linphone_address_set_display_name(uri,NULL);
	str=linphone_address_as_string(uri);
1666
	linphonec_out("address: %s\n", str);
aymeric's avatar
aymeric committed
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
}

static int
linphonec_friend_list(LinphoneCore *lc, char *pat)
{
	const MSList *friend;
	int n;

	if (pat) {
		pat=lpc_strip_blanks(pat);
		if (!*pat) pat = NULL;
	}

	friend = linphone_core_get_friend_list(lc);
	for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
	{
		if ( pat ) {
1684
			const char *name = linphone_address_get_display_name(
1685
			    linphone_friend_get_address((LinphoneFriend*)friend->data));
1686
			if (name && ! strstr(name, pat) ) continue;
aymeric's avatar
aymeric committed
1687
		}
1688
		linphonec_out("****** Friend %i *******\n",n);
aymeric's avatar
aymeric committed
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
		linphonec_friend_display((LinphoneFriend*)friend->data);
	}

	return 1;
}

static int
linphonec_friend_call(LinphoneCore *lc, unsigned int num)
{
	const MSList *friend = linphone_core_get_friend_list(lc);
	unsigned int n;
	char *addr;

	for(n=0; friend!=NULL; friend=ms_list_next(friend), ++n )
	{
		if ( n == num )
		{
1706
			int ret;
1707
			addr = linphone_address_as_string(linphone_friend_get_address((LinphoneFriend*)friend->data));
1708 1709 1710
			ret=lpc_cmd_call(lc, addr);
			ms_free(addr);
			return ret;
aymeric's avatar
aymeric committed
1711 1712
		}
	}
1713
	linphonec_out("No such friend %u\n", num);
aymeric's avatar
aymeric committed
1714