summaryrefslogtreecommitdiffstats
path: root/fb
blob: 6b0eabd595f5017fe72252e0e25e9a0ec0970f55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/sh
#----------------------------------------------------
# Author:       Florian "Bluewind" Pritz <flo@xssn.at>
# Contributor:  Moritz Wilhelmy
#
# Licensed under WTFPL v2
#   (see COPYING for full license text)
#
#----------------------------------------------------
# Dependencies: curl
# Optional: xclip
#----------------------------------------------------

VERSION="0.6.7.1"

DELETE=
EXTENSION=""
GET=
PASTEBIN="http://paste.xinu.at"
WARNSIZE=10485760
USERAGENT="fb-client/$VERSION"
CLIPBOARD=""
EXITCODE=0

do_upload() {
  local EXTRA=""
  if [ "$EXTENSION" ]; then
    EXTRA="-F extension=$EXTENSION"
  fi
  TMPFILE=`mktemp "$TMPDIR/data.XXXXXX"`
  if [ `stat -c %s "$1"` -gt "$WARNSIZE" ]; then
    WARNSIZE=`curl -s "$PASTEBIN/file/get_max_size"`
    if [ `stat -c %s "$1"` -gt "$WARNSIZE" ]; then
      echo "Warning: Your upload is too big and would be rejected. Maximum size is: $WARNSIZE bytes. Skipping..." >&2
      EXITCODE=1
      return 1
    fi
  fi
  if ! curl -# -n -L -A $USERAGENT $EXTRA -F "file=@$1" "$PASTEBIN/file/do_upload" > $TMPFILE; then
    EXITCODE=1
    return 1
  fi
  sed '$d' $TMPFILE >&2
  URL=`tail -1 $TMPFILE`
  echo $URL
  CLIPBOARD="$CLIPBOARD $URL"
}

read_stdin() {
  if tty -s; then
    echo "^C to exit, ^D to send"
  fi
  cat > "$1"
}

help() {
  cat <<!
fb-client version $VERSION
usage: [cat |] `basename "$0"` [switches] [file(s)|ID(s)]

  Upload/nopaste file(s)/stdin to paste.xinu.at and copy URL(s) to clipboard.
  ~/.netrc: machine paste.xinu.at password PASSWORD

  Switches:
    -e EXTENSION   extension for default highlighting (e.g. "diff")
    -d             delete the IDs
    -g             download the IDs and output on stdout (use with care!)
    -h             this help
!
}

while getopts "e:gdh" OPTION; do
  case $OPTION in
    e) EXTENSION="$OPTARG";;
    g) GET=1;;
    d) DELETE=1;;
    h|\?) help; exit 0;;
  esac
done

shift `expr $OPTIND - 1`


TMPDIR="`mktemp -d "/tmp/fb.XXXXXX"`"
trap "rm -rf '${TMPDIR}'" EXIT TERM

if [ $# -eq 0 ]; then
  read_stdin "$TMPDIR/stdin"
  do_upload "$TMPDIR/stdin"
else
  for i in "$@"; do
    if [ "$DELETE" ]; then
      if ! curl -n -L -A $USERAGENT "$PASTEBIN/file/delete/$i"; then
        EXITCODE=1
      fi
    elif [ "$GET" ]; then
      if ! curl -s -o - -A $USERAGENT "$PASTEBIN/$i"; then
        EXITCODE=1
      fi
    elif echo "$i" | grep -qE "^(f|ht)tp(s)?://.+"; then
      cd $TMPDIR
      if ! curl -# -A $USERAGENT -O "$i"; then
        EXITCODE=1
        continue
      fi
      for f in *; do
        do_upload "$f" && rm -f "$f"
      done
    else
      do_upload "$i"
    fi
  done
fi

[ "`which xclip 2>/dev/null`" ] && echo -n $CLIPBOARD | nohup xclip >/dev/null 2>&1

exit $EXITCODE