package Mail::SNCF;

use warnings;
use strict;

use Data::Dump qw/dump/;

use Email::Folder;
use MIME::QuotedPrint;

=head1 NAME

Mail::SNCF - A parser for booking messages sent by the French rail company

=head1 VERSION

Version 0.02


our $VERSION = '0.02';


This module is not intended to be used directly, instead, use
L<SNCF::Ical>, L<SNCF::Text> or L<SNCF::Remind>.

    use Mail::SNCF::Backend;

    my $foo = Mail::SNCF::Backend->parse("Mail/sncf");


=head2 parse

Parse a mail box (as supported by L<Email::Folder>) and create the


sub parse {
    my ($class, $folder_path) = @_;
    my $folder = Email::Folder->new($folder_path);

    my $self = [];
    bless $self, $class;

    while (my $a = $folder->next_message()) {
		my $subject = $a->header("Subject");
        next unless $subject =~ /Confirmation.*(commande|voyage).*/;
        my @lines = split /\n/, $a->body();
        for(my $i = 0; $i < @lines; $i++) {
            next unless $lines[$i] =~ /=A0------------------------------------------------/;
            next unless $lines[++$i] =~ /=A0TRAIN/;
            while ($lines[$i] !~
            my $line_from = decode_qp($lines[++$i]);
            my $line_to = decode_qp($lines[++$i]);

            my $trip = {};
            $line_from =~ m!\s*:\s*((?:\w|\s)*\w)\s*-\s*(\d\d)h(\d\d)\s*-\s*(\d\d)/(\d\d)/(\d\d\d\d)!;
            $trip->{from}  = $1;
            $trip->{start} = [$2, $3];
            $trip->{date}  = [$4, $5, $6];
            $line_to =~ m!\s*:\s*((?:\w|\s)*\w)\s*-\s*(\d\d)h(\d\d)!;
            $trip->{to}  = $1;
            $trip->{end} = [$2, $3];

            push @{$self}, $trip;

    return $self;

=head2 file

Output to a file.


sub file {
    my ($self, $file) = @_;
    open FILE, ">", $file;
    print FILE $self->as_string;
    close FILE;

=head2 print

Print the data returned by as_string.


sub print {
    my ($self) = @_;
    print $self->as_string;

=head2 as_string

Give a string representation of the data.


sub as_string {
    my ($self) = @_;

=head1 AUTHOR

Olivier Schwander, C<< <iderrick at> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-mail-sncf at>, or through
the web interface at
L<>.  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 Mail::SNCF

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker


=item * AnnoCPAN: Annotated CPAN documentation


=item * CPAN Ratings


=item * Search CPAN




Copyright 2009 Olivier Schwander, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.


1; # End of Mail::SNCF