#!/bin/sh
##########################################################################
# Shellscript:  postbordom.sh
# Version    :  0.00001
# Author     :  joeyo
# Date       :  27 Sept, 2005
#
##########################################################################
# Greetz     : #BoR especially oblio, who wrote 3ipost.sh
##########################################################################
# Requires
#	A text editor, awk, wc, and netcat.
##########################################################################

#### User Options ####
NICK=jar_jar
######################

StringInputed=$@

umask 022
TMPFL=/tmp/postbordom
CONTENTFL=$TMPFL-content$$
POSTFL=$TMPFL-post$$

trap 'rm -f $TMPFL-*; echo; exit 1' TERM INT

PN=`basename "$0"`
VER='0.00001'

: ${WC=wc}
: ${AWK=awk}
: ${NETCAT=nc}

Usage () {
    echo >&2 "$PN - bordom.net meme submitter, $VER
usage: $PN [ URI ]

A URI is required."
    exit 1
}

Msg () {
    for MsgLine
    do echo "$PN: $MsgLine" >&2
    done
}

Fatal () { Msg "$@"; exit 1; }

while [ $# -gt 0 ] # Print usage if any command line options are given.
do
    case "$1" in
	--)     shift; break;;
	-h)     Usage; Fatal;;
	-*)     Usage; Fatal;;
	*)	break;;
    esac
    shift
done

if [ "$#" == "0" ]; # Print usage if no options passed.
then Usage; Fatal;
fi

Encode () {
    $AWK '
        BEGIN {
	    # We assume an awk implementation that is just plain dumb.
	    # We will convert a character to its ASCII value with the
	    # table ord[], and produce two-digit hexadecimal output
	    # without the printf("%02X") feature

	    EOL = "%0D%0A"             # "end of line" string (encoded)
	    split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ")
	    hextab [0] = 0
	    for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
	    if ("'"$EncodeEOL"'" == "yes") EncodeEOL = 1; else EncodeEOL = 0
        }
        {
	    encoded = ""
	    for ( i=1; i<=length ($0); ++i ) {
		c = substr ($0, i, 1)
		if ( c ~ /[a-zA-Z0-9.-]/ ) {
		    encoded = encoded c             # safe character
	        } else if ( c == " " ) {
	            encoded = encoded "+"   # special handling
	        } else {
	            # unsafe character, encode it as a two-digit hex-number
	            lo = ord [c] % 16
		    hi = int (ord [c] / 16);
	    	    encoded = encoded "%" hextab [hi] hextab [lo]
	    	}
	    }
	    if ( EncodeEOL ) {
		printf ("%s", encoded EOL)
	    } else {
	        print encoded
	    }
	}
	END {
            #if ( EncodeEOL ) print ""
	}
    ' "$@"
}

EncodeEOL=no # URL / file path shouldn't have newlines... 
String=`echo $StringInputed | Encode;` # Encode string

echo nick=$NICK\&url=$StringInputed\&submit=Post+Item. >> $CONTENTFL # Create Content Field

ContentLength=`$WC -c $CONTENTFL | $AWK {'print $1'}` # Find Content-Length:

echo POST /submit/ HTTP/1.1 > $POSTFL
echo Referer: http://postbordom.sh/ >> $POSTFL
echo Connection: close >> $POSTFL
echo User-Agent: Mozilla/9.0 \(compatible\; joeyoRulez/3.14\; BASH\) >> $POSTFL
echo Host: bordom.net >> $POSTFL
echo Accept: text/\*, image/jpeg, image/png, image/\*, \*/\* >> $POSTFL
echo Accept-Encoding: gzip >> $POSTFL
echo Accept-Charset: iso-8859-1, utf-8, \* >> $POSTFL
echo Accept-Language: en >> $POSTFL
echo Content-Type: application/x-www-form-urlencoded >> $POSTFL
echo Content-Length: $ContentLength >> $POSTFL
echo >> $POSTFL
cat $CONTENTFL >> $POSTFL

if cat $POSTFL | $NETCAT bordom.net 80 > /dev/null;
then echo Posted.. n stuff. [\;;
else
    echo Houston, we have a problem.;
    echo See $TMPFL;
    Fatal;
fi

# Cleanin' up n stuff
rm -f $TMPFL-*
