| 1 | #!/usr/local/bin/perl | ||
| 2 | # -*- Perl -*- # Mon Apr 26 14:09:21 CDT 2004 | ||
| 3 | ############################################################################### | ||
| 4 | # Written by Tim Skirvin <tskirvin@killfile.org>. Copyright 2004, Tim Skirvin. | ||
| 5 | # Redistribution terms are below. | ||
| 6 | ############################################################################### | ||
| 7 | our $VERSION = "0.11"; | ||
| 8 | |||
| 9 | ############################################################################### | ||
| 10 | ### User Configuration ######################################################## | ||
| 11 | ############################################################################### | ||
| 12 | use vars qw( $LOCALCONF $KIBOZEDIR $DB_TYPE $ARCHIVEGROUP $DEBUG $QUIET | ||
| 13 | $VERBOSE ); | ||
| 14 | |||
| 15 | ## Rather than having everything in this shared configuration, load this | ||
| 16 | ## file to get additional configuration. This file contains additional | ||
| 17 | ## perl. | ||
| 18 | |||
| 19 | $LOCALCONF = "$ENV{'HOME'}/.kibozerc"; | ||
| 20 | |||
| 21 | ## Where should we store all of our files? This needs to be set or | ||
| 22 | ## nothing will run; also, the directory must already exist. | ||
| 23 | |||
| 24 | $KIBOZEDIR = ""; | ||
| 25 | |||
| 26 | ## What kind of database should we use to store history information? | ||
| 27 | |||
| 28 | $DB_TYPE = "DB_File"; | ||
| 29 | |||
| 30 | ## If we're storing everything to an archive group, you can set it here. | ||
| 31 | |||
| 32 | $ARCHIVEGROUP = ""; | ||
| 33 | |||
| 34 | ## Do we want to print debugging information? Use verbose status messages? | ||
| 35 | ## Be entirely silent? Set these things here. | ||
| 36 | |||
| 37 | $DEBUG = 0; | ||
| 38 | $VERBOSE = 0; | ||
| 39 | $QUIET = 0; | ||
| 40 | |||
| 41 | ## If the modules are set up in a non-standard place, edit this line | ||
| 42 | ## as appropriate. | ||
| 43 | BEGIN { use lib '/home/tskirvin/dev/news-archive'; } | ||
| 44 | |||
| 45 | ############################################################################### | ||
| 46 | ### main() #################################################################### | ||
| 47 | ############################################################################### | ||
| 48 | |||
| 49 | use strict; # Good programming is our friend | ||
| 50 | use warnings; | ||
| 51 | |||
| 52 | use Getopt::Std; # Command line functions | ||
| 53 | use News::Archive; # News functions | ||
| 54 | use News::Article; | ||
| 55 | use News::Article::Mbox; | ||
| 56 | |||
| 57 | # Error Codes | ||
| 58 | our %ERROR = ( 'SUCCESS' => 0, 'CONFIG' => 1, 'SERVER' => 2 ); | ||
| 59 | |||
| 60 | # Command-line configuration | ||
| 61 | use vars qw( %OPTS ); | ||
| 62 | getopts('hvVQdc:a:', \%OPTS); | ||
| 63 | |||
| 64 | # Load local configuration from local configuration file | ||
| 65 | $LOCALCONF = $OPTS{'c'} if $OPTS{'c'}; | ||
| 66 | if ( $LOCALCONF && -r $LOCALCONF ) { do $LOCALCONF } | ||
| 67 | |||
| 68 | &Usage if $OPTS{'h'}; # Print usage information and exit | ||
| 69 | &Version if $OPTS{'v'}; # Print version information and exit | ||
| 70 | |||
| 71 | # Other command-line parsing; overrides localconf | ||
| 72 | $DEBUG = $OPTS{'d'} if defined $OPTS{'d'}; | ||
| 73 | $VERBOSE = $OPTS{'V'} || $DEBUG || $VERBOSE || 0; # Give verbose messages? | ||
| 74 | $QUIET = $OPTS{'Q'} || $QUIET || 0; | ||
| 75 | $ARCHIVEGROUP = $OPTS{'A'} if defined $OPTS{'A'}; | ||
| 76 | |||
| 77 | # Create the options hash to start a News::Archive object | ||
| 78 | our %OPTHASH; | ||
| 79 | $OPTHASH{ 'basedir' } = $KIBOZEDIR if $KIBOZEDIR; | ||
| 80 | $OPTHASH{ 'db_type' } = $DB_TYPE if $DB_TYPE; | ||
| 81 | $OPTHASH{ 'debug' } = $DEBUG if $DEBUG; | ||
| 82 | |||
| 83 | # Create the News::Archive object | ||
| 84 | my $archive = new News::Archive ( %OPTHASH ) | ||
| 85 | or Exit('SERVER', "Couldn't create/load archive item: ", | ||
| 86 | News::Archive->error); | ||
| 87 | |||
| 88 | # If we've got $ARCHIVEGROUP set, then make sure we're subscribed to it. | ||
| 89 | $archive->subscribe($ARCHIVEGROUP) if ($ARCHIVEGROUP); | ||
| 90 | |||
| 91 | # Read from STDIN | ||
| 92 | my @articles = News::Article::Mbox->read_mbox( \*STDIN ); | ||
| 93 | |||
| 94 | my $count = 0; | ||
| 95 | foreach my $article (@articles) { | ||
| 96 | next unless $article; | ||
| 97 | my $messageid = $article->header('message-id'); | ||
| 98 | |||
| 99 | # If this article has already been processed, skip it | ||
| 100 | next unless $messageid; | ||
| 101 | if ( $archive->article( $messageid ) ) { | ||
| 102 | warn "Already processed '$messageid'\n" if $VERBOSE; | ||
| 103 | next; | ||
| 104 | } | ||
| 105 | |||
| 106 | # We need at least this many more headers to do anything with it | ||
| 107 | next unless $article->header('from'); | ||
| 108 | next unless $article->header('newsgroups'); | ||
| 109 | next unless $article->header('subject'); | ||
| 110 | next unless $article->header('date'); | ||
| 111 | |||
| 112 | # Debugging hook; don't worry about this in general. | ||
| 113 | # $article->write(\*STDOUT); | ||
| 114 | |||
| 115 | # Get the list of groups we're supposed to be saving the article into | ||
| 116 | my @groups = split('\s*,\s*', $article->header('newsgroups') ); | ||
| 117 | map { s/\s+//g } @groups; | ||
| 118 | |||
| 119 | # Make sure we're subscribed to all these groups | ||
| 120 | foreach (@groups) { $archive->subscribe($_) } | ||
| 121 | push @groups, $ARCHIVEGROUP if $ARCHIVEGROUP; # We're subscribed already | ||
| 122 | |||
| 123 | # Actually save the article. | ||
| 124 | my $ret = $archive->save_article( | ||
| 125 | [ @{$article->rawheaders}, '', @{$article->body} ], @groups ); | ||
| 126 | $count++ if $ret; | ||
| 127 | if ($VERBOSE) { warn $ret ? "Accepted article $messageid\n" | ||
| 128 | : "Couldn't save article $messageid\n" } | ||
| 129 | } | ||
| 130 | |||
| 131 | $archive->close; | ||
| 132 | |||
| 133 | Exit('SUCCESS', "$count articles archived"); | ||
| 134 | |||
| 135 | ############################################################################### | ||
| 136 | ### Subroutines ############################################################### | ||
| 137 | ############################################################################### | ||
| 138 | |||
| 139 | ## Usage () | ||
| 140 | # Prints out a short help file and exits. | ||
| 141 | sub Usage { | ||
| 142 | my $prog = $0; $prog =~ s%.*/%%g; # Clean up the program name | ||
| 143 | Exit('CONFIG', version($VERSION), <<EOM); | ||
| 144 | Archives news articles for later use with News::Archive | ||
| 145 | |||
| 146 | $prog is part of the News::Archive package, which is used to archive | ||
| 147 | news articles in a reasonably efficient manner. This particular script | ||
| 148 | is meant for use for personal archives, loading articles from an mbox-style | ||
| 149 | archie on STDIN and loading them into @{[ $KIBOZEDIR || "(unset)" ]}. | ||
| 150 | |||
| 151 | Configuration information is stored in the below configuration file; | ||
| 152 | please read the manual pages for more information on its configuration. | ||
| 153 | |||
| 154 | Usage: $prog [-hvVQd] [-c configfile] [-a groupname] < ARTICLE | ||
| 155 | -h Print this usage information and exit. | ||
| 156 | -v Print version information and exit. | ||
| 157 | -c configfile Load this configuration file instead of default. | ||
| 158 | Default: $LOCALCONF | ||
| 159 | -a groupname Add this group to all articles we download, so | ||
| 160 | that they can be accessed uniformly. | ||
| 161 | Default: @{[ $ARCHIVEGROUP || "Not Set" ]} | ||
| 162 | -V Verbose; print information on every article. | ||
| 163 | Default: @{[ $VERBOSE ? "On" : "Off" ]} | ||
| 164 | -Q Quiet mode; only print that which is absolutely | ||
| 165 | necessary (and even then think about it). | ||
| 166 | Default: @{[ $QUIET ? "On" : "Off" ]} | ||
| 167 | -d Debug; print debugging information (implies -V) | ||
| 168 | Default: @{[ $DEBUG ? "On" : "Off" ]} | ||
| 169 | EOM | ||
| 170 | } | ||
| 171 | |||
| 172 | ## Version () | ||
| 173 | # Prints out the version number and exits | ||
| 174 | sub Version { Exit('CONFIG', version($VERSION)) } | ||
| 175 | |||
| 176 | ## Exit ( CODE, REASON ) | ||
| 177 | # Exits the program cleanly with a proper error message | ||
| 178 | sub Exit { | ||
| 179 | my ($code, $reason, @details) = @_; | ||
| 180 | exit $ERROR{$code} unless $reason; | ||
| 181 | # map { $_ = " - $_" } @details; | ||
| 182 | $reason = join("\n", $reason, @details, ''); | ||
| 183 | warn $reason if (!$QUIET && $reason); | ||
| 184 | exit $ERROR{$code}; | ||
| 185 | } | ||
| 186 | |||
| 187 | ## version ( [VERSION] ) | ||
| 188 | # Returns the program name and version number | ||
| 189 | sub version { | ||
| 190 | my $version = shift || $VERSION || "unknown"; | ||
| 191 | my $prog = $0; $prog =~ s%.*/%%g; # Clean up the program name | ||
| 192 | "$prog v$VERSION" | ||
| 193 | } | ||
| 194 | |||
| 195 | ############################################################################### | ||
| 196 | ### Documentation ############################################################# | ||
| 197 | ############################################################################### | ||
| 198 | |||
| 199 | =head1 NAME | ||
| 200 | |||
| 201 | mbox2news.pl - Archives news articles for later use with News::Archive | ||
| 202 | |||
| 203 | =head1 SYNOPSIS | ||
| 204 | |||
| 205 | mbox2news.pl [-hvVQd] [-c configfile] [-a groupname] < ARTICLE | ||
| 206 | |||
| 207 | =head1 DESCRIPTION | ||
| 208 | |||
| 209 | mbox2news.pl is a part of the News::Archive package, which is used to | ||
| 210 | archive news articles in a reasonably efficient manner. This particular | ||
| 211 | script is meant for use for personal archives, loading articles (or | ||
| 212 | mbox-style archives) on STDIN and loading them into $KIBOZEDIR. | ||
| 213 | |||
| 214 | Without any options, mbox2news.pl will look for input from STDIN. | ||
| 215 | Configuration information is stored in the configuration file | ||
| 216 | (B<kibozerc>). Command-line options are as follows: | ||
| 217 | |||
| 218 | =over 4 | ||
| 219 | |||
| 220 | =item -h | ||
| 221 | |||
| 222 | Print this usage information and exit. | ||
| 223 | |||
| 224 | =item -v | ||
| 225 | |||
| 226 | Print version information and exit. | ||
| 227 | |||
| 228 | =item -d | ||
| 229 | |||
| 230 | Debug; print debugging information (implies -V) | ||
| 231 | |||
| 232 | =item -c configfile | ||
| 233 | |||
| 234 | Load this configuration file instead of default (~/.kibozerc). | ||
| 235 | |||
| 236 | =item -a groupname | ||
| 237 | |||
| 238 | Add this group to all articles we download, so that they can be accessed | ||
| 239 | uniformly. | ||
| 240 | |||
| 241 | =item -V | ||
| 242 | |||
| 243 | Verbose; print information on every article. | ||
| 244 | |||
| 245 | =item -Q | ||
| 246 | |||
| 247 | Quiet mode; only print that which is absolutely necessary (and even then | ||
| 248 | think about it). | ||
| 249 | |||
| 250 | =back | ||
| 251 | |||
| 252 | =head1 NOTES | ||
| 253 | |||
| 254 | This is essentially a counterpart to kiboze.pl; while that program goes to | ||
| 255 | news servers and downloads new messages, mbox2news.pl uses existing | ||
| 256 | messages. It is primarily meant to convert from old kiboze archives, or | ||
| 257 | anything similar. | ||
| 258 | |||
| 259 | This script was meant to work with mbox-style archives; this is compatible | ||
| 260 | with some single articles, however, so you can feed them in directly as well. | ||
| 261 | |||
| 262 | You may well need a helper script to make some programs give you | ||
| 263 | mbox-compatible archives. This distribution comes with 'nnparse', which | ||
| 264 | is used to fix the 'sentnews' items from the nn news reader; you can use | ||
| 265 | it as an example if you wish. | ||
| 266 | |||
| 267 | =head1 REQUIREMENTS | ||
| 268 | |||
| 269 | B<News::Archive>, B<News::Article::Mbox> | ||
| 270 | |||
| 271 | =head1 SEE ALSO | ||
| 272 | |||
| 273 | B<kibozerc>, B<newsarchive.pl>, B<kiboze.pl> | ||
| 274 | |||
| 275 | =head1 AUTHOR | ||
| 276 | |||
| 277 | Tim Skirvin <tskirvin@killfile.org> | ||
| 278 | |||
| 279 | =head1 LICENSE | ||
| 280 | |||
| 281 | This code may be redistributed under the same terms as Perl itself. | ||
| 282 | |||
| 283 | =head1 HOMEPAGE | ||
| 284 | |||
| 285 | http://www.killfile.org/~tskirvin/software/news-archive/ | ||
| 286 | |||
| 287 | =head1 COPYRIGHT | ||
| 288 | |||
| 289 | Copyright 2003-2004, Tim Skirvin <tskirvin@killfile.org> | ||
| 290 | |||
| 291 | =cut | ||
| 292 | |||
| 293 | ############################################################################### | ||
| 294 | ### Version History ########################################################### | ||
| 295 | ############################################################################### | ||
| 296 | # v0.1a Mon Oct 06 15:49:16 CDT 2003 | ||
| 297 | ### Just starting out. | ||
| 298 | # v0.10 Mon Apr 26 13:46:01 CDT 2004 | ||
| 299 | ### Actually calling this program something, documenting it, making it pretty. | ||
| 300 | # v0.11 Thu Apr 29 12:51:22 CDT 2004 | ||
| 301 | ### Small bugfixes. |