#!/bin/bash

Tor_enable(){
# Here we set the proxy address. Several applications use these environment
# variables already, so we don't need to set another one for those.

	export http_proxy="http://localhost:8118"
	export HTTP_PROXY="${http_proxy}"

# applications know to use these variables:
# gpg, wget, curl

# helper functions
#{{{ tor_resolve "hostname"
	tor_resolve() {
# This function first tries /etc/hosts for the name
# then tries tor. echos either the parameter or the IP address
# return value 0 for success (resolved or hosts), 1 otherwise
		if egrep -q "[[:blank:]]${1}|[[:blank:]]${1}[[:blank:]]" /etc/hosts ; then
			echo "${1}" 
			return 0
		else
			echo -n "Resolving ${1} through tor... " >&2
			ip="$( tor-resolve ${1} 2>/dev/null )"
			if [ -z "${ip}" ] ; then
				echo "FAILED! Passing ${1} to program" >&2
				echo ${1}
				return 1
			else
				echo ${ip} >&2
				echo ${ip}
				return 0
			fi
		fi
	}
#}}}

# irssi, a console IRC client
	alias irssi="torify irssi"

# subversion
	alias svn="torify svn"

# BitTorrent
	for x in bittorrent bittorrent-console bittorrent-curses launchmany-console launchmany-curses ; do
		alias ${x}="${x} --tracker_proxy '${http_proxy#http://}'"
	done

# {{{ ssh
	ssh() {
		unset cmd
		cmd="ssh"
		while [ -n "${1}" ] ; do
			if [ "${1:0:1}" == "-" ] ; then
				found=0
				for white in -1 -2 -4 -6 -A -a -C -f -g \
					-k -N -n -q -s -T -t -V -v -X -x \
					-Y \ ; do
					[ "${1}" == "${white}" ] && found=1
				done
				if [ "${found}" == "0" ] ; then
					cmd="${cmd} \"${1}\" \"${2}\""
					shift # the second shift happens below
				else
					cmd="${cmd} \"${1}\""
				fi
			else
				if [ "${1//@}" != "${1}" ] ; then
					cmd="${cmd} \"${1%%@*}@"
					ip="$(tor_resolve "${1##*@}")"
					cmd="${cmd}${ip}\""
				else
					ip="$(tor_resolve "${1}")"
					cmd="${cmd} \"${ip}\""
				fi
			fi
			shift
		done
		eval torify ${cmd}
	}
# }}}
# {{{ scp
	scp() {
		unset cmd
		cmd="scp"
		while [ -n "${1}" ] ; do
			if [ "${1:0:1}" == "-" ] ; then
				for white in -1 -2 -4 -6 -B -C -p -q -r -v \ ; do
					[ "${1}" == "${white}" ] && found=1
				done
				if [ "${found}" == "0" ] ; then
					cmd="${cmd} \"${1}\" \"${2}\""
					shift # the second shift happens below
				else
					cmd="${cmd} \"${1}\""
				fi
			else
				if [ "${1//:}" != "${1}" ] ; then
					cmd="${cmd} \""
					host="${1}"
					if [ "${1//@}" != "${1}" ] ; then
						cmd="${cmd}${1%%@*}@"
						host="${1#*@}"
					fi
					ip="$(tor_resolve "${host%%:*}")"
					cmd="${cmd}${ip}:${host#*:}\""
				else
					cmd="${cmd} \"${1}\""
				fi
			fi
			shift
		done
		eval torify ${cmd}
	}
# }}}
# {{{ telnet
	telnet() {
		unset cmd
		cmd="telnet"
		unset host
		while [ -n "${1}" ] ; do
			if [ "${1:0:1}" == "-" ] ; then
				found=0
				for white in -8 -E -L -a -d -r ; do
					[ "${1}" == "${white}" ] && found=1
				done
				if [ "${found}" == "0" ] ; then
					cmd="${cmd} \"${1}\" \"${2}\""
					shift # the second shift happens below
				else
					cmd="${cmd} \"${1}\""
				fi
			else
				if [ -z "${host}" ] ; then
					host="$(tor_resolve "${1}")"
					cmd="${cmd} \"${host}\""
				else
					cmd="${cmd} \"${1}\""
				fi
			fi
			shift
		done
		eval torify ${cmd}
	}
# }}}
}

Tor_disable(){
	unset http_proxy HTTP_PROXY
	unalias irssi svn bittorrent bittorrent-console bittorrent-curses launchmany-console launchmany-curses
	unset ssh scp telnet
}

Tor_enable
