| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package Mail::Summary::Tools::CLI::Download::nntp; | ||
| 4 | use base qw/Mail::Summary::Tools::CLI::Command/; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | #use Class::Autouse (<<'#\'END_USE' =~ m!(\w+::[\w:]+)!g); | ||
| 10 | #\ | ||
| 11 | |||
| 12 | use Net::NNTP; | ||
| 13 | use Mail::Box::Manager; | ||
| 14 | use Mail::Message; | ||
| 15 | |||
| 16 | use DateTime; | ||
| 17 | use DateTime::Format::DateManip; | ||
| 18 | |||
| 19 | use Mail::Summary::Tools::Downloader::NNTP; | ||
| 20 | |||
| 21 | #'END_USE | ||
| 22 | |||
| 23 | use constant options => ( | ||
| 24 | [ 'server|s=s' => "The NNTP server to download from", { required => 1 } ], | ||
| 25 | [ 'group|g=s@' => "The groups to download", { required => 1 } ], | ||
| 26 | [ 'output|o=s' => "Output mailbox" ], | ||
| 27 | [ 'from|f=s' => "Something Date::Manip can parse" ], | ||
| 28 | [ 'to|t=s' => "Something Date::Manip can parse" ], | ||
| 29 | [ 'recursive|r' => "Fetch `References` headers recursively", { default => 1 } ], | ||
| 30 | ); | ||
| 31 | |||
| 32 | sub validate { | ||
| 33 | my ( $self, $opt, $args ) = @_; | ||
| 34 | @$args and $opt->{output} ||= shift @$args; | ||
| 35 | |||
| 36 | my $mgr = Mail::Box::Manager->new; | ||
| 37 | |||
| 38 | $opt->{output} = $mgr->open( | ||
| 39 | folder => $opt->{output}, | ||
| 40 | create => 1, | ||
| 41 | access => "rw", | ||
| 42 | ); | ||
| 43 | |||
| 44 | foreach my $mbox ( @$args ) { | ||
| 45 | $mbox = $mgr->open( | ||
| 46 | folder => $mbox, | ||
| 47 | create => 0, | ||
| 48 | access => "r", | ||
| 49 | ) | ||
| 50 | }; | ||
| 51 | |||
| 52 | $_ = $_ ? DateTime::Format::DateManip->parse_datetime($_) : DateTime::Infinite::Past->new for $opt->{from}; | ||
| 53 | $_ = $_ ? DateTime::Format::DateManip->parse_datetime($_) : DateTime::Infinite::Future->new for $opt->{to}; | ||
| 54 | |||
| 55 | $self->usage_error("You must provide an output mailbox.") unless $opt->{output}; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub run { | ||
| 59 | my ( $self, $opt, $args ) = @_; | ||
| 60 | |||
| 61 | |||
| 62 | my $nntp = Mail::Summary::Tools::Downloader::NNTP->new( | ||
| 63 | server => $opt->{server}, | ||
| 64 | fetch_recursively => $opt->{recursive}, | ||
| 65 | cache => $self->app->context->nntp_overviews, | ||
| 66 | ); | ||
| 67 | |||
| 68 | foreach my $group ( @{ $opt->{group} } ){ | ||
| 69 | $nntp->download( | ||
| 70 | group => $group, | ||
| 71 | from => $opt->{from}, | ||
| 72 | to => $opt->{to}, | ||
| 73 | mailbox => $opt->{output}, | ||
| 74 | extra_mailboxes => $args, | ||
| 75 | ); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | __PACKAGE__; | ||
| 81 | |||
| 82 | __END__ |