diff options
-rwxr-xr-x | computer-time-limit | 65 |
1 files changed, 40 insertions, 25 deletions
diff --git a/computer-time-limit b/computer-time-limit index 85e8974..638a2d7 100755 --- a/computer-time-limit +++ b/computer-time-limit @@ -18,16 +18,15 @@ if [ -d "${dir}"'/.git' ] && [ $# -eq 0 ] && [ "$(stat -c%U "${dir}"'/.git')" = fi fi -unset key_file - -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' \ +mapfile -t users < <( + cut -d: -f1,6 /etc/passwd \ + | while IFS=':' read -r user home; do + dir="${home}/.local/keys" + [ -d "${dir}" ] || continue + find "${dir}" -mindepth 1 -maxdepth 1 -type f -name '*.sec' -printf "${user}"' %p\n' \ | head -n1 - ) - [ -f "${key_file}" ] && break -done + done +) sende_msg() { curl \ @@ -36,16 +35,16 @@ sende_msg() { --data "msg=$1" \ --data "sig=$( echo "$1" \ - | signify -S -s "${key_file}" -m /dev/stdin -x /dev/stdout \ + | signify -S -s "$2" -m /dev/stdin -x /dev/stdout \ | base64 -w0 )" \ 'https://eckner.net/computer-time-limit.php' } wie_lange_noch() { - if [ -f "${key_file}" ]; then + if [ -f "$1" ]; then erg=$( - sende_msg start + sende_msg start "$1" ) if [ -n "${erg}" ] && ! echo "${erg}" | grep -qvx '[0-9]\+'; then noch="${erg}" @@ -60,18 +59,9 @@ wie_lange_noch() { fi } -still_logged_in() { - if users | grep -qwF "$(whoami)"; then - wie_lange_noch - return 0 - fi - sende_msg stop - return 1 -} - check_time() { if [ "${noch}" -le 0 ]; then - sende_msg stop + sende_msg stop "$1" loginctl session-status | sed -n 's/ .*$//;1p' | xargs -r loginctl terminate-session fi if [ "${noch}" -lt 3630 ] && [ "${noch}" -gt 3570 ]; then @@ -106,11 +96,36 @@ check_time() { fi } -trap 'sende_msg stop' EXIT +is_logged_in() { + if users | grep -qwF "$1"; then + return 0 + fi + if loginctl list-users -l | sed -n '2,/^$/p' | awk '{print $2}' | grep -qxF "$1"; then + return 0 + fi + return 1 +} + +stop_all() { + for key_file in "${users[@]#* }"; do + sende_msg stop "${key_file}" + done +} + +trap 'stop_all' EXIT noch=0 ab=$(date +%s) -while still_logged_in; do +while true; do + for user_and_key in "${users[@]}"; do + user="${user_and_key%% *}" + key="${user_and_key#* }" + if is_logged_in "${user}"; then + wie_lange_noch "${key}" + check_time "${user}" "${key}" + else + sende_msg stop "${key}" + fi + done sleep $((15 + ((noch + 15) % 30))) - check_time done |