2012-09-16 [mones] 3.8.1cvs60
[claws.git] / tools / csv2addressbook.pl
index ced0b821619a12560d5aaa3086ece1e72642af7e..309cfd54c761c402163e3b7d39e6b984b2ec81d7 100644 (file)
@@ -18,7 +18,7 @@ use Text::CSV_XS;
 #  * along with this program; if not, write to the Free Software
 #  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 #  *
-#  * Copyright 2007 Paul Mangan <paul@claws-mail.org>
+#  * Copyright 2007/2008 Paul Mangan <paul@claws-mail.org>
 #  *
 
 #
@@ -26,10 +26,17 @@ use Text::CSV_XS;
 # Supported address books: 
 #      Becky >= 2.41
 #      Thunderbird >= 2.0.0.6
+#      Kmail >= 1.9.7 / Kaddressbook >= 3.5.7          
+#              ** kmail bug: can export badly formatted csv **
+#      Gmail
+#      Fox Mail
 #
 
 # Becky: full export with titles
 # thunderbird: export as 'comma separated'
+# kmail/kaddressbook: Export CSV list
+# gmail: export Outlook format
+# foxmail: export with all possible headers
 
 ###
 my $quote_char = '"';
@@ -43,7 +50,7 @@ my $csvfile = '';
 my $bookname = '';
 my $iNeedHelp = '';
 
