]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/slrnspool2maildir
pop3: reduce memory use while generating the mailbox cache
[public-inbox.git] / scripts / slrnspool2maildir
1 #!/usr/bin/perl -w
2 # Copyright (C) 2013-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # One-off script to convert an slrnpull news spool to Maildir
6 =begin usage
7         ./slrnspool2maildir SLRNPULL_ROOT/news/foo/bar /path/to/maildir/
8 =cut
9 use strict;
10 use warnings;
11 use Email::Filter;
12 use Email::LocalDelivery;
13 use File::Glob qw(bsd_glob GLOB_NOSORT);
14 sub usage { "Usage:\n".join('',grep(/\t/, `head -n 12 $0`)) }
15 my $spool = shift @ARGV or die usage();
16 my $dir = shift @ARGV or die usage();
17 -d $dir or die "$dir is not a directory\n";
18 $dir .= '/' unless $dir =~ m!/\z!;
19 foreach my $sub (qw(cur new tmp)) {
20         my $nd = "$dir/$sub";
21         -d $nd and next;
22         mkdir $nd or die "mkdir $nd failed: $!\n";
23 }
24
25 foreach my $n (grep(/\d+\z/, bsd_glob("$spool/*", GLOB_NOSORT))) {
26         if (open my $fh, '<', $n) {
27                 my $f = Email::Filter->new(data => do { local $/; <$fh> });
28                 my $s = $f->simple;
29
30                 # gmane rewrites Received headers, which increases spamminess
31                 # Some older archives set Original-To
32                 foreach my $x (qw(Received To)) {
33                         my @h = $s->header("Original-$x");
34                         if (@h) {
35                                 $s->header_set($x, @h);
36                                 $s->header_set("Original-$x");
37                         }
38                 }
39
40                 # triggers for the SA HEADER_SPAM rule
41                 foreach my $drop (qw(Approved)) { $s->header_set($drop) }
42
43                 # appears to be an old gmane bug:
44                 $s->header_set('connect()');
45
46                 $f->exit(0);
47                 $f->accept($dir);
48         } else {
49                 warn "Failed to open $n: $!\n";
50         }
51 }