zpaste

zpaste is a script that allows you to paste on rafb.net the content of a file or the content of the X11 clipboard.

zurlencode needs to be put into a seperate file when submitted to -workers!

#!/usr/bin/env zsh
local OLDIFS
OLDIFS=$IFS

PROG=$0
usage() {
	print "usage:\n$PROG [-f file] [-l lang] [-x]"
	print ""
	print -- "-l lang: \n\t available languages:  C, CPP, Csharp, Java, Pascal, Perl, PHP, Python, Ruby, SQL"
	print -- "-f file: \n\t paste content of file"
	print -- "-x: \n\t paste from X11 clipboard using xclip"
	print ""
	print "If no option is given $PROG will paste content read from STDIN"

	exit 1
}
zmodload -i zsh/net/tcp
zurlencode() {
	setopt localoptions extendedglob
	for line ($@) {
		input=( ${(s::)line} )
		URLREPLY+=(${(j::)input/(#b)([^A-Za-z0-9_.!~*\'\(\)-])/%${(l:2::0:)$(([##16]#match))}})
	}
	URLREPLY=${(j:%0D%0A:)URLREPLY}

}
if [ $# -ne 0 ];then
	case $1 in
		-*);;
		*) usage;;
	esac
fi
option_f=0
while getopts "hl:f:x" opt;do
	case $opt in 
		f)
                IFS=''
		zurlencode "${(f)"$(<$OPTARG)"}"
		option_f=1
                IFS=$OLDIFS
		;;
		l)
		case $OPTARG in
			C)LANGPASTE="C%20%28C99%29";;
			CPP)LANGPASTE="C%2b%2b";;
			Csharp)LANGPASTE="C%23";;
			Java)LANGPASTE="Java";;
			Pascal)LANGPASTE="Pascal";;
			Perl)LANGPASTE="Perl";;
			PHP)LANGPASTE="PHP";;
			Python)LANGPASTE="Python";;
			Ruby)LANGPASTE="Ruby";;
			SQL)LANGPASTE="SQL";;
			*)LANGPASTE="Plain%20Text";;
		esac
		;;
		x) 
		[[code:x_xclip]] || print "xclip not found" && exit 1 
                IFS=''
		INPUT=$(xclip -o 2>/dev/null)
                IFS=$OLDIFS
		zurlencode "$INPUT"
		;;
		h) usage
		;;
		*)
		usage
		;;
	esac
done
if [ $option_f -eq 1 ];then
	true
else
        IFS=''
	COMPLETEINPUT=()
	while read INPUT;do
		COMPLETEINPUT+=$INPUT
	done
        IFS=$OLDIFS
	zurlencode $COMPLETEINPUT
fi

autoload -U tcp_open
TCP_SILENT=1
print 'pasting, please wait..'
tcp_open -q rafb.net 80 rafb || return 2
url="lang=${LANGPASTE:=Plain%20Text}&text=$URLREPLY"
tcp_send -s rafb -- "POST /paste/paste.php HTTP/1.1"
tcp_send -s rafb -- "HOST: rafb.net"
tcp_send -s rafb -- "Accept-Charset: utf-8"
tcp_send -s rafb -- "Content-Type: application/x-www-form-urlencoded"
tcp_send -s rafb -- "Content-Length: $#url"
tcp_send -s rafb -- ""
tcp_send -s rafb -- "$url"
tcp_read -b -d -s rafb 
tcp_close -q -s rafb
print ${tcp_lines[5]/*Location: /}
 
code/scripts/zpaste.txt · Last modified: 2010/01/05 09:20 (external edit)