Added away-actions
[tpope-extra.git] / bin / away-detect
1 #!/bin/sh
2 # $Id$
3 # -*- sh -*- vim: ft=sh sw=4 sts=4
4
5 # Checks several sources of information and updates my away status
6 # appropriately.  Designed to be periodically called by cron on marge.
7
8 PATH=/home/tpope/bin:/bin:/usr/bin
9
10 out="Out"
11 sleeping="Sleeping"
12 afk="Away from keyboard"
13 off="Desktop is off"
14
15 old_away="`tpope away`"
16 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
17 cat /dev/null > "$HOME/.away-smart"
18
19 ssh="ssh -a -x -oBatchmode=yes -oSetupTimeOut=20"
20
21 if [ -f "/tmp/LCK..`basename $0`" ]; then
22     if [ -d "/tmp/LCK..`basename $0`" ]; then
23         #echo "Locked.  Exiting." >&2
24         exit 1
25     else
26         #echo "Stale lock file found.  Removing." >&2
27         rm -f "/tmp/LCK..`basename $0`"
28     fi
29 fi
30
31 trap 'rm -f "/tmp/LCK..`basename $0`"' EXIT
32 echo $$ > "/tmp/LCK..`basename $0`"
33
34 set_smart() {
35     [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
36     tpope away "$*"
37     smart="$*"
38 }
39
40 set_custom() {
41     [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
42     tpope away "$*"
43 }
44
45 set_out() {
46     # Away message is empty or older then our "smart" flag
47     #[ -n "$old_away" -a "$HOME/.away" -nt "$HOME/.away-smart" ] || \
48     [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \
49     tpope away "$*"
50     set_smart "$*"
51 }
52
53 set_in() {
54     if [ "`date +%k`" -ge 2 -a "`date +%k`" -le 9 ]; then
55         #[ -n "$old_away" -a "$HOME/.away" -nt "$HOME/.away-smart" ] || \
56         set_smart "$sleeping"
57     else
58         set_smart "$*"
59     fi
60 }
61
62 set_active() {
63     if [ -n "$old_smart" -a -n "$old_away" ]; then
64         tpope away -c
65         set_smart ""
66     fi
67     #[ -f "$HOME/.away-smart" ] && tpope away -c
68 }
69
70 now="$(expr 60 \* $(date +%H) + $(date +%M))"
71
72 today|sed -e s/^..........//|grep '[0-9][0-9]:[0-9][0-9]-[0-9][0-9]:[0-9][0-9]'| \
73 while read times event; do
74     begin="$(expr 60 \* $(echo $times|sed -e 's/-.*//' -e 's/:/ + /g'))"
75     end="$(expr 60 \* $(echo $times|sed -e 's/.*-//' -e 's/:/ + /g'))"
76     max_end="$end"
77     case "$event" in
78         *[Cc]lass*) begin="`expr $begin - 10`"
79             end="`expr $begin / 2 + $end / 2`"
80         ;;
81     esac
82     if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then
83         echo "scheduled=\"$event\"" > "$HOME/.away-smart"
84         break
85     elif [ "$old_away" = "$event" -a "$now" -gt "$max_end" ]; then
86         echo "old_smart=\"$old_away\"" > "$HOME/.away-smart"
87     fi
88 done
89
90 [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart"
91 old_sched="$scheduled"
92
93 for host in abe homer lisa mona; do
94     ping -c 1 $host >/dev/null 2>&1 && livehosts="$livehosts $host"
95 done
96
97 for host in $livehosts; do
98     # True if a non-blanked display is found
99     $ssh $host 'if DISPLAY=:0.0 xscreensaver-command -time >/dev/null 2>&1; then if DISPLAY=:0.0 xscreensaver-command -time 2>/dev/null|grep non-blanked >/dev/null; then true; else pid=`ps ax|grep "[0-9]:[0-9][0-9] ssh .*exec screen.*RR irc"|sed -e "s/^  *//"|cut -d" " -f 1`; [ -f "$HOME/.irc.lock" -o -z "$pid" ] || kill $pid; false; fi; else false; fi' && alive=$host
100 done
101
102 if ! ping -c 1 mona >/dev/null 2>/dev/null; then
103     phone=unknown
104 else
105     last_slh="`$ssh mona cat .blue/last_slh 2>/dev/null`"
106     if [ -z "$last_slh" ]; then
107         phone=unknown
108     elif [ "$(expr "$(date +%s)" - "$last_slh")" -lt 600 ]; then
109         phone=present
110     else
111         phone=absent
112     fi
113 fi
114
115 if [ -n "$scheduled" ]; then
116     set_custom "$scheduled"
117 elif [ -n "$alive" ]; then
118     set_active
119 elif [ "$phone" = unknown ]; then
120     set_in "$afk"
121 elif [ "$phone" = present ]; then
122     set_in "$afk"
123 else
124     set_out "$out"
125 fi
126
127 cat /dev/null > "$HOME/.away-smart"
128 echo "old_smart=\"$smart\"" >>~/.away-smart
129 echo "old_phone=\"$phone\"" >>~/.away-smart
130 echo "old_alive=\"$alive\"" >>~/.away-smart
131 echo "old_sched=\"${scheduled:-$old_sched}\"" >>~/.away-smart
132
133 exit 0