#!/usr/bin/perl # # Convert a pine address book to gmail-friendly csv # # If you have email addresses in lists that _don't_ have domain # names, you should edit "$default_suffix". # # Usage: cat [addresbook] | pine2csv.pl > [outfile] # # Does not print groups by default, since gmail is confused by # groups. If you change $include_groups to 1, it will output # groups in the format that _does_ work for gmail when you # _manually_ enter groups as contacts. # # Dan Morris # dmorris@cs.stanford.edu # http://cs.stanford.edu/~dmorris # $include_groups = 0; print "name,email address,nickname\n"; $default_suffix = '@cs.stanford.edu'; $name = "DEFAULT"; $email = "DEFAULT"; $nickname = ""; $ingroup = 0; while(<>) { chomp $_; # Is this the beginning of a group? if ($_ =~ m/^([^\t]*)\t([^\t]*)\t?\(.*$/) { $email = ""; $name = $1; $nickname = $2; $ingroup = 1; } if ($ingroup) { # Split up everything by commas @emails = split(/,/); for($i=0; $i<@emails; $i++) { $curemail = $emails[$i]; $raw_curemail = $curemail; # Remove leading whitespace if ($curemail =~ /^\s*(.*)/) { $curemail = $1; } # Is this the first email in a list? if ($curemail =~ /\(([^\(\)]*)/) { $curemail = $1; } # Is there a <> address here? if ($curemail =~ /<(.*)>/) { $curemail = $1; } # Otherwise grab everything for this address elsif ($curemail =~ /([^\(\)]*)/) { $curemail = $1; # Append default suffix if necessary if (!($curemail =~ /@/)) { $curemail = $curemail . $default_suffix; } } $email = $email . "<$curemail>"; # Is this the end of our group? if ($raw_curemail =~ /\)/) { $ingroup = 0; if ($include_groups) { print "$name,$email,$nickname\n"; } } else { $email = $email . "\\,"; } } next; } # Stanard email entry if ($_ =~ m/^([^\t]*)\t([^\t]*)\t([^\t]*)$/) { $nickname = $1; $name = $2; $email = $3; } # Illegal entry else { next; } # Extract actual emails if <>'s are used if ($email =~ m/<(.*@.*)>/) { $email = $1; } # Turn "last, first" into "first last" if ($name =~ m/^([^,]*), ([^,]*)$/) { $name = "$2 $1"; } # If there's no name, use the nickname if (length($name) == 0) { $name = $nickname; } # If the name is an email address, use the nickname if ($name =~ m/.+@.+\..+/) { $name = $nickname; } print "$name,$email,$nickname\n"; }