- #!/usr/bin/env bash
- # shellcheck shell=bash
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ##@Version : 202208141743-git
- # @@Author : Jason Hempstead
- # @@Contact : jason@casjaysdev.com
- # @@License : WTFPL
- # @@ReadME : pb --help
- # @@Copyright : Copyright: (c) 2022 Jason Hempstead, Casjays Developments
- # @@Created : Sunday, Aug 14, 2022 17:43 EDT
- # @@File : pb
- # @@Description : Send output to a pastebin
- # @@Changelog : New script
- # @@TODO : Better documentation
- # @@Other :
- # @@Resource :
- # @@Terminal App : no
- # @@sudo/root : no
- # @@Template : bash/system
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- APPNAME="$(basename "$0" 2>/dev/null)"
- VERSION="202208141743-git"
- HOME="${USER_HOME:-$HOME}"
- USER="${SUDO_USER:-$USER}"
- RUN_USER="${SUDO_USER:-$USER}"
- SCRIPT_SRC_DIR="${BASH_SOURCE%/*}"
- PB_REQUIRE_SUDO="${PB_REQUIRE_SUDO:-no}"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Reopen in a terminal
- #if [ ! -t 0 ] && { [ "$1" = --term ] || [ $# = 0 ]; }; then { [ "$1" = --term ] && shift 1 || true; } && TERMINAL_APP="TRUE" myterminal -e "$APPNAME $*" && exit || exit 1; fi
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Initial debugging
- [ "$1" = "--debug" ] && set -x && export SCRIPT_OPTS="--debug" && export _DEBUG="on"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Disables colorization
- [ "$1" = "--raw" ] && export SHOW_RAW="true"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # pipes fail
- set -o pipefail
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Import functions
- CASJAYSDEVDIR="${CASJAYSDEVDIR:-/usr/local/share/CasjaysDev/scripts}"
- SCRIPTSFUNCTDIR="${CASJAYSDEVDIR:-/usr/local/share/CasjaysDev/scripts}/functions"
- SCRIPTSFUNCTFILE="${SCRIPTSAPPFUNCTFILE:-testing.bash}"
- SCRIPTSFUNCTURL="${SCRIPTSAPPFUNCTURL:-https://github.com/dfmgr/installer/raw/main/functions}"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- if [ -f "$PWD/$SCRIPTSFUNCTFILE" ]; then
- . "$PWD/$SCRIPTSFUNCTFILE"
- elif [ -f "$SCRIPTSFUNCTDIR/$SCRIPTSFUNCTFILE" ]; then
- . "$SCRIPTSFUNCTDIR/$SCRIPTSFUNCTFILE"
- else
- echo "Can not load the functions file: $SCRIPTSFUNCTDIR/$SCRIPTSFUNCTFILE" 1>&2
- exit 1
- fi
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Options are: *_install
- # desktopmgr devenvmgr dfmgr dockermgr fontmgr iconmgr
- # pkmgr system systemmgr thememgr user wallpapermgr
- user_install && __options "$@"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Send all output to /dev/null
- __devnull() {
- tee &>/dev/null && exitCode=0 || exitCode=1
- return ${exitCode:-$?}
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
- # Send errors to /dev/null
- __devnull2() {
- [ -n "$1" ] && local cmd="$1" && shift 1 || return 1
- eval $cmd "$*" 2>/dev/null && exitCode=0 || exitCode=1
- return ${exitCode:-$?}
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
- # See if the executable exists
- __cmd_exists() {
- exitCode=0
- [ -n "$1" ] && local exitCode="" || return 0
- for cmd in "$@"; do
- builtin command -v "$cmd" &>/dev/null && exitCode+=$(($exitCode + 0)) || exitCode+=$(($exitCode + 1))
- done
- [ $exitCode -eq 0 ] || exitCode=3
- return ${exitCode:-$?}
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Check for a valid internet connection
- __am_i_online() {
- local exitCode=0
- curl -q -LSsfI --max-time 1 --retry 0 "${1:-http://1.1.1.1}" 2>&1 | grep -qi 'server:.*cloudflare' || exitCode=4
- return ${exitCode:-$?}
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # colorization
- if [ "$SHOW_RAW" = "true" ]; then
- printf_color() { printf '%b' "$1" | tr -d '\t' | sed '/^%b$/d;s,\x1B\[ 0-9;]*[a-zA-Z],,g'; }
- else
- printf_color() { printf "%b" "$(tput setaf "${2:-7}" 2>/dev/null)" "$1" "$(tput sgr0 2>/dev/null)"; }
- fi
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Additional printf_ colors
- __printf_head() { printf_blue "$1"; }
- __printf_opts() { printf_purple "$1"; }
- __printf_line() { printf_cyan "$1"; }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # output version
- __version() { printf_cyan "$VERSION"; }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # list options
- __list_options() {
- printf_color "$1: " "$5"
- echo -ne "$2" | sed 's|:||g;s/'$3'/ '$4'/g'
- printf_newline
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # create the config file
- __gen_config() {
- local NOTIFY_CLIENT_NAME="$APPNAME"
- if [ "$INIT_CONFIG" != "TRUE" ]; then
- printf_blue "Generating the config file in"
- printf_cyan "$PB_CONFIG_DIR/$PB_CONFIG_FILE"
- fi
- [ -d "$PB_CONFIG_DIR" ] || mkdir -p "$PB_CONFIG_DIR"
- [ -d "$PB_CONFIG_BACKUP_DIR" ] || mkdir -p "$PB_CONFIG_BACKUP_DIR"
- [ -f "$PB_CONFIG_DIR/$PB_CONFIG_FILE" ] &&
- cp -Rf "$PB_CONFIG_DIR/$PB_CONFIG_FILE" "$PB_CONFIG_BACKUP_DIR/$PB_CONFIG_FILE.$$"
- cat <<EOF >"$PB_CONFIG_DIR/$PB_CONFIG_FILE"
- # Settings for pb
- PB_SAVED_FILE="${PB_SAVED_FILE:-}"
- PB_PRIVATE_URL="${PRIVATE_PASTEBIN_URL:-}"
- PB_PRIVATE_API_URL="${PRIVATE_PASTEBIN_API_URL:-}"
- PB_PRIVATE_API_KEY="${PRIVATE_PASTEBIN_API_KEY:-}"
- PB_PASTEBIN_COM_API_DEV="${PB_PASTEBIN_COM_API_DEV:-}"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Color Settings
- PB_OUTPUT_COLOR_1="${PB_OUTPUT_COLOR_1:-}"
- PB_OUTPUT_COLOR_2="${PB_OUTPUT_COLOR_2:-}"
- PB_OUTPUT_COLOR_GOOD="${PB_OUTPUT_COLOR_GOOD:-}"
- PB_OUTPUT_COLOR_ERROR="${PB_OUTPUT_COLOR_ERROR:-}"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Notification Settings
- PB_NOTIFY_ENABLED="${PB_NOTIFY_ENABLED:-}"
- PB_GOOD_NAME="${PB_GOOD_NAME:-}"
- PB_ERROR_NAME="${PB_ERROR_NAME:-}"
- PB_GOOD_MESSAGE="${PB_GOOD_MESSAGE:-}"
- PB_ERROR_MESSAGE="${PB_ERROR_MESSAGE:-}"
- PB_NOTIFY_CLIENT_NAME="${PB_NOTIFY_CLIENT_NAME:-}"
- PB_NOTIFY_CLIENT_ICON="${PB_NOTIFY_CLIENT_ICON:-}"
- PB_NOTIFY_CLIENT_URGENCY="${PB_NOTIFY_CLIENT_URGENCY:-}"
- EOF
- if builtin type -t __gen_config_local | grep -q 'function'; then __gen_config_local; fi
- if [ -f "$PB_CONFIG_DIR/$PB_CONFIG_FILE" ]; then
- [ "$INIT_CONFIG" = "TRUE" ] || printf_green "Your config file for $APPNAME has been created"
- . "$PB_CONFIG_DIR/$PB_CONFIG_FILE"
- exitCode=0
- else
- printf_red "Failed to create the config file"
- exitCode=1
- fi
- return ${exitCode:-$?}
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Help function - Align to 50
- __help() {
- __printf_head "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
- __printf_opts "pb: Send output to a pastebin - $VERSION"
- __printf_head "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
- __printf_line "Usage: pb [options] [commands]"
- __printf_line " - "
- __printf_line " - "
- __printf_line "--dir - Sets the working directory"
- __printf_head "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
- __printf_opts "Other Options"
- __printf_head "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
- __printf_line "--help - Shows this message"
- __printf_line "--config - Generate user config file"
- __printf_line "--version - Show script version"
- __printf_line "--options - Shows all available options"
- __printf_line "--debug - Enables script debugging"
- __printf_line "--raw - Removes all formatting on output"
- __printf_head "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Is current user root
- __user_is_root() {
- { [ $(id -u) -eq 0 ] || [ $EUID -eq 0 ] || [ "$WHOAMI" = "root" ]; } && return 0 || return 1
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Is current user not root
- __user_is_not_root() {
- { [ $(id -u) -ne 0 ] || [ $EUID -ne 0 ] || [ "$WHOAMI" != "root" ]; } && return 0 || return 1
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Check if user is a member of sudo
- __sudo_group() {
- grep -s "${1:-$USER}" "/etc/group" | grep -Eq 'wheel|adm|sudo' || return 1
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # # Get sudo password
- __sudoask() {
- ask_for_password sudo true && return 0 || return 1
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Run sudo
- __sudorun() {
- __sudoif && __cmd_exists sudo && sudo -HE "$@" || { __sudoif && eval "$@"; }
- return $?
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Test if user has access to sudo
- __can_i_sudo() {
- (sudo -vn && sudo -ln) 2>&1 | grep -vq 'may not' >/dev/null && return 0
- __sudo_group "${1:-$USER}" || __sudoif || __sudo true &>/dev/null || return 1
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # User can run sudo
- __sudoif() {
- __user_is_root && return 0
- __can_i_sudo "${RUN_USER:-$USER}" && return 0
- __user_is_not_root && __sudoask && return 0 || return 1
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Run command as root
- requiresudo() {
- if [ "$PB_REQUIRE_SUDO" = "yes" ] && [ -z "$PB_REQUIRE_SUDO_RUN" ]; then
- export PB_REQUIRE_SUDO="no"
- export PB_REQUIRE_SUDO_RUN="true"
- __sudo "$@"
- exit $?
- else
- return 0
- fi
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Execute sudo
- __sudo() {
- CMD="${1:-echo}" && shift 1
- CMD_ARGS="${*:--e "${RUN_USER:-$USER}"}"
- SUDO="$(builtin command -v sudo 2>/dev/null || echo 'eval')"
- [ "$(basename "$SUDO" 2>/dev/null)" = "sudo" ] && OPTS="--preserve-env=PATH -HE"
- if __sudoif; then
- export PATH="$PATH"
- $SUDO ${OPTS:-} $CMD $CMD_ARGS && true || false
- exitCode=$?
- else
- printf '%s\n' "This requires root to run"
- exitCode=1
- fi
- return ${exitCode:-1}
- }
- # End of sudo functions
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- __trap_exit() {
- exitCode=${exitCode:-$?}
- [ -f "$PB_TEMP_FILE" ] && rm -Rf "$PB_TEMP_FILE" &>/dev/null
- if builtin type -t __trap_exit_local | grep -q 'function'; then __trap_exit_local; fi
- return $exitCode
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # User defined functions
- __private() {
- if [ -n "$PB_PRIVATE_API_KEY" ] && [ "$PB_PRIVATE_API_KEY" != "no_api_key_needed_for_casjay_cc" ]; then
- res="$(printf "%s\n" "$ARG" | curl -q -LSsf -H "Accept: application/json" -H 'Authorization: Bearer '$PB_PRIVATE_API_KEY'' -d lang="$language" -d name="$name" --data-urlencode text@- "${PB_PRIVATE_API_URL:-https://casjay.cc/api/create}" 2>/dev/null | grep '^')"
- else
- res="$(printf "%s\n" "$ARG" | curl -q -LSsf -d lang="$language" -d name="$name" --data-urlencode text@- "${PB_PRIVATE_API_URL:-https://casjay.cc/api/create}" 2>/dev/null | grep '^')"
- fi
- [ -n "$res" ] && printf '%s\n' "$res" || printf ''
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- __execute_pb() {
- printf_cyan "Attempting to send data to $1"
- local paste="" savedir="" date="" service=""
- service="$1"
- date="$(date +'%a %b %d, %Y at %H:%M:%S %Z')"
- savedir="$(dirname "$PB_SAVED_FILE" | grep '^' || echo "$HOME/Documents")"
- case "$1" in
- ix | ix.io)
- shift 1
- get_response="$(curl -q -LSsf -A "$PB_USER_AGENT" 'f:1=<-' "http://ix.io" <"$ARG" 2>/dev/null | grep '^')"
- ;;
- 0x0 | null | nullbyte | 0*)
- shift 1
- get_response="$(curl -q -LSsf -A "$PB_USER_AGENT" -F 'file=@-' "https://0x0.st" <"$ARG" 2>/dev/null | grep '^')"
- ;;
- sprunge | sprunge.us | s*)
- shift 1
- get_response="$(curl -q -LSsf -A "$PB_USER_AGENT" -F 'sprunge=<-' "http://sprunge.us" <"$ARG" 2>/dev/null | grep '^')"
- ;;
- iotek | iopaste | p.iotek.org | iop*)
- shift 1
- get_response="$(curl -q -LSsf -A "$PB_USER_AGENT" -sT- "https://p.iotek.org" <"$ARG" 2>/dev/null | grep '^')"
- ;;
- clbin | clbin.com | cl*)
- shift 1
- get_response="$(curl -q -LSsf -A "$PB_USER_AGENT" -F 'clbin=<-' "https://clbin.com" <"$ARG" 2>/dev/null | grep '^')"
- ;;
- uguu | uguu.se | u*)
- shift 1
- get_response="$(curl -q -LSsf -A "$PB_USER_AGENT" -F 'file=@-' "https://uguu.se/api.php?d=upload-tool" <"$ARG" 2>/dev/null | grep '^')"
- ;;
- lewd | lewd.se | l*)
- shift 1
- get_response="$(curl -q -LSsf -A "$PB_USER_AGENT" -F 'file=@-' "https://lewd.se/api.php?d=upload-tool" <"$ARG" 2>/dev/null | grep '^')"
- ;;
- fiery | fiery.me | f*)
- shift 1
- get_response="$(curl -q -LSsf -A "$PB_USER_AGENT" -F 'files[]=@-' "https://safe.fiery.me/api/upload" <"$ARG" 2>/dev/null | grep -Po '"url":"[A-Za-z0-9]+.*?"' | sed 's/"url":"//;s/"//' | grep '^')"
- ;;
- catbox | catbox.moe | cat*)
- shift 1
- get_response="$(curl -q -LSsf -A "$PB_USER_AGENT" -F 'reqtype=fileupload' -F 'fileToUpload=@-' "https://catbox.moe/user/api.php" <"$ARG" 2>/dev/null | grep '^')"
- ;;
- dmca | dmca.gripe | gripe | dm*)
- shift 1
- get_response="$(curl -q -LSsf -A "$PB_USER_AGENT" -F 'files[]=@-' "http://dmca.gripe/api/upload" <"$ARG" 2>/dev/null | grep -Po '"url":"[A-Za-z0-9]+.*?"' | sed 's/"url":"//;s/"//;s/[\]//g' | grep '^')"
- ;;
- dumpz | dumpz.org | du*)
- shift 1
- get_response="$(curl -q -LSsf -A "$PB_USER_AGENT" -s --data-binary @- "https://dumpz.org" <"$ARG" 2>/dev/null | sed 's/http/&s/;s!$!/text/!' | grep '^')"
- ;;
- w1r3 | wire | w1r3.net | w*)
- shift 1
- get_response="$(curl -q -LSsf -F 'upload=@-' "https://w1r3.net" <"$ARG" 2>/dev/null | grep '^')"
- ;;
- pb | pastebin | pastebin.com)
- shift 1
- qs="api_option=paste&api_dev_key=${PB_PASTEBIN_COM_API_DEV:-97d4dbcc849cf72fe005154e40904f71}&api_paste_expire_date=N&api_paste_private=public&api_paste_name=$(basename "${ARG}")"
- get_response="$(curl -q -LSsf -A "$PB_USER_AGENT" -d "${qs}" --data-urlencode "api_paste_code=$(<"$ARG")" "https://pastebin.com/api" 2>/dev/null | grep '^')"
- ;;
- private)
- shift 1
- get_response="$(__private | grep '^')"
- ;;
- *)
- echo "$ARRAY" | prinf_column $PB_OUTPUT_COLOR_2
- exit 1
- ;;
- esac
- if [ -n "$get_response" ]; then
- printf '%s\n' "$get_response" | printf_readline $PB_OUTPUT_COLOR_2
- printf 'Paste from %s: %s\n' "$date" "$get_response" >>"$PB_SAVED_FILE"
- __cmd_exists clipboard && printf '%s\n' "$get_response" | NOTIFY_CLIENT_NAME="$PB_NOTIFY_CLIENT_NAME" clipboard
- return 0
- else
- return 1
- fi
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # User defined variables/import external variables
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Application Folders
- PB_CONFIG_FILE="${PB_CONFIG_FILE:-settings.conf}"
- PB_CONFIG_DIR="${PB_CONFIG_DIR:-$HOME/.config/myscripts/pb}"
- PB_CONFIG_BACKUP_DIR="${PB_CONFIG_BACKUP_DIR:-$HOME/.local/share/myscripts/pb/backups}"
- PB_LOG_DIR="${PB_LOG_DIR:-$HOME/.local/log/pb}"
- PB_TEMP_DIR="${PB_TEMP_DIR:-$HOME/.local/tmp/system_scripts/pb}"
- PB_CACHE_DIR="${PB_CACHE_DIR:-$HOME/.cache/pb}"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Color Settings
- PB_OUTPUT_COLOR_1="${PB_OUTPUT_COLOR_1:-4}"
- PB_OUTPUT_COLOR_2="${PB_OUTPUT_COLOR_2:-5}"
- PB_OUTPUT_COLOR_GOOD="${PB_OUTPUT_COLOR_GOOD:-2}"
- PB_OUTPUT_COLOR_ERROR="${PB_OUTPUT_COLOR_ERROR:-1}"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Notification Settings
- PB_NOTIFY_ENABLED="${PB_NOTIFY_ENABLED:-yes}"
- PB_GOOD_NAME="${PB_GOOD_NAME:-Great:}"
- PB_ERROR_NAME="${PB_ERROR_NAME:-Error:}"
- PB_GOOD_MESSAGE="${PB_GOOD_MESSAGE:-No errors reported}"
- PB_ERROR_MESSAGE="${PB_ERROR_MESSAGE:-Errors were reported}"
- PB_NOTIFY_CLIENT_NAME="${PB_NOTIFY_CLIENT_NAME:-$APPNAME}"
- PB_NOTIFY_CLIENT_ICON="${PB_NOTIFY_CLIENT_ICON:-notification-new}"
- PB_NOTIFY_CLIENT_URGENCY="${PB_NOTIFY_CLIENT_URGENCY:-normal}"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Additional Variables
- PB_SAVED_FILE="${PB_SAVED_FILE:-$HOME/Documents/pb_links.txt}"
- PB_PRIVATE_URL="${PRIVATE_PASTEBIN_URL:-https://casjay.cc}"
- PB_PRIVATE_API_URL="${PRIVATE_PASTEBIN_API_URL:-https://casjay.cc/api/create}"
- PB_PRIVATE_API_KEY="${PRIVATE_PASTEBIN_API_KEY:-no_api_key_needed_for_casjay_cc}"
- PB_PASTEBIN_COM_API_DEV="${PB_PASTEBIN_COM_API_DEV:-97d4dbcc849cf72fe005154e40904f71}"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Generate config files
- [ -f "$PB_CONFIG_DIR/$PB_CONFIG_FILE" ] ||
- [ "$*" = "--config" ] || INIT_CONFIG="${INIT_CONFIG:-TRUE}" __gen_config ${SETARGS:-$@}
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Import config
- [ -f "$PB_CONFIG_DIR/$PB_CONFIG_FILE" ] &&
- . "$PB_CONFIG_DIR/$PB_CONFIG_FILE"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Ensure Directories exist
- [ -d "$PB_LOG_DIR" ] ||
- mkdir -p "$PB_LOG_DIR" |& __devnull
- [ -d "$PB_TEMP_DIR" ] ||
- mkdir -p "$PB_TEMP_DIR" |& __devnull
- [ -d "$PB_CACHE_DIR" ] ||
- mkdir -p "$PB_CACHE_DIR" |& __devnull
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- PB_TEMP_FILE="${PB_TEMP_FILE:-$(mktemp $PB_TEMP_DIR/XXXXXX 2>/dev/null)}"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Setup trap to remove temp file
- trap '__trap_exit' EXIT
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Setup notification function
- __notifications() {
- __cmd_exists notifications || return
- [ "$PB_NOTIFY_ENABLED" = "yes" ] || return
- [ "$SEND_NOTIFICATION" = "no" ] && return
- (
- set +x
- export SCRIPT_OPTS="" _DEBUG=""
- export NOTIFY_GOOD_MESSAGE="${NOTIFY_GOOD_MESSAGE:-$PB_GOOD_MESSAGE}"
- export NOTIFY_ERROR_MESSAGE="${NOTIFY_ERROR_MESSAGE:-$PB_ERROR_MESSAGE}"
- export NOTIFY_CLIENT_ICON="${NOTIFY_CLIENT_ICON:-$PB_NOTIFY_CLIENT_ICON}"
- export NOTIFY_CLIENT_NAME="${NOTIFY_CLIENT_NAME:-$PB_NOTIFY_CLIENT_NAME}"
- export NOTIFY_CLIENT_URGENCY="${NOTIFY_CLIENT_URGENCY:-$PB_NOTIFY_CLIENT_URGENCY}"
- notifications "$@"
- retval=$?
- return $retval
- ) |& __devnull &
- }
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Set custom actions
- unset title
- unset private
- unset language
- unset expire
- unset reply
- name=${SUDO_USER:-$USER}
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Argument/Option settings
- SETARGS=("$@")
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- SHORTOPTS="t:,n:,p,l:,e:,r:,b:,a:"
- SHORTOPTS+=""
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- LONGOPTS="completions:,config,debug,dir:,help,options,raw,version,silent"
- LONGOPTS+=",title:,name:,private,language:,expire:,reply:,apikey:"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- ARRAY="ix 0x0 nullbyte sprunge iopaste clbin uguu lewd fiery catbox dmca dumpz w1r3 pastebin private"
- ARRAY+=""
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- LIST=""
- LIST+=""
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Setup application options
- setopts=$(getopt -o "$SHORTOPTS" --long "$LONGOPTS" -a -n "$APPNAME" -- "$@" 2>/dev/null)
- eval set -- "${setopts[@]}" 2>/dev/null
- while :; do
- case "$1" in
- --raw)
- shift 1
- export SHOW_RAW="true"
- printf_column() { tee | grep '^'; }
- printf_color() { printf '%b' "$1" | tr -d '\t' | sed '/^%b$/d;s,\x1B\[ 0-9;]*[a-zA-Z],,g'; }
- ;;
- --debug)
- shift 1
- set -xo pipefail
- export SCRIPT_OPTS="--debug"
- export _DEBUG="on"
- __devnull() { tee || return 1; }
- __devnull2() { eval "$@" |& tee || return 1; }
- ;;
- --completions)
- if [ "$2" = "short" ]; then
- printf '%s\n' "-$SHORTOPTS" | sed 's|"||g;s|:||g;s|,|,-|g' | tr ',' '\n'
- elif [ "$2" = "long" ]; then
- printf '%s\n' "--$LONGOPTS" | sed 's|"||g;s|:||g;s|,|,--|g' | tr ',' '\n'
- elif [ "$2" = "array" ]; then
- printf '%s\n' "$ARRAY" | sed 's|"||g;s|:||g' | tr ',' '\n'
- elif [ "$2" = "list" ]; then
- printf '%s\n' "$LIST" | sed 's|"||g;s|:||g' | tr ',' '\n'
- else
- exit 1
- fi
- shift 2
- exit $?
- ;;
- --options)
- shift 1
- printf_blue "Current options for ${PROG:-$APPNAME}"
- [ -z "$SHORTOPTS" ] || __list_options "Short Options" "-${SHORTOPTS}" ',' '-' 4
- [ -z "$LONGOPTS" ] || __list_options "Long Options" "--${LONGOPTS}" ',' '--' 4
- [ -z "$ARRAY" ] || __list_options "Base Options" "${ARRAY}" ',' '' 4
- exit $?
- ;;
- --version)
- shift 1
- __version
- exit $?
- ;;
- --help)
- shift 1
- __help
- exit $?
- ;;
- --config)
- shift 1
- __gen_config
- exit $?
- ;;
- --silent)
- shift 1
- PB_SILENT="true"
- ;;
- --dir)
- CWD_IS_SET="TRUE"
- PB_CWD="$2"
- [ -d "$PB_CWD" ] || mkdir -p "$PB_CWD" |& __devnull
- shift 2
- ;;
- -t | --title)
- title="$2"
- shift 2
- ;;
- -n | --name)
- name="$2"
- shift 2
- ;;
- -p | --private)
- shift 1
- private=1
- ;;
- -l | --language)
- language="$2"
- shift 2
- ;;
- -e | --expire)
- expire="$2"
- shift 2
- ;;
- -r | --reply)
- reply="$2"
- shift 2
- ;;
- -b)
- PASTE_URL="$2"
- shift 2
- ;;
- -a | --apikey)
- PASTE_APIKEY="$2"
- shift 2
- ;;
- --)
- shift 1
- break
- ;;
- esac
- done
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Get directory from args
- # set -- "$@"
- # for arg in "$@"; do
- # if [ -d "$arg" ]; then
- # PB_CWD="$arg" && shift 1 && SET_NEW_ARGS=("$@") && break
- # elif [ -f "$arg" ]; then
- # PB_CWD="$(dirname "$arg" 2>/dev/null)" && shift 1 && SET_NEW_ARGS=("$@") && break
- # else
- # SET_NEW_ARGS+=("$arg")
- # fi
- # done
- # set -- "${SET_NEW_ARGS[@]}"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Set directory to first argument
- # [ -d "$1" ] && PB_CWD="$1" && shift 1 || PB_CWD="${PB_CWD:-$PWD}"
- PB_CWD="$(realpath "${PB_CWD:-$PWD}" 2>/dev/null)"
- # if [ -d "$PB_CWD" ] && cd "$PB_CWD"; then
- # if [ "$PB_SILENT" != true ]; then
- # printf_cyan "Setting working dir to $PB_CWD"
- # fi
- # else
- # printf_exit "? $PB_CWD does not exist ?"
- # fi
- export PB_CWD
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Set actions based on variables
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Check for required applications/Network check
- #requiresudo "$0" "$@" || exit 2 # exit 2 if errors
- cmd_exists --error --ask bash || exit 3 # exit 3 if not found
- #am_i_online --error || exit 4 # exit 4 if no internet
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # APP Variables overrides
- { [ -p "/dev/stdin" ] && ARG="$(<"/dev/stdin")" || false; } || { [ -f "$2" ] && ARG="$(<"$@")" || false; } || ARG="$(echo -e "${@:-}")"
- PROGRAM=${0##*/}
- PB_PRIVATE_URL="${PB_PRIVATE_URL:-}"
- PB_PRIVATE_API_KEY="${PB_PRIVATE_API_KEY:-$PASTE_APIKEY}"
- PB_USER_AGENT=${PB_USER_AGENT:="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"}
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Actions based on env
- [ -n "$ARG" ] || printf_exit "There was no input recieved"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # begin main app
- __execute_pb "$1"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Set exit code
- exitCode="${exitCode:-$?}"
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # End application
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # lets exit with code
- exit ${exitCode:-$?}
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # end