Skip to content

Instantly share code, notes, and snippets.

@antonlindstrom
Created May 17, 2009 19:07
Show Gist options
  • Save antonlindstrom/113113 to your computer and use it in GitHub Desktop.
Save antonlindstrom/113113 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
#
# Parsing an IRC logfile and writing it
# to a colored html-file. When generated
# it prints amount of different messages
# exists.
#
# Colorlist exists as plain text at
# http://code.antonlindstrom.com/colorlist
#
# Author Anton Lindstrom
# me@antonlindstrom.com
#
use strict;
use warnings;
# Open file, irc.log
open(LOG,"irc.log");
my @irclog = <LOG>;
close(LOG);
my @users;
my @htmlshizzle;
my $systemcount=0;
my $usercount=0;
my $emocount=0;
foreach (@irclog) {
$_ =~ m/(\d+:\d+)?\s+((<\s([\w\-]+)>(.+))|(-!-\s(.+))|(\*\s([\w\-]+).+)|(.+([0-9]{2}:[0-9]{2}):))/gi;
# Parts of regular expression is set to variables for better readability.
my ($date, $userfull, $username, $usermsg, $sys, $sysmsg, $emo, $emouser, $syslog, $syslogt)
= ($1, $3, $4, $5, $6, $7, $8, $9, $10, $11);
# If something is matched expression is true.
if ($syslog) {
print "$syslog\n";
push(@htmlshizzle, &format($syslogt, "logfile", "system", $syslog));
$systemcount++;
}
if ($sys) {
push(@htmlshizzle, &format($date, "sys", "system", $sysmsg));
$systemcount++;
}
if ($userfull) {
push(@htmlshizzle, &format($date, $username, "user", $usermsg));
$usercount++;
unless (grep(/$username/,@users)) {
push(@users, $username);
}
}
if ($emo) {
push(@htmlshizzle, &format($date, "e", "emotion", $emo));
$emocount++;
}
}
# For fun print how many different types.
print "Generating HTML.\n";
print "There are $systemcount system messages.\n";
print "There are $usercount user messages.\n";
print "There are $emocount emotions.\n";
print "DONE.\n";
# Print to file:
my $htmlhead = "<html><head><link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\"><title>IRCLOG</title></head><body><table>";
open(HTML, "> irclog.html");
print HTML "$htmlhead\n";
foreach (@htmlshizzle) {
print HTML "$_\n";
}
close(HTML);
# Print CSS
open(CSS, "> style.css");
print CSS &color(@users);
close(CSS);
# Format HTML.
sub format {
# Input.
my ($time, $user, $type, $message) = @_;
# Replace <> with &lt; and &gt;
$message =~ s/</&lt;/g;
$message =~ s/>/&gt;/g;
# Replace smilies with images and make HTML urls.
$message =~ s/\:\-\)/<img src="happy.png">/g;
$message =~ s/\=\)/<img src="happy.png">/g;
$message =~ s/\:\-\(/<img src="sad.png">/g;
$message =~ s/\=\(/<img src="sad.png">/g;
$message =~ s!(http://[^\s]+)!<a href="$1" target="_new">$1</a>!gi;
# Formatted table row.
my $tablerow = "<tr><td class=\"time\">$time</td><td class=\"$user\">$user</td><td class=\"$type\">$message</td></tr>";
return $tablerow;
}
# Check colors.
sub color {
my (@allusers) = @_;
# Stdcolors is on system, user and emotion backgrounds.
my $stdcolors = ".system{background: #FF00FF}\n.user{background: #CCC}\n.emotion{background: #00FFFF}\n";
my $generatedcolors;
my $counter=0;
# Open colorlist.
open (COLOR, "colorlist2");
my @colors = <COLOR>;
my $amountofcolors = scalar(@colors);
foreach (@colors) {
# If there are no users, go next.
next if (scalar(@users) == $counter);
chomp($_);
# Remove whitespaces from file.
$_ =~ s/\s//g;
# Make CSS background for user.
$generatedcolors .= ".$allusers[$counter] {background: $_}\n";
# Increment counter.
$counter++;
# If colors are used, reuse from the top.
if($amountofcolors == $counter) {
$counter = 0;
}
}
close(COLOR);
# CSS output.
my $css = $stdcolors.$generatedcolors;
return $css;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment