Last active
February 14, 2016 10:34
-
-
Save lexszero/3afb77cf031e9527aee8 to your computer and use it in GitHub Desktop.
A simple colorized pretty printer for Tkabber and MCabber XMPP clients chatlogs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Term::ANSIColor; | |
my %nick_colors; | |
sub unescape($) { | |
my $x = shift; | |
$x =~ s/(^\{|\}$)//g; | |
$x =~ s/\\n/\n/g; | |
$x =~ s/\\\\/\\/g; | |
return $x; | |
} | |
sub parse { | |
my $fh = shift; | |
while (<$fh>) { | |
/^timestamp (?<time>.*?) jid (?:.*?) nick (?<nick>.*?) body (?<body>.*?)( me \d)?( type \w+)?$/ or | |
/^M[A-Z] (?<time>.*?) (?<lines>\d{3}) (?:<(?<nick>.*?)> (?<body>.*)|(?<body>.*))$/ or next; | |
my ($time, $nick, $body) = ($+{time}, unescape($+{nick} or ""), unescape($+{body})); | |
if ($+{lines}) { | |
my $i = int($+{lines}); | |
while (($i-- > 0) and defined($_ = <$fh>)) { | |
chomp; | |
#print "EXTRA LINE $i: $_\n"; | |
$body .= "\n$_"; | |
}; | |
} | |
$time =~ s/(....)(..)(..)T(..):?(..):?(..).*/$3.$2.$1 $4:$5:$6/; | |
if ($nick) { | |
my $col = $nick_colors{$nick}; | |
if (not $col) { | |
my $c = unpack("%32C*"); | |
my @rgb = ($c % 6, (int($c / 10) % 6), (int($c / 100) % 6)); | |
if ($rgb[0] == $rgb[1]) { | |
$rgb[0] = ($rgb[0] + 1) % 6; | |
} | |
if ($rgb[1] == $rgb[2]) { | |
$rgb[1] = ($rgb[1] + 2) % 6; | |
} | |
$col = "rgb$rgb[0]$rgb[1]$rgb[2]"; | |
$nick_colors{$nick} = $col; | |
}; | |
print colored('[', 'grey3'), | |
colored($time, 'grey16'), | |
colored('] ', 'grey3'), | |
colored($nick, $col, 'bold'), | |
": $body\n"; | |
} else { | |
print colored($body, 'grey8'), "\n"; | |
} | |
#print "$body\n"; | |
} | |
} | |
if ($#ARGV == -1) { | |
parse(\*STDIN); | |
} else { | |
foreach my $fn (@ARGV) { | |
print "\n$fn:\n"; | |
open my $fh, "<$fn" or die $!; | |
parse($$fh); | |
close $fh; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment