#!/usr/bin/perl -w
# LICENSE:                                                                                                                                                                    
# This program is free software; you can redistribute it and/or modify                                                                                                        
# it under the same terms as Perl itself, either Perl version 5.8.6 or,                                                                                                       
# at your option, any later version of Perl 5 you may have available.     
#
# Authors:
# 	Moritz Lenz <moritz@faui2k3.org>, http://moritz.faui2k3.org/
#	(conversion from icq name to filename)
#
#	Daniel Michalik <dm@blackamp.de>, http://www.blackamp.de/
#	(input routine)
#
# This script comes with no warranty. It was tested using centericq 4.21.0.
#
# KNOWN BUGS:
# 	- Deletes empty lines from messages.
#	- Errors in input file format are not handled (you may consider this as a feature as well ;)

#--------

use strict;
use warnings;

use Term::ANSIColor qw(:constants color colored);

my $found = 0;
my @nonexact_matches;
my $base_dir = glob("~/.centericq/");
if (@ARGV){
	opendir(DIR, glob("~/.centericq/")) 
		or die "Cant read dir $base_dir: $!";

	foreach (@ARGV){
		while(my $d = readdir DIR){
#		print STDERR $d, $/;
			my $file = $base_dir . $d . "/info";
#			print STDERR $file, $/;
			if ($d =~ m/^\d+$/ and -d $base_dir . $d and -r $file){
				open(INFO, $file) or die "Cant read $file: $!";
				my $hist_file = $base_dir . $d . "/history";
				# the first line is nick, second is first name
				# and third ist last name.
				# if nick matches the argument, print out the file.
				my $nick = <INFO>;
				chomp $nick;
				if (uc($nick) eq uc){
					print STDERR "Reading $hist_file ... \n";
					read_and_print($hist_file);
					$found = 1;
				} else {
					my $first_name = <INFO>;
					my $last_name = <INFO>;
					chomp ($first_name, $last_name);
					if (uc ($first_name) eq uc or
							uc ($last_name) eq uc){
						push @nonexact_matches, $hist_file;
					}
				}
				close INFO;
			}
		}

		close DIR;
		if ((not $found) and @nonexact_matches){
			print STDERR "No such nick, falling back to detection names\n";
			foreach (@nonexact_matches){
				print STDERR "Name of $_ matches...\n";
				read_and_print($_);
			}
		}
	}
} else {
	read_and_print();
}

sub read_and_print {
	my $fh;
#	print STDERR "Arg: @_\n";
	if (@_){
		my $fn = shift;
		open ($fh, $fn) or die "Can't read $fn: $!";
	} else {
		$fh = \*STDIN;
	}
		
	if(!<$fh>) {
		die "Input empty.\n";
	}
	my $cntin = 0;
	my $cntout = 0;
	while(1){
		my $in = (<$fh> =~ /IN/) ? 1 : 0;
		my $msg = (<$fh> =~ /MSG/) ? 1 : 0;
		my $time = <$fh>;
		my $first = 1;
		<$fh>;
		my $data = "";
		my $temp;
		$temp = <$fh>;
		last if (eof $fh);
		while(!($temp =~ /^$/) ) {
			chomp $temp;
			$temp =~ s/\n//;
			$temp =~ s/\r//;
			if($temp ne "") {
				if($first == 1) {
					$data .= $temp;
					$first = 0;
				} else {
					$data .= "\n" . "                        "  . $temp;
				}
			}
			$temp = <$fh>;
			last if (eof $fh);
		}
		chomp $in;
		if($msg) {
			if (not -p STDOUT){
				if ($in){
					print color 'bold blue';
					print time_fmt($time), ", In:   "; 
				} else {
					print color 'bold red';
					print time_fmt($time), ", Out:  "; 
				}
				print color 'reset';
				print $data, $/;
			} else {
				print time_fmt($time),  ", ", 
				      ($in ? "In:   " : "Out:  "), $data, $/;
			}
		}
		if($in) {
			$cntin++;
		} else {
			$cntout++;
		}
	}

	print "created " . localtime(time()) . ", messages: " . ($cntin + $cntout) . ", outgoing: " . $cntout . ", incoming: " .$cntin . "\n";
}

sub time_fmt {
	my $t = shift;
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($t);
	$year += 1900;
	$mon ++;
	return sprintf("%04d-%02d-%02d %02d:%02d", $year, $mon, $mday, $hour, $min);
#	return "$year-$mon-$mday $hour:$min";
}
