| 1 | $VERSION = 0.02; | ||
| 2 | package News::Article::Mbox; | ||
| 3 | our $VERSION = 0.02; | ||
| 4 | |||
| 5 | # -*- Perl -*- # Thu Apr 29 12:06:38 CDT 2004 | ||
| 6 | ############################################################################### | ||
| 7 | # Written by Tim Skirvin <tskirvin@killfile.org>. Copyright 2004, Tim Skirvin. | ||
| 8 | # Redistribution terms are below. | ||
| 9 | ############################################################################### | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | News::Article::Mbox - combines News::Article and Mail::Mbox::MessageParser | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use News::Article::Mbox; | ||
| 18 | my @articles = News::Article::Mbox->read_mbox( \*STDIN ); | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | News::Article is capable of reading in individual messages from STDIN or | ||
| 23 | other sources to make into Article objects. However, it is often helpful | ||
| 24 | to be able to read many Articles out of an mbox-class storage format, as | ||
| 25 | created by many newsreader programs. This package adds a single function, | ||
| 26 | read_mbox(), to News::Article which makes this easy and accurate, using | ||
| 27 | the functionality of Mail::Mbox::MessageParser. | ||
| 28 | |||
| 29 | =head1 USAGE | ||
| 30 | |||
| 31 | Mostly the same as News::Article, except with the following function: | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | ############################################################################### | ||
| 36 | ### main() #################################################################### | ||
| 37 | ############################################################################### | ||
| 38 | |||
| 39 | use strict; | ||
| 40 | use News::Article; # CPAN | ||
| 41 | use Mail::Mbox::MessageParser; # CPAN | ||
| 42 | use Carp; # Perl Distribution | ||
| 43 | use Exporter; | ||
| 44 | use File::Temp qw/tempfile/; | ||
| 45 | |||
| 46 | use vars qw( @ISA @EXPORT @EXPORT_OK ); | ||
| 47 | @ISA = qw ( News::Article ); | ||
| 48 | @EXPORT_OK = qw ( read_mbox ); | ||
| 49 | |||
| 50 | =over 4 | ||
| 51 | |||
| 52 | =item read_mbox ( SOURCE [, MAXSIZE, MAXHEAD] ) | ||
| 53 | |||
| 54 | Acts like B<News::Article::read()>, except that it loads a large number of | ||
| 55 | articles instead of each one. C<SOURCE> is parsed with | ||
| 56 | C<Mail::Mbox::MessageParser>; if it turns out to only be a single news | ||
| 57 | message and not a full mbox, then this is bypassed and that message is | ||
| 58 | parsed. All messages make B<News::Article> references. Returns an array | ||
| 59 | or arrayref of articles. | ||
| 60 | |||
| 61 | This function is exportable, but is not exported by default. | ||
| 62 | |||
| 63 | =back | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | sub read_mbox { | ||
| 68 | my ($self, $source, $maxsize, $maxhead) = @_; | ||
| 69 | |||
| 70 | # Set up the data source, same as News::Article's read() | ||
| 71 | $source = News::Article::source_init($source); | ||
| 72 | ( my ($fh, $file_name) ) = tempfile(UNLINK => 1) | ||
| 73 | or croak "Could not create temporary file: $!"; | ||
| 74 | return undef unless defined $source; | ||
| 75 | my @lines; | ||
| 76 | while ( my $line = &$source ) { print $fh $line; push @lines, $line; } | ||
| 77 | |||
| 78 | my @articles; | ||
| 79 | my $parser = Mail::Mbox::MessageParser->new({ 'file_name' => $file_name, | ||
| 80 | 'enable_cache' => 0 }); | ||
| 81 | |||
| 82 | if (ref $parser) { | ||
| 83 | while (! $parser->end_of_file) { | ||
| 84 | my $mailref = $parser->read_next_email; | ||
| 85 | my @lines = split("\n", $$mailref); | ||
| 86 | my $article = News::Article->new(\@lines, $maxsize, $maxhead); | ||
| 87 | $$article{TIMESTAMP} = $lines[0]; | ||
| 88 | push @articles, $article if $article; | ||
| 89 | } | ||
| 90 | } else { | ||
| 91 | my $article = News::Article->new(\@lines, $maxsize, $maxhead); | ||
| 92 | push @articles, $article if $article; | ||
| 93 | } | ||
| 94 | |||
| 95 | wantarray ? @articles : [ @articles ]; | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; | ||
| 99 | |||
| 100 | =head1 REQUIREMENTS | ||
| 101 | |||
| 102 | B<News::Article>, B<File::Temp>, B<Mail::Mbox::MessageParser> | ||
| 103 | |||
| 104 | =head1 SEE ALSO | ||
| 105 | |||
| 106 | B<News::Article> | ||
| 107 | |||
| 108 | =head1 TODO | ||
| 109 | |||
| 110 | This actually works pretty much the way it's supposed to. | ||
| 111 | |||
| 112 | It might be nice if we didn't have to use File::Temp... | ||
| 113 | |||
| 114 | =head1 AUTHOR | ||
| 115 | |||
| 116 | Tim Skirvin <tskirvin@killfile.org> | ||
| 117 | |||
| 118 | =head1 HOMEPAGE | ||
| 119 | |||
| 120 | B<http://www.killfile.org/~tskirvin/software/news-archive/> | ||
| 121 | |||
| 122 | B<http://www.killfile.org/~tskirvin/software/newslib/> | ||
| 123 | |||
| 124 | =head1 LICENSE | ||
| 125 | |||
| 126 | This code may be redistributed under the same terms as Perl itself. | ||
| 127 | |||
| 128 | =head1 COPYRIGHT | ||
| 129 | |||
| 130 | Copyright 2004, Tim Skirvin. | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | ############################################################################### | ||
| 135 | ##### Version History ######################################################### | ||
| 136 | ############################################################################### | ||
| 137 | # v0.01 Mon Apr 26 13:31:00 CDT 2004 | ||
| 138 | ### First version. Documentation is coming with it. Will be a part of | ||
| 139 | ### News::Archive, not newslib or anything like that. | ||
| 140 | # v0.02 Thu Apr 29 12:06:15 CDT 2004 | ||
| 141 | ### If the file turns out to not be an mbox, try just loading it as a | ||
| 142 | ### single article. |