# -*- 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");
$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;
}
sub cycle {
- do_schedule();
- do_hosts();
do_phone();
+ do_hosts();
+ do_schedule();
process_away();
save();
do_custom();
}
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(<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(<SCHEDULE>) {
my ($begin, $end, $maxend);
next unless /(\d\d):(\d\d)-(\d\d):(\d\d) (.*)/;
$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 {
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') {
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";