#!/bin/bash # $Id$ # -*- sh -*- vim:set ft=sh sw=4 sts=4: # OBSOLETE. See perl/away-tpope instead. # Checks several sources of information and updates my away status # appropriately. Designed to be periodically called by cron on marge. PATH="$HOME/bin:/bin:/usr/bin" out="Out" sleeping="Sleeping" afk="Away from keyboard" off="Desktop is off" NAME="away-detect" if [ -f "$HOME/.LCK..$NAME" ]; then if [ -d "/proc/`cat $HOME/.LCK..$NAME`" ]; then echo "Locked. Exiting." >&2 exit 1 else echo "Stale lock file found. Removing." >&2 rm -f $HOME/.LCK..$NAME fi fi trap 'rm -f "$HOME/.LCK..$NAME"' EXIT old_away="`tpope away`" [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart" cat /dev/null > "$HOME/.away-smart" find "$HOME" -maxdepth 1 -name .activity -mtime +1 -exec rm {} \; 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() { if [ -z "$old_away" -o "$old_away" = "$old_smart" ] || [ "$phone" = present -a "$old_phone" = absent ]; then tpope away "$*" fi smart="$*" } set_custom() { [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \ tpope away "$*" } set_out() { [ -z "$old_away" -o "$old_away" = "$old_smart" ] && \ tpope away "$*" set_smart "$*" } set_in() { if [ "`date +%k`" -ge 0 -a "`date +%k`" -le 9 ]; then 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 --category=school | sed -e s/^..........// -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="`expr $end + 5`" end="`expr 3 \* $begin / 4 + $end / 4`" begin="`expr $begin - 25`" if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then echo "scheduled=\"Class: $event\"" >> "$HOME/.away-smart" break elif [ "$old_away" = "Class: $event" -a "$now" -gt "$max_end" ]; then echo "old_smart=\"$old_away\"" > "$HOME/.away-smart" fi done today --category='!school'\ '!private' | sed -e s/^..........// -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" 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 buster maeby lindsay tobias; 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 pidof xscreensaver >/dev/null && DISPLAY=:0.0 xscreensaver-command -version >/dev/null 2>&1; then if DISPLAY=:0.0 xscreensaver-command -time 2>&1 |egrep "non-blanked|no saver status" >/dev/null; then true; else pid=`ps ax|egrep "[0-9]:[0-9][0-9] ssh marge .*(screen.*RR irc|Chat)"|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 # Former away-actions away="`tpope away`" activity="`tpope activity`" case "$away" in Sleeping|"Away from keyboard"|TV*|"") is_away=0 ;; *[Cc]lass*) is_away=1 ;; esac [ "$phone" = absent ] && is_away=1 if [ "$is_away" = 1 ]; then [ ! -f /tmp/.tpope_lights ] && touch /tmp/.tpope_lights && \ $ssh maeby br -F -F elif [ -f /tmp/.tpope_lights ]; then $ssh maeby br -n 5 rm -f /tmp/.tpope_lights $ssh maeby br -n 5 fi if [ "$activity" ]; then information="$activity" elif [ "$away" ]; then information="$away" else case "$alive" in lindsay*) information="On laptop" ;; buster*) information="In bed" ;; tobias*|lucille*) information="On desktop" ;; *) information="Away from keyboard?" ;; esac fi if [ -z "$information" ]; then echo > "$HOME/.caminfo.deskcam" echo "- On desktop" > "$HOME/.caminfo.bedcam" #echo "On desktop" > "$HOME/.status" rm -f "$HOME/.plan" else echo "- $information" > "$HOME/.caminfo.deskcam" if [ "$information" = "In bed" ]; then echo "" > "$HOME/.caminfo.bedcam" else echo "- $information" > "$HOME/.caminfo.bedcam" fi #echo "$information" > "$HOME/.status" echo "$information" > "$HOME/.plan" fi for host in gob maeby buster oscar; do ping -c 1 $host >/dev/null && scp -q "$HOME/.status" $host: 2>/dev/null done ping -c 1 lucille >/dev/null && scp -q "$HOME/.caminfo.deskcam" lucille:.caminfo 2>/dev/null ping -c 1 buster >/dev/null && scp -q "$HOME/.caminfo.bedcam" buster:.caminfo 2>/dev/null rm -f .caminfo.bedcam exit 0