#!/usr/bin/perl # irclog2html.pl Version 2.1mg - 27th July, 2001 # Copyright (C) 2000, Jeffrey W. Waugh # Changed by Marius Gedminas (marius@pov.lt) on 2005-01-09: # changed time regexps to support ISO-8601 (YYYY-MM-DDTHH:MM:SS) timestamps # commented out print $time; # fixed bugs in code that tried to preserve colours accross nick changes # Author: # Jeff Waugh # Contributors: # Rick Welykochy # Alexander Else # Released under the terms of the GNU GPL v2 or later # http://www.gnu.org/copyleft/gpl.html # Usage: irclog2html filename # irclog2html will write out a colourised irc log, appending a .html # extension to the output file. #################################################################################### # Perl Configuration use strict; #$^W = 1; #RW# turn on warnings my $VERSION = "2.1mg"; my $RELEASE = "27th July, 2001"; # Colouring stuff my $a = 0.95; # tune these for the starting and ending concentrations of R,G,B my $b = 0.5; my $rgb = [ [$a,$b,$b], [$b,$a,$b], [$b,$b,$a], [$a,$a,$b], [$a,$b,$a], [$b,$a,$a] ]; my $rgbmax = 125; # tune these two for the outmost ranges of colour depth my $rgbmin = 240; #################################################################################### # Preferences # Comment out the "table" assignment to use the plain version my %prefs_colours = ( "part" => "#000099", "join" => "#009900", "server" => "#009900", "nickchange" => "#009900", "action" => "#CC00CC", ); my %prefs_colour_nick = ( "jdub" => "#993333", "cantanker" => "#006600", "chuckd" => "#339999", ); my %prefs_styles = ( "simplett" => "Text style with little use of colour", "tt" => "Text style using colours for each nick", "simpletable" => "Table style, without heavy use of colour", "table" => "Default style, using a table with bold colours", ); my $STYLE = "table"; #################################################################################### # Utility Functions & Variables sub output_nicktext { my ($nick, $text, $htmlcolour) = @_; if ($STYLE eq "table") { print OUTPUT "$nick"; print OUTPUT "$text<\/font>\n"; } elsif ($STYLE eq "simpletable") { print OUTPUT "$nick"; print OUTPUT "$text\n"; } elsif ($STYLE eq "simplett") { print OUTPUT "<\;$nick>\; $text
\n"; } else { print OUTPUT "<\;$nick>\;<\/font> $text<\/font>
\n"; } } sub output_servermsg { my ($line) = @_; if ($STYLE =~ /table/) { print OUTPUT "$line\n"; } else { print OUTPUT "$line
\n"; } } sub html_rgb { my ($i,$ncolours) = @_; $ncolours = 1 if $ncolours == 0; my $n = $i % @$rgb; my $m = $rgbmin + ($rgbmax - $rgbmin) * ($ncolours - $i) / $ncolours; my $r = $rgb->[$n][0] * $m; my $g = $rgb->[$n][1] * $m; my $b = $rgb->[$n][2] * $m; sprintf("#%02x%02x%02x",$r,$g,$b); } my $msg_usage = "Usage: irclog2html.pl [OPTION]... [FILE] Colourises and converts IRC logs to HTML format for easy web reading. -s, --style=[STYLE] format log according to specific style. style formats described using irclog2html [-s|--style] --colour-=[COLOUR] format output colour scheme. attributes described using irclog2html [--colour] Report bugs to Jeff Waugh . "; my $msg_styles = "The following styles are available for use with irclog2html.pl: simplett Text style with little use of colour tt Text style using colours for each nick simpletable Table style, without heavy use of colour table Default style, using a table with bold colours "; my $msg_colours = "The following attributes may be customized using the --colour parameter: join, part, action, server, nickchange "; ################################################################################ # Main sub main { my $inputfile; my $nick; my $time; my $line; my $text; my $htmlcolour; my $nickcount = 0; my $NICKMAX = 30; my %colours = %prefs_colours; my %colour_nick = %prefs_colour_nick; my %styles = %prefs_styles; # Quit if there is no filename specified on the command line # if ($#ARGV == -1) { die "Required parameter missing\n\n$msg_usage"; } # Loop through parameters, bringing filenames into $files # my $count = 0; while ($ARGV[$count]) { if ($ARGV[$count] =~ /-s|--style.*/) { $STYLE = $ARGV[$count]; if ($STYLE =~ /--style=.*/) { $STYLE =~ s/--style=(.*)/$1/; } else { $count++; $STYLE = $ARGV[$count]; } if ($STYLE eq "") { print $msg_styles; return 0; } elsif (!defined($styles{$STYLE})) { die "irclog2html.pl: invalid style: `$STYLE'\n\n$msg_styles"; } } elsif ($ARGV[$count] =~ /--colou?r.*/) { my $colour_pref = $ARGV[$count]; my $colour = $colour_pref; if ($colour_pref =~ /--colou?r$/) { print $msg_colours; return 0; } else { $colour_pref =~ s/--colou?r-(.*)?=.*/$1/; $colour =~ s/--colou?r-.*?=(.*)/$1/; $colours{$colour_pref} = $colour; } } else { $inputfile = $ARGV[$count]; } $count++; } # Open input and output files # if (!$inputfile) { # no file to open, print appropriate usage information die "\n$msg_usage"; } elsif (!open(INPUT, $inputfile)) { # not a vaild file to open, spew error and usage information die "irclog2html.pl: cannot open $inputfile for reading\n\n$msg_usage"; } if (!open(OUTPUT, ">$inputfile.html")) { # can't open file for output, spew error die "irclog2html.pl: cannot open $inputfile.html for writing\n"; } # Begin output # print OUTPUT qq{ $inputfile }; if ($STYLE =~ /table/) { print OUTPUT "\n"; } while ($line = ) { chomp $line; if ($line ne "") { # Replace ampersands, pointies, control characters # $line =~ s/&/&\;/g; $line =~ s//>\;/g; $line =~ s/[\x00-\x1f]+//g; # Replace possible URLs with links # $line =~ s/((http|https|ftp|gopher|news):\/\/\S*)/$1<\/a>/g; # Rip out the time # if ($line =~ /^\[?(?:\d\d\d\d-\d\d-\d\dT)?\d\d:\d\d(:\d\d)?\]? +.*$/) { $time = $line; $time =~ s/^\[?(?:\d\d\d\d-\d\d-\d\dT)?(\d\d:\d\d(:\d\d)?)\]? +.*$/$1/; $line =~ s/^\[?(?:\d\d\d\d-\d\d-\d\dT)?\d\d:\d\d(:\d\d)?\]? +(.*)$/$2/; ## print $time; } # Colourise the comments if ($line =~ /^<\;.*?>\;\s.*/) { # Split $nick and $line $nick = $line; $nick =~ s/^<\;(.*?)>\;\s.*$/$1/; # $nick =~ tr/[A-Z]/[a-z]/; # <======= move this into another function when getting nick colour $text = $line; $text =~ s/^<\;.*?>\;\s(.*)$/$1/; $text =~ s/ / \; \;/g; $htmlcolour = $colour_nick{$nick}; if (!defined($htmlcolour)) { # new nick $nickcount++; # if we've exceeded our estimate of the number of nicks, double it $NICKMAX *= 2 if $nickcount >= $NICKMAX; $htmlcolour = $colour_nick{$nick} = html_rgb($nickcount, $NICKMAX); } output_nicktext($nick, $text, $htmlcolour); } else { # Colourise the /me's # if ($line =~ /^\* .*$/) { $line =~ s/^(\*.*)$/$1<\/font>/; } # Colourise joined/left messages # elsif ($line =~ /^(\*\*\*|-->) .*joined/) { $line =~ s/(^(\*\*\*|-->) .*)/$1<\/font>/; } elsif ($line =~ /^(\*\*\*|<--) .*left|quit/) { $line =~ s/(^(\*\*\*|<--) .*)/$1<\/font>/; } # Process changed nick results, and remember colours accordingly # elsif ($line =~ /^(\*\*\*|---) (.*?) are|is now known as (.*)/) { my $nick_old; my $nick_new; $nick_old = $line; $nick_old =~ s/^(?:\*\*\*|---) (.*?) (?:are|is) now known as .*/$1/; $nick_new = $line; $nick_new =~ s/^(?:\*\*\*|---) .*? (?:are|is) now known as (.*)/$1/; $colour_nick{$nick_new} = $colour_nick{$nick_old}; $colour_nick{$nick_old} = undef; $line =~ s/^((\*\*\*|---) .*)/$1<\/font>/ } # server messages elsif ($line =~ /^(\*\*\*|---) /) { $line =~ s/^((\*\*\*|---) .*)$/$1<\/font>/; } output_servermsg($line); } } } if ($STYLE =~ /table/) { print OUTPUT "
\n"; } print OUTPUT qq{
Generated by irclog2html.pl $VERSION by
Jeff Waugh - find it at freshmeat.net!
}; close INPUT; close OUTPUT; return 0; } exit main;