Random Perl

I found some really old perl scripts from last century or so:

This will convert irc logs from axur to mirc:

 #!/usr/local/bin/perl -w
 #
 #

use strict;
use Time::Local;

my $month1 = 0;
 my $day1 = 0;
 my $year1 = 0;
 my $hour1 = 0;
 my $minutes1 = 0;
 my $seconds1 = 0;
 my $month2 = 0;
 my $day2 = 0;
 my $year2 = 0;
 my $hour2=0;
 my $minutes2 = 0;
 my $seconds2 = 0;
 my $time1 = "";
 my $time2 = "";
 my @calendar = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );

#open(LOGFILE, "$ARGV[0]") || die "can't open logfile: $!";
 while() {
 chomp();
 ($month1, $day1, $year1, $hour1, $minutes1, $seconds1) = /^[(d+)/(d+)/(d+) @ (d+):(d+):(d+)]/;
 if ( $. == 1 ) { # start of log file
 #print "Session Start: $calendar[$month1-1] $day1 $hour1:$minutes1:$seconds1 20$year1 n";
 $time1 = timelocal($seconds1, $minutes1, $hour1, $day1, $month1-1, $year1);
 print "Session Start: ", scalar(localtime($time1)), " n";
 }
 if ( ( $day1 != $day2 ) && ( $. != 1) ) { # days changed
 #print "Session Close: $calendar[$month2-1] $day2 $hour2:$minutes2:$seconds2 20$year2 n";
 #print "Session Start: $calendar[$month1-1] $day1 $hour1:$minutes1:$seconds1 20$year1 n";
 $time2 = timelocal($seconds2, $minutes2, $hour2, $day2, $month2-1, $year2);
 $time1 = timelocal($seconds1, $minutes1, $hour1, $day1, $month1-1, $year1);
 print "Session Start: ", scalar(localtime($time2)), " n";
 print "Session Close: ", scalar(localtime($time1)), " n";
 }
 # if ( ( />(deimos)/i ) || ( />(sleepy)/i ) ) {
 # s/>(deimos)//ig;
 # s/>(sleepy)//ig;
 #}
 split( /]/);
 print "[$hour1:$minutes1]$' n";
 ($month2, $day2, $year2, $hour2, $minutes2, $seconds2) = ($month1, $day1, $year1, $hour1, $minutes1, $seconds1);
 }
 $time1 = timelocal($seconds1, $minutes1, $hour1, $day1, ($month1-1), $year1);
 print "Session Close: ", scalar(localtime($time1)), " n";
 #close(LOGFILE);

This converts sar output to a graph:

#!/usr/bin/perl

@cpu=`sar`;
 $datafile="/tmp/cpu-data$$";
 $cmndfile="/tmp/cpu-cmnd$$";
 foreach $line (@cpu) {
 if ( $line=~/d+:d+:d+s+d+/ ) {
 $_ = $line;
 ($hr)=/^(d+)/;
 ($min)=/^d+:(d+)/;
 ($usr)=/s+(d+)/;
 ($sys)=/s+d+s+(d+)/;
 ($wio)=/s+d+s+d+s+(d+)/;
 ($idle)=/s+d+s+d+s+d+s+(d+)/;
 @hrs[$hr]=$hr;
 @usr[$hr]=@usr[$hr]+$usr;
 @sys[$hr]=@sys[$hr]+$sys;
 @wio[$hr]=@wio[$hr]+$wio;
 @idle[$hr]=@idle[$hr]+$idle;
 @ints[$hr]=@ints[$hr]+1;
 }
 }
 open DATA, ">$datafile" or die "Cannot open $datafile for writing";
 open CMND, ">$cmndfile" or die "Cannot open $cmndfile for writing";
 foreach $hr (@hrs) {
 if ($hr != 0) {
 $usr=sprintf("%.1f",$usr[$hr]/$ints[$hr]);
 $sys=sprintf("%.1f",$sys[$hr]/$ints[$hr]);
 $wio=sprintf("%.1f",$wio[$hr]/$ints[$hr]);
 $idle=sprintf("%.1f",$idle[$hr]/$ints[$hr]);
 print DATA "$hrt$usrt$syst$wiot$idlen";
 }
 }
 close DATA;
 print CMND "set term png colorn";
 print CMND "set out 'cpu.png'n";
 print CMND "set gridn";
 print CMND "set title 'CPU Usage'n";
 print CMND "set ylabel 'percent'n";
 print CMND "plot '$datafile' using 1:2 title 'usr' with lines, \n";
 print CMND " '$datafile' using 1:3 title 'sys' with lines, \n";
 print CMND " '$datafile' using 1:4 title 'wio' with lines, \n";
 print CMND " '$datafile' using 1:5 title 'idle' with linesn";
 # create graph using constructed gnuplot command file and data
 # NOTE: the datafile is specified within the command file
 system("/opt/sfw/bin/gnuplot $cmndfile");

# clean up temp files
 system("rm $datafile");
 system("rm $cmndfile");

Here's a script to "invoke" someone in irssi via irc:

#!/usr/bin/perl -w
 use strict;
 use vars qw($VERSION %IRSSI);

use Irssi;
 $VERSION = '1.00';
 %IRSSI = (
 authors => 'deimos',
 contact => 'deimos',
 name => 'Invokation',
 description => 'This script allows ' .
 'someone to invoke you. Thereby alerting' .
 'you to them.',
 license => 'Public Domain',
 );

sub event_privmsg {
 # $data = "nick/#channel :text"
 my ($server, $data, $nick, $address) = @_;
 my ($target, $text) = split(/ :/, $data, 2);

Irssi::signal_stop() if ($text =~ /invokes/ || $nick =~ /idiot/);
 }

Irssi::signal_add("event privmsg", "event_privmsg")

And here's way to grab urls out of axur log files:

 #!/usr/bin/perl -w
 #

use strict;
 my ($urls, $ltrs, $gunk, $punc, $any, $foundurl) = "";

open (LOGFILE,"/home/deimos/public_html/urls/chan_$ARGV[0].html") || die "can't open logfile:$!";

$urls = '(http|https|ftp)';
 $ltrs = 'w';
 $gunk = '/#~:.?+=&%@!-';
 $punc = '.:?-';
 $any = "${ltrs}${gunk}${punc}";

while () {
 chomp();
 if (/ b($urls:[$any]+?) (?=[$punc]*[^$any]|$) /igox) {
 print "$1n";
 };
 }
 close LOGFILE;