#!/bin/bash
####################################################################
# Prey Core Push Functions - by Tomas Pollak (bootlog.org)
# URL: http://preyproject.com
# License: GPLv3
####################################################################

update_device_info_with(){

	[ -z "$device_key" ] && return 1

	log ' -- Updating device info...'
	send_request "$check_url/devices/$device_key.xml" "--connect-timeout 10 -X PUT -u $api_key:x -d "$1""

	if [ "$response_status" == "200" ]; then
		log " -- Device updated."
	elif [ "$response_status" == "422" ]; then
		log " -- Couldn't update your device. Seems some data was missing or incorrectly sent."
	else
		log " -- Couldn't update your device. Got status code ${response_status}."
	fi

}

deactivate_modules_on_panel(){

	[ -z "$device_key" ] && return 1

	log " -- Deactivating module(s) ${1} on Control Panel..."

	local param_string=""
	until [ -z "$1" ]; do
		local param_string="${param_string}device[deactivate_modules][]=$1&"
		shift
	done

	update_device_info_with "$param_string"
}

send_report(){
	log ' -- Packing all gathered traces...'
	[ "$post_method" == "http" ] && trace_list=$(generate_query_string 'traces') || trace_list=$(generate_list 'traces')
	file_list=$(list_files)
	if [[ $trace_list || $file_list ]]; then
			if [ -n "$test_mode" ]; then
				log ' ** This is where the data gets sent. Not in test mode though!'
			else
				trace_file="$tmpdir/prey_traces.tmp"
				echo -e "$trace_list" > "$trace_file"
				log " -- Posting data via $post_method..."
				eval 'send_via_'"${post_method}"''
				local rs=$?
				rm -f "$trace_file" 2> /dev/null
			fi
		remove_traces
		remove_files
	else
		log " -- No data to send. It seems you didn't activate any report modules. Skipping..."
	fi
	return $rs
}

send_via_email(){

	local complete_subject="$mail_subject @ $(date +"%a, %e %B %Y %T %z")"
	local decrypted_pass=$(decrypt "$smtp_password")
	echo -e "${EMAIL_NOTICE}${EMAIL_HEADER}$(urldecode "$trace_list")${EMAIL_FOOTER}" > "$trace_file.msg"

	# only add user/pass if set
	if [ -n "$smtp_username" ]; then
		response=$(mailsender -f "$mail_from" -t "$mail_to" -u "$complete_subject" -s $smtp_server -a $file_list -o message-file="$trace_file.msg" tls=auto username="$smtp_username" password="$decrypted_pass")
	else
		response=$(mailsender -f "$mail_from" -t "$mail_to" -u "$complete_subject" -s $smtp_server -a $file_list -o message-file="$trace_file.msg" tls=auto)
	fi

	if [ -n "$(find_in "$response" 'ERROR')" ]; then
		log "$STRING_ERROR_EMAIL"
		log "\n This is the complete error message: \n $response\n"
	else
		log ' -- Report successfully sent! Check your inbox now.'
	fi

	rm "$trace_file.msg"
}

send_via_http(){
	if [ -z "$api_key" ]; then
		log ' -- API key not set! Cannot post data to server.'
		return 1
	else
		local args="-u $api_key:x"
	fi
	if [ -n $(find_in "$post_url" "https:") ]; then
		log " -- Using SSL encryption."
		[ -z $(find_in "$post_url" "$control_panel_url") ] && args="$args -k"
	fi
	send_request "$post_url" "$args -K "$trace_file" $file_list"
	log " -- $response_body"
}

send_via_scp(){
	if [[ -n "$scp_server" && -n "$scp_path" ]]; then
		log " -- Uploading the stuff to $scp_path in $scp_server..."
		local new_folder="prey_data_$(echo $start_time | sed 'y/ :/_-/')"
		ssh $scp_user@$scp_server mkdir $scp_path/$new_folder
		response=$(scp $ssh_options "$trace_file" $file_list $scp_user@$scp_server:$scp_path/$new_folder)
	else
		log ' !! You need to set up a server in order to send the report via SCP!'
	fi
}

# SFTP posting by http://github.com/birdtori
send_via_sftp(){
	if [[ -n "$sftp_server" && -n "$sftp_path" ]]; then
		local new_folder="prey_data_$(echo $start_time | sed 'y/ :/_-/')"
		log " -- Uploading the stuff to $sftp_path/$new_folder in $sftp_server..."
		local batch="$tmpdir/sftp.script"
		echo "mkdir $sftp_path/$new_folder"                           >  "$batch"
		echo "put "$trace_file" $sftp_path/$new_folder/"              >> "$batch"
		for f in $file_list; do echo "put $f $sftp_path/$new_folder/" >> "$batch"; done
		echo "bye"                                                    >> "$batch"
		response=$(sftp $ssh_options -b $batch $sftp_user@$sftp_server)
	else
		log ' !! You need to set up a server in order to send the report via SFTP!'
	fi
}
