]> mj.ucw.cz Git - misc.git/blob - pine2mutt
Merge branch 'master' of git+ssh://git.ucw.cz/home/mj/GIT/misc
[misc.git] / pine2mutt
1 #!/usr/bin/perl
2 use common::sense;
3 use Email::Address;
4 use Encode;
5
6 binmode STDOUT, ':utf8';
7
8 my $curr = "";
9
10 sub parse_addr {
11         my ($x) = @_;
12         if ($x =~ m{^\s*=\?}) {
13                 $x = Encode::decode('MIME-Header', $x);
14         }
15         return Email::Address->parse($x);
16 }
17
18 sub flush {
19         return if $curr eq "";
20         return if $curr =~ /^#/;
21         my @a = split /\t/, $curr;
22         # print join("", map { "<$_>" } @a), "\n";
23         @a <= 5 or die "Too many fields: $curr\n";
24         my ($nick, $full, $addr, $fcc, $note) = @a;
25
26         if (!length $nick) {
27                 print STDERR "Ignoring because of no nick: $curr\n";
28                 return;
29         }
30
31         my @addrs = ();
32         if ($addr =~ m{^\((.*)\)$}) {
33                 $addr = $1;
34                 pos $addr = 0;
35                 while ($addr =~ m{\G,*(([^,"]|"[^"]*")+)}g) {
36                         my $x = $1;
37                         my @as = parse_addr($x);
38                         @as == 1 or die "Cannot parse $x\n";
39                         push @addrs, @as;
40                 }
41                 pos $addr and die "Cannot parse $addr\n";
42         } else {
43                 @addrs = parse_addr($addr);
44                 @addrs == 1 or die "Cannot parse $addr\n";
45         }
46
47         if ($full ne "") {
48                 if ($full =~ m{^\s*=\?}) {
49                         $full = Encode::decode('MIME-Header', $full);
50                 }
51                 $full =~ s{"}{}g;
52                 if ($full =~ m{^(.*), (.*)$}) {
53                         $full = "$2 $1";
54                 }
55
56                 for my $a (@addrs) {
57                         if (!length $a->phrase) {
58                                 $a->phrase($full);
59                         }
60                 }
61         }
62
63         # for my $a (@addrs) {
64         #       print "\t", $a->format, "\n";
65         # }
66
67         print "alias $nick ", join(", ", map { $_->format } @addrs), "\n";
68 }
69
70 while (<>) {
71         chomp;
72         if (/^ +:? *(.*)/) {
73                 $curr .= " $1";
74         } else {
75                 flush();
76                 $curr = $_;
77         }
78 }
79 flush();