#!/bin/bash
# support - support helper

set -e

lib_load 'sys/pid'
lib_load 'net/email'
lib_load 'mcui'


######
# Config

sys_support_config_init() {
	lib_setting_vars support_tunnel_user
	lib_setting_vars support_tunnel_host
	lib_setting_vars --null support_tunnel_port
	lib_setting_vars -ro support_tunnel_keyfile
	lib_setting_vars -ro support_tunnel_logfile
	lib_setting_vars -ro support_tunnel_pidfile
}

sys_support_config_check() {
	:
}

######
# Support email functions

support_email() {
	[ "$user_name" ] || error "\$user_name is not set"
	[ "$user_email" ] || error "\$user_email is not set"

	local to from
	to="$package_support_name <$package_support_email>"
	from="$user_name <$user_email>"

	local draft
	draft="$script_tempdir/support.eml"
	file_mkdir "$draft"

	local subject body
	if [ -f "$draft" ]; then
		subject=$(email_header "$draft" subject | sed 's,^[^]]*\] ,,')
		body=$(email_content "$draft")
	fi

	local result
	local mcui_title
	local sent=false
	while ! $sent; do
		mcui_title="Email Support"
		mcui_input result "Support Email Subject:" "$subject"
		[ "$result" ] && subject=$result || break

		mcui_input_text result "Support Email Body:" "$body"
		[ "$result" ] && body=$result || break

		support_email_gen "$to" "$from" "$subject" "$body" >"$draft"

		mcui_text_box "$draft"

		mcui_yesnocancel_warn result "Send your message?"
		case "$result" in
		(cancel) break ;;
		(no) continue ;;
		(yes) ;;
		esac

		support_email_send "$draft"
		sent=true
	done

	if $sent; then
		app_echo "support email sent!"
		mcui_yesno result "Delete sent message?"
		if [ "$result" = yes ]; then
			run rm -f "$draft"
		fi
		return
	else
		warn "email aborted..."
		if [ -f "$draft" ]; then
			warn "draft saved in '$draft'"
		fi
	fi
}

support_email_gen() {
	local to=$1
	local from=$2
	local subject=$3
	local body=$4

	cat <<MAIL
To: $to
From: $from
Cc: $from
Subject: [$script_name email] $subject

$body

-- 
Sent using $(app_version).
MAIL
}

support_email_send() {
	local file=$1
	[ -f "$file" ] || error "'$file' does not exist"
	run sendmail "$package_support_email" <"$file"
}


######
# Support tunnel configuration and status

support_tunnel_config() {
	has_args 1 "$@"
	local keyfile=$1
	[ -f "$keyfile" ] || error "$keyfile: key file missing"

	local result
	if [ -f "$support_tunnel_keyfile" ]; then
		local msg="Support tunnel key exists.  Overwrite?"
		mcui_yesno_warn result "$msg"
		if [ "$result" != yes ]; then
			warn "aborting support tunnel configuration..."
			return
		fi
	fi

	run cp -f "$keyfile" "$support_tunnel_keyfile"
	run chmod 400 "$support_tunnel_keyfile"

	read_yesno_warn result "$keyfile: delete original?"
	[ "$result" != yes ] || run rm -v "$keyfile"
}

support_tunnel_running() { pid_exists "$support_tunnel_pidfile"; }


######
# Support tunnel management

support_tunnel_open() {
	! support_tunnel_running || error "support tunnel is open"
	[ "$support_tunnel_port" ] || error "\$support_tunnel_port is not set"

	local ssh_user="$support_tunnel_user"
	local ssh_host="$support_tunnel_host"
	local ssh_logfile=$support_tunnel_logfile
	local ssh_identity="$support_tunnel_keyfile"

	local ssh_no_login=true
	local ssh_no_stdin=true
	local ssh_background=true

	# forward port on the remote support server to the client's SSH service
	local -a ssh_remote_portmap
	ssh_remote_portmap=( "localhost:$support_tunnel_port:localhost:22" )

	support_tunnel_log "=== opening tunnel to $ssh_host ==="
	if run_ssh; then
		! $verbose || support_tunnel_log_show
		error "unable to open tunnel"
	else
		support_tunnel_log "=== tunnel opened ==="
	fi
	local pid="$!"
	disown

	pid_save "$pid" "$support_tunnel_pidfile"

}

support_tunnel_close() {
	support_tunnel_running || error "support tunnel is not running"

	support_tunnel_log "=== closing tunnel to $support_tunnel_host ==="
	pid_kill HUP "$support_tunnel_pidfile" true
	support_tunnel_log "=== support tunnel closed ==="
}

######
# Support tunnel log

support_tunnel_log() { $pretend || app_echo "$*" >>"$support_tunnel_logfile"; }
support_tunnel_log_show() { run_pager +G "$support_tunnel_logfile"; }
support_tunnel_log_reset() {
	$pretend || : >"$support_tunnel_logfile"

	local msg="=== support tunnel log reset on $(date +'%B %d, %Y') ==="
	support_tunnel_log "$msg"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:36:04 PDT 2017 by mcsh d14 v0.23.0.