2023-10-13 04:05:16 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# Paste69 CLI script
|
|
|
|
|
|
|
|
PASTE69_URL=${PASTE69_URL:-"https://0x45.st"}
|
|
|
|
|
|
|
|
# Check if `curl` is installed
|
|
|
|
if ! command -v curl &> /dev/null; then
|
|
|
|
echo "Error: curl is not installed"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if `jq` is installed
|
|
|
|
if ! command -v jq &> /dev/null; then
|
|
|
|
echo "Error: jq is not installed"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check for the presence of a clipboard program
|
|
|
|
cliptools=("xclip" "xsel" "pbcopy")
|
|
|
|
for tool in "${cliptools[@]}"; do
|
|
|
|
if command -v $tool &> /dev/null; then
|
|
|
|
case $tool in
|
|
|
|
xclip)
|
|
|
|
clipboard="xclip -selection clipboard"
|
|
|
|
;;
|
|
|
|
xsel)
|
|
|
|
clipboard="xsel --clipboard"
|
|
|
|
;;
|
|
|
|
pbcopy)
|
|
|
|
clipboard="pbcopy"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# Show help text
|
|
|
|
function show_help {
|
|
|
|
echo "Paste69 CLI script"
|
|
|
|
echo ""
|
|
|
|
echo "Usage:"
|
|
|
|
echo " paste69 <file> [options]"
|
|
|
|
echo " cat <file> | paste69 [options]"
|
|
|
|
echo ""
|
|
|
|
echo "Options:"
|
|
|
|
echo " -h, --help Show this help text."
|
|
|
|
echo " -l, --language <language> Set the language of the paste."
|
|
|
|
echo " -p, --password <password> Set a password for the paste. This enables encryption."
|
|
|
|
echo " -x, --burn Burn the paste after it is viewed once."
|
|
|
|
echo " -r, --raw Return the raw JSON response."
|
|
|
|
echo " -c, --copy Copy the paste URL to the clipboard."
|
|
|
|
}
|
|
|
|
|
|
|
|
burn=false
|
|
|
|
encrypt=false
|
2023-10-22 07:03:54 +00:00
|
|
|
raw=false
|
2023-10-13 04:05:16 +00:00
|
|
|
|
|
|
|
# Parse arguments
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
key="$1"
|
|
|
|
case $key in
|
|
|
|
-h|--help)
|
|
|
|
show_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-l|--language)
|
|
|
|
language="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-p|--password)
|
|
|
|
password="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-x|--burn)
|
|
|
|
burn=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-r|--raw)
|
|
|
|
raw=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-c|--copy)
|
|
|
|
copy=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
if [ -z "$file" ]; then
|
|
|
|
file=$1
|
|
|
|
extension="${file##*.}"
|
|
|
|
else
|
|
|
|
echo "Error: too many arguments"
|
|
|
|
show_help
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# Check if data or a file was provided, otherwise error out
|
|
|
|
if [ -z "$file" ] && ! [ -p /dev/stdin ]; then
|
|
|
|
echo "Error: no data or file provided"
|
|
|
|
show_help
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Build the URL
|
|
|
|
url="$PASTE69_URL/api/pastes"
|
|
|
|
|
|
|
|
# Make the request
|
|
|
|
if [ ! -z "$file" ]; then
|
|
|
|
contents=$(/usr/bin/cat $file)
|
|
|
|
else
|
|
|
|
data=$(/usr/bin/cat)
|
|
|
|
# Check if the data is too large
|
|
|
|
if [ ${#data} -gt 10000 ]; then
|
|
|
|
echo "Error: stdin input too large. Use a file instead."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
contents=$data
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If password is set, we need to set the `encrypt` flag
|
|
|
|
if [ ! -z "$password" ]; then
|
|
|
|
encrypt=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Create the JSON payload, and then clean it up
|
|
|
|
json=$(jq -n \
|
|
|
|
--arg contents "$contents" \
|
|
|
|
--arg extension "$extension" \
|
|
|
|
--arg language "$language" \
|
|
|
|
--arg password "$password" \
|
|
|
|
--argjson encrypt $encrypt \
|
|
|
|
--argjson burn $burn \
|
2023-10-22 07:03:54 +00:00
|
|
|
--argjson raw $raw \
|
|
|
|
'{"contents": $contents, "extension": $extension, "language": $language, "password": $password, "encrypt": $encrypt, "burnAfterReading": $burn, "raw": $raw}')
|
2023-10-13 04:05:16 +00:00
|
|
|
|
|
|
|
response=$(
|
|
|
|
curl -s -X POST $url \
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
-d "$json"
|
|
|
|
)
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "An error occurred while making the request"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if there is an error
|
2023-10-22 07:03:54 +00:00
|
|
|
error=$(echo "$response" | jq -r '.message' 2>/dev/null || true)
|
|
|
|
if [ -n "$error" ] && [ "$error" != "null" ]; then
|
2023-10-13 04:05:16 +00:00
|
|
|
echo "Error: $error"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-10-22 07:03:54 +00:00
|
|
|
paste_url=$response
|
|
|
|
if [ "$raw" = true ]; then
|
|
|
|
paste_url=$(echo $response | jq -r '.url')
|
2023-10-13 04:05:16 +00:00
|
|
|
echo $response
|
2023-10-22 07:03:54 +00:00
|
|
|
else
|
|
|
|
echo $paste_url
|
2023-10-13 04:05:16 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# If copy is set, copy the paste URL to the clipboard
|
|
|
|
if [ ! -z "$copy" ]; then
|
|
|
|
if [ ! -z "$clipboard" ]; then
|
|
|
|
echo $paste_url | $clipboard
|
|
|
|
else
|
|
|
|
echo "Error: the --copy flag requires a clipboard program to be installed"
|
|
|
|
echo "Install one of the following:"
|
|
|
|
for tool in "${cliptools[@]}"; do
|
|
|
|
echo " $tool"
|
|
|
|
done
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|