-my $known_types = qr/^(?:becky|thunderbird)$/;
+my $known_types = qr/^(?:becky|thunderbird|kmail|gmail|foxmail)$/;
 
 GetOptions("type=s" => \$type,
           "csv=s"  => \$csvfile,
@@ -64,7 +71,38 @@ my @tbird_fields = ('First Name','Last Name','Display Name','Nickname',
                    'Work City','Work State','Work ZipCode','Work Country',
                    'Job Title','Department','Organization','Web Page 1',
                    'Web Page 2','Birth Year','Birth Month','Birth Day',
-                   'Custom 1','Custom 2','Custom 3','Custom 4','Notes','junk');
+                   'Custom 1','Custom 2','Custom 3','Custom 4','Notes',
+                   'Anniversary Year','Anniversary Month','Anniversary Day',
+                   'Category','Spouse name');
+my @kmail_fields = ('Formatted Name','Family Name','Given Name',
+                   'Additional Names','Honorific Prefixes','Honorific Suffixes',
+                   'Nick Name','Birthday','Home Address Street',
+                   'Home Address City','Home Address Region',
+                   'Home Address Post Code','Home Address Country',
+                   'Home Address Label','Business Address Street',
+                   'Business Address City','Business Address Region',
+                   'Business Address Post Code','Business Address Country',
+                   'Business Address Label','Home Phone','Business Phone',
+                   'Mobile Phone','Home Fax','Business Fax','Car Phone','ISDN',
+                   'Pager','Email Address','Mail Client','Title','Role',
+                   'Organisation','Department','Note','Homepage','Profession',
+                   'Assistant\'s Name','Manager\'s Name','Partner\'s Name',
+                   'Office','IM Address','Anniversary','Blog');
+my @gmail_fields = ('Name','E-mail Address','Notes','E-mail 2 Address',
+                   'E-mail 3 Address','Mobile Phone','Pager','Company',
+                   'Job Title','Home Phone','Home Phone 2','Home Fax',
+                   'Home Address','Business Phone','Business Phone 2',
+                   'Business Fax','Business Address','Other Phone','Other Fax',
+                   'Other Address','junk');
+my @foxmail_fields = ('First Name','Last Name','Name','Nickname','e-mail Address',
+                     'Mobile Phone','Pager Number','QQ','ICQ','Personal Home Page',
+                     'Sex','Birthday','Interest','Home Country','Home Province',
+                     'Home City','Home Postal Code','Home Street Address',
+                     'Home Telephone 1','Home Telephone 2','Home Fax','Office Company',
+                     'Office Country','Office Province','Office City',
+                     'Office Postal Code','Office Address','Office HomePage',
+                     'Office Position','Office Department','Office Telephone 1',
+                     'Office Telephone 2','Office Fax','Memo','foxaddrID');
 
 if (grep m/claws-mail/ => `ps -U $ENV{USER}`) {
        die("You must quit claws-mail before running this script\n");
@@ -86,10 +124,11 @@ if ($csvfile eq "" || $type eq "" || $type !~ m/$known_types/ || $iNeedHelp) {
 Usage:
        $script [OPTIONS]
 Options:
-       --help                          Show this screen
-       --type=becky|thunderbird        Type of exported address book
-       --csv=FILENAME                  Full path to CSV file
-       --name="My new address book"    Name of new Claws address book (optional)
+       --help                                  Show this screen
+       --type=becky|thunderbird|kmail|gmail|foxmail
+                                               Type of exported address book
+       --csv=FILENAME                          Full path to CSV file
+       --name="My new address book"            Name of new Claws address book (optional)
 ~;
 exit;
 }
@@ -184,18 +223,39 @@ sub get_book_name {
                return("Becky address book");
        } elsif ($type eq "thunderbird") {
                return("Thunderbird address book");
+       } elsif ($type eq "kmail") {
+               return("Kmail address book");
+       } elsif ($type eq "gmail") {
+               return("gmail address book");
+       } elsif ($type eq "foxmail") {
+               return("foxmail address book");
        }
 }
 
 sub check_fields {
        if ($type eq "becky") {
                if ($#csvfields != $#becky_fields) {
-                       die("ERROR:\n\tNot enough fields!\n"
+                       die("ERROR:\n\tInvalid field count!\n"
                           ."\tYou need to do a Full Export With Titles\n");
                }
        } elsif ($type eq "thunderbird") {
                if ($#csvfields != $#tbird_fields) {
-                       die("ERROR:\n\tNot enough fields!\n"
+                       die("ERROR:\n\tInvalid field count!\n"
+                          ."\tProblem with your exported CSV file\n");
+               }
+       } elsif ($type eq "kmail") {
+               if ($#csvfields != $#kmail_fields) {
+                       die("ERROR:\n\tInvalid field count!\n"
+                          ."\tProblem with your exported CSV file\n");
+               }
+       } elsif ($type eq "gmail") {
+               if ($#csvfields != $#gmail_fields) {
+                       die("ERROR:\n\tInvalid field count!\n"
+                          ."\tProblem with your exported CSV file\n");
+               }
+       } elsif ($type eq "foxmail") {
+               if ($#csvfields != $#foxmail_fields) {
+                       die("ERROR:\n\tInvalid field count!\n"
                           ."\tProblem with your exported CSV file\n");
                }
        }
@@ -255,6 +315,26 @@ sub write_xml {
                                      ."remarks=\"\" />    \n";
                        }
                        $xml .= "    </address-list>    \n";
+               } elsif ($type eq "foxmail") {
+                       $xml .= "<address-list>\n      ";
+                       if ($fields[$std_items[4]] =~ m/,/) {
+                               my @addrs = split(",", $fields[$std_items[4]]);
+                               my $addr_one = pop(@addrs);
+                               $xml .= "<address uid=\"$time\" alias=\"\" "
+                                       ."email=\"$addr_one\" "
+                                       ."remarks=\"$fields[$std_items[5]]\" />    \n";
+                               foreach my $eaddr (@addrs) {
+                                       $time++;
+                                       $xml .= "<address uid=\"$time\" alias=\"\" "
+                                               ."email=\"$eaddr\" "
+                                               ."remarks=\"\" />    \n";
+                               }
+                       } else {
+                               $xml .= "<address uid=\"$time\" alias=\"\" "
+                                       ."email=\"$fields[$std_items[4]]\" "
+                                       ."remarks=\"$fields[$std_items[5]]\" />    \n";
+                       }
+                       $xml .= "</address-list>    \n";
                } else {
                        $xml .= "<address-list>\n      "
                               ."<address uid=\"$time\" alias=\"\" "
@@ -289,6 +369,12 @@ sub get_items {
                return ('10','9','2','0','1','4');
        } elsif ($type eq "thunderbird") {
                return ('0','1','3','2','4','5','38');
+       } elsif ($type eq "kmail") {
+               return ('2','1','6','0','28','34');
+       } elsif ($type eq "gmail") {
+               return('0','0','0','0','1','2');
+       } elsif ($type eq "foxmail") {
+               return ('0','1','3','2','4','33');
        }
 }
 
@@ -297,6 +383,12 @@ sub get_fields {
                return(@becky_fields);
        } elsif ($type eq "thunderbird") {
                return(@tbird_fields);
+       } elsif ($type eq "kmail") {
+               return(@kmail_fields);
+       } elsif ($type eq "gmail") {
+               return(@gmail_fields);
+       } elsif ($type eq "foxmail") {
+               return(@foxmail_fields);
        }
 }