App-Spoor

 view release on metacpan or  search on metacpan

lib/App/Spoor/ParsedFileManager.pm  view on Meta::CPAN

=cut

our $VERSION = '0.06';


=head1 SYNOPSIS

This module is responsible for looping through the contents of the parsed files directory and passing them to the
transmitter.

=head1 SUBROUTINES

=head2 process_parsed_files

Loops through all files in the /var/lib/spoor/parsed directory. If a file passes a rudimentary security check the
contents of the file are passed to the transmitter. If the file fails the security check or does not match the expected
naming convention, it is left where it is.


If the file is successfully transmitted, it is moved into /var/lib/spoor/transmitted. If not, it is moved into 
/var/lib/spoor/transmission_failed - for transmission to be reattempted, it must be manually moved into
/var/lib/spoor/parsed once more.


  my $application_config = App::Spoor::Config::get_application_config();
  my $transmission_config = App::Spoor::Config::get_transmission_config();
  $transmission_config->{'reporter'} = hostname;

  sub transmitter {
    App::Spoor::EntryTransmitter::transmit(
      App::Spoor::TransmissionFormatter::format(shift, $transmission_config),
      LWP::UserAgent->new,
      $transmission_config
    );
  }

  sub parsed_file_security_check {
    App::Spoor::Security::check_file(shift, $>, 0600);
  }

  while(1) {
    App::Spoor::ParsedFileManager::process_parsed_files(
      $application_config, \&parsed_file_security_check, \&transmitter
    );
    sleep 5;
  }

=cut

sub process_parsed_files {
  my $config = shift;
  my $file_security_check = shift;
  my $transmitter = shift;

  my ($source_file_path, $file_contents);
  opendir my $parsed_entries_dir, $config->{parsed_entries_path};

  my @file_names = grep { /\A((error|access|login)\.\d+\.\d+\.json)\z/ } readdir $parsed_entries_dir;

  foreach my $file_name (@file_names) {
    # Untaint 
    if ($file_name =~ /\A((error|access|login)\.\d+\.\d+\.json)\z/) {
      my $sanitised_file_name = $1;
      $source_file_path = File::Spec->catfile($config->{parsed_entries_path}, $sanitised_file_name);
      if ($file_security_check->($source_file_path)) {
        $file_contents = from_json(path($source_file_path)->slurp_utf8());

        if ($transmitter->($file_contents)) {
          move($source_file_path, $config->{transmitted_entries_path});
        } else {
          move($source_file_path, $config->{transmission_failed_entries_path});
        }
      }
    }
  }

  closedir $parsed_entries_dir;
}

=head1 AUTHOR

Rory McKinley, C<< <rorymckinley at capefox.co> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-. at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=.>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.


=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc App::Spoor::ParsedFileManager


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker (report bugs here)

L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=.>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/.>

=item * CPAN Ratings

L<https://cpanratings.perl.org/d/.>

=item * Search CPAN

L<https://metacpan.org/release/.>

=back

=head1 LICENSE AND COPYRIGHT



( run in 1.297 second using v1.01-cache-2.11-cpan-7fcb06a456a )