3 # -*- perl -*- vim:set ft=perl sw=4 sts=4:
6 use vars qw(%state @ssh $last $pipe);
7 @ssh=("ssh","-a","-x","-oBatchmode=yes","-oSetupTimeOut=20");
8 $pipe = "/tmp/.away-tpope";
11 daemonize() if ($arg eq "-d");
12 daemon() if ($arg eq "-D");
15 if($arg eq "activity" || $arg eq "away") {
16 if(exists($ARGV[0])) {
17 die "Daemon not running\n" unless (-p $pipe);
18 my $a = join(" ",@ARGV);
23 $arg =~ s/^away$/customaway/;
25 print $state{$arg}."\n" if($state{$arg});
32 open TMP, ">/tmp/.away-tpope.pid";
36 $SIG{'INT'} = \&quit_handler;
37 $SIG{'TERM'} = \&quit_handler;
38 $SIG{'QUIT'} = \&quit_handler;
39 $SIG{'HUP'} = \&cycle;
40 die "Daemon already running\n" if (-r "/tmp/.tpope-away.pid");
43 system("mkfifo", $pipe);
58 elsif(defined($pid)) {
66 local $SIG{ALRM} = sub { die "alarm\n" };
68 open FIFO, "<$pipe" and
74 die unless $@ eq "alarm\n";
79 $read =~ s/^([a-z]*)( |$)//;
81 if($c eq "activity") {
82 custom_activity($read);
83 } elsif($c eq "away") {
86 print "Unknown command: $c\n";
89 if(time-$last >=300) {
90 local $SIG{'HUP'} = "IGNORE";
106 open(SOUT, ">&STDOUT");
108 my $ret=!(system('ping','-q','-c','1',shift)>>8);
109 open(STDOUT, ">&SOUT");
115 my @now=localtime(time);
116 my $now=$now[3]*60+$now[2];
118 open(SCHEDULE, "today --category=school|");
120 my ($begin, $end, $maxend);
121 next unless /(\d\d):(\d\d)-(\d\d):(\d\d) (.*)/;
122 my ($hs,$ms,$he,$me,$ev) = ($1, $2, $3, $4, $5);
123 $begin = $hs*60+$ms-25;
124 $maxend = $he*60+$me+5;
125 $end = ($begin+25)*3/4+($maxend-5)/4;
126 if($begin <= $now && $now < $end) {
127 $state{'class'} = $ev;
129 } elsif ($state{'class'} eq $ev && $now > $maxend) {
130 $state{'class'} = '';
136 open(SCHEDULE, "today --category='!school !private'|");
138 my ($begin, $end, $maxend);
139 next unless /(\d\d):(\d\d)-(\d\d):(\d\d) (.*)/;
140 my ($hs,$ms,$he,$me,$ev) = ($1, $2, $3, $4, $5);
142 $maxend = $he*60+$me;
143 $end = ($begin)*3/4+($maxend)/4;
144 if($begin <= $now && $now < $end) {
145 $state{'schedule'} = $ev;
147 } elsif ($state{'schedule'} eq $ev && $now > $maxend) {
148 $state{'schedule'} = '';
156 foreach my $host ("sarah", "homer", "lisa", "mona") {
157 push @livehosts, $host if(ping($host));
160 foreach my $host (@livehosts) {
161 $alive=$host unless(system(@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') >> 8);
163 $state{'alive'} = $alive;
171 my $last_slh=`@ssh mona cat .blue/last_slh 2>/dev/null`;
174 } elsif (time-$last_slh < 600) {
180 if($state{'phone'} eq 'absent' && $phone eq 'present') {
186 if ($state{'customaway'}) {
187 $state{'away'}=$state{'customaway'};
188 } elsif ($state{'school'}) {
189 $state{'away'}="Class: ".$state{'school'};
190 } elsif ($state{'schedule'}) {
191 $state{'away'}=$state{'schedule'};
192 } elsif ($state{'alive'}) {
193 undef($state{'away'});
194 } elsif ($state{'phone'} eq 'absent') {
195 $state{'away'}="Out";
196 } elsif ($state{'phone'} ne 'absent') {
199 $state{'away'}="Sleeping";
201 $state{'away'}="Away from keyboard";
208 $state{'customaway'} = $_[0];
210 undef $state{'customaway'};
216 sub custom_activity {
218 $state{'activity'} = $_[0];
220 undef $state{'activity'};
226 if (defined($state{'activity'})) {
227 $state{'status'}=$state{'activity'};
228 } elsif (defined($state{'away'})) {
229 $state{'status'}=$state{'away'};
230 } elsif ($state{'alive'} eq "mona" || $state{'alive'} eq "homer") {
231 $state{'status'}="On desktop";
232 } elsif ($state{'alive'} eq "lisa") {
233 $state{'status'}="On laptop";
234 } elsif ($state{'alive'} eq "sarah") {
235 $state{'status'}="In bed";
237 open CONF, '>' . $ENV{'HOME'} . "/.away-tpope" || die $!;
238 foreach my $k (keys %state) {
242 print CONF "$k=$val\n";
245 if(defined($state{'activity'})) {
246 open TMP, '>' . $ENV{'HOME'} . "/.activity";
247 print TMP $state{'activity'}."\n";
250 unlink($ENV{'HOME'} . "/.activity");
252 if(defined($state{'away'})) {
253 open TMP, '>' . $ENV{'HOME'} . "/.away";
254 print TMP $state{'away'}."\n";
257 unlink($ENV{'HOME'} . "/.away");
259 open TMP, '>' . $ENV{'HOME'} . "/.status";
260 print TMP $state{'status'}."\n";
265 open CONF, '<' . $ENV{'HOME'} . "/.away-tpope" or return;
266 while(my $line=<CONF>) {
268 $line =~ s/^([^=]*)=//;
277 unlink "/tmp/.away-tpope.pid";
283 system("away-actions &");