X-Git-Url: https://git.jankratochvil.net/?p=nethome.git;a=blobdiff_plain;f=bin%2Ffup;h=ec166d425628bbace3fbe45f384ee45df706476a;hp=bfb53c434086630f82d2cb0e790bc79a278575a9;hb=e9c5c74b8f4cd59588fe518b4753d121b4877536;hpb=175065f62e64f99d78fbe9441010f6ea2083e339 diff --git a/bin/fup b/bin/fup index bfb53c4..ec166d4 100755 --- a/bin/fup +++ b/bin/fup @@ -14,12 +14,17 @@ my $USER="adsl_303141"; my $USERNAME="Jan Kratochvil"; my $PASSWORD=&_priv_postget("uzivatel.gtsnovera.cz.pwd"); my $IFDEV="eth0"; -my $SECS=8; -my $RX_THRESHOLD=200000; # 226085 -my $TX_THRESHOLD=200000; # 291251 -my $STABILIZE_OFF=2; -my $STABILIZE_ON=10; - +my $SECS=4; +my $RX_THRESHOLD=210000; # 226085 +my $TX_THRESHOLD=210000; # 291251 +my $STABILIZE_OFF=1; +my $STABILIZE_ON=4*60; +my $INPUT_IDLE=10*60; + +$RX_THRESHOLD=$RX_THRESHOLD*$SECS/8; +$TX_THRESHOLD=$TX_THRESHOLD*$SECS/8; +$STABILIZE_OFF/=$SECS; +$STABILIZE_ON /=$SECS; $|=1; my $ua=LWP::UserAgent->new(); #$ua->timeout(60); @@ -60,11 +65,15 @@ sub login() $login=~m{\Q$USERNAME\E,} or die $login."\nNo name found"; } +my $on=-1; +my($ignore_read,$ignore_write); sub change($) { -my($on)=@_; +my($new)=@_; - $on=($on ? 1 : 0); + $new=($new ? 1 : 0); + return if $on==$new; + return if $ignore_read; my $try=0; my $change; do { @@ -72,15 +81,16 @@ my($on)=@_; die "Looping to login" if $try++>0x10; login() if $try; $change=simple("https://uzivatel.gtsnovera.cz/cgi-bin/fup.pl", - 'action.'.($on ? 'setManual' : 'unsetManual').'=1', - )->decoded_content() or die "change: ".$on; + 'action.'.($new ? 'setManual' : 'unsetManual').'=1', + )->decoded_content() or die "change: ".$new; } while $change=~m{No authentication cookie was sent.}; my $is_on =($change=~m{\bFUP active\b}); my $is_off=($change=~m{\bFUP not active\b}); $is_on==!$is_off or die $change."\nInvalid change state"; - $on==$is_on or die $change."\nChange not changed, wanted: ".$on; - print "<$on>"; - return $on; + $new==$is_on or die $change."\nChange not changed, wanted: ".$new; + print "<$new>"; + $ignore_write=1; + $on=$new; } if (defined(my $change=$ARGV[0])) { @@ -91,20 +101,78 @@ if (defined(my $change=$ARGV[0])) { } my($rx_last,$tx_last); -my $on=0; -change($on); +change(0); my @history; sub history($$) { my($what,$length)=@_; + return if $what==$on; my $idx=@history; while ($length-->0) { return if --$idx<0; return if $what!=$history[$idx]; } - return 1; + change($what); +} + +use User::Utmp; + +# Users respected for the 'idle' state (see $IdleMax): +my @ValidUsers=qw(root lace jkratoch); + +sub useridle() +{ + my %valid_users=map(($_=>1),@ValidUsers); + my($idlebest,$linebest); + for my $utmp (User::Utmp::getut()) { + local $_; + next if defined($_=$utmp->{"ut_type"}) && $_!=&User::Utmp::USER_PROCESS(); + next if defined($_=$utmp->{"ut_user"}) && !$valid_users{$_}; + my $line="/dev/".$utmp->{"ut_line"}; + my $atime=(stat $line)[8]; + my $what="user \"".($utmp->{"ut_user"} || "")."\", line \"$line\""; + warn "Unable to stat $what" and next if !$atime; + my $idle=time()-$atime; + warn "atime in future for $what" and next if $idle<0; + next if defined $idlebest && $idlebest<=$idle; + $idlebest=$idle; + $linebest=$line; + } + return !wantarray() ? $idlebest : ($idlebest,$linebest); +} + +sub input_idle() +{ + return useridle() > $INPUT_IDLE; +} + +sub mplayer_running() +{ + local *F; + open F,"/proc/net/unix" or die; + my %unix; + ; + local $_; + while () { + /^(?:\S+\s+){6}(\d+)\s/ or die; + $unix{$1}=1; + } + close F or die; + + local $_; + while () { + (my $fdname=$_)=~s{/exe$}{/fd}; + $_=readlink or next; + m{/mplayer$} or next; + while (<$fdname/*>) { + $_=readlink or next; + my $inode=/^\Qsocket:[\E(\d+)\Q]\E$/ or next; + return 1 if $unix{$1}; + } + } + return; } for (;;) { @@ -119,11 +187,20 @@ for (;;) { $on_now=0 if ($rx-$rx_last) > $RX_THRESHOLD; $on_now=0 if ($tx-$tx_last) > $TX_THRESHOLD; } + my $print=$on_now; + my $input_idle=input_idle(); + my $mplayer_running=mplayer_running() if $input_idle; + $on_now=1 if $input_idle && !$mplayer_running; $rx_last=$rx; $tx_last=$tx; push @history,$on_now; - print $on_now; - $on=change(1) if $on==0 && history(1,$STABILIZE_ON); - $on=change(0) if $on==1 && history(0,$STABILIZE_OFF); + $print=($print ? "." : "v") if $input_idle; + $print="M" if $mplayer_running; + $print="*" if $ignore_read; + print($print); + history(1,(($input_idle && !$mplayer_running) ? 0 : $STABILIZE_ON)); + history(0,$STABILIZE_OFF); + $ignore_read=$ignore_write; + $ignore_write=0; sleep $SECS; }