aboutsummaryrefslogtreecommitdiff
path: root/rat
blob: c1eed9ed3d7546e3e78625851b3780e94194fae9 (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
#!/usr/bin/env bash

#
# the world wide web is a mischievous rat which aims to control you
# I aim to control the rat with ufw
# user interface handled by my ration script
#

SHARE=$HOME/.local/share/rat

update_waybar () {
    killall -35 waybar
}

cull_rats () {
    pkill -f -A " $(which rat) "
}

firewall_set () {
    case $1 in
        disable)
            echo $2 > $SHARE/status
            sudo ufw disable
            notify-send "Firewall has been disabled." ;;
        enable)
            sudo ufw enable
            notify-send -u critical "Firewall has been enabled."
            echo 0 > $SHARE/status
            update_waybar ;;
        *) echo "Invalid argument to firewall_set" > /dev/stderr
            exit 1 ;;
    esac
}

for_duration () {
    cull_rats
    firewall_set disable 1
    endtime=$(($1*60+$(date +%s)))
    echo $endtime > $SHARE/data
    while [[ $endtime > $(date +%s) ]]; do
        update_waybar
        sleep 1
    done
    firewall_set enable
}

for_task () {
    cull_rats
    firewall_set disable 2
    update_waybar
    while true; do
        sleep 5m
        notif="$(notify-send -A open "Ask yourself: do I still need the rat to $1?")"
        if [[ -n "$notif" ]]; then
            kitty --app-id ration bash -i ration
            break
        fi
    done
}

for_program () {
    cull_rats
    firewall_set disable 3
    echo $1 > $SHARE/data
    update_waybar
    $@
    firewall_set enable
}

for_work () {
    endtime=$(date -d 1700 +%s)
    if [[ $(date +%s) > $endtime ]]; then
        notify-send "It's past work hours!"
        exit 0
    fi
    cull_rats
    firewall_set disable 4
    update_waybar
    while [[ $(date +%s) < $endtime ]]; do
        sleep 1
    done
    firewall_set enable
}

case $1 in
    duration) for_duration $2 ;;
    task) for_task "$2" ;;
    program) for_program ${@:2} ;;
    work) for_work ;;
    reset) cull_rats; firewall_set enable ;;
    *) echo "Invalid positional parameter" > /dev/stderr
        exit 1 ;;
esac