daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest May 16th, 2018 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. # original by towo <towo@ydal.de> under CC-BY
  3.  
  4. use strict;
  5. use File::Basename qw( basename );
  6. use File::Spec::Functions qw( catfile );
  7. use Scalar::Utils qw( openhandle );
  8.  
  9. my %monthnum = qw(
  10.     Jan 01    Feb 02    Mar 03
  11.     Apr 04    May 05    Jun 06
  12.     Jul 07    Aug 08    Sep 09
  13.     Oct 10    Nov 11    Dec 12
  14. );
  15.  
  16. foreach my $file ( @ARGV ) {
  17.  
  18.     open my $fh, '<', $file or do {
  19.         warn "Unable to open $file for reading: $!\n";
  20.         next;
  21.     };
  22.  
  23.     ( my $header = <$fh> ) =~ s!<.*?>!!g;
  24.     if ( $header !~ m{^(<HTML><HEAD><TITLE>)?IM Sessions with .*?(</TITLE></HEAD><BODY BGCOLOR=".*?">)?$}i ) {
  25.         warn "$file does not seem to be a Gaim conversation.\n";
  26.         next;
  27.     }
  28.  
  29.     my $target = basename $file, '.log';
  30.     unless ( -d $target or mkdir $target ) {
  31.         warn "Could not create directory $target: $!\n";
  32.         next;
  33.     }
  34.  
  35.     my $outfh;
  36.  
  37.     while ( <$fh> ) {
  38.  
  39.         s!</?(FONT|B|I|ALIGN|HTML|HEAD|TITLE|HR|BR|BODY|H3).*?>!!ig;
  40.  
  41.         if ( m/^ ?---- New Conversation @ (.*) ----$/ ) {
  42.             my $session = $1;
  43.             close $outfh if openhandle $outfh;
  44.  
  45.             my ( $mname, $day, $hr, $min, $sec, $year )
  46.                 = ( $session =~ m/\w{3} (\w{3}) ([0-9 ]{2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})/;
  47.  
  48.             if ( not defined $year ) {
  49.                 warn "Could not recognize session identifier: Ā«$sessionĀ»\n";
  50.                 last;
  51.             }
  52.  
  53.             my $date = sprintf '%s-%s-%02d.%s%s%s', $year, $monthnum{$mname}, $day, $hr, $min, $sec;
  54.  
  55.             my $outfile = catfile $target, $date . '.txt';
  56.             open $outfh, '>', $outfile or do {
  57.                 warn "Could not write to $outfile: $!\n";
  58.                 last;
  59.             };
  60.         }
  61.  
  62.         # proper files start with a session header
  63.         # so we should have an outfile open by now
  64.         if ( not openhandle $outfh ) {
  65.             warn "$file starts with garbage instead of a session identifier\n";
  66.             last;
  67.         }
  68.  
  69.         print { $outfh } $_;
  70.     }
  71. }
RAW Paste Data
Top