summaryrefslogtreecommitdiff
path: root/computer-time-limit
blob: f214f995a6a27cfd007f025f3e7d81ab1743f10f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash

dir=$(dirname "$(readlink -e "${0}")")
url='https://eckner.net/computer-time-limit.php'

lock_file=~/.computer-time-limit.lock

exec 9> "${lock_file}"
if ! flock -n 9; then
  exit
fi

if [ -d "${dir}"'/.git' ] && [ $# -eq 0 ] && [ "$(stat -c%U "${dir}"'/.git')" = "$(whoami)" ]; then
  git -C "${dir}" fetch --all -p 2>/dev/null
  if [ -z "$(git -C "${dir}" status --porcelain)" ]; then
    git -C "${dir}" pull --ff-only 2>/dev/null
    exec "$0" '-'
  fi
fi

. /etc/computer-time-limit.conf

sende_msg() {
  data="$(uname -n) $1"
  curl \
    -s \
    -X POST \
    --data "msg=${data}" \
    --data "sig=$(
      echo "${data}" \
      | signify -S -s "$2" -m /dev/stdin -x /dev/stdout \
      | base64 -w0
    )" \
    'https://eckner.net/computer-time-limit.php'
}

wie_lange_noch() {
  if [ -f "$1" ]; then
    erg=$(
      sende_msg start "$1"
    )
    if [ -n "${erg}" ] && ! echo "${erg}" | grep -qvx '[0-9]\+'; then
      noch="${erg}"
      ab=$(date +%s)
    else
      noch=$((ab+noch-$(date +%s)))
      ab=$(date +%s)
    fi
  else
    noch=0
    ab=$(date +%s)
  fi
}

check_time() {
  if [ "${noch}" -le 0 ]; then
    sende_msg stop "$2" >/dev/null
    loginctl terminate-session "$1"
  fi
  if [ "${noch}" -lt 3630 ] && [ "${noch}" -gt 3570 ]; then
    espeak-ng -vde -w /tmp/stunde.wav 'noch eine Stunde'
    xmessage -nearmouse 'Noch 1 Stunde' &
    play /tmp/stunde.wav
    sleep 1m
  fi
  if [ "${noch}" -lt 1830 ] && [ "${noch}" -gt 1770 ]; 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 [ "${noch}" -lt 630 ] && [ "${noch}" -gt 570 ]; then
    espeak-ng -vde -w /tmp/zehn.wav 'noch zehn Minuten'
    xmessage -nearmouse 'Noch 10 Minuten' &
    play /tmp/zehn.wav
    sleep 1m
  fi
  if [ "${noch}" -lt 330 ] && [ "${noch}" -gt 270 ]; 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 [ "${noch}" -lt 90 ] && [ "${noch}" -gt 30 ]; then
    espeak-ng -vde -w /tmp/eins.wav 'noch eine Minute'
    xmessage -nearmouse 'Noch 1 Minute' &
    play /tmp/eins.wav
    sleep 1m
  fi
}

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 "${keys[@]}"; do
    sende_msg stop "${key_file}" >/dev/null
  done
}

trap 'stop_all' EXIT
noch=0
ab=$(date +%s)

while true; do
  for user in "${!keys[@]}"; do
    key="${keys["${user}"]}"
    if is_logged_in "${user}"; then
      wie_lange_noch "${key}"
      check_time "${user}" "${key}"
    else
      sende_msg stop "${key}" >/dev/null
    fi
  done
  sleep $((15 + ((noch + 15) % 30)))
done