Assorted away-tpope updates
[tpope-extra.git] / perl / away-tpope
index 38ce70d978bd7af59bea4ff4483395d6f5cf9577..7ec9c19f3843b572cd1341d983a7e0911c491879 100755 (executable)
@@ -4,7 +4,7 @@
 
 use strict;
 use vars qw(%state @ssh $last $pipe $arg $slow);
-@ssh=("ssh","-a","-x","-oBatchmode=yes","-oSetupTimeOut=20");
+@ssh=("ssh","-a","-x","-oBatchmode=yes","-oSetupTimeOut=20","-qq");
 $pipe = "/tmp/.away-tpope";
 
 $arg=shift || "";
@@ -107,6 +107,7 @@ sub cycle {
        do_phone();
        $slow=0;
     }
+    eval_chat();
     do_schedule();
     do_decision();
     save();
@@ -138,13 +139,16 @@ sub do_schedule {
        next unless /(\d\d):(\d\d)-(\d\d):(\d\d) (.*)/;
        my ($hs,$ms,$he,$me,$ev) = ($1, $2, $3, $4, $5);
        $ev =~ s/ *\[.*$//;
-       $begin = $hs*60+$ms-25;
+       $begin = $hs*60+$ms-10;
        $maxend = $he*60+$me+10;
-       $end = ($begin+25)*3/4+($maxend-10)/4;
+       $end = ($begin+10)*3/4+($maxend-10)/4;
        if($begin <= $now && $now < $end) {
            $state{'class'} = $ev;
+           internal_out("School");
            $familiar = 1;
            last;
+       } elsif($begin-35<=$now && $now<$end && $state{'phone'} eq "absent") {
+           internal_out("School");
        } elsif (($state{'class'}||'') eq $ev) {
            $familiar = 1;
            undef $state{'class'}
@@ -154,7 +158,6 @@ sub do_schedule {
     close(SCHEDULE);
     undef $state{'class'} unless ($familiar);
 
-
     open(SCHEDULE, "today --category='!school !private'|");
     $familiar = 0;
     while(<SCHEDULE>) {
@@ -191,8 +194,16 @@ sub do_chat {
     }
 }
 
+sub eval_chat {
+    if(($state{'chat'}||'') =~ /^(tpope-\d+|jmwaller|arwen|george)$/) {
+       internal_out("Work",3*60*60);
+    } elsif(($state{'chat'}||'') eq "accd") {
+       #internal_out("School",30*60);
+    }
+}
+
 sub do_hosts {
-    my (@check) = ("mona", "lisa", "homer", "sarah");
+    my (@check) = ("tobias", "lindsay");
     my (@uphosts, @livehosts, $host, $hostlist);
     if(($_[0] || 0) == 1) {
        $hostlist=$state{'hosts'};
@@ -232,7 +243,7 @@ sub is_alive {
     eval {
        local $SIG{ALRM} = sub { die "alarm\n" };
        alarm(30);
-       $ret=!(system(@ssh,shift, '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') >> 8);
+       $ret=!(system(@ssh,shift, '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 michael .*(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') >> 8);
        alarm(0);
     };
     if($@) {
@@ -244,26 +255,27 @@ sub is_alive {
 
 sub do_phone {
     my $phone;
-    if(!ping('mona')) {
+    if(!ping('tobias')) {
        $phone="unknown";
     } else {
-       my $last_slh=`@ssh mona cat .blue/last_slh 2>/dev/null`;
+       my $last_slh=`@ssh tobias cat .blue/last_slh .blue/last_gm 2>/dev/null`;
        if(!$last_slh) {
            $phone="unknown";
-       } elsif (time-$last_slh < 540) {
+       } elsif (time-$last_slh < 1140) {
            $phone="present";
        } else {
            $phone="absent";
        }
     }
-    if($state{'phone'} ne 'present' && $phone eq 'present') {
+    if(($state{'phone'}||"") ne 'present' && $phone eq 'present') {
        custom_out("");
+       internal_out("");
     }
     $state{'phone'} = $phone;
 }
 
 sub do_power {
-    open TMP, "upsc milhouse\@localhost|";
+    open TMP, "upsc sally\@localhost 2>/dev/null|";
     my $ups='';
     while(<TMP>) {
        chomp;
@@ -280,9 +292,11 @@ sub do_decision {
        if ($state{'customawayexp'} && $state{'customawayexp'}<time);
     custom_out("")
        if ($state{'customoutexp'} && $state{'customoutexp'}<time);
+    internal_out("")
+       if ($state{'internaloutexp'} && $state{'internaloutexp'}<time);
     custom_activity("")
        if ($state{'customactivityexp'} && $state{'customactivityexp'}<time);
-    if ($state{'customactivity'}) {
+    if (exists($state{'customactivity'})) {
        $state{'activity'}=$state{'customactivity'};
     } elsif ($state{'ups'} eq 'OB' || $state{'ups'} eq 'LB') {
        $state{'activity'}="Power outage";
@@ -297,13 +311,15 @@ sub do_decision {
        $state{'away'}=$state{'schedule'};
     } elsif ($state{'phone'} ne 'present' && $state{'class'}) {
        $state{'away'}="Class: ".$state{'class'};
+    } elsif ($state{'internalout'} && $state{'phone'} eq 'absent') {
+       $state{'away'}=$state{'internalout'};
     } elsif ($state{'alive'}) {
        undef($state{'away'});
     } elsif ($state{'phone'} eq 'absent') {
        $state{'away'}="Out";
     } elsif ($state{'phone'} ne 'absent') {
        my @now=localtime;
-       if (1 <= $now[2] && $now[2] < 9) {
+       if (0 <= $now[2] && $now[2] < 7) {
            $state{'away'}="Sleeping";
        } else {
            $state{'away'}="Away from keyboard";
@@ -362,6 +378,18 @@ sub custom_out {
     save();
 }
 
+sub internal_out {
+    if($_[0]) {
+       $state{'internalout'} = $_[0];
+       $state{'internaloutexp'} = time+($_[1]||150*60);
+    } else {
+       undef $state{'internalout'};
+       undef $state{'internaloutexp'};
+    }
+    do_decision();
+    save();
+}
+
 sub custom_activity {
     if($_[0]) {
        $state{'customactivity'} = $_[0];
@@ -376,38 +404,38 @@ sub custom_activity {
 
 sub save {
     if (defined($state{'activity'})) {
-       $state{'status'}=$state{'activity'};
+        $state{'status'}=$state{'activity'};
     } elsif (defined($state{'away'})) {
-       $state{'status'}=$state{'away'};
-    } elsif ($state{'alive'} eq "mona" || $state{'alive'} eq "homer") {
-       $state{'status'}="On desktop";
-    } elsif ($state{'alive'} eq "lisa") {
-       $state{'status'}="On laptop";
-    } elsif ($state{'alive'} eq "sarah") {
-       $state{'status'}="In bed";
+        $state{'status'}=$state{'away'};
+    } elsif ($state{'alive'} eq "tobias") {
+        $state{'status'}="On desktop";
+    } elsif ($state{'alive'} eq "lindsay") {
+        $state{'status'}="On laptop";
+    #} elsif ($state{'alive'} eq "buster") {
+        #$state{'status'}="In bed";
     }
     open CONF, '>' . $ENV{'HOME'} . "/.away-tpope" || die $!;
     foreach my $k (keys %state) {
-       my $val=$state{$k};
-       next unless($val);
-       $val =~ s/\n/\\n/g;
-       $val =~ s/"/\\"/g;
-       print CONF "$k=\"$val\"\n";
+        my $val=$state{$k};
+        next unless($val);
+        $val =~ s/\n/\\n/g;
+        $val =~ s/"/\\"/g;
+        print CONF "$k=\"$val\"\n";
     }
     close CONF;
     if(defined($state{'activity'})) {
-       open TMP, '>' . $ENV{'HOME'} . "/.activity";
-       print TMP $state{'activity'}."\n";
-       close TMP;
+        open TMP, '>' . $ENV{'HOME'} . "/.activity";
+        print TMP $state{'activity'}."\n";
+        close TMP;
     } else {
-       unlink($ENV{'HOME'} . "/.activity");
+        unlink($ENV{'HOME'} . "/.activity");
     }
     if(defined($state{'away'})) {
-       open TMP, '>' . $ENV{'HOME'} . "/.away";
-       print TMP $state{'away'}."\n";
-       close TMP;
+        open TMP, '>' . $ENV{'HOME'} . "/.away";
+        print TMP $state{'away'}."\n";
+        close TMP;
     } else {
-       unlink($ENV{'HOME'} . "/.away");
+        unlink($ENV{'HOME'} . "/.away");
     }
     open TMP, '>' . $ENV{'HOME'} . "/.status";
     print TMP $state{'status'}."\n";
@@ -416,13 +444,17 @@ sub save {
 
 sub load {
     open CONF, '<' . $ENV{'HOME'} . "/.away-tpope" or return;
+    undef $state{'customactivity'};
+    undef $state{'customaway'};
+    undef $state{'customout'};
+    undef $state{'internalout'};
     while(my $line=<CONF>) {
-       $line =~ s/\\n/\n/g;
-       $line =~ s/\\"/"/g;
-       $line =~ s/"$//;
-       $line =~ s/^([^=]*)="?//;
-       chomp $line;
-       $state{$1}=$line;
+        $line =~ s/\\n/\n/g;
+        $line =~ s/\\"/"/g;
+        $line =~ s/"$//;
+        $line =~ s/^([^=]*)="?//;
+        chomp $line;
+        $state{$1}=$line;
     }
     close CONF;
 }