ufw-tools/ufw

264 lines
5.2 KiB
Plaintext
Raw Normal View History

2014-10-04 08:19:37 +00:00
#!/usr/bin/env bash
2014-10-04 08:33:23 +00:00
# Copyright (c) 2012 fbt <fbt@fleshless.org>
#
# About:
# A simple upload script for ZFH (http://zerofiles.org)
2014-10-04 08:19:37 +00:00
_self="${0##*/}"
2015-10-13 10:52:26 +00:00
# Defaults for XDG
if ! [[ "$XDG_RUNTIME_DIR" ]]; then
XDG_RUNTIME_DIR="/run/user/$UID"
fi
2014-10-04 08:19:37 +00:00
cfg_url_regex='^[A-Za-z]([A-Za-z0-9+.-]+)?://.+'
2015-10-13 10:52:26 +00:00
cfg_tmp_dir="$XDG_RUNTIME_DIR/sup"; TEMPDIR="$cfg_tmp_dir"
2015-06-21 06:52:29 +00:00
cfg_service_url='https://zfh.so'
2014-10-04 08:19:37 +00:00
cfg_screenshot_ext='png'
[[ -f $HOME/.suprc ]] && { source "$HOME/.suprc"; }
2014-10-04 08:19:37 +00:00
2015-03-08 16:48:26 +00:00
_cat() {
while read; do
printf '%s\n' "$REPLY"
done
}
2015-10-13 10:52:26 +00:00
is_url() {
[[ "$1" =~ $cfg_url_regex ]]
}
sup.msg() { printf '%s\n' "[sup] $1"; }
2014-10-04 08:19:37 +00:00
sup.err() { sup.msg "(error) $1" >&2; }
sup.usage() {
2015-03-08 16:48:26 +00:00
_cat <<- EOF
Usage: ${_self} [-RsF] [-D num] [file/url]"
Flags:
-R # Remove the file after uploading.
-s # Make a screenshot and upload it instead of a file.
-F # Make a fullscreen shot instead of prompting for a window/area. Implies -s.
2015-06-23 20:25:48 +00:00
-d # Supply a dexcription.
2015-03-08 16:48:26 +00:00
-D <num> # Delay the shot by <num> seconds.
-p # Make the file private. Requires \$secret to be set in the config.
2015-06-21 13:05:12 +00:00
-u # Generate a shortlink from URL.
2015-03-08 16:48:26 +00:00
Config options (~/.suprc):
secret # Your personal token. Get it at https://zfh.so/settings_form
cfg_screenshot_ext # Screenshot file type, used by scrot.
# Others are self-explanatory:
cfg_url_regex
cfg_tmp_dir
2015-06-21 06:52:29 +00:00
cfg_service_url
2015-03-08 16:48:26 +00:00
EOF
2014-10-04 08:19:37 +00:00
}
sup.env() {
for i in "$cfg_tmp_dir"; do
[[ -d "$i" ]] || { mkdir -p "$i"; }
done
}
2015-06-21 06:52:29 +00:00
sup.get_hash() {
read file_hash _ < <( sha1sum "$1" )
}
2014-10-04 08:19:37 +00:00
sup.upload() {
2015-06-21 13:05:12 +00:00
if (( flag_scrot )); then
2015-10-13 10:57:56 +00:00
flag_rm=1
2015-06-21 13:05:12 +00:00
file=$(sup.scrot) || { return 1; }
else
(( $# )) || {
sup.usage
return 1
}
target="$1"
2015-10-13 10:52:26 +00:00
if is_url "$target"; then
2015-10-13 10:57:56 +00:00
flag_rm=1
2015-06-21 13:05:12 +00:00
file=$(sup.mktemp "$cfg_tmp_dir")
curl -skL "$target" > "$file" || {
sup.err "Could not download file."
return 1
}
else
file="$target"
[[ -f "$file" ]] || {
sup.err "No such file: ${file}"
return 1
}
fi
fi
2015-06-21 06:52:29 +00:00
sup.get_hash "$file"
file_url=$( curl -fsL "${cfg_service_url}/api?mode=file&file=${file_hash}&direct=1&short=${flag_shortlink}" )
2015-06-21 23:44:11 +00:00
if ! (( $? )); then
printf '%s\n' "$file_url"
2015-06-21 06:52:29 +00:00
else
curl -sL \
2015-06-21 06:52:29 +00:00
-F file="@$file" \
-F upload_mode='api' \
-F flag_private="$flag_private" \
-F short="$flag_shortlink" \
2015-06-23 20:25:48 +00:00
-F description="$description" \
2015-06-21 06:52:29 +00:00
-F secret="$secret" \
-F submit="" \
-A 'zerofiles.org upload script' \
"$cfg_service_url/upload"
fi
if (( $? )); then
sup.err 'Something has gone wrong with the upload.'
return 7
else
if (( flag_rm )); then
sup.msg "Removing file: $file"
rm "$file"
2015-06-21 13:05:12 +00:00
fi
fi
}
sup.url() {
declare url=$1
if ! curl -fsL "$cfg_service_url/api?mode=url_add&url=${url}&raw=1"; then
sup.err 'Something went wrong!'
2015-06-21 06:52:29 +00:00
fi
2014-10-04 08:19:37 +00:00
}
sup.mktemp() {
>"$1" || {
sup.err "Cannot create $1!"
return 1
}
printf '%s\n' "$1"
2014-10-04 08:19:37 +00:00
}
sup.scrot() {
2015-02-10 13:51:20 +00:00
declare tmp_file
2014-10-04 08:19:37 +00:00
[[ "$scrot_exec" ]] || { scrot_exec=$(type -P scrot); }
2014-10-04 08:19:37 +00:00
[[ "$scrot_exec" ]] || {
sup.err "Please install scrot to use this function"
return 1
}
2015-02-10 13:45:15 +00:00
(( "$flag_scrot_fullscreen" )) || { scrot_args+=( '-s' ); }
2014-10-04 08:19:37 +00:00
[[ "$cfg_scrot_delay" ]] && { scrot_args+=( "-d $cfg_scrot_delay" ); }
2014-10-04 08:50:49 +00:00
tmp_file="$(sup.mktemp "${cfg_tmp_dir}" ".${cfg_screenshot_ext}")"
2014-10-04 08:19:37 +00:00
2014-10-04 08:50:49 +00:00
scrot "${scrot_args[@]}" "$tmp_file" || {
2014-10-04 08:19:37 +00:00
sup.err "Failed to take a screenshot."
return 1
}
printf '%s\n' "$tmp_file"
2014-10-04 08:19:37 +00:00
}
2014-10-04 08:45:31 +00:00
sup.mktemp() {
2015-02-10 13:51:20 +00:00
declare tmp_file_name tmp_file_name_extra="$2" tmp_dir="$1"
2014-10-04 08:45:31 +00:00
[[ -d "$tmp_dir" ]] || {
sup.err "${tmp_dir} does not exist or is not a directory."
return 1
}
until [[ ! -e "${tmp_dir}/${tmp_file_name}" ]]; do
tmp_file_name="${RANDOM}${RANDOM}${tmp_file_name_extra}"
done
printf '%s\n' "${tmp_dir}/${tmp_file_name}"
2014-10-04 08:45:31 +00:00
}
2015-10-12 14:59:33 +00:00
sup.get_my_ip() {
my_ip=$( curl -fsL https://zfh.so/ip )
if ! (( $? )); then
printf '%s\n' $my_ip
fi
}
2015-02-20 08:23:26 +00:00
sup.set_argv() {
declare arg opt c
declare -g argv
2015-02-01 14:33:10 +00:00
while (( $# )); do
2015-02-20 08:23:26 +00:00
unset -v arg opt c
2015-02-01 14:33:10 +00:00
case "$1" in
2015-02-20 08:23:26 +00:00
(--) argv+=( "$1" ); break;;
2015-02-01 14:33:10 +00:00
(--*)
IFS='=' read arg opt <<< "$1"
2015-02-20 08:23:26 +00:00
argv+=( "$arg" )
2015-02-01 14:33:10 +00:00
[[ "$opt" ]] && {
2015-02-20 08:23:26 +00:00
argv+=( "$opt" )
2015-02-01 14:33:10 +00:00
}
2014-10-04 09:17:06 +00:00
;;
2015-02-01 14:33:10 +00:00
(-*)
while read -n1 c
do
case "$c" in
-|'') :;;
2015-02-20 08:23:26 +00:00
*) argv+=( "-$c" );;
2015-02-01 14:33:10 +00:00
esac
done <<< "$1"
;;
2015-02-20 08:23:26 +00:00
(*) argv+=( "$1" );;
2014-10-04 09:17:06 +00:00
esac
2015-02-01 14:33:10 +00:00
shift
2014-10-04 09:17:06 +00:00
done
}
2014-10-04 08:19:37 +00:00
main() {
2015-02-10 13:45:15 +00:00
declare args file target flag_private flag_rm flag_scrot flag_scrot_fullscreen cfg_scrot_delay
declare flag_shortlink=0
2014-10-04 09:17:06 +00:00
2014-10-04 08:19:37 +00:00
while [[ "$1" ]]; do
case "$1" in
-p|--private) flag_private='true';;
-R|--remove-file) flag_rm='1';;
-s|--screenshot) flag_scrot='1';;
2015-06-23 20:25:48 +00:00
-d|--description) description="$2"; shift;;
2014-10-04 08:19:37 +00:00
-D|--screenshot-delay) cfg_scrot_delay="$2"; shift;;
2015-03-08 16:48:26 +00:00
-F|--fullscreen)
flag_scrot='1'
flag_scrot_fullscreen='1'
;;
2015-06-21 13:05:12 +00:00
2015-10-12 14:59:33 +00:00
-i|--my-ip) action='getmyip';;
-S|--short) flag_shortlink='1';;
2015-06-21 13:05:12 +00:00
-u|--url) action='url';;
2014-10-04 08:33:23 +00:00
2014-10-04 08:19:37 +00:00
-h|--help|--usage) sup.usage; return;;
2015-02-10 13:45:15 +00:00
--) shift; break;;
2014-10-04 08:19:37 +00:00
2015-02-10 13:45:15 +00:00
*) break;;
2014-10-04 08:19:37 +00:00
esac
shift
done
sup.env
2015-06-21 13:05:12 +00:00
case "${action:-upload}" in
2015-06-21 23:29:57 +00:00
upload) sup.upload "$1";;
2015-06-21 13:05:12 +00:00
url) sup.url "$1";;
2015-10-12 14:59:33 +00:00
getmyip) sup.get_my_ip;;
2015-06-21 13:05:12 +00:00
esac
2014-10-04 08:19:37 +00:00
}
2015-02-20 08:23:26 +00:00
sup.set_argv "$@"
main "${argv[@]}"