--- /dev/null
+#!/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();