summaryrefslogtreecommitdiff
path: root/notmuch-slurp-debbug
blob: f5ae3fcec5c4001e4d772603a74c78da97019ff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl

# notmuch-slurp-debbug -- add messages from a Debian bug to notmuch

# Copyright (C) 2018-2020 Sean Whitton
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use strict;
use warnings;

use Config::Tiny;
use File::Spec::Functions qw(catfile);
use File::Which;
use Getopt::Long;
use IPC::System::Simple qw(systemx capturex);
use Mail::Box::Manager;

my $bts = "https://bugs.debian.org";
GetOptions "bts-server=s" => \$bts;

die "usage: notmuch-slurp-debbug [--bts-server=SERVER] BUG"
  unless @ARGV == 1;
die "notmuch-slurp-debbug: this script requires notmuch to be installed"
  unless which "notmuch";
die "notmuch-slurp-debbug: this script requires the 'devscripts' apt package"
  unless which "bts";
my $bug = pop @ARGV;

my $mgr = Mail::Box::Manager->new;
my $maildir;
my $conf_r = $ENV{XDG_CONFIG_HOME} || catfile $ENV{HOME}, ".config";
my $conf_f = catfile $conf_r, "mailscripts", "notmuch-slurp-debbug";
if (-f $conf_f) {
    $maildir = glob Config::Tiny->new->read($conf_f)->{_}->{maildir};
} else {
    # default to where a lot of people have their inbox
    chomp(my $database_path = `notmuch config get database.path`);
    $maildir = catfile $database_path, "inbox";
}
$maildir = $mgr->open(
    folder    => $maildir,
    access    => "a",
    keep_dups => 1,
    type      => "maildir"
) or die "failed to open target maildir: $!\n";

# we use bts(1) to download the mbox because it has some logic to find
# the right URI and the user might have enabled its caching features.
# see #904182 for why we invoke it like this
systemx(
    qw(bts --bts-server),
    $bts, qw(--mbox --mailreader),
    "true %s", "show", $bug
);

my $cache_r = $ENV{XDG_CACHE_HOME} || catfile $ENV{HOME}, ".cache";
my $cache_d = catfile $cache_r, "devscripts", "bts";
my $mbox = $mgr->open(
    folder    => catfile($cache_d, "$bug.mbox"),
    access    => "r",
    keep_dups => 1,
    type      => "mbox"
) or die "failed to open $bug.mbox: $!\n";

foreach my $message ($mbox->messages) {
    my $mid = $message->messageId;
    # if this message does not have a message-id, do not import it;
    # that would be asking for trouble
    next unless defined $mid;
    $mid =~ s/(<|>)//g;

    chomp(my $match = capturex(qw(notmuch search), "id:$mid"));

    $mgr->copyMessage($maildir, $message) unless $match;
}

systemx(qw(notmuch new));