Major changes to away system
[tpope-extra.git] / perl / away-tpope
index e5b7479b163e2b958026c4d3fbe1cf8e5a8bbde4..dd26efb682f04a53f4d01f43029d174ec93cd643 100755 (executable)
@@ -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(<SCHEDULE>) {
@@ -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(<SCHEDULE>) {
        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";