commit cde530ad5edbfa5b0fa1d0a2f0ccd4b5800b7321 parent a99bb2ef518854a6f7dd628d75bc1eb26b610c9c Author: Claudio Alessi <smoppy@gmail.com> Date: Tue, 24 Nov 2015 13:41:31 +0100 [sober] New script Diffstat:
M | README.md | | | 23 | +++++++++++++++++++++++ |
A | src/sober | | | 46 | ++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 69 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md @@ -189,3 +189,26 @@ diffmon ------- Simple script which monitor a file and show lines that are changed since you ran it. + +sober +----- +Simple sobriety checker inspired by [DrunkGuard](https://github.com/jkingsman/DrunkGuard). + +In a nutshell: +``` +$ sudo rm -rf / +How much is 7 + 1? 8 +How much is 5 + 9? 12 +You're drunk, I'll take care of you. +``` + +Use sober by creating aliases in your ~/.${SHELL}rc file: + +alias sudo="/path/of/sober sudo" +alias rm="/path/of/sober rm" +alias mv="/path/of/sober mv" +alias apt-get="/path/of/sober apt-get" +alias dpkg="/path/of/sober dpkg" + +You may want to toggle this aliases at specific times (e.g. after work hours). +This is easily achievable with simple shell scripts. diff --git a/src/sober b/src/sober @@ -0,0 +1,46 @@ +#!/bin/sh +# Simple sobriety checker inspired by DrunkGuard. + +NCHECKS=3 +DRUNKSFILE=/tmp/drunks + +die() { + echo "$@" + exit 1 +} + +rnd() { + nofd="$1" # n. of digits + cset="$2" # character set + [ -z "$nofd" ] && nofd=1 + [ -z "$cset" ] && cset="[:digit:]" + dd if=/dev/urandom of=/dev/stdout bs=4096 count=1 2>/dev/null |tr -dc "$cset" |head -c "$nofd" +} + +check() { + n1="$(rnd)" + n2="$(rnd)" + sum="$(echo "${n1}+${n2}" | bc)" + printf "How much is %d + %d? " "$n1" "$n2" + read r + if [ "$r" -ne "$sum" ]; then + return 1 + fi + return 0 +} + +drunk() { + echo "$(date +'%d/%m/%Y %H:%M:%S') | $(id -un) | $@" >> "$DRUNKSFILE" + die "You're drunk, I'll take care of you." +} + +main() { + [ "$#" -eq 0 ] && die "Are you sober?" + for i in $(seq 1 $NCHECKS); do + check + [ $? -ne 0 ] && drunk "$@" + done + $@ +} + +main "$@"