From f807f20f81b26c275e32dbbf342609a448f0199c Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Tue, 26 Oct 2021 16:04:21 +0300 Subject: [PATCH] Rewrite Python to Perl --- bin/bin/mail_dup_hdrs_remove.pl | 46 +++++++++++++++++++++++++++++++++ bin/bin/mail_dup_hdrs_remove.py | 34 ------------------------ 2 files changed, 46 insertions(+), 34 deletions(-) create mode 100755 bin/bin/mail_dup_hdrs_remove.pl delete mode 100755 bin/bin/mail_dup_hdrs_remove.py diff --git a/bin/bin/mail_dup_hdrs_remove.pl b/bin/bin/mail_dup_hdrs_remove.pl new file mode 100755 index 0000000..dcc78ca --- /dev/null +++ b/bin/bin/mail_dup_hdrs_remove.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +my %hdrs; +my $hdr; +my @hdrs_order; +my %hdrs_dup = ( + "Content-Disposition", 1, + "Date" => 1, + "From" => 1, + "Message-ID" => 1, + "Subject" => 1, + "To" => 1, +); + +while (<>) { + chomp; + last if ($_ eq ""); + /^([^:]+): (.*)$/; + if (/^\s/) { + @{$hdrs{$hdr}}[-1] .= "\n$_"; + next; + }; + $hdr = $1; + my $val = $2; + (defined $hdrs{$hdr}) ? (push @{$hdrs{$hdr}}, $val) : ($hdrs{$hdr} = [$val]); + push @hdrs_order, $hdr; +}; + +my %seen; +foreach (@hdrs_order) { + next if (defined $seen{$_}); + $seen{$_} = 1; + if (defined $hdrs_dup{$_}) { + print "$_: @{$hdrs{$_}}[-1]\n"; + next; + }; + foreach my $val (@{$hdrs{$_}}) { + print "$_: $val\n"; + }; +}; + +print "\n"; +while (<>) { print }; diff --git a/bin/bin/mail_dup_hdrs_remove.py b/bin/bin/mail_dup_hdrs_remove.py deleted file mode 100755 index 669d8b4..0000000 --- a/bin/bin/mail_dup_hdrs_remove.py +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env python3.6 - -import sys - -fn = sys.argv[1] -out = sys.argv[2] -with open(fn, "rb") as fd: - lines = fd.read().split(b"\n") - -def dup(lines, what): - idx = [] - met = False - for i, line in enumerate(lines): - if line == "": - break - if met: - if line.startswith(b" "): - idx.append(i) - continue - else: - met = False - if line.startswith(what): - if len(idx) == 0: - idx.append(i) - met = True - else: - return [l for n, l in enumerate(lines) if n not in idx] - return lines - -lines = dup(lines, b"To") -lines = dup(lines, b"From") -lines = dup(lines, b"Subject") -with open(out, "wb") as fd: - fd.write(b"\n".join(lines)) -- 2.44.0