]> mj.ucw.cz Git - misc.git/commitdiff
pine2mutt
authorMartin Mares <mj@ucw.cz>
Wed, 5 Aug 2015 22:38:26 +0000 (00:38 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 5 Aug 2015 22:38:26 +0000 (00:38 +0200)
pine2mutt [new file with mode: 0755]

diff --git a/pine2mutt b/pine2mutt
new file mode 100755 (executable)
index 0000000..3e9ca2a
--- /dev/null
+++ b/pine2mutt
@@ -0,0 +1,79 @@
+#!/usr/bin/perl
+use common::sense;
+use Email::Address;
+use Encode;
+
+binmode STDOUT, ':utf8';
+
+my $curr = "";
+
+sub parse_addr {
+       my ($x) = @_;
+       if ($x =~ m{^\s*=\?}) {
+               $x = Encode::decode('MIME-Header', $x);
+       }
+       return Email::Address->parse($x);
+}
+
+sub flush {
+       return if $curr eq "";
+       return if $curr =~ /^#/;
+       my @a = split /\t/, $curr;
+       # print join("", map { "<$_>" } @a), "\n";
+       @a <= 5 or die "Too many fields: $curr\n";
+       my ($nick, $full, $addr, $fcc, $note) = @a;
+
+       if (!length $nick) {
+               print STDERR "Ignoring because of no nick: $curr\n";
+               return;
+       }
+
+       my @addrs = ();
+       if ($addr =~ m{^\((.*)\)$}) {
+               $addr = $1;
+               pos $addr = 0;
+               while ($addr =~ m{\G,*(([^,"]|"[^"]*")+)}g) {
+                       my $x = $1;
+                       my @as = parse_addr($x);
+                       @as == 1 or die "Cannot parse $x\n";
+                       push @addrs, @as;
+               }
+               pos $addr and die "Cannot parse $addr\n";
+       } else {
+               @addrs = parse_addr($addr);
+               @addrs == 1 or die "Cannot parse $addr\n";
+       }
+
+       if ($full ne "") {
+               if ($full =~ m{^\s*=\?}) {
+                       $full = Encode::decode('MIME-Header', $full);
+               }
+               $full =~ s{"}{}g;
+               if ($full =~ m{^(.*), (.*)$}) {
+                       $full = "$2 $1";
+               }
+
+               for my $a (@addrs) {
+                       if (!length $a->phrase) {
+                               $a->phrase($full);
+                       }
+               }
+       }
+
+       # for my $a (@addrs) {
+       #       print "\t", $a->format, "\n";
+       # }
+
+       print "alias $nick ", join(", ", map { $_->format } @addrs), "\n";
+}
+
+while (<>) {
+       chomp;
+       if (/^ +:? *(.*)/) {
+               $curr .= " $1";
+       } else {
+               flush();
+               $curr = $_;
+       }
+}
+flush();