kmail2sylpheed_v2.pl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/perl
  2. # * Copyright © 2002 Paul Mangan <claws@thewildbeast.co.uk>
  3. # *
  4. # * This file is free software; you can redistribute it and/or modify it
  5. # * under the terms of the GNU General Public License as published by
  6. # * the Free Software Foundation; either version 2 of the License, or
  7. # * (at your option) any later version.
  8. # *
  9. # * This program is distributed in the hope that it will be useful, but
  10. # * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # * General Public License for more details.
  13. # *
  14. # * You should have received a copy of the GNU General Public License
  15. # * along with this program; if not, write to the Free Software
  16. # * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. ## script name : kmail2sylpheed_v2.pl
  18. ## script purpose : convert an exported Kmail addressbook csv file
  19. ## into a Sylpheed addressbook
  20. ## tested with Kmail 1.4.7 and KAddressBook 3.1beta1
  21. use Getopt::Long;
  22. $kmailfile = '';
  23. $iNeedHelp = '';
  24. GetOptions("kmailfile=s" => \$kmailfile,
  25. "help" => \$iNeedHelp);
  26. if ($kmailfile eq "" || $iNeedHelp) {
  27. if (!$iNeedHelp) {
  28. print "No filename given\n";
  29. }
  30. print "Use the following format:\n";
  31. print "\tkmail2sylpheed_v2.pl --kmailfile=/path/to/addressbook.csv\n";
  32. exit;
  33. }
  34. $sylph_dir = ".sylpheed-claws";
  35. $addr_index = "$sylph_dir/addrbook--index.xml";
  36. $new_addressbook = "Kmail address book";
  37. $time = time;
  38. chdir;
  39. opendir(SYLPHEED, $sylph_dir) || die("Can't open $sylph_dir directory\n");
  40. push(@cached,(readdir(SYLPHEED)));
  41. closedir(SYLPHEED);
  42. foreach $cached (@cached) {
  43. if ($cached =~ m/^addrbook/ && $cached =~ m/[0-9].xml$/) {
  44. push(@addr, "$cached");
  45. }
  46. }
  47. @sorted = sort {$a cmp $b} @addr;
  48. $last_one = pop(@sorted);
  49. $last_one =~ s/^addrbook-//;
  50. $last_one =~ s/.xml$//;
  51. $last_one++;
  52. $new_addrbk = "addrbook-"."$last_one".".xml";
  53. open (KFILE, "<$kmailfile") || die("Can't open the kmail file [$kmailfile]\n");
  54. @kmaillines = <KFILE>;
  55. close KFILE;
  56. $count = 0;
  57. $defs = shift(@kmaillines);
  58. @extra_def = (3,4,5,7 ... 27,29 ... 32,34 ... 42);
  59. (@kmaildefs) = split(/,/,$defs);
  60. $sylph_addr = "<?xml version=\"1.0\" encoding=\"US-ASCII\" ?>\n";
  61. $sylph_addr .= "<address-book name=\"Kmail address book\" >\n";
  62. foreach $kmailline (@kmaillines) {
  63. (@kmaildata) = split(/,/,$kmailline);
  64. foreach $kmaildata (@kmaildata) {
  65. $kmaildata =~ s/^"//;
  66. $kmaildata =~ s/"$//;
  67. $kmaildata =~ s/"/&quot;/g;
  68. $kmaildata =~ s/&/&amp;/g;
  69. $kmaildata =~ s/'/&apos;/g;
  70. $kmaildata =~ s/</&lt;/g;
  71. $kmaildata =~ s/>/&gt;/g;
  72. $kmaildata =~ s/\\n/, /g;
  73. chomp $kmaildata;
  74. }
  75. $sylph_addr .= " <person uid=\"$time\" first-name=\"$kmaildata[2]\""
  76. ." last-name=\"$kmaildata[1]\" nick-name=\"$kmaildata[6]\""
  77. ." cn=\"$kmaildata[2] $kmaildata[1]\" >\n"
  78. ." <address-list>\n";
  79. $time++;
  80. $sylph_addr .= " <address uid=\"$time\" alias=\"\" email=\"$kmaildata[28]\""
  81. ." remarks=\"$kmaildata[33]\" />\n"
  82. ." </address-list>\n";
  83. foreach $extra_def (@extra_def) {
  84. if ($kmaildata[$extra_def] ne "") {
  85. push (@def_exist, $extra_def);
  86. }
  87. }
  88. if ($def_exist[0]) {
  89. $sylph_addr .= " <attribute-list>\n";
  90. }
  91. foreach $def_exist (@def_exist) {
  92. $kmaildefs[$def_exist] =~ s/^"//;
  93. $kmaildefs[$def_exist] =~ s/"$//;
  94. $kmaildefs[$def_exist] =~ s/'/&apos;/g;
  95. $time++;
  96. $sylph_addr .= " <attribute uid=\"$time\" name=\"$kmaildefs[$def_exist]\" >"
  97. ."$kmaildata[$def_exist]</attribute>\n";
  98. $attribs = 1;
  99. }
  100. if ($attribs == 1) {
  101. $sylph_addr .= " </attribute-list>\n";
  102. }
  103. $sylph_addr .= " </person>\n";
  104. $time++;
  105. $count++;
  106. }
  107. $sylph_addr .= "</address-book>\n";
  108. open (NEWADDR, ">$sylph_dir/$new_addrbk");
  109. print NEWADDR $sylph_addr;
  110. close NEWADDR;
  111. open (ADDRIN, "<$addr_index")
  112. || die("can't open $addr_index for reading");
  113. @addrindex_file = <ADDRIN>;
  114. close ADDRIN;
  115. foreach $addrindex_line (@addrindex_file) {
  116. if ($addrindex_line =~ m/<\/book_list>/) {
  117. $rw_addrindex .= " <book name=\"$new_addressbook\" file=\"$new_addrbk\" />\n"
  118. ." </book_list>\n";
  119. } else {
  120. $rw_addrindex .= "$addrindex_line";
  121. }
  122. }
  123. open (NEWADDRIN, ">$addr_index")
  124. || die("Can't open $addr_index for writing");
  125. print NEWADDRIN "$rw_addrindex";
  126. close NEWADDRIN;
  127. print "Done. $count address(es) converted successfully.\n";
  128. exit;