From 51068517d036b37e4ab86bfdde9a7708351316b0 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Sun, 25 Jul 2004 00:28:17 +0000 Subject: [PATCH] Added away-detect --- bin/away-detect | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100755 bin/away-detect diff --git a/bin/away-detect b/bin/away-detect new file mode 100755 index 0000000..d6245c6 --- /dev/null +++ b/bin/away-detect @@ -0,0 +1,133 @@ +#!/bin/sh +# $Id$ +# -*- sh -*- vim: ft=sh sw=4 sts=4 + +# Checks several sources of information and updates my away status +# appropriately. Designed to be periodically called by cron on marge. + +PATH=/home/tpope/bin:/bin:/usr/bin + +out="Out" +sleeping="Sleeping" +afk="Away from keyboard" +off="Desktop is off" + +old_away="`tpope away`" +[ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart" +cat /dev/null > "$HOME/.away-smart" + +ssh="ssh -a -x -oBatchmode=yes -oSetupTimeOut=20" + +if [ -f "/tmp/LCK..`basename $0`" ]; then + if [ -d "/tmp/LCK..`basename $0`" ]; then + #echo "Locked. Exiting." >&2 + exit 1 + else + #echo "Stale lock file found. Removing." >&2 + rm -f "/tmp/LCK..`basename $0`" + fi +fi + +trap 'rm -f "/tmp/LCK..`basename $0`"' EXIT +echo $$ > "/tmp/LCK..`basename $0`" + +set_smart() { + [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \ + tpope away "$*" + smart="$*" +} + +set_custom() { + [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \ + tpope away "$*" +} + +set_out() { + # Away message is empty or older then our "smart" flag + #[ -n "$old_away" -a "$HOME/.away" -nt "$HOME/.away-smart" ] || \ + [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \ + tpope away "$*" + set_smart "$*" +} + +set_in() { + if [ "`date +%k`" -ge 2 -a "`date +%k`" -le 9 ]; then + #[ -n "$old_away" -a "$HOME/.away" -nt "$HOME/.away-smart" ] || \ + set_smart "$sleeping" + else + set_smart "$*" + fi +} + +set_active() { + if [ -n "$old_smart" -a -n "$old_away" ]; then + tpope away -c + set_smart "" + fi + #[ -f "$HOME/.away-smart" ] && tpope away -c +} + +now="$(expr 60 \* $(date +%H) + $(date +%M))" + +today|sed -e s/^..........//|grep '[0-9][0-9]:[0-9][0-9]-[0-9][0-9]:[0-9][0-9]'| \ +while read times event; do + begin="$(expr 60 \* $(echo $times|sed -e 's/-.*//' -e 's/:/ + /g'))" + end="$(expr 60 \* $(echo $times|sed -e 's/.*-//' -e 's/:/ + /g'))" + max_end="$end" + case "$event" in + *[Cc]lass*) begin="`expr $begin - 10`" + end="`expr $begin / 2 + $end / 2`" + ;; + esac + if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then + echo "scheduled=\"$event\"" > "$HOME/.away-smart" + break + elif [ "$old_away" = "$event" -a "$now" -gt "$max_end" ]; then + echo "old_smart=\"$old_away\"" > "$HOME/.away-smart" + fi +done + +[ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart" +old_sched="$scheduled" + +for host in abe homer lisa mona; do + ping -c 1 $host >/dev/null 2>&1 && livehosts="$livehosts $host" +done + +for host in $livehosts; do + # True if a non-blanked display is found + $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 +done + +if ! ping -c 1 mona >/dev/null 2>/dev/null; then + phone=unknown +else + last_slh="`$ssh mona cat .blue/last_slh 2>/dev/null`" + if [ -z "$last_slh" ]; then + phone=unknown + elif [ "$(expr "$(date +%s)" - "$last_slh")" -lt 600 ]; then + phone=present + else + phone=absent + fi +fi + +if [ -n "$scheduled" ]; then + set_custom "$scheduled" +elif [ -n "$alive" ]; then + set_active +elif [ "$phone" = unknown ]; then + set_in "$afk" +elif [ "$phone" = present ]; then + set_in "$afk" +else + set_out "$out" +fi + +cat /dev/null > "$HOME/.away-smart" +echo "old_smart=\"$smart\"" >>~/.away-smart +echo "old_phone=\"$phone\"" >>~/.away-smart +echo "old_alive=\"$alive\"" >>~/.away-smart +echo "old_sched=\"${scheduled:-$old_sched}\"" >>~/.away-smart + +exit 0 -- 2.30.2