From f797889ab9eeec92b620e50455ad4392f9ee6d96 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Sun, 29 May 2005 03:42:06 +0000 Subject: [PATCH] Major changes to away system --- bin/away-actions | 56 ++++++++++++++++++---------- bin/away-detect | 96 ++++++++++++++++++++++++++++++++++++++++++------ perl/away-tpope | 44 ++++++++++++++++------ 3 files changed, 153 insertions(+), 43 deletions(-) diff --git a/bin/away-actions b/bin/away-actions index c49a1dc..2ae60ef 100755 --- a/bin/away-actions +++ b/bin/away-actions @@ -1,48 +1,66 @@ -#!/bin/sh +#!/bin/bash # $Id$ -# -*- sh -*- vim: ft=sh sw=4 sts=4 +# -*- sh -*- vim:set ft=sh sw=4 sts=4: -# Performs routine actions based on my away status. Designed to be -# periodically called by cron on homer. +# Checks several sources of information and updates my away status +# appropriately. Designed to be periodically called by away-tpope. + +PATH="$HOME/bin:/bin:/usr/bin" away="`tpope away`" activity="`tpope activity`" -ping -c 1 marge >/dev/null 2>&1 && eval `ssh marge cat .away-smart` case "$away" in - Sleeping|"Away from keyboard"|"Adult Swim"|"") is_away=0 ;; + Sleeping|"Away from keyboard"|TV*|"") is_away=0 ;; *[Cc]lass*) is_away=1 ;; esac -[ "$old_phone" = absent ] && is_away=1 +[ "$phone" = absent ] && is_away=1 if [ "$is_away" = 1 ]; then - [ ! -f /tmp/.tpope_lights ] && touch /tmp/.tpope_lights && br -F -F + [ ! -f /tmp/.tpope_lights ] && touch /tmp/.tpope_lights && \ + $ssh homer br -F -F elif [ -f /tmp/.tpope_lights ]; then - br -n 5 + $ssh homer br -n 5 rm -f /tmp/.tpope_lights - br -n 5 + $ssh homer br -n 5 fi if [ "$activity" ]; then information="$activity" elif [ "$away" ]; then information="$away" -elif [ "$old_alive" = "lisa" ]; then - information="On laptop" else - information= + case "$alive" in + lisa*|abe*) information="On laptop" ;; + sarah*) information="In bed" ;; + mona*|homer*) information="On desktop" ;; + *) information="Away from keyboard?" ;; + esac fi if [ -z "$information" ]; then - echo > "$HOME/.caminfo" - echo Here > "$HOME/.status" + echo > "$HOME/.caminfo.deskcam" + echo "- On desktop" > "$HOME/.caminfo.bedcam" + #echo "On desktop" > "$HOME/.status" rm -f "$HOME/.plan" else - echo "- $information" > "$HOME/.caminfo" - echo "$information" > "$HOME/.status" + 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 -ping -c 1 bart >/dev/null && scp -q "$HOME/.status" bart: 2>/dev/null -ping -c 1 abe >/dev/null && scp -q "$HOME/.status" abe: 2>/dev/null +for host in bart homer sarah clancy; do + ping -c 1 $host >/dev/null && scp -q "$HOME/.status" $host: 2>/dev/null +done + +ping -c 1 homer >/dev/null && scp -q "$HOME/.caminfo.deskcam" homer:.caminfo 2>/dev/null +ping -c 1 sarah >/dev/null && scp -q "$HOME/.caminfo.bedcam" sarah:.caminfo 2>/dev/null +rm -f .caminfo.bedcam + +exit 0 diff --git a/bin/away-detect b/bin/away-detect index 8424b9c..745f10f 100755 --- a/bin/away-detect +++ b/bin/away-detect @@ -1,21 +1,37 @@ -#!/bin/sh +#!/bin/bash # $Id$ -# -*- sh -*- vim: ft=sh sw=4 sts=4 +# -*- 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/tpope/bin:/bin:/usr/bin +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 @@ -50,7 +66,7 @@ set_out() { } set_in() { - if [ "`date +%k`" -ge 2 -a "`date +%k`" -le 9 ]; then + if [ "`date +%k`" -ge 0 -a "`date +%k`" -le 9 ]; then set_smart "$sleeping" else set_smart "$*" @@ -71,19 +87,17 @@ today --category=school | sed -e s/^..........// -e 's/ \[.*\]$//'|grep '[0-9][0 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" + max_end="`expr $end + 5`" end="`expr 3 \* $begin / 4 + $end / 4`" - begin="`expr $begin - 15`" + begin="`expr $begin - 25`" if [ "$begin" -le "$now" -a "$now" -lt "$end" ]; then - echo "scheduled=\"Class ($event)\"" >> "$HOME/.away-smart" + echo "scheduled=\"Class: $event\"" >> "$HOME/.away-smart" break - elif [ "$old_away" = "Class ($event)" -a "$now" -gt "$max_end" ]; then + elif [ "$old_away" = "Class: $event" -a "$now" -gt "$max_end" ]; then echo "old_smart=\"$old_away\"" > "$HOME/.away-smart" fi done -cat ~/.away-smart - 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'))" @@ -100,13 +114,13 @@ done [ -f "$HOME/.away-smart" ] && . "$HOME/.away-smart" old_sched="$scheduled" -for host in abe homer lisa mona; do +for host in sarah 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 -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|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 + $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 @@ -140,4 +154,62 @@ 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 homer br -F -F +elif [ -f /tmp/.tpope_lights ]; then + $ssh homer br -n 5 + rm -f /tmp/.tpope_lights + $ssh homer br -n 5 +fi + +if [ "$activity" ]; then + information="$activity" +elif [ "$away" ]; then + information="$away" +else + case "$alive" in + lisa*|abe*) information="On laptop" ;; + sarah*) information="In bed" ;; + mona*|homer*) 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 bart homer sarah clancy; do + ping -c 1 $host >/dev/null && scp -q "$HOME/.status" $host: 2>/dev/null +done + +ping -c 1 homer >/dev/null && scp -q "$HOME/.caminfo.deskcam" homer:.caminfo 2>/dev/null +ping -c 1 sarah >/dev/null && scp -q "$HOME/.caminfo.bedcam" sarah:.caminfo 2>/dev/null +rm -f .caminfo.bedcam + exit 0 diff --git a/perl/away-tpope b/perl/away-tpope index e5b7479..dd26efb 100755 --- a/perl/away-tpope +++ b/perl/away-tpope @@ -3,11 +3,11 @@ # -*- perl -*- vim:set ft=perl sw=4 sts=4: use strict; -use vars qw(%state @ssh $last $pipe); +use vars qw(%state @ssh $last $pipe $arg); @ssh=("ssh","-a","-x","-oBatchmode=yes","-oSetupTimeOut=20"); $pipe = "/tmp/.away-tpope"; -my $arg=shift || ""; +$arg=shift || ""; daemonize() if ($arg eq "-d"); daemon() if ($arg eq "-D"); @@ -37,6 +37,7 @@ sub daemon { $SIG{'TERM'} = \&quit_handler; $SIG{'QUIT'} = \&quit_handler; $SIG{'HUP'} = \&cycle; + $SIG{'USR2'} = \&restart_handler; die "Daemon already running\n" if (-r "/tmp/.tpope-away.pid"); unless (-p $pipe) { unlink $pipe; @@ -94,9 +95,9 @@ sub main_loop { } sub cycle { - do_schedule(); - do_hosts(); do_phone(); + do_hosts(); + do_schedule(); process_away(); save(); do_custom(); @@ -112,8 +113,9 @@ sub ping { } sub do_schedule { - my @now=localtime(time); - my $now=$now[3]*60+$now[2]; + my @now=localtime; + my $now=$now[2]*60+$now[1]; + my $familiar = 0; open(SCHEDULE, "today --category=school|"); while() { @@ -125,15 +127,20 @@ sub do_schedule { $end = ($begin+25)*3/4+($maxend-5)/4; if($begin <= $now && $now < $end) { $state{'class'} = $ev; + $familiar = 1; last; - } elsif ($state{'class'} eq $ev && $now > $maxend) { - $state{'class'} = ''; + } elsif (($state{'class'}||'') eq $ev) { + $familiar = 1; + undef $state{'class'} + if ($now > $maxend || $state{'phone'} eq "present"); } } close(SCHEDULE); + undef $state{'class'} unless ($familiar); open(SCHEDULE, "today --category='!school !private'|"); + $familiar = 0; while() { my ($begin, $end, $maxend); next unless /(\d\d):(\d\d)-(\d\d):(\d\d) (.*)/; @@ -143,12 +150,16 @@ sub do_schedule { $end = ($begin)*3/4+($maxend)/4; if($begin <= $now && $now < $end) { $state{'schedule'} = $ev; + $familiar = 1; last; - } elsif ($state{'schedule'} eq $ev && $now > $maxend) { - $state{'schedule'} = ''; + } elsif ($state{'schedule'} eq $ev) { + $familiar = 1; + undef $state{'schedule'} + if ($now > $maxend || $state{'phone'} eq "present"); } } close(SCHEDULE); + undef $state{'schedule'} unless ($familiar); } sub do_hosts { @@ -185,10 +196,10 @@ sub do_phone { sub process_away { if ($state{'customaway'}) { $state{'away'}=$state{'customaway'}; - } elsif ($state{'school'}) { - $state{'away'}="Class: ".$state{'school'}; } elsif ($state{'schedule'}) { $state{'away'}=$state{'schedule'}; + } elsif ($state{'phone'} ne 'present' && $state{'class'}) { + $state{'away'}="Class: ".$state{'class'}; } elsif ($state{'alive'}) { undef($state{'away'}); } elsif ($state{'phone'} eq 'absent') { @@ -272,6 +283,15 @@ sub load { close CONF; } +sub restart_handler { + alarm 0; + unlink $pipe; + unlink "/tmp/.away-tpope.pid"; + save(); + exec($0,$arg) || die $!; + print "Restarting $0 $arg...\n"; +} + sub quit_handler { unlink $pipe; unlink "/tmp/.away-tpope.pid"; -- 2.30.2