#!/usr/bin/perl
$DEBUG = 0;
@COLOR = qw/31 32 33 34 35 36 37/ ;
%ENTITY = ( '"' => '"' ,
'&' => '&' ,
'<' => '<' ,
'>' => '>' ,
' ' => ' ' ,
) ;
if( $ARGV[0] =~ /^-(\d)/ ) {
$MODE = $1 ;
shift @ARGV ;
} elsif( $ARGV[0] eq '-h') {
usage();
} else {
$MODE = 2;
}
$filename = $ARGV[0] ;
$content = join( '' , <> ) ;
if( $MODE == 3 )
{
print '' . "\n";
print '' . "\n";
print '
' . "\n";
print '' . ($filename ? $filename : '' ) . '' . "\n";
print '' . "\n";
print '' . "\n\n";
print '' . "\n";
print '' . "\n";
print '| 日期 | 時間 | 傳訊者 | 收訊者 | 訊息 |
' . "\n";
}
while( $content =~ /()/sg )
{
$message = $1 ;
print "[$message]\n" if $DEBUG ;
( $date , $time , $sessionid , $from , $to , $msg ) =
( $message =~ /<\/From><\/To>(.+?)<\/Text><\/Message>/s ) ;
for $ent ( keys %ENTITY ) {
$msg =~ s/$ent/$ENTITY{$ent}/g ;
}
print "date\t: $date\ntime\t: $time\nsessionid\t: $sessionid\nfrom\t: $from\nto\t: $to\nmsg\t: $msg\n" if $DEBUG ;
if( $sessionid != $lastid ) {
$color++;
$color = 0 if ($color > $#COLOR);
if( $bgcolor ) {
$bgcolor = '';
} else {
$bgcolor= 'bgcolor="#99FFFF"';
}
}
$lastid = $sessionid ;
if( $MODE == 1 )
{
print "[1;" . $COLOR[$color] . "m";
print "$date $time\n";
print "【$from】 對 【$to】 說:\n";
print "$msg[m\n";
print "[1;30m" . "="x100 . "[m\n";
}
elsif( $MODE == 2 )
{
print "[1;" . $COLOR[$color] . "m";
print "$date $time , 【$from】 說:\n\n";
print "$msg[m\n";
print "[1;30m" . "="x100 . "[m\n";
}
elsif( $MODE == 3 )
{
print "";
print "| $date | ";
print "$time | ";
print "$from | ";
print "$to | ";
print "$msg | ";
print "
\n";
}
}
if( $MODE == 3 )
{
print '' . "\n";
print '' . "\n";
}
sub usage
{
print "Usage: $0 [-{123}] filename\n\n";
print "\t-1 : normal format\n";
print "\t-2 : simple format (default)\n";
print "\t-3 : html format\n\n";
print "\tTips:\n";
print "\tcat filename | $0\n";
print "\tmerge.pl file1 file2 | $0\n";
print "\t\t$0 filename | less -R\n";
print "\t\t$0 filename > newfile\n";
exit(-1);
}