Update tilde

This commit is contained in:
deepend-tildeclub 2023-08-02 15:01:20 -06:00 committed by GitHub
parent 1b8e94fe7a
commit 0e3797fb7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 222 additions and 202 deletions

340
tilde
View File

@ -1,10 +1,8 @@
#!/usr/bin/env bash #!/bin/sh
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# tilde - manage user-submitted scripts # tilde - manage user-submitted scripts and apps
# forked from tilde.team
# Copyright 2018-2019, Ben Harris <ben@tilde.team> # Copyright 2018, Ben Harris <ben@tilde.team>
# Copyright 2019, Sven Kinne <creme@envs.net>
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -20,126 +18,125 @@
# Usage: tilde [-h|--help] # Usage: tilde [-h|--help]
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
export TERM=xterm-256color
PROGNAME=${0##*/} PROGNAME=${0##*/}
VERSION="0.0.5" VERSION="0.1.0"
user=$(whoami)
hostname=$(hostname -f)
INFO_FILE="/home/$(id -un)/.tilde" # check coreutils and wrap stat for portability
if stat -c"%U" /dev/null >/dev/null 2>/dev/null ; then
# GNU environment
stat_func () {
stat -c '%U' "$1"
}
else
# BSD environment
stat_func () {
stat -f %Su "$1"
}
fi
### isroot() {
[ "$(id -u)" = "0" ]
clean_up() { # Perform pre-exit housekeeping
return
} }
error_exit() { error_exit() {
echo -e "${1:-"unknown Error"}" >&2 printf "%s\n" "${1:-"unknown Error"}" >&2
clean_up
exit 1 exit 1
} }
graceful_exit() {
clean_up
exit
}
signal_exit() { # Handle trapped signals signal_exit() { # Handle trapped signals
case $1 in case $1 in
INT) INT)
error_exit "program interrupted by user" ;; error_exit "program interrupted by user"
;;
TERM) TERM)
printf '\n%s: program terminated\n' "$PROGNAME" >&2 printf "\n%s: program terminated" "$PROGNAME" >&2
graceful_exit ;; exit
;;
*) *)
error_exit "$PROGNAME: terminating on unknown signal" ;; error_exit "$PROGNAME: terminating on unknown signal"
;;
esac esac
} }
prompt_confirm() { prompt_confirm() {
while true; do while true; do
read -r -n 1 -p "${1:-continue?} [y/n]: " REPLY printf "%s [y/n]: " "${1:-continue?}"
case "$REPLY" in read -r REPLY
[yY]) echo ; return 0 ;; case $REPLY in
[nN]) echo ; return 1 ;; [yY]) printf "\n" ; return 0 ;;
*) printf ' \033[31m %s \n\033[0m' "invalid input" [nN]) printf "\n" ; return 1 ;;
*) printf " \033[31m %s \n\033[0m" "invalid input" ;;
esac esac
done done
} }
usage() {
printf '\n%sUser Scripts%s\n' "$(tput setaf 6)" "$(tput sgr0)"
printf '\nusage: %s [help|list|submit|about|script_name]\n' "$PROGNAME"
printf ' %s list - show a list of approved userscripts\n' "$PROGNAME"
printf ' %s submit - start the submission flow for your own script\n' "$PROGNAME"
[[ $(id -u) == 0 ]] && {
printf ' %s approve - enter the approval queue\n' "$PROGNAME"
printf ' %s revoke <script_name> - send a script back to the author and remove from /tilde/bin\n' "$PROGNAME"
}
printf ' %s about <script_name> - get the description for script_name\n' "$PROGNAME"
printf ' %s <script_name> - run script_name with all remaining args are passed to the script\n' "$PROGNAME"
if [[ :$PATH: != *:"/tilde/bin":* ]] ; then
printf "\nadd /tilde/bin to your PATH to use approved scripts without this wrapper\n"
printf "if you're using bash, run the following to add it quickly\n"
printf " echo 'export PATH=\$PATH:/tilde/bin' >> ~/.bashrc && source ~/.bashrc\n"
fi
printf '\nSource is on github: https://github.com/tildeclub/launcher, please open an issue or send in a PR!\n'
}
help_message() { help_message() {
cat <<- EOF cat <<-EOF
$(tput setaf 6)$PROGNAME (ver. $VERSION)$(tput sgr0) Usage: $PROGNAME [options] [command]
wrapper for user-submitted scripts
supports user settings, submission and admin approval
$(usage)
EOF
return
}
Options:
-h, --help Show this help message and exit
-v, --version Show version information
Commands:
list Show a list of approved user scripts
submit Start the submission flow for your own script
about <script> Show description and details of a specific script
approve Enter the approval queue for pending scripts (requires root)
revoke <script> Revoke a previously approved script (requires root)
run <script> Run an approved script
Please add /tilde/bin to your PATH to use approved scripts without this wrapper.
EOF
}
verify_script_name() { verify_script_name() {
[[ $1 == "" ]] && error_exit "please start over and enter the script name" if [ -z "$1" ]; then
if [[ "$(type "$1" > /dev/null 2>&1)" ]]; then error_exit "please enter a script name"
fi
if command -v "$1"; then
if [ "$(command -v "$1")" != "/home/$user/bin/$1" ]; then
error_exit "$1 already exists. rename your script and try again." error_exit "$1 already exists. rename your script and try again."
fi fi
[[ -x /tilde/bin/"$1" ]] && error_exit "$1 is already taken. rename your script and try again." fi
case "$1" in
help|about|description|desc|list|ls|submit|apropos|approve|revoke|show) if [ -x "/tilde/bin/$1" ]; then
error_exit "$1 is a subcommand of tilde. rename your script and try again.";; error_exit "$1 is already taken. rename your script and try again."
fi
case $1 in
about|description|list|ls|submit|about|help|apropos|submit|approve)
error_exit "$1 is a subcommand of tilde. rename your script and try again."
;;
*) *)
return;; return
;;
esac esac
} }
submission_checklist() { submission_checklist() {
cat <<- EOF cat <<- _EOF_
requirements for submitting a user script or program: Requirements for submitting a user script or program:
- placed in your ~/bin - Placed in your ~/bin
- executable - Executable
- responds to help or --help - Responds to help or --help
- no name collisions with existing scripts or $PROGNAME subcommands - No name collisions with existing scripts or $PROGNAME subcommands
EOF _EOF_
} }
mail_body() { mail_body() {
cat <<- EOF cat <<- _EOF_
Subject: tilde script submission from $USER Subject: tilde script submission from ${user}
From: $USER@tilde.club From: ${user}@${hostname}
To: root@tilde.club To: root@${hostname}
tilde script submission from $USER tilde script submission from ${user}
script name: $1 script name: $1
@ -149,138 +146,161 @@ description:
$2 $2
----------------------------------------------------------------------- -----------------------------------------------------------------------
you'll find the script and description in: /tilde/pending-submissions/$USER/$1 You'll find the script and description in: /tilde/pending-submissions/$user/$1
run this to see the approval queue: Run this to see the approval queue:
sudo tilde approve sudo tilde approve
EOF _EOF_
} }
# Trap signals # Trap signals
trap "signal_exit TERM" TERM HUP trap "signal_exit TERM" TERM HUP
trap "signal_exit INT" INT trap "signal_exit INT" INT
# Parse command-line # Parse command-line
case "$1" in case $1 in
-h | --help | help) -h | --help)
help_message; graceful_exit help_message
exit 0
;; ;;
-v | --version) -v | --version)
printf '%s\n' "$VERSION" printf "%s\n" "$VERSION"
exit 0
;; ;;
'-*' | '--*') list)
usage # List approved scripts
error_exit "Unknown option $1" printf "Available scripts:\n\n"
;;
list | ls)
printf 'available scripts:\n\n'
for scr in /tilde/bin/*; do for scr in /tilde/bin/*; do
if [ -f "$scr" ]; then
script_name=$(basename "$scr") script_name=$(basename "$scr")
target=$(readlink -f "$scr") target=$(readlink -f "$scr")
printf '%s%s by %s%s\n' "$(tput setaf 6)" "$script_name" "$(stat -c '%U' "$target")" "$(tput sgr0)" printf "%s by %s\n" "$script_name" "$(stat_func "$target")"
cat /tilde/descriptions/"$script_name" cat "/tilde/descriptions/$script_name"
printf '\n' printf "\n"
fi
done done
;; ;;
about | apropos | description | desc) about | description)
if [[ -f /tilde/descriptions/"$2" ]]; then # Show details about a specific script
cat /tilde/descriptions/"$2" if [ -z "$2" ]; then
printf "Please provide a script name.\n"
exit 1
fi
if [ -f "/tilde/descriptions/$2" ]; then
cat "/tilde/descriptions/$2"
else else
printf '%s not found. try %s list to see available user scripts.\n' "$2" "$PROGNAME" printf "%s not found. Use '%s list' to see available user scripts.\n" "$2" "$PROGNAME"
exit 1
fi fi
;; ;;
submit) submit)
printf "hello, %s! so it's time to submit your script?\n" "$USER" # Submit a user script
printf "Hello, %s! Ready to submit your script?\n" "$user"
submission_checklist submission_checklist
prompt_confirm "are you ready to continue?" || graceful_exit prompt_confirm "Are you ready to continue?" || exit
printf 'enter the name of your script: '
read -r script_name
printf "Enter the name of your script: "
read -r script_name
verify_script_name "$script_name" verify_script_name "$script_name"
if [[ -x "$HOME"/bin/"$script_name" ]]; then if [ ! -x "$HOME/bin/$script_name" ]; then
printf '\ncool, found your script\n'
[[ -x /tilde/pending-submissions/"$USER"/"$script_name"/"$script_name" ]] && error_exit "you've already submitted $script_name"
else
error_exit "$script_name not found in ~/bin" error_exit "$script_name not found in ~/bin"
fi fi
printf 'enter a description of your script:\n' printf "Enter a description for your script: \n"
read -r description read -r description
printf '\nyour script, along with your description will be sent to the admins for approval\n' printf "\nYour script and description will be sent to the admins for approval.\n"
prompt_confirm "ready to submit?" || graceful_exit prompt_confirm "Ready to submit?" || exit
# submit now # Submit now
mkdir -p /tilde/pending-submissions/"$USER"/"$script_name" mkdir -p "/tilde/pending-submissions/$user/$script_name"
ln -s "$HOME"/bin/"$script_name" /tilde/pending-submissions/"$USER"/"$script_name"/"$script_name" ln -s "$HOME/bin/$script_name" "/tilde/pending-submissions/$user/$script_name/$script_name"
echo "$description" > /tilde/pending-submissions/"$USER"/"$script_name"/description.txt printf "%s\n" "$description" > "/tilde/pending-submissions/$user/$script_name/description.txt"
mail_body "$script_name" "$description" | /usr/sbin/sendmail root mail_body "$script_name" "$description" | /usr/sbin/sendmail root
printf 'script submitted. thanks! :)\n' printf "Script submitted. Thank you! :)\n"
;; ;;
approve) approve)
[[ $(id -u) != 0 ]] && error_exit "re-run this as sudo to access the approval queue" # Approve pending scripts (requires root)
isroot || \
error_exit "Re-run this as root to access the approval queue."
printf "Welcome to the approval queue\n\n"
printf 'welcome to the approval queue\n\n' for user_submission in /tilde/pending-submissions/*; do
user=$(basename "$user_submission")
for script in "$user_submission"/*; do
script_name=$(basename "$script")
[ -f "$script/approved" ] && continue
for user in /tilde/pending-submissions/*; do printf "Script name: %s\n" "$script_name"
for scr in "$user"/*; do cat "$script/description.txt"
user="$(basename "$user")" prompt_confirm "Approve?" || continue
script_name="$(basename "$scr")"
[[ -f $scr/approved ]] && continue
script="$scr"/"$script_name"
printf '%s by %s\n' "$script_name" "$user"
cat "$scr"/description.txt
prompt_confirm "approve?" || continue
sudo ln -s "$(readlink -f "$script")" /tilde/bin/"$script_name" ln -s "$(readlink -f "$script/$script_name")" "/tilde/bin/$script_name"
sudo cp "$scr"/description.txt /tilde/descriptions/"$script_name" cp "$script/description.txt" "/tilde/descriptions/$script_name"
sudo touch "$scr"/approved touch "$script/approved"
sudo chmod 664 /tilde/descriptions/* chmod 664 "/tilde/descriptions/$script_name"
echo "your submission of $script_name has been approved and is now available at /tilde/bin/$script_name" | /usr/sbin/sendmail "$user" printf "Your submission of %s has been approved and is now available at /tilde/bin/%s\n" "$script_name" "$script_name" \
| sendmail "$user"
done done
done done
echo "~~done for now~~" printf "~~Done for now~~\n"
;; ;;
revoke) revoke)
[[ "$(id -u)" != 0 ]] && error_exit "re-run this as sudo to access the revoke menu" # Revoke an approved script (requires root)
[[ -f /tilde/bin/"$2" ]] || error_exit "$2 isn't an approved script" isroot || \
error_exit "Re-run this as root to access the revoke menu."
[ -f "/tilde/bin/$2" ] || \
error_exit "$2 isn't an approved script"
prompt_confirm "revoke $2?" prompt_confirm "revoke $2?"
printf 'please provide a reason: ' printf "please provide a reason: "
read -r reason read -r reason
original_script="$(readlink -f /tilde/bin/"$2")" original_script=$(readlink -f "/tilde/bin/$2")
author="$(stat -c '%U' "$original_script")" author=$(stat_func "$original_script")
sudo rm /tilde/{bin,descriptions}/"$2" rm "/tilde/bin/$2"
sudo rm -rf /tilde/pending-submissions/"$author"/"$2" rm "/tilde/descriptions/$2"
rm -rf "/tilde/pending-submissions/$author/$2"
echo -e "your script $2 has been returned because: $reason\nfeel free to resubmit" | /usr/sbin/sendmail "$author" printf "your script %s has been returned because: %s\nfeel free to resubmit\n" "$2" "$reason" \
printf '%s revoked and returned to author\n' "$2" | sendmail "$author"
printf "%s revoked and returned to author" "$2"
;; ;;
*) run)
if [[ -f /tilde/bin/"$1" ]]; then # Run an approved script
prog=/tilde/bin/"$1" if [ -z "$2" ]; then
printf "Please provide a script name.\n"
help_message
exit 1
fi
if [ -x "/tilde/bin/$2" ]; then
prog="/tilde/bin/$2"
shift shift
$prog "$@" exec "$prog" "$@"
graceful_exit
else else
[[ "$1" == "" ]] || printf "%s not found. try %s list to see what's available\n" "$1" "$PROGNAME" printf "%s not found or not approved yet. Use '%s list' to see available user scripts.\n" "$2" "$PROGNAME"
help_message; graceful_exit; exit 1
fi fi
;; ;;
"")
help_message
exit 0
;;
*)
printf "Invalid command: %s\n" "$1"
help_message
exit 1
;;
esac esac
#
graceful_exit