pstsrv: rewrite, drop ix.io, add uguu.se

This commit is contained in:
Lucas 2023-06-19 15:56:43 +00:00
parent 9f004d4faf
commit 432832cd5a

View File

@ -1,6 +1,6 @@
#!/bin/sh
# pstsrv
# Written in 2020 by Lucas
# Written in 2020-2021,2023 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
@ -12,7 +12,7 @@
usage()
{
printf "Usage: %s [-T] service [files...]\n" "${0##*/}" >&2
printf "Usage: %s [-x proxy] service file [file ...]\n" "${0##*/}" >&2
exit 1
}
@ -22,91 +22,85 @@ err()
exit 1
}
check_prog()
{
if ! command -v "$1" >/dev/null 2>&1; then
printf "%s: missing required program %s\n" \
"${0##*/}" "$1" >&2
return 1
fi
return 0
}
check_runtime()
{
rc=0
for prog; do
check_prog "$prog" || rc=1
_s=
for _prog; do
command -v "$_prog" >/dev/null 2>&1 || _s=${_s:+"$_s "}$_prog
done
return $rc
if [ -n "$_s" ]; then
err "missing required programs: $_s"
fi
}
pst_catbox_moe()
_curl()
{
curl $tor_flags -F "reqtype=fileupload" -F "fileToUpload=@$1" \
curl ${proxy:+-x "$proxy"} -Ss
}
impl_catbox_moe()
{
check_runtime curl
_curl -F "reqtype=fileupload" -F "fileToUpload=@$1" \
https://catbox.moe/user/api.php
}
pst_ix_io()
impl_pst_moe()
{
curl $tor_flags -F "f:1=@$1" http://ix.io/
check_runtime curl
_curl -F "content=<$1" -F "expire_after=86400" https://pst.moe/paste
}
pst_pst_moe()
impl_uguu()
{
curl $tor_flags -F "content=<$1" -F "expire_after=86400" \
https://pst.moe/paste
}
check_runtime curl jq
do_upload()
{
uploader=$1
shift
rc=0
if [ $# -eq 0 ]; then
$uploader - || rc=1
_jq_prog='
if .success then
(.files[] | [.url, .name] | @tsv)
else
for arg; do
$uploader "$arg" || rc=1
done
fi
"Error \(.errorcode): \(.description)"
end
'
return $rc
for _f; do
shift
set -- "$@" -F "files[]=@$_f"
done
_curl "$@" "https://$uguu_host/upload.php" | jq -r "$_jq_prog"
}
tor_flags="-x socks5h://localhost:9050"
while getopts T flag; do
proxy=
while getopts x: flag; do
case $flag in
T) tor_flags= ;;
x) proxy=$OPTARG ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
if [ $# -eq 0 ]; then
if [ $# -lt 2 ]; then
usage
fi
srv=$1
shift
check_runtime curl ${tor_flags:+tor}
url=
[ $# -eq 0 ] && set -- -
case $srv in
catbox.moe)
url=$(do_upload pst_catbox_moe "$@")
;;
ix.io)
url=$(do_upload pst_ix_io "$@")
impl=impl_catbox_moe
;;
pst.moe)
url=$(do_upload pst_pst_moe "$@")
impl=impl_pst_moe
;;
uguu.se|cockfile.com)
impl=impl_uguu uguu_host=$srv
;;
*)
err "unknown service $srv"
;;
esac
[ $? -eq 0 ] || err "Couldn't upload files"
printf "%s\n" "$url"
$impl "$@"