#!/usr/bin/env perl use warnings; # # xchatlogsplit - split IRC logs, created by xchat, by date # # $Id$ use strict; my $me = $0; $me =~ s#.*/##; sub usage { @_ and warn join( ' ', "Error:", @_ ), "\n"; die "Usage: $me xchatlog1 [xchatlog2 [...]] targetdirectory\n"; } my $targetdir = pop(@ARGV); defined $targetdir or &usage; -d $targetdir && -w $targetdir or &usage("no directory or not writeable: $targetdir"); my @files = grep { !/^-/ } @ARGV or &usage("no file arguments given"); map { -f $_ && -r $_ or &usage("nonexistent, nonreadable or non-plain file: '$_'") } @files; sub start2file # given a $starttime (a starting moment) # supplies the basename of a file in which the chunk will be stored # note that multiple chunks may be stored within that file { # no check on the input format defined $_[0] or die "bug: start2file called with undefined argument"; my ( $weekday, $month, $monthday, $time, $year ) = grep { defined $_ } split( / +/, $_[0] ); defined $year ? lc( $monthday . $month . $year ) : $_[0]; } sub startofchunk # given the BEGIN LOGGING AT line of a chunk, returns the starting moment { if ( $_[0] =~ /BEGIN LOGGING AT / ) { my $start = $'; chomp $start; $start; } else { undef; } } my %starttime2lines; # a hash mapping chunk names to refs of arrays of lines while ( my $inputfile = shift @files ) { my $starttime = undef; open( INPUT, '<' . $inputfile ) or &usage("cannot open input file '$inputfile'"); while () { if (/^\*\*\*\* BEGIN LOGGING AT /) { # a new chunk begins if ( defined $starttime ) { warn "unfinished chunk '$starttime' at $inputfile:$.\n"; } $starttime = &startofchunk($_); warn "chunk saved to file ", &start2file($starttime), "\n"; push( @{ $starttime2lines{$starttime} }, $_ ); } elsif (/^\*\*\*\* ENDING LOGGING AT /) { if ( !defined $starttime ) { warn "unstarted chunk at $inputfile:$.\n" if !defined $starttime; } else { push( @{ $starttime2lines{$starttime} }, $_ ); warn scalar( @{ $starttime2lines{$starttime} } ) . " lines in chunk started at $starttime\n"; undef $starttime; } } #elsif (!defined($starttime) && defined($_) && /\S/) elsif ( !defined $starttime ) { /\S/ and warn "text outside chunk at $inputfile:$.\n"; } else { push( @{ $starttime2lines{$starttime} }, $_ ); } } close(INPUT); } warn "hey\n"; foreach my $starttime ( sort keys %starttime2lines ) { warn "saving for $starttime\n"; map { print "$starttime: $_" } @{ $starttime2lines{$starttime} }; }