plug leaks
[claws.git] / tools / cm-break.pl
blob7553b17506ba510e32b1af8e4ee5cb75e897e017
1 #!/usr/bin/perl
3 use 5.14.1;
4 use warnings;
6 our $VERSION = "1.05 - 2018-10-08";
7 our $cmd = $0 =~ s{.*/}{}r;
9 sub usage {
10 my $err = shift and select STDERR;
11 say "usage: $cmd file ...";
12 exit $err;
13 } # usage
15 use Date::Parse;
16 use Getopt::Long;
17 GetOptions (
18 "help|?" => sub { usage (0); },
19 "V|version" => sub { say "$cmd [$VERSION]"; exit 0; },
20 ) or usage (1);
22 my %f;
23 foreach my $fn (@ARGV) {
25 open my $fh, "<", $fn or die "$fn: $!\n";
26 my ($hdr, $body) = split m/(?<=\n)(?=\r?\n)/ => do { local $/; <$fh> }, 2;
27 close $fh;
29 $hdr && $hdr =~ m/\b(?:In-Reply-To|References)\b/i or next;
31 my ($mid) = $hdr =~ m{^Message-Id: (?:[\x20\t]*\n)?[\x20\t]+ (\S.*)}xmi;
32 my ($dte) = $hdr =~ m{^Date: (?:[\x20\t]*\n)?[\x20\t]+ (\S.*)}xmi;
33 my ($irt) = $hdr =~ m{^In-Reply-To: (?:[\x20\t]*\n)?[\x20\t]+ (\S.*)}xmi;
34 my ($ref) = $hdr =~ m{^References: (?:[\x20\t]*\n)?[\x20\t]+ (\S.*)}xmi;
36 my $stamp = str2time ($dte) or die $dte;
37 my $date = $stamp ? do {
38 my @d = localtime $stamp;
39 sprintf "%4d-%02d-%02d %02d:%02d:%02d", $d[5] + 1900, ++$d[4], @d[3,2,1,0];
40 } : "-";
41 #printf "%12s %-20s %s\n", $stamp // "-", $date, $rcv;
43 $f{$fn} = {
44 msg_id => $mid,
45 refs => $ref,
46 irt => $irt,
47 date => $dte,
48 stamp => $stamp,
49 sdate => $date,
51 hdr => $hdr,
52 body => $body,
56 foreach my $fn (sort keys %f) {
58 my $c = 0;
60 my $f = $f{$fn};
61 if ($f->{refs}) {
62 $c++;
63 $f->{hdr} =~ s{\nReferences:.*(?:\n\s+.*)*+}{}ig;
65 if ($f->{irt}) {
66 $c++;
67 $f->{hdr} =~ s{\nIn-Reply-To:.*(?:\n\s+.*)*+}{}ig;
70 $c or next; # No changes required
72 say "$f->{msg_id} => -";
74 my @t = stat $fn;
75 open my $fh, ">", $fn or die "$fn: $!\n";
76 print $fh $f->{hdr}, $f->{body};
77 close $fh or die "$fn: $!\n";
78 utime $t[8], $t[9], $fn;
81 __END__
83 =head1 NAME
85 cm-break.pl - remove mail from thread
87 =head1 SYNOPSIS
89 cm-break.pl ~/Mail/inbox/23 ~/Mail/inbox/45 ...
91 =head1 DESCRIPTION
93 This script should be called from within Claws-Mail as an action
95 Define an action as
97 Menu name: Unthread (break threading)
98 Command: cm-break.pl %F
100 Then select from the message list all files that should be un-threaded
102 Then invoke the action
104 All of those mails will be modified (if needed): their C<In-Reply-To:>
105 and C<References:> header tags are removed from the header.
107 =head1 SEE ALSO
109 L<Date::Parse>, L<Claws Mail|http://www.claws-mail.org>
110 cm-reparent.pl
112 =head1 AUTHOR
114 H.Merijn Brand <h.m.brand@xs4all.nl>
116 =head1 COPYRIGHT AND LICENSE
118 Copyright (C) 2018-2018 H.Merijn Brand. All rights reserved.
120 This library is free software; you can redistribute and/or modify it under
121 the same terms as Perl itself.
122 See the L<Artistic license|http://dev.perl.org/licenses/artistic.html>.
124 =cut