#!/usr/bin/perl

# irclog2html.pl Version 1.5 - 11th May 2000
# Copyright (C) 2000, Jeffrey W. Waugh

# Author:
#   Jeff Waugh <jdub@aphid.net>

# Contributors:
#   Rick Welykochy <rick@praxis.com.au>
#   Alexander Else <aelse@uu.net>

# Released under the terms of the GNU GPL
# 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


####################################################################################
# Preferences

# Comment out the "table" assignment to use the plain version

#my $STYLE		=	"tt";
#my $STYLE		=	"simplett";
my $STYLE		=	"table";
#my $STYLE		=	"simpletable";

my $colour_left			=	"#000099";	# nick leaving channel
my $colour_joined		=	"#009900";	# nick joining channel
my $colour_server		=	"#009900";	# server message (***)
my $colour_nickchange	=	"#009900";	# nick change
my $colour_action		=	"#CC00CC";	# nick action (/me waves)

my %prefs_colour_nick = (
	"jdub"			=>	"#993333",
	"cantanker"		=>	"#006600",
	"chuckd"		=>	"#339999",
);


####################################################################################
# Utility Functions

sub output_nicktext {
	my ($nick, $text, $htmlcolour) = @_;

	if ($STYLE eq "table") {
		print OUTPUT "<tr><th bgcolor=\"$htmlcolour\"><font color=\"#ffffff\"><tt>$nick</tt></font></th>";
		print OUTPUT "<td width=\"100%\" bgcolor=\"#eeeeee\"><tt><font color=\"$htmlcolour\">$text<\/font></tt></td></tr>\n";
	}
	elsif ($STYLE eq "simpletable") {
		print OUTPUT "<tr bgcolor=\"#eeeeee\"><th><font color=\"$htmlcolour\"><tt>$nick</tt></font></th>";
		print OUTPUT "<td width=\"100%\"><tt>$text</tt></td></tr>\n";
	}
	elsif ($STYLE eq "simplett") {
		print OUTPUT "&lt\;$nick&gt\; $text<br>\n";
	}
	else {
		print OUTPUT "<font color=\"$htmlcolour\">&lt\;$nick&gt\; $text<\/font><br>\n";
	}
}

sub output_servermsg {
	my ($line) = @_;

	if ($STYLE =~ /table/) {
		print OUTPUT "<tr><td colspan=2><tt>$line</tt></td></tr>\n";
	}
	else {
		print OUTPUT "$line<br>\n";
	}
}

sub html_rgb
{
	my ($i,$ncolours) = @_;
	$ncolours = 1 if $ncolours == 0;

	my $rgbmax = 125;		# tune these two for the outmost ranges of colour depth
	my $rgbmin = 240;

	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 $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);
}


####################################################################################
# Main

sub main {

	my $files;
	my $filecount = 0;

	my $line;
	my $nick;
	my $text;

	my $htmlcolour;
	my $nickcount = 0;
	my $NICKMAX = 30;

	my %colour_nick = %prefs_colour_nick;


	# Quit if there is no filename specified on the command line #
	if ($#ARGV == -1) {
		die "Required parameter missing\n\nUsage: irclog2html filenames\n\n";
	}


	# Loop through parameters, bringing filenames into $files #
	while ($ARGV[$filecount]) {
		
		$filecount++;
	}

	# Open input and output files #
	if (!open(INPUT, "$ARGV[0]")) {
		die "Cannot open $ARGV[0] for reading!\n\n";
	}
	if (!open(OUTPUT, ">$ARGV[0].html")) {
		die "Cannot open $ARGV[0].html for writing!\n\n";
	}


	# Begin output #
	print OUTPUT qq{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
	<title>$ARGV[0]</title>
	<meta name="generator" content="irclog2html.pl by Jeff Waugh">
	<meta name="version" content="Version 0.9 - 5th April 2000">
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body text="#000000" bgcolor="#ffffff"><tt>
};

	if ($STYLE =~ /table/) {
		print OUTPUT "<table cellspacing=3 cellpadding=2 border=0>\n";
	}

	while ($line = <INPUT>) {

		chomp $line;

		if (!$line eq "") {

			# Replace ampersands, pointies, control characters #
			$line =~ s/&/&amp\;/g;
			$line =~ s/</&lt\;/g;
			$line =~ s/>/&gt\;/g;
			$line =~ s/[\x00-\x1f]+//g;

			# Replace possible URLs with links #
			$line =~ s/((http|https|ftp|gopher|news):\/\/\S*)/<a href="$1">$1<\/a>/g;

			# Take out those nasty timestamps that mIRC puts in #
			if ($line =~ /^\[\d\d:\d\d\] .*$/) {
				$line =~ s/^\[\d\d:\d\d\] .*$/$1/;
			}

			# Colourise the comments
			if ($line =~ /^&lt\;.*?&gt\; .*$/) {

				# Split $nick and $line
				$nick = $line;
				$nick =~ s/^&lt\;(.*?)&gt\; .*$/$1/;

				# $nick =~ tr/[A-Z]/[a-z]/;
				# <======= move this into another function when getting nick colour

				$text = $line;
				$text =~ s/^&lt\;.*?&gt\; (.*)$/$1/;
				$text =~ s/  /&nbsp\;&nbsp\;/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 {
				# Process changed nick results, and remember colours accordingly #
				if ($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 (.*)/$2/;

					$colour_nick{$nick_new} = $colour_nick{$nick_old};
					$colour_nick{$nick_old} = undef;

					$line =~ s/(\*\*\* .*)/<font color=\"$colour_nickchange\">$1<\/font>/
				}

				# Colourise joined/left/server messages #
				elsif ($line =~ /\*\*\* .*left|quit/) {
					$line =~ s/(\*\*\* .*)/<font color=\"$colour_left\">$1<\/font>/;
				}
				elsif ($line =~ /\*\*\* .*joined/) {
					$line =~ s/(\*\*\* .*)/<font color=\"$colour_joined\">$1<\/font>/;
				}
				elsif ($line =~ /\*\*\* /) {
					$line =~ s/(\*\*\* .*)$/<font color=\"$colour_server\">$1<\/font>/;
				}

				# Colourise the /me's #
				elsif ($line =~ /^\* .*$/) {
					$line =~ s/^(\*.*)$/<font color=\"$colour_action\">$1<\/font>/;
				}

				output_servermsg($line);
			}
		}
	}

	if ($STYLE =~ /table/) {
		print OUTPUT "</table>\n";
	}

	print OUTPUT qq{
<br>Generated by irclog2html.pl by <a href="mailto:jdub\@NOSPAMaphid.net">Jeff Waugh</a>
 - find it at <a href="http://freshmeat.net/appindex/2000/03/28/954251322.html">freshmeat.net</a>!
</tt></body></html>};

	close INPUT;
	close OUTPUT;

	return 0;
}

exit main;
