<?php
#
# egg2html.php (c) 2000,2001 by maol
# available under the GNU license
#
# Design notes: it's about the first serious piece of php programming I'm
# doing, so be kind ;-) I hope I don't do too bad.
#
# TODO: 
# maybe save parsed file to a file, reducing load
#
# global vars
#########################

# channel vars
$channels	= array (
	"symlink"	=> "",
	"gtk-gnutella"		=> "",
	"icewm"		=> ""
);
//if (!isset ($_GET[channel])) { $_GET[channel] = "symlink"; }
$channels["$_GET[channel]"] = "selected";

# date vars
$today  =	getEggDate(getdate());
if (!isset ($_GET[date])) { $_GET[date] = $today; }
# view vars
$MODECOLOR	=	"gray"; // alternatively use a color like "#c6c6c6"
$ACTIONCOLOR	=	"red";  // same applies here.
$colors	= array (  "#0000FF", "#00FF00", "#FF0000", "#FF00FF", "#888800",
	"#000088", "#008800", "#880000", "#008800", "#880088", "#FF88FF",
	"#FF8888", "#8888FF","#FF0088", "#FF8800", "#0088FF", "#8800FF");
# shitty colors: "#FFFF00", "#00FFFF", "#FFFF88", "#88FFFF", "#88FF88",
#                "#00FF88", "#88FF00". No. of good colors: 17, that's enough.

# file system vars
$BASE 	= 	".";
$LOGS 	=	"/home/melone/botlogs";
$HEADER = 	"$BASE/header.inc";
$FOOTER = 	"$BASE/footer.inc";
$LOG	=	"$LOGS/$_GET[channel].log.$_GET[date]";

# variable variables
$nicks = array ("maol", "MelOne");
$line;		// the chat text
$linedate;	// the date of the chat line
$previous	= getPrev($_GET[date]);
$next		= getNext($_GET[date]);

#######################################################################
#                                                                     #
#    P R O C E D U R E    D I V I S I O N        ( M A I N )          #
#                                                                     #
#######################################################################
# include header
include ("$HEADER");

if (isset ($_GET[channel])) { // normal operation
	if (file_exists ($LOG)) {

		// the table 'previous' and 'next' a.k.a. yesterday and tomorrow
		echo "<table width=\"100%\" border=\"0\"><tr>\n";
		echo "<td align=\"left\"><a href=\"$_SERVER[PHP_SELF]?channel=$_GET[channel]&date=$previous\">previous day</a></td>\n";
		echo "<td align=\"right\"><a href=\"$_SERVER[PHP_SELF]?channel=$_GET[channel]&date=$next\">next day</a></td>\n";
		echo "</tr></table>\n";

		echo "<pre>\n";
		$file = fopen ("$LOG", r);
		while ($chatline = fgets ($file, 4096)) {
			sepDate($chatline);
//			$line	= nl2br (htmlentities ($line)); // replace newlines and entities
			$line	= htmlentities ($line); 		// replace newlines and entities
			$line	= doIt($line);	// do the main stuff
			$line	= parseURL($line);	// <a href=...>
			echo "$linedate $line";
		}
		echo "</pre>\n";

		// the table 'previous' and 'next' a.k.a. yesterday and tomorrow
		echo "<table width=\"100%\" border=\"0\"><tr>\n";
		echo "<td align=\"left\"><a href=\"$_SERVER[PHP_SELF]?channel=$_GET[channel]&date=$previous\">previous day</a></td>\n";
		echo "<td align=\"right\"><a href=\"$_SERVER[PHP_SELF]?channel=$_GET[channel]&date=$next\">next day</a></td>\n";
		echo "</tr></table>\n";
	} else { // file not opened
		echo "bad luck, there's no log file for $_GET[channel] on $_GET[date]";
	}
} else { // no input
	echo "no channel selected yet, please do so now!";
}

# include footer
include ("$FOOTER");

#######################################################################
#                                                                     #
#     S U B F U N C T I O N S    O F    E G G 2 H T M L . P H P       #
#                                                                     #
#######################################################################

function parseURL($line) {
# parse $line for URLs and return <a href=url>url</a> formatted
//	$line = eregi_replace ("(http://[[:alnum:]\.\-]+[:alpha:]{2,3}/[[:alnum:]/;~\?\&_\-=]*)", "<a href=\"\\1\">\\1</a>", $line);
	$line = ereg_replace("((ftp://)|(http://))(([[:alnum:]]|[[:punct:]])*)", "<a href=\"\\0\">\\0</a>",$line);
//	$line = eregi_replace ("(http://[[:alnum:]\.\-]*\.[:alpha:]{2,3}/?)", "<a href=\"\\1\">\\1</a>", $line);
	return $line;
}

function sepDate($chatline) {
# separate the date from the rest of the line
	global $line, $linedate;
	$twobits = explode (" ", $chatline, 2);
	$line = $twobits[1]; $linedate = $twobits[0];
}

function colorNicks($line, $nick) {
# add colors to the nicks
	global $nicks, $colors;
	for ($i = 0; $i < sizeof ($nicks); $i++) {
		if ($nicks[$i] == $nick) {
			$color = $colors[$i % 17];
//			$line = str_replace ("&lt;$nicks[$i]&gt;", "&lt;<font color=\"$colors[$i]\">$nicks[$i]</font>&gt;", "$line");
			return str_replace ("&lt;$nick&gt;", "&lt;<font color=\"$color\">$nick</font>&gt;", "$line");
		}
	}
//	return $line;
}

function doIt($line) {
# the main routine
	global $nicks, $ACTIONCOLOR, $MODECOLOR;

	# check out if it's a normal line (beginning with <nick>)
	if (ereg ("^&lt;([][a-zA-Z0-9`{}_^|-]*)&gt;", $line, $newnick)) {
		# yup, a normal line, add nick if it's new
		if (!in_array ($newnick[1], $nicks)) {
			array_push ($nicks, $newnick[1]);
		}
		# and colorize it
		return colorNicks ($line, $newnick[1]);
	} else {
		# an administrativia or action line
		if (isActionLine ($line)) {
			# an action line
			return "<font color=\"$ACTIONCOLOR\">$line</font>";
		}
		# detect nick changes, then return a $MODECOLOR colored line
		nickChange ($line);
		return "<font color=\"$MODECOLOR\">$line</font>";
	}
}

function getEggDate($date) {
# return a string for a date in eggdrop's represantion
# i.e. DDMonYYYY
	$day	= $date[mday];
	if ($day < 10) { $day = "0$day"; }
	$month	= substr ("$date[month]", 0, 3);
	$year	= $date[year];
	return "$day$month$year";
}

function getPrev($date) {
# calculate the day before
	return getEggDate(getdate(strtotime("$date") - 86400));
}

function getNext($date) {
# calculate the next day
	return getEggDate(getdate(strtotime("$date") + 86400));
}

function isActionLine($line) {
# detect /me lines...
	if (strstr ($line, "Action: ")) {
		return true;
	} // else...
	return false;
}

function nickChange($line) {
# change our nicks hash if someone changes his/her nick
	global $nicks;
	if (strstr ($line, "Nick change: ")) {
		# nick change detected
		$boobs = explode (" ", $line); // $boobs[2] is the old nick, $boobs[4] the new
		for ($i = 0; $i < sizeof ($nicks); $i++) {
			if ($nicks[$i] == $boobs[2]) {
//				$nicks[$i] = chop (str_replace ("<br>", "", $boobs[4]));;
				$nicks[$i] = chop ($boobs[4]);
				break;
			}
		}
	}
}
php?> 
