-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbr
executable file
·100 lines (80 loc) · 2.17 KB
/
pbr
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
#!/bin/sh
# [CMD] | pbr [FILE..] - send data to various paste services
#
# SPDX-License-Identifier: 0BSD
set -eu
: "${PBR_INDEX_FILE="${XDG_CACHE_HOME:-"$HOME/.cache"}/pbr/last_service_index"}"
: "${PBR_MODE:="round-robin"}"
error() {
printf 'ERROR: %s\n' "$@"
exit 1
}
store_index() {
index="$1"
mkdir -p "$(dirname "$PBR_INDEX_FILE")"
printf '%s' "$index" >"$PBR_INDEX_FILE"
}
# services
#
# all service wrapper functions expect exactly one argument, which needs to be
# a path. convention is to redirect stdin from file ( < ) to ensure reading
# from /dev/stdin works correctly
_curl() {
curl -s "$@"
}
dpaste_com() {
_curl -F "content=<-" https://dpaste.com/api/v2/ <"$1"
}
ix_io() {
_curl -F 'f:1=@-' ix.io <"$1"
}
paste_rs() {
_curl --data-binary '@-' https://paste.rs <"$1"
}
termbin_com() {
nc termbin.com 9999 <"$1"
}
ttm_sh() {
_curl -F "file=@-" https://ttm.sh <"$1"
}
OxO_st() {
_curl -F'file=@-' https://0x0.st <"$1"
}
available_services="dpaste_com ix_io paste_rs termbin_com ttm_sh OxO_st"
service_index_current=0
service_index_max="$(printf '%s' "$available_services" | wc -w)"
# determine the next service's index
get_current_service_index() {
case "$PBR_MODE" in
round-robin)
service_index_current="$(cat "$PBR_INDEX_FILE" 2>/dev/null || echo 0)"
service_index_current="$((service_index_current + 1))"
[ "$service_index_current" -le "$service_index_max" ] || service_index_current=1
store_index "$service_index_current"
;;
random)
service_index_current="$(shuf -i 1-"$service_index_max" -n1)"
;;
esac
printf '%s' "$service_index_current"
}
# return service wrapper function name from the list of available services
get_service_handler() {
echo "$available_services" | cut -d' ' -f"$(get_current_service_index)"
}
# pipe mode
if [ -p /dev/stdin ]; then
# technically, it's not wrong to read from stdin and from arguments at the same
# time, but this is smell we better warn about
[ "$#" -eq 0 ] || error "Do not mix pipes with args"
set -- /dev/stdin "$@"
fi
for f; do
[ -p "$f" ] && continue
[ -f "$f" ] || error "File not found: $f"
[ -s "$f" ] || error "File empty: $f"
done
for f; do
srv="$(get_service_handler)"
"$srv" "$f"
done