#!/bin/sh #---------------------------------------------------- # Author: Florian "Bluewind" Pritz # Contributor: Moritz Wilhelmy # # Licensed under GPLv3 # (see COPYING for full license text) # #---------------------------------------------------- # Optional dependency: xclip #---------------------------------------------------- VERSION="@VERSION@" DELETE= EXTENSION="" GET= TAR= COMPRESS=0 DISPLAYHISTORY= PASTEBIN="http://paste.xinu.at" WARNSIZE=10485760 CLIPBOARD="" EXITCODE=0 LIBDIR="@LIBDIR@" require_executable() { if ! type $1 >/dev/null; then echo "Error: $1 not found. Please install." >&2 exit 1 fi } is_url() { if echo "$i" | grep -qE "^(f|ht)tp(s)?://.+"; then return 0 fi return 1 } do_tar_upload() { if [ "$COMPRESS" = "1" ]; then file="$TMPDIR/upload.tar.gz" tar -cf - -- "$@" | gzip -c > "$file" || return 1 elif [ "$COMPRESS" = "2" ]; then file="$TMPDIR/upload.tar.xz" tar -cf - -- "$@" | xz -c > "$file" || return 1 else file="$TMPDIR/upload.tar" tar -cf "$file" -- "$@" || return 1 fi COMPRESS=0 do_upload "$file" || return 1 } do_upload() { local EXTRA="" file="$1" basefilename="`basename -- "$file"`" basedirname="`dirname -- "$file"`" if [ ! -e "$file" ]; then echo "Error: File \"$file\" doesn't exist" >&2 return 1 fi if [ -d "$file" ]; then cd "$basedirname" if [ "$COMPRESS" = "1" ]; then file="$TMPDIR/$basefilename.tar.gz" tar -cf - -- "$basefilename" | gzip -c > "$file" || return 1 elif [ "$COMPRESS" = "2" ]; then file="$TMPDIR/$basefilename.tar.xz" tar -cf - -- "$basefilename" | xz -c > "$file" || return 1 else file="$TMPDIR/$basefilename.tar" tar -cf "$file" -- "$basefilename" || return 1 fi else if [ "$COMPRESS" = "1" ]; then gzip -c -- "$file" > "$TMPDIR/$basefilename.gz" || return 1 file="$TMPDIR/$basefilename.gz" elif [ "$COMPRESS" = "2" ]; then xz -c -- "$file" > "$TMPDIR/$basefilename.xz" || return 1 file="$TMPDIR/$basefilename.xz" fi fi TMPFILE=`mktemp "$TMPDIR/data.XXXXXX"` if [ `stat -c %s -- "$file"` -gt "$WARNSIZE" ]; then WARNSIZE=`$LIBDIR/fb-helper d "$PASTEBIN/file/get_max_size"` if [ `stat -c %s -- "$file"` -gt "$WARNSIZE" ]; then echo "Warning: Your upload is too big and would be rejected. Maximum size is: $WARNSIZE bytes. Skipping..." >&2 return 1 fi fi $LIBDIR/fb-helper u "$PASTEBIN/file/do_upload" "$file" > $TMPFILE || return 1 sed '$d' $TMPFILE >&2 URL=`tail -1 $TMPFILE`"$EXTENSION" echo $URL if echo $URL | grep -qE "^https?://"; then CLIPBOARD="$CLIPBOARD $URL" fi } read_stdin() { if tty -s; then echo "^C to exit, ^D to send" fi cat > "$1" } id_from_arg() { if echo "$1" | grep -qE "^https?://"; then echo "$1" | sed -r 's/https?:\/\/[^\/]+\/([^\/]+).*/\1/' else echo "$1" fi } help() { cat <] Upload/nopaste file(s)/stdin to paste.xinu.at and copy URL(s) to clipboard. ~/.netrc: machine paste.xinu.at password PASSWORD Switches: -d delete the IDs -g download the IDs and output on stdout (use with care!) -h this help -v show the client version -H display an upload history Options: -e extension for default highlighting (e.g. "diff") -t upload a tar file containing all files (and directories) -c compress the file being uploaded with gz or xz if used 2 times When used in conjunction with -g this decompresses the download ! } while getopts "e:gdhHtcv" OPTION; do case $OPTION in e) EXTENSION="$OPTARG";; g) GET=1;; c) COMPRESS=`expr $COMPRESS + 1`;; t) TAR=1;; d) DELETE=1;; H) DISPLAYHISTORY=1;; v) echo "$VERSION"; exit 0;; h|\?) help; exit 0;; esac done shift `expr $OPTIND - 1` if [ "$COMPRESS" = "1" ]; then require_executable gzip elif [ "$COMPRESS" = "2" ]; then require_executable xz fi TMPDIR="`mktemp -dt "fb.XXXXXX"`" trap "rm -rf '${TMPDIR}'" EXIT TERM if [ "$DELETE" ] || [ "$GET" ]; then if [ $# -eq 0 ]; then echo "Error: no ID specified" >&2 exit 1 fi for i in "$@"; do i=$(id_from_arg "$i") if [ "$DELETE" ]; then $LIBDIR/fb-helper d "$PASTEBIN/file/delete/$i" || EXITCODE=1 elif [ "$GET" ]; then if [ "$COMPRESS" = "1" ]; then $LIBDIR/fb-helper d "$PASTEBIN/$i" | gzip -cd || EXITCODE=1 elif [ "$COMPRESS" = "2" ]; then $LIBDIR/fb-helper d "$PASTEBIN/$i" | xz -cd || EXITCODE=1 else $LIBDIR/fb-helper d "$PASTEBIN/$i" || EXITCODE=1 fi fi done elif [ "$DISPLAYHISTORY" ]; then $LIBDIR/fb-helper d "$PASTEBIN/file/upload_history" || EXITCODE=1 elif [ $# -eq 0 ]; then if [ "$TAR" ]; then echo "Error: -t is not supported when operating on stdin" >&2 exit 1 fi read_stdin "$TMPDIR/stdin" do_upload "$TMPDIR/stdin" || EXITCODE=1 else if [ "$TAR" ]; then HAVE_URL= for i in "$@"; do if is_url "$i"; then HAVE_URL=1 fi done if [ "$HAVE_URL" ]; then # TODO: support -t when passing URLs as arguments echo "Error: -t is not yet supported when operating on a URL" >&2 exit 1 else do_tar_upload "$@" || EXITCODE=1 fi else for i in "$@"; do if is_url "$i"; then cd $TMPDIR if ! $LIBDIR/fb-helper d "$i" > "`basename "$i"`"; then EXITCODE=1 continue fi for f in *; do if ! do_upload "$f"; then EXITCODE=1 fi rm -f -- "$f" done else do_upload "$i" || EXITCODE=1 fi done fi fi if [ "$CLIPBOARD" != "" ]; then type xclip >/dev/null 2>&1 && echo -n $CLIPBOARD | nohup xclip >/dev/null 2>&1 fi exit $EXITCODE #vim: set noet: