#!/usr/bin/perl

# irclog2html.pl Version 0.5 - 28th March 2000

# by Jeff Waugh
# jdub@aphid.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.

# irclog2html will eventually output randomly generated coloured text
# for each unique user. It doesn't do this currently, as Perl is fscking with
# my mind... Search for automagic and you'll see the area of trouble. I would
# love to see a solution to this, as I've been trying to beat it into
# submission for a while now! :)


use strict;


# Quit if there is no filename specified on the command line #
if ($#ARGV == -1) {
  die "Required parameter missing\n\nUsage: irclog2html filename\n\n";
}


# Preferences #
my %colour_nick = (
  "jdub", "#993333",
  "cantanker", "#006600",
  "chuckd", "#339999",
);
my $colour_left = "#000099";
my $colour_joined = "#009900";
my $colour_action = "#CC00CC";

my $line;
my $nick;


# 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 "<html><body color=\"#000000\" bgcolor=\"#ffffff\"><tt>";

while ($line = <INPUT>) {
  # Change "pointies" to character codes #
  $line =~ s/</&lt\;/g;
  $line =~ s/>/&gt\;/g;

  # Take out those nasty timestamps that mIRC puts in #
  $line =~ s/^\[\d\d:\d\d\] (.*)/$1/;

  # Colourise joined/left/server messages #
  if ($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_joined">$1<\/font>/;
  }

  # Colourise the /me's #
  elsif ($line =~ /^\* /) {
    $line =~ s/^(\*.*)/<font color="$colour_action">$1<\/font>/;
  }

  # Starting to build automagic colouring #
  # Which we're mpt doing because the if exists line doesn't work... #

  elsif ($line =~ /^&lt\;.*?&gt\;/) {
    #$nick = $line;
    #$nick =~ s/^&lt\;(.*?)&gt\;.*$/$1/;

    # Colour each line by nick colour preferences #
    #if (exists $colour_nick{$nick}) {

    $line =~ s/^&lt\;(.*?)&gt\;(.*)$/<font color="$colour_nick{$1}">&lt\;$1&gt\;$2<\/font>/;

    #}
    #else {
    #  $colour_nick{$nick} = "#999900"
    #}
  }

  # Throw in linebreaks at the end of every line #
  $line =~ s/^(.*)$/$1<br>/;

  print OUTPUT $line;
}

print OUTPUT "<br>Generated by irclog2html.pl by <a href=\"mailto:jdub\@NOSPAMaphid.net\">Jeff Waugh</a>";
print OUTPUT " - find it at <a href=\"http://freshmeat.net/appindex/2000/03/28/954251322.html\">freshmeat.net</a>!</tt></body></html>";

close INPUT;
close OUTPUT;
