tilde-launcher/tilde

312 lines
8.9 KiB
Plaintext
Raw Permalink Normal View History

2020-06-11 19:54:04 +00:00
#!/bin/sh
2018-06-12 01:32:37 +00:00
# ---------------------------------------------------------------------------
# tilde - manage user-submitted scripts and apps
# Copyright 2018, Ben Harris <ben@tilde.team>
# 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
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License at <http://www.gnu.org/licenses/> for
# more details.
# Usage: tilde [-h|--help]
# ---------------------------------------------------------------------------
PROGNAME=${0##*/}
2020-06-11 02:21:08 +00:00
VERSION="0.1.0"
2020-06-11 19:54:04 +00:00
user=$(whoami)
hostname=$(hostname -f)
2018-06-12 01:32:37 +00:00
2020-06-11 00:45:17 +00:00
# check coreutils and wrap stat for portability
if stat -c"%U" /dev/null >/dev/null 2>/dev/null ; then
2020-06-11 02:21:08 +00:00
# GNU environment
stat_func () {
stat -c '%U' "$1"
}
2020-06-11 00:45:17 +00:00
else
2020-06-11 02:21:08 +00:00
# BSD environment
stat_func () {
stat -f %Su "$1"
}
2020-06-11 00:45:17 +00:00
fi
2020-06-11 19:54:04 +00:00
isroot() {
[ "$(id -u)" = "0" ]
}
2018-06-12 01:32:37 +00:00
error_exit() {
2020-06-11 02:21:08 +00:00
printf "%s\n" "${1:-"unknown Error"}" >&2
exit 1
2018-06-12 01:32:37 +00:00
}
signal_exit() { # Handle trapped signals
2020-06-11 02:21:08 +00:00
case $1 in
INT)
error_exit "program interrupted by user"
;;
TERM)
printf "\n%s: program terminated" "$PROGNAME" >&2
exit
;;
*)
error_exit "$PROGNAME: terminating on unknown signal"
;;
esac
2018-06-12 01:32:37 +00:00
}
prompt_confirm() {
2020-06-11 02:21:08 +00:00
while true; do
2020-06-11 19:54:04 +00:00
printf "%s [y/n]: " "${1:-continue?}"
read -r REPLY
2020-06-11 02:21:08 +00:00
case $REPLY in
[yY]) printf "\n" ; return 0 ;;
[nN]) printf "\n" ; return 1 ;;
*) printf " \033[31m %s \n\033[0m" "invalid input" ;;
esac
done
2018-06-12 01:32:37 +00:00
}
2020-06-11 02:21:08 +00:00
help_message() {
printf "%s version %s\n" "$PROGNAME" "$VERSION"
printf "wrapper for user-submitted scripts\n"
printf "supports submission and admin approval\n"
usage
}
2018-06-12 01:32:37 +00:00
usage() {
2020-06-11 02:22:52 +00:00
printf "\nusage: %s [help|list|submit|about|script_name]\n\n" "$PROGNAME"
2020-06-11 02:21:08 +00:00
printf " list - show a list of approved userscripts\n"
printf " submit - start the submission flow for your own script\n"
2018-06-12 01:32:37 +00:00
2020-06-11 19:54:04 +00:00
if isroot; then
2020-06-11 02:21:08 +00:00
printf " approve - enter the approval queue\n"
printf " revoke <script_name> - send a script back to the author and remove from /tilde/bin\n"
fi
2018-06-12 01:32:37 +00:00
2020-06-11 02:21:08 +00:00
printf " about <script_name> - get the description for script_name\n"
printf " <script_name> - run script_name with all remaining args are passed to the script\n"
2018-06-12 01:32:37 +00:00
2020-06-11 19:54:04 +00:00
case ":$PATH:" in
*:/tilde/bin:*)
;;
*)
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"
;;
esac
2020-06-11 02:21:08 +00:00
}
2018-06-12 01:32:37 +00:00
verify_script_name() {
2020-06-11 19:54:04 +00:00
if [ -z "$1" ]; then
error_exit "please enter a script name"
fi
2020-06-11 02:21:08 +00:00
2020-06-11 19:54:04 +00:00
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."
fi
2020-06-11 19:54:04 +00:00
fi
2020-06-11 02:21:08 +00:00
2020-06-11 19:54:04 +00:00
if [ -x "/tilde/bin/$1" ]; then
2020-06-11 02:21:08 +00:00
error_exit "$1 is already taken. rename your script and try again."
2020-06-11 19:54:04 +00:00
fi
2020-06-11 02:21:08 +00:00
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
;;
esac
}
2018-06-12 01:32:37 +00:00
submission_checklist() {
2020-06-11 02:21:08 +00:00
cat <<- _EOF_
2018-06-12 01:32:37 +00:00
requirements for submitting a user script or program:
- placed in your ~/bin
- executable
- responds to help or --help
- no name collisions with existing scripts or $PROGNAME subcommands
2018-06-12 01:32:37 +00:00
_EOF_
}
mail_body() {
2020-06-11 02:21:08 +00:00
cat <<- _EOF_
2020-06-11 19:54:04 +00:00
Subject: tilde script submission from ${user}
From: ${user}@${hostname}
To: root@${hostname}
2018-06-12 01:32:37 +00:00
2020-06-11 19:54:04 +00:00
tilde script submission from ${user}
2018-06-12 01:32:37 +00:00
script name: $1
description:
-----------------------------------------------------------------------
$2
-----------------------------------------------------------------------
2020-06-11 19:54:04 +00:00
you'll find the script and description in: /tilde/pending-submissions/$user/$1
2018-06-12 01:32:37 +00:00
run this to see the approval queue:
sudo tilde approve
_EOF_
}
# Trap signals
trap "signal_exit TERM" TERM HUP
trap "signal_exit INT" INT
# Parse command-line
case $1 in
2020-06-11 02:21:08 +00:00
-h | --help | help)
help_message; exit
;;
-v | --version)
printf "%s" "$VERSION"
;;
-* | --*)
usage
error_exit "Unknown option $1"
;;
list | ls)
printf "available scripts:\n\n"
for scr in /tilde/bin/*; do
if [ -f "$scr" ]; then
script_name=$(basename "$scr")
target=$(readlink -f "$scr")
printf "%s by %s\n" "$script_name" "$(stat_func "$target")"
cat "/tilde/descriptions/$script_name"
printf "\n"
fi
done
;;
about | apropos | description)
2020-06-11 19:54:04 +00:00
if [ -f "/tilde/descriptions/$2" ]; then
2020-06-11 02:21:08 +00:00
cat "/tilde/descriptions/$2"
else
printf "%s not found. try %s list to see available user scripts.\n" "$2" "$PROGNAME"
fi
;;
submit)
2020-06-11 19:54:04 +00:00
printf "hello, %s! so it's time to submit your script?\n" "$user"
2020-06-11 02:21:08 +00:00
submission_checklist
prompt_confirm "are you ready to continue?" || exit
printf "enter the name of your script: "
read -r script_name
verify_script_name "$script_name"
2020-06-11 19:54:04 +00:00
if [ -x "$HOME/bin/$script_name" ]; then
2020-06-11 02:21:08 +00:00
printf "cool, found your script\n"
2020-06-11 19:54:04 +00:00
if [ -x "/tilde/pending-submissions/$user/$script_name/$script_name" ]; then
2020-06-11 02:21:08 +00:00
error_exit "you've already submitted $script_name"
2020-06-11 19:54:04 +00:00
fi
2020-06-11 02:21:08 +00:00
else
error_exit "$script_name not found in ~/bin"
fi
printf "enter a description of your script: \n"
read -r description
printf "\nyour script, along with your description will be sent to the admins for approval\n"
prompt_confirm "ready to submit?" || exit
# submit now
2020-06-11 19:54:04 +00:00
mkdir -p "/tilde/pending-submissions/$user/$script_name"
ln -s "$HOME/bin/$script_name" "/tilde/pending-submissions/$user/$script_name/$script_name"
printf "%s\n" "$description" > "/tilde/pending-submissions/$user/$script_name/description.txt"
2020-06-11 02:21:08 +00:00
mail_body "$script_name" "$description" | sendmail root
printf "script submitted. thanks! :)\n"
;;
approve)
2020-06-11 19:54:04 +00:00
if ! isroot; then
error_exit "re-run this as root to access the approval queue"
fi
2020-06-11 02:21:08 +00:00
printf "welcome to the approval queue\n\n"
for user in /tilde/pending-submissions/*; do
for scr in $user/*; do
user=$(basename "$user")
script_name=$(basename "$scr")
2020-06-11 19:54:04 +00:00
[ -f "$scr/approved" ] && continue
2020-06-11 02:21:08 +00:00
script="$scr/$script_name"
if [ -f "$script" ]; then
printf "%s by %s\n" "$script_name" "$user"
cat "$scr/description.txt"
prompt_confirm "approve?" || continue
ln -s "$(readlink -f "$script")" "/tilde/bin/$script_name"
cp "$scr/description.txt" "/tilde/descriptions/$script_name"
touch "$scr/approved"
chmod 664 /tilde/descriptions/*
printf "your submission of %s has been approved and is now available at /tilde/bin/%s" "$script_name" "$script_name" \
| sendmail "$user"
fi
done
done
printf "~~done for now~~\n"
;;
revoke)
2020-06-11 19:54:04 +00:00
isroot || \
2020-06-11 02:21:08 +00:00
error_exit "re-run this as sudo to access the revoke menu"
2020-06-11 19:54:04 +00:00
[ -f "/tilde/bin/$2" ] || \
2020-06-11 02:21:08 +00:00
error_exit "$2 isn't an approved script"
prompt_confirm "revoke $2?"
printf "please provide a reason: "
read -r reason
original_script=$(readlink -f "/tilde/bin/$2")
author=$(stat_func "$original_script")
rm "/tilde/bin/$2"
rm "/tilde/descriptions/$2"
rm -rf "/tilde/pending-submissions/$author/$2"
printf "your script %s has been returned because: %s\nfeel free to resubmit\n" "$2" "$reason" \
| sendmail "$author"
printf "%s revoked and returned to author" "$2"
;;
2018-06-12 01:32:37 +00:00
2020-06-11 02:21:08 +00:00
*)
2020-06-11 20:01:01 +00:00
if [ -z "$1" ]; then
help_message
exit
elif [ -x "/tilde/bin/$1" ]; then
2020-06-11 02:21:08 +00:00
prog="/tilde/bin/$1"
shift
exec "$prog" "$@"
else
2020-06-11 20:01:01 +00:00
printf "%s not found. check %s list to see what's available\n\n" "$1" "$PROGNAME"
2020-06-11 02:21:08 +00:00
help_message
exit
fi
;;
2018-06-12 01:32:37 +00:00
2020-06-11 02:21:08 +00:00
esac
2018-06-12 01:32:37 +00:00