]> Sergey Matveev's repositories - dotfiles.git/blob - bin/bin/mail_dup_hdrs_remove.pl
Have not used zshfe for years
[dotfiles.git] / bin / bin / mail_dup_hdrs_remove.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 my %hdrs;
7 my $hdr;
8 my @hdrs_order;
9 my %hdrs_dup = (
10     "Content-Disposition", 1,
11     "Date" => 1,
12     "From" => 1,
13     "Message-ID" => 1,
14     "Subject" => 1,
15     "To" => 1,
16 );
17
18 while (<>) {
19     chomp;
20     last if ($_ eq "");
21     /^([^:]+): (.*)$/;
22     if (/^\s/) {
23         @{$hdrs{$hdr}}[-1] .= "\n$_";
24         next;
25     };
26     $hdr = $1;
27     my $val = $2;
28     (defined $hdrs{$hdr}) ? (push @{$hdrs{$hdr}}, $val) : ($hdrs{$hdr} = [$val]);
29     push @hdrs_order, $hdr;
30 };
31
32 my %seen;
33 foreach (@hdrs_order) {
34     next if (defined $seen{$_});
35     $seen{$_} = 1;
36     if (defined $hdrs_dup{$_}) {
37         print "$_: @{$hdrs{$_}}[-1]\n";
38         next;
39     };
40     foreach my $val (@{$hdrs{$_}}) {
41         print "$_: $val\n";
42     };
43 };
44
45 print "\n";
46 while (<>) { print };