kmail2sylpheed.pl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.pl
  18. ## script purpose : convert a Kmail addressbook into a Sylpheed addressbook
  19. use Getopt::Long;
  20. $kmailfile = '';
  21. GetOptions("kmailfile=s" => \$kmailfile);
  22. $time = time;
  23. $sylph_addr = "<?xml version=\"1.0\" encoding=\"US-ASCII\" ?>\n";
  24. $sylph_addr .= "<address-book name=\"Kmail Address Book\" >\n";
  25. chdir;
  26. opendir(SYLPHEED, ".sylpheed-claws") || die("Can't open .sylpheed-claws directory\n");
  27. push(@cached,(readdir(SYLPHEED)));
  28. closedir(SYLPHEED);
  29. foreach $cached (@cached) {
  30. if ($cached =~ m/^addrbook/ && $cached =~ m/[0-9].xml$/) {
  31. push(@addr, "$cached");
  32. }
  33. }
  34. @sorted = sort {$a cmp $b} @addr;
  35. $last_one = pop(@sorted);
  36. $last_one =~ s/^addrbook-//;
  37. $last_one =~ s/.xml$//;
  38. $last_one++;
  39. $new_addrbk = "addrbook-"."$last_one".".xml";
  40. open (KFILE, "<$kmailfile") || die("Can't find the kmail file\n");
  41. @kmaillines = <KFILE>;
  42. close KFILE;
  43. $dross = shift(@kmaillines);
  44. foreach $kmailline (@kmaillines) {
  45. (@kmaildata) = split(/,/,$kmailline);
  46. foreach $kmaildata (@kmaildata) {
  47. $kmaildata =~ s/^"//;
  48. $kmaildata =~ s/"$//;
  49. $kmaildata =~ s/"/&quot;/g;
  50. $kmaildata =~ s/&/&amp;/g;
  51. $kmaildata =~ s/'/&apos;/g;
  52. $kmaildata =~ s/</&lt;/g;
  53. $kmaildata =~ s/>/&gt;/g;
  54. }
  55. $sylph_addr .= " <person uid=\"$time\" first-name=\"$kmaildata[0]\""
  56. ." last-name=\"$kmaildata[1]\" nick-name=\"$kmaildata[7]\""
  57. ." cn=\"$kmaildata[0] $kmaildata[1]\" >\n"
  58. ." <address-list>\n";
  59. $time++;
  60. $sylph_addr .= " <address uid=\"$time\" alias=\"\" email=\"$kmaildata[6]\""
  61. ." remarks=\"$kmaildata[8]\" />\n"
  62. ." </address-list>\n";
  63. if ($kmaildata[13] ne "" || $kmaildata[9] ne "" || $kmaildata[21] ne "" ||
  64. $kmaildata[16] ne "" || $kmaildata[5] ne "" || $kmaildata[24] ne "" ||
  65. $kmaildata[19] ne "" || $kmaildata[12] ne "" || $kmaildata[10] ne "" ||
  66. $kmaildata[4] ne "" || $kmaildata[2] ne "" || $kmaildata[11] ne "" ||
  67. $kmaildata[3] ne "" || $kmaildata[14] ne "" || $kmaildata[22] ne "" ||
  68. $kmaildata[17] ne "" || $kmaildata[20] ne "" || $kmaildata[15] ne "" ||
  69. $kmaildata[23] ne "" || $kmaildata[18] ne "") {
  70. $sylph_addr .= " <attribute-list>\n";
  71. if ($kmaildata[3] ne "" || $kmaildata[2] ne "") {
  72. $time++;
  73. $sylph_addr .= " <attribute uid=\"$time\" name=\"Full Name\" >"
  74. ."$kmaildata[3] $kmaildata[0] $kmaildata[2] $kmaildata[1]</attribute>\n";
  75. }
  76. if ($kmaildata[15] ne "") {
  77. $time++;
  78. $sylph_addr .= " <attribute uid=\"$time\" name=\"Home Street Address\" >"
  79. ."$kmaildata[15]</attribute>\n";
  80. }
  81. if ($kmaildata[16] ne "") {
  82. $time++;
  83. $sylph_addr .= " <attribute uid=\"$time\" name=\"Home City Address\" >"
  84. ."$kmaildata[16]</attribute>\n";
  85. }
  86. if ($kmaildata[17] ne "") {
  87. $time++;
  88. $sylph_addr .= " <attribute uid=\"$time\" name=\"Home State Address\" >"
  89. ."$kmaildata[17]</attribute>\n";
  90. }
  91. if ($kmaildata[18] ne "") {
  92. $time++;
  93. $sylph_addr .= " <attribute uid=\"$time\" name=\"Home Zip Address\" >"
  94. ."$kmaildata[18]</attribute>\n";
  95. }
  96. if ($kmaildata[19] ne "") {
  97. $time++;
  98. $sylph_addr .= " <attribute uid=\"$time\" name=\"Home Country Address\" >"
  99. ."$kmaildata[19]</attribute>\n";
  100. }
  101. if ($kmaildata[10] ne "") {
  102. $time++;
  103. $sylph_addr .= " <attribute uid=\"$time\" name=\"Home Phone\" >"
  104. ."$kmaildata[10]</attribute>\n";
  105. }
  106. if ($kmaildata[12] ne "") {
  107. $time++;
  108. $sylph_addr .= " <attribute uid=\"$time\" name=\"Home Fax\" >"
  109. ."$kmaildata[12]</attribute>\n";
  110. }
  111. if ($kmaildata[11] ne "") {
  112. $time++;
  113. $sylph_addr .= " <attribute uid=\"$time\" name=\"Mobile Phone\" >"
  114. ."$kmaildata[11]</attribute>\n";
  115. }
  116. if ($kmaildata[14] ne "") {
  117. $time++;
  118. $sylph_addr .= " <attribute uid=\"$time\" name=\"Pager\" >"
  119. ."$kmaildata[14]</attribute>\n";
  120. }
  121. if ($kmaildata[5] ne "") {
  122. $time++;
  123. $sylph_addr .= " <attribute uid=\"$time\" name=\"Company\" >"
  124. ."$kmaildata[5]</attribute>\n";
  125. }
  126. if ($kmaildata[4] ne "") {
  127. $time++;
  128. $sylph_addr .= " <attribute uid=\"$time\" name=\"Job Title\" >"
  129. ."$kmaildata[4]</attribute>\n";
  130. }
  131. if ($kmaildata[20] ne "") {
  132. $time++;
  133. $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Street Address\" >"
  134. ."$kmaildata[20]</attribute>\n";
  135. }
  136. if ($kmaildata[21] ne "") {
  137. $time++;
  138. $sylph_addr .= " <attribute uid=\"$time\" name=\"Business City Address\" >"
  139. ."$kmaildata[21]</attribute>\n";
  140. }
  141. if ($kmaildata[22] ne "") {
  142. $time++;
  143. $sylph_addr .= " <attribute uid=\"$time\" name=\"Business State Address\" >"
  144. ."$kmaildata[22]</attribute>\n";
  145. }
  146. if ($kmaildata[23] ne "") {
  147. $time++;
  148. $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Zip Address\" >"
  149. ."$kmaildata[23]</attribute>\n";
  150. }
  151. if ($kmaildata[24] ne "") {
  152. $time++;
  153. $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Country Address\" >"
  154. ."$kmaildata[24]</attribute>\n";
  155. }
  156. if ($kmaildata[9] ne "") {
  157. $time++;
  158. $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Phone\" >"
  159. ."$kmaildata[9]</attribute>\n";
  160. }
  161. if ($kmaildata[13] ne "") {
  162. $time++;
  163. $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Fax\" >"
  164. ."$kmaildata[13]</attribute>\n";
  165. }
  166. $sylph_addr .= " </attribute-list>\n";
  167. }
  168. $sylph_addr .= " </person>\n";
  169. $time++;
  170. }
  171. $sylph_addr .= "</address-book>\n";
  172. open (NEWADDR, ">.sylpheed-claws/$new_addrbk");
  173. print NEWADDR $sylph_addr;
  174. close NEWADDR;
  175. open (ADDRIN, "<.sylpheed-claws/addrbook--index.xml") || die("can't open addrbook--index.xml");
  176. @addrindex_file = <ADDRIN>;
  177. close ADDRIN;
  178. foreach $addrindex_line (@addrindex_file) {
  179. if ($addrindex_line =~ m/<\/book_list>/) {
  180. $rewrite_addrin .= " <book name=\"Kmail Address Book\" file=\"$new_addrbk\" />\n"
  181. ." </book_list>\n";
  182. } else {
  183. $rewrite_addrin .= "$addrindex_line";
  184. }
  185. }
  186. open (NEWADDRIN, ">.sylpheed-claws/addrbook--index.xml");
  187. print NEWADDRIN "$rewrite_addrin";
  188. close NEWADDRIN;
  189. print "\nYou have sucessfully converted your Kmail addressbook\n";
  190. exit;