diff options
-rwxr-xr-x | computer-time-limit | 98 |
1 files changed, 43 insertions, 55 deletions
diff --git a/computer-time-limit b/computer-time-limit index 632f5ec..ca429fb 100755 --- a/computer-time-limit +++ b/computer-time-limit @@ -18,93 +18,81 @@ if [ -d "${dir}"'/.git' ] && [ $# -eq 0 ] && [ "$(stat -c%U "${dir}"'/.git')" = fi fi -curl -X POST --data "key=$( - { - gpg --list-secret-keys --with-colons \ - | awk -F: '$1 == "sec" {print $5 " " $7}' - printf 'SCHNIPP ' - date '+%s' - } \ - | sort -k2nr,2 \ - | sed '/^SCHNIPP /,$ d' \ - | cut -d' ' -f1 \ - | xargs -r gpg -a --export \ - | base64 -w0 -)" "${url}" +unset key_file -time_file=~/.computer-time-limit -time_per_day=$((60*60/2)) -max_time=$((4*60*60)) -start_time=$(date +%s) +for dir in ~/.local/keys $(mount | cut -d' ' -f3)/keys ''; do + [ -d "${dir}" ] || continue + key_file=$( + find "${dir}" -mindepth 1 -maxdepth 1 -type f -name '*.sec' \ + | head -n1 + ) + [ -f "${key_file}" ] && break +done + +sende_msg() { + curl \ + -s \ + -X POST \ + --data "msg=$1" \ + --data "sig=$( + echo "$1" \ + | signify -S -s "${key_file}" -m /dev/stdin -x /dev/stdout \ + | base64 -w0 + )" \ + 'https://eckner.net/computer-time-limit.php' +} -save_left_time() { - now=$(date +%s) - time_left=$((initial_time_left - now + start_time)) - if [ "${time_left}" -lt 0 ]; then - time_left=0 +wie_lange_noch() { + if [ -f "${key_file}" ]; then + erg=$( + sende_msg start | tee /dev/stderr + ) + if [ -n "${erg}" ] && ! echo "${erg}" | grep -qvx '[0-9]\+'; then + noch="${erg}" + fi + else + noch=0 fi - printf '%s\n' "${time_left}" > "${time_file}" } still_logged_in() { if users | grep -qwF "$(whoami)"; then + wie_lange_noch return 0 fi + sende_msg stop return 1 } -if [ ! -f "${time_file}" ]; then - initial_time_left="${time_per_day}" - save_left_time -else - initial_time_left=$(cat "${time_file}") -fi - -day_number() { - echo "$(($1 / 60 / 60 / 24))" -} - check_time() { - last_day_number=$(day_number "$(stat -c%Y "${time_file}")") - current_day_number=$(day_number "$(date +%s)") - if [ "${last_day_number}" -lt "${current_day_number}" ]; then - touch "${time_file}" - now=$(date +%s) - time_left=$((initial_time_left - now + start_time + time_per_day * (current_day_number - last_day_number))) - if [ "${time_left}" -gt "${max_time}" ]; then - time_left="${max_time}" - fi - initial_time_left="${time_left}" - safe_left_time - fi - if [ "${time_left}" -le 0 ]; then - loginctl session-status | sed -n 's/ .*$//;1p' | xargs -r loginctl terminate-session + if [ "${noch}" -le 0 ]; then + loginctl session-status | sed -n 's/ .*$//;1p' | xargs -r echo loginctl terminate-session fi - if [ "${time_left}" -lt 3600 ] && [ "${time_left}" -gt 3540 ]; then + if [ "${noch}" -lt 3600 ] && [ "${noch}" -gt 3540 ]; then espeak-ng -vde -w /tmp/stunde.wav 'noch eine Stunde' xmessage -nearmouse 'Noch 1 Stunde' & play /tmp/stunde.wav sleep 1m fi - if [ "${time_left}" -lt 1800 ] && [ "${time_left}" -gt 1740 ]; then + if [ "${noch}" -lt 1800 ] && [ "${noch}" -gt 1740 ]; then espeak-ng -vde -w /tmp/halbe_stunde.wav 'noch eine halbe Stunde' xmessage -nearmouse 'Noch 1/2 Stunde' & play /tmp/halbe_stunde.wav sleep 1m fi - if [ "${time_left}" -lt 600 ] && [ "${time_left}" -gt 540 ]; then + if [ "${noch}" -lt 600 ] && [ "${noch}" -gt 540 ]; then espeak-ng -vde -w /tmp/zehn.wav 'noch zehn Minuten' xmessage -nearmouse 'Noch 10 Minuten' & play /tmp/zehn.wav sleep 1m fi - if [ "${time_left}" -lt 300 ] && [ "${time_left}" -gt 240 ]; then + if [ "${noch}" -lt 300 ] && [ "${noch}" -gt 240 ]; then espeak-ng -vde -w /tmp/fuenf.wav 'noch fünf Minuten' xmessage -nearmouse 'Noch 5 Minuten' & play /tmp/fuenf.wav sleep 1m fi - if [ "${time_left}" -lt 60 ] && [ "${time_left}" -gt 30 ]; then + if [ "${noch}" -lt 60 ] && [ "${noch}" -gt 30 ]; then espeak-ng -vde -w /tmp/eins.wav 'noch eine Minute' xmessage -nearmouse 'Noch 1 Minute' & play /tmp/eins.wav @@ -112,10 +100,10 @@ check_time() { fi } -trap 'save_left_time' EXIT +trap 'sende_msg stop' EXIT +noch=0 while still_logged_in; do sleep 1 check_time - save_left_time done |