# This file is part of bish-bosh. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/raphaelcohn/bish-bosh/master/COPYRIGHT. No part of bish-bosh, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of bish-bosh. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/raphaelcohn/bish-bosh/master/COPYRIGHT.


core_usesIn core functions
core_functions_register _bishbosh_backend_registration ncDebianTraditional

# Not installed by default on Ubuntu/Debian (the version by Hobbit, last updated 2004 on freecode, now points to GNU netcat)
bishbosh_backend_ncDebianTraditional_check()
{
	if [ "$bishbosh_tunnel" != 'none' ]; then
		return 1
	fi
	
	local nameVariant
	for nameVariant in nc.traditional netcat nc
	do
		if core_compatibility_whichNoOutput "$nameVariant"; then
			bishbosh_backend_name=ncDebianTraditional
			bishbosh_backend_path="$(core_compatibility_which "$nameVariant")"
			return 0
		fi
	done
	return 1
}

bishbosh_backend_ncDebianTraditional_checkIfNetcatVariant()
{
	# eg [v1.10-40] \n connect to somewhere:   nc [-options] hostname port[s] [ports] ...
	# Checking on a version number is a poor choice, but Hobbit's netcat is so old the minor version doesn't change [Debian just patches it]
	# The help message, BTW, is identical to cryptcat
	bishbosh_backend_netcatVariantHelpMessageLineExists '^\[v1\.10'
}

bishbosh_backend_ncDebianTraditional_port()
{
	printf '%s' 1883
}

# Almost identical to ncGNU
core_usesIn core variable variable/array
bishbosh_backend_ncDebianTraditional_start()
{
	local options
	local options_initialised
	core_variable_array_initialise options
	
	case "$bishbosh_transport" in
		
		inet)
			:
		;;
		
		inet4)
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncDebianTraditional does not support the 'inet4' transport explicitly."
		;;
		
		inet6)
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncDebianTraditional does not support the 'inet6' transport explicitly."
		;;
		
		unix)
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncDebianTraditional does not support the 'unix' transport."
		;;
		
		serial)
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncDebianTraditional does not support the 'serial' transport."
		;;
		
		*)
			core_exitError $core_commandLine_exitCode_SOFTWARE "Please validate the values for bishbosh_transport ('$bishbosh_transport')"
		;;
		
	esac
	
	if core_variable_isSet bishbosh_sourceAddress; then
		core_variable_array_append options '-s' "$bishbosh_sourceAddress"
	fi
	
	if core_variable_isSet bishbosh_sourcePort; then
		core_variable_array_append options '-p' "$bishbosh_sourcePort"
	fi
	
	case "$bishbosh_proxyKind" in
		
		SOCKS4)
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncDebianTraditional does not support the 'SOCKS4' bishbosh_proxyKind."
		;;
		
		SOCKS4a)
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncDebianTraditional does not support the 'SOCKS4a' bishbosh_proxyKind."
		;;
		
		SOCKS5)
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncDebianTraditional does not support the 'SOCKS5' bishbosh_proxyKind."
		;;
		
		HTTP)
			core_exitError $core_commandLine_exitCode_CONFIG "The backend ncDebianTraditional does not support the 'HTTP' bishbosh_proxyKind."
		;;
		
	esac
	
	if [ $bishbosh_connectTimeout -ne 0 ]; then
		core_variable_array_append options -w $bishbosh_connectTimeout
	fi
	
	case $(core_init_verbosity) in
		
		0)
			:
		;;
		
		1)
			core_variable_array_append options -v
		;;
		
		*)
			core_variable_array_append options -v -v
		;;
	esac

	core_variable_array_append options "$bishbosh_server"
		
	core_variable_array_append options "$bishbosh_port"
	
	bishbosh_backend_debugOptions
	
	# Problem: nc listens for SIGINT (even if we do trap '' INT), and so we can't stop Ctrl-C killing it before we've written a disconnect
	core_variable_array_passToFunctionAsArguments options "$bishbosh_backend_path" <"$bishbosh_connection_toServerFifo" >"$bishbosh_connection_fromServerFifo" &
}