2 # Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Useful for mapping article IDs from existing NNTP servers to MIDs
8 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
9 my $usage = "usage: NNTPSERVER=news.example.org $0 GROUP [FIRST_NUM]\n";
11 my %opts = ( '--msgmap=s' => \$msgmap );
12 GetOptions(%opts) or die "bad command-line args\n$usage";
15 require PublicInbox::Msgmap;
16 require PublicInbox::MID; # mid_clean
17 $mm = PublicInbox::Msgmap->new_file($msgmap, 1);
20 my $group = shift or die $usage;
21 my $nntp = Net::NNTP->new($ENV{NNTPSERVER} || '127.0.0.1');
22 my ($num, $first, $last) = $nntp->group($group);
23 die "Invalid group\n" if !(defined $num && defined $first && defined $last);
24 my $arg_first = shift;
25 if (defined $arg_first) {
26 $arg_first =~ /\A[0-9]+\z/ or die $usage;
29 my $last_article = $mm->meta_accessor('last_article');
30 $first = $last_article + 1 if defined $last_article;
35 for ($i = $first; $i < $last; $i += $batch) {
36 my $j = $i + $batch - 1;
37 $j = $last if $j > $last;
38 my $num2mid = $nntp->xhdr('Message-ID', "$i-$j");
40 $mm->{dbh}->begin_work if $mm;
42 defined(my $mid = $num2mid->{$n}) or next;
45 $mid = PublicInbox::MID::mid_clean($mid);
46 $mm->mid_set($n, $mid);
50 $mm->meta_accessor('last_article', $j);