0x0

on kitten's pastes | raw

expires: 19 Jun, 2024

  1#!/bin/sh
  2#
  3# The MIT License (MIT)
  4#
  5# Copyright (c) 2024 sewn <sewn@disroot.org>
  6#
  7# Permission is hereby granted, free of charge, to any person obtaining a copy
  8# of this software and associated documentation files (the "Software"), to deal
  9# in the Software without restriction, including without limitation the rights
 10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11# copies of the Software, and to permit persons to whom the Software is
 12# furnished to do so, subject to the following conditions:
 13#
 14# The above copyright notice and this permission notice shall be included in all
 15# copies or substantial portions of the Software.
 16#
 17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 23# SOFTWARE.
 24
 25set -eu
 26
 27storefile="${XDG_CACHE_HOME:-"$HOME/.cache"}/0x0"
 28url="https://0x0.st"
 29
 30usage() {
 31	echo "usage: ${0##*/} [-dls] [-e expire] [-t token] file|url" >&2
 32	exit "${1:-0}"
 33}
 34
 35while getopts dlse:t: arg; do case $arg in
 36	d) delete=1 ;;
 37	l) cat "$storefile"; exit 0 ;;
 38	s) secret=1 ;;
 39	e) expire="$OPTARG" ;;
 40	t) token="$OPTARG" ;;
 41	?) usage 1 ;;
 42	*) usage ;;
 43esac; done
 44shift "$((OPTIND-1))"
 45
 46[ "$#" = "1" ] || usage 1
 47
 48case "$1" in
 49	https://0x0.st/*) modify="$1" ;;
 50	http[s]://*) up=-Furl="$1" ;;
 51	*)
 52		up=-Ffile=@"$1"
 53		[ -t 0 ] && ! [ -r "$1" ] &&
 54			{ echo "file is unreadable" >&2; exit 1; }
 55	;;
 56esac
 57
 58[ ! "$delete" ] && [ ! "$expire" ] && [ ! "$up" ] &&
 59	{ echo "0x0 url given but no deletion/expiration update is asked" >&2; exit 1 ;}
 60[ "$token" ] && [ ! "$modify" ] && 
 61	{ echo "token given but no 0x0 url is given" >&2; exit 1 ;}
 62{ [ "$delete" ] || [ "$expire" ] ;} && {
 63	[ "$up" ] && { [ "$delete" ] && [ "$up" ] ;} &&
 64		{ echo "deletion is asked but an invalid url was given" >&2; exit 1 ;}
 65	[ ! "$up" ] && [ ! "$token" ] &&
 66		{ echo "token required for deletion/expiration update" >&2; usage 1; }
 67}
 68
 69# copy stdout to change curl's stdout to the copied stdout,
 70# and direct curl's stderr (which is where verbose info is sent,
 71# and is what is used to extract headers) to stdout,
 72# since while read reads only from stdout.
 73exec 3>&1
 74
 75# curl seems to add carriage return at the end
 76# of verbose statements, remove them
 77cr="$(printf '\r')"
 78
 79# shellcheck disable=SC2086
 80# ^ curl: blank argument where content is expected
 81while read -r i k v; do case "$i" in
 82	'*'|'>') ;;
 83	'<')
 84		case "$k" in
 85			"HTTP/2") status="${v%%"$cr"}" ;;
 86			"x-token:") xtoken="${v%%"$cr"}" ;;
 87			"x-expires:") xexpires="${v%%"$cr"}" ;;
 88		esac
 89	;;
 90	*) echo "$i $k $v" ;; # non-verbose line
 91esac; done <<-EOF
 92	$(curl -v $up \
 93		${secret:+-Fsecret=} \
 94		${delete:+-Fdelete=} \
 95		${token:+-Ftoken=$token} \
 96		${expire:+-Fexpires=$expire} \
 97		"$url${modify:+${modify##"$url"}}" 2>&1 >&3)
 98EOF
 99
100exec 3>&-
101
102# usually if 0x0 had an issue it'll just print it out
103# 'Segmentation fault is funnily enough sort of misleading
104[ ! "${status%%200*}" ]
105
106! { [ "$xtoken" ] && [ "$xexpires" ]; } || {
107	printf '%s %s\n' "$xexpires" "$xtoken" >> "$storefile"
108}
109