]> Sergey Matveev's repositories - dotfiles.git/commitdiff
Rewrite Python to Perl
authorSergey Matveev <stargrave@stargrave.org>
Tue, 26 Oct 2021 13:04:21 +0000 (16:04 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Tue, 26 Oct 2021 13:04:21 +0000 (16:04 +0300)
bin/bin/mail_dup_hdrs_remove.pl [new file with mode: 0755]
bin/bin/mail_dup_hdrs_remove.py [deleted file]

diff --git a/bin/bin/mail_dup_hdrs_remove.pl b/bin/bin/mail_dup_hdrs_remove.pl
new file mode 100755 (executable)
index 0000000..dcc78ca
--- /dev/null
@@ -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 (executable)
index 669d8b4..0000000
+++ /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))