#!/usr/bin/perl -w # ## ptkppp # ## a Perl/Tk PPP dialer frontend # ## (c) 1999 Lukas Ertl # use Tk; ######################################################### # Configuration ## adjust to your needs # The path to the pppd(8) daemon $pppd = "/usr/sbin/pppd"; # Your modem device $device = "your modem device"; # The path to the chat(8) program $chat = "/usr/bin/chat"; # The phone number to dial $phone = "your phone number"; # $user and $remotename are usually defined in /etc/ppp/pap-secrets, in the # form: # username serveralias $user = "your username to login"; $serveralias = "your serveralias as defined in /etc/ppp/pap-secrets"; # This is the full dial command, you may need to adjust it, see pppd(8) # and chat(8) $dialcommand = <new; %counter = ( 'window' => $mw, 'hrs' => 0, 'min' => 0, 'sec' => 0, 'time' => '00:00:00', 'stop' => 1 ); $program_name = $mw->Label(-textvariable => \$credits)->pack; $status = $mw->Label(-textvariable => \$message)->pack; $count = $mw->Label(-textvariable => \$counter{'time'})->pack; $dial_button = $mw->Button(-text => "Dial", -command => \&dial)->pack; $hangup_button = $mw->Button(-text => "Hangup", -command => \&hangup)->pack; $log_view_button = $mw->Button(-text => "Logfile", -command => \&logview)->pack; $exit_button = $mw->Button(-text => "Quit", -command => sub {exit} )->pack; $dial_button->configure(-width => 8); $hangup_button->configure(-width => 8); $log_view_button->configure(-width => 8); $exit_button->configure(-width => 8); MainLoop; sub dial { $counter{'hrs'} = $counter{'min'} = $counter{'sec'} = 0; $status = system("$dialcommand"); die $! unless ($status == 0); $message = "Running..."; if ($counter{'stop'}) { $counter{'stop'} = 0; &switch; } } sub hangup { open (LOG, ">> $logfile") || die $!; $status = system("$hangupcommand"); die $! unless ($status == 0); $counter{'stop'} = 1; $today = localtime; print LOG "$today : hangup after $counter{'time'}\n"; $message = "Inactive..."; close (LOG); } sub switch { return if $counter{'stop'}; if ($counter{'sec'} > 59) { $counter{'sec'} = 0; $counter{'min'}++; } if ($counter{'min'} > 59) { $counter{'min'} = 0; $counter{'hrs'}++; } $counter{'time'} = sprintf("%02d:%02d:%02d", $counter{'hrs'}, $counter{'min'}, $counter{'sec'}); $counter{'window'}->after(1000, \&switch); $counter{'sec'}++; } sub logview { my ($total_hrs, $total_min, $total_sec, $impuls_sum) = 0; my @lines = (); my $logview = $mw->Toplevel(); my $top = $logview->Frame()->pack; my $bottom = $logview->Frame()->pack; my $clear_button = $bottom->Button(-text => "Clear log", -command => \&clear_log)->pack; open (LOG, "$logfile") || die $!; while () { next if /^\n/; chomp; if (/^(\w+)\s+(\w+)\s+(\d+)\s+(\d+):.+after\s+(\d+):(\d+):(\d+)$/) { $wday = $1; $month = $2; $day = $3; $downtime = $4; $total_hrs += $5; $total_min += $6; $total_sec += $7; checktime($downtime); checkday($wday, $month, $day); calc_impulse($5, $6, $7, $freizeit); $impuls_sum += $impulse; push @lines, $_; } } close (LOG) || die $!; my $listbox = $top->Scrolled('Listbox', -scrollbars => 'oe'); $listbox->insert('end', @lines); $listbox->configure(-width => 45); $listbox->pack; if ($total_sec > 59) { $sec = $total_sec % 60; $min_from_sec = int($total_sec / 60); $total_min += $min_from_sec; } if ($total_min > 59) { $min = $total_min % 60; $hrs_from_min = int($total_min / 60); $total_hrs += $hrs_from_min; } else { $min = $total_min; } $money = $impuls_sum * $impulse_cost; $output = sprintf("Total online time:\t%d:%02d:%02d\n\nTotal costs: %d %s", $total_hrs, $min, $sec, $money, $currency); my $label = $bottom->Label(-textvariable => \$output)->pack; my $quit_button = $bottom->Button(-text => "Close", -command => sub { $logview->destroy })->pack; } sub clear_log { system("cp $logfile $logfile.old"); unlink("$logfile") || die $!; } sub checkday { my ($wday, $month, $day) = @_; if ($wday eq "Sat" || $wday eq "Sun") { return $freizeit = 1; } else { my $date = "$month $day"; open (HOLIDAYS, "$holidayfile") or die $!; while ($x = ) { chomp $x; $x =~ /$month/ ? return $freizeit = 1 : return $freizeit = 0; } close (HOLIDAYS); } } sub checktime { my $time = shift; ($time >= $cheapbegin || $time <= $cheapend) ? return $freizeit = 1 : return $freizeit = 0; } sub calc_impulse { my $hours = shift; my $minutes = shift; my $seconds = shift; my $factor = shift; $minutes += $hours * 60; $seconds += $minutes * 60; if ($factor == 1) { $impulse = int($seconds / $impulse_delay_long) + $initial_impulse; return $impulse; } else { $impulse = int($seconds / $impulse_delay_short) + $initial_impulse; return $impulse; } }