tb2claws-mail 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #!/usr/bin/perl
  2. # Script name : tb2claws-mail
  3. # Script version: 1.0.2
  4. # Script based on : script kmail2claws-mail.pl
  5. # Script purpose : convert The Bat! addressbook into Claws Mail addressbook
  6. # Author : Aleksandar Urosevic aka Urke MMI <urke@gmx.net>
  7. # Licence : GPL
  8. #
  9. # Thanks goes to : Paul Mangan <paul@claws-mail.org>
  10. #
  11. # Usage: Export The Bat! Address Book to CSV file format
  12. # with all fields selected to YES and then start:
  13. # tb2claws-mail --tbfile=/full/path/to/thebat/addressbook.csv
  14. #
  15. # Change Log:
  16. #
  17. # 18-12-2003 v 1.0.2
  18. # - bugfix: added missing attribute-list start
  19. #
  20. # 01-01-2003 v 1.0.1
  21. # - bugfix: no more empty Business Homepage entry
  22. # - bugfix: no more \0D\0A´s in Notes entry
  23. # - bugfix: no more double space in Full Name entry
  24. # - code utilization
  25. # - add info about number of converted addresses
  26. #
  27. # 15-08-2002 v 1.0.0
  28. # - first public release
  29. #
  30. # TODO:
  31. #
  32. # * Add switch for Full Name entry on atrybute part
  33. #
  34. use Getopt::Long;
  35. GetOptions("tbfile=s" => \$tbfile);
  36. #$tbfile = '/home/urke/bin/sylpheed/tb2ldif/thebat-addressbook.csv';
  37. $total_addresses = 0;
  38. chdir;
  39. # check is Claws-Mail instrtalled
  40. opendir(CLAWS, ".claws-mail") || die("Can't open Claws Mail directory! Conversion abroted\n");
  41. push(@cached, (readdir(CLAWS)));
  42. closedir(CLAWS);
  43. # get last existing addressbook filename
  44. # to set filename for newest addressbook
  45. # get all existing addressbook filenames
  46. foreach $cached (@cached) {
  47. if ($cached =~ m/^addrbook/ && $cached =~ m/[0-9].xml$/) {
  48. push(addr, "$cached");
  49. }
  50. }
  51. # sort filenames, get last and set newest filename
  52. @sorted = sort {$a cmp $b} @addr;
  53. $last_one = pop(@sorted);
  54. $last_one =~ s/^addrbook-//;
  55. $last_one =~ s/.xml$//;
  56. $last_one++;
  57. $new_addrbk = "addrbook-"."$last_one".".xml";
  58. # open thebat file to stack
  59. open (TBFILE, "<$tbfile") || die("Specified Address Book file does not exist.\n\033[5m\033[31mYou must specify full path to input file!\033[0m\nConversion aborted.\n");
  60. @tblines = <TBFILE>;
  61. close TBFILE;
  62. # shift firs line from file because this is field names
  63. $dross = shift(@tblines);
  64. # set time mark and header of addressbook
  65. $time = time;
  66. $claws_addr = "<?xml version=\"1.0\" encoding=\"ISO-8859-2\" ?>\n";
  67. $claws_addr .= "<address-book name=\"The Bat! Address Book\" >\n";
  68. # create addressbook entry from The Bat! addressbook
  69. foreach $tbline (@tblines) {
  70. $total_addresses += 1;
  71. (@tbdata) = split(/,/,$tbline);
  72. foreach $tbdata (@tbdata) {
  73. # fix nonacceptable characters
  74. $tbdata =~ s/^"//;
  75. $tbdata =~ s/"$//;
  76. $tbdata =~ s/"/&quot;/g;
  77. $tbdata =~ s/&/&amp;/g;
  78. $tbdata =~ s/'/&apos;/g;
  79. $tbdata =~ s/</&lt;/g;
  80. $tbdata =~ s/>/&gt;/g;
  81. $tbdata =~ s/\\2C\ /, /g;
  82. $tbdata =~ s/(\\0D\\0A){1,}/, /g;
  83. $tbdata =~ s/\ {2,}/ /g;
  84. }
  85. # set addressbook field values
  86. $claws_addr .= " <person uid=\"$time\" first-name=\"$tbdata[1]\""
  87. ." last-name=\"$tbdata[2]\" nick-name=\"$tbdata[5]\""
  88. ." cn=\"$tbdata[0]\" >\n"
  89. ." <address-list>\n";
  90. $time++;
  91. $claws_addr .= " <address uid=\"$time\" alias=\"$tbdata[5]\" email=\"$tbdata[4]\""
  92. ." remarks=\"\" />\n"
  93. ." </address-list>\n";
  94. # find is need to make entry attributes
  95. $check = 0;
  96. for($i=6; $i<=31; $i++) {
  97. $tbdata[$i] =~ s/^\s+//;
  98. $tbdata[$i] =~ s/\s+$//;
  99. if ($tbdata[$i] ne "") { $check += 1; }
  100. }
  101. if ($check > 0) {
  102. $claws_addr .= " <attribute-list>\n";
  103. if ($tbdata[1] ne "" || $tbdata[2] ne "") {
  104. $time++;
  105. if($tbdata[29] ne "" && $tbdata[1] ne "") { $full_name = "$tbdata[29] $tbdata[1]"; } else { $full_name = "$tbdata[1]"; }
  106. if($tbdata[3] ne "") { $full_name .= " $tbdata[3]"; }
  107. if($tbdata[2] ne "") { $full_name .= " $tbdata[2]"; }
  108. if($tbdata[28] ne "") { $full_name .= " $tbdata[28]"; }
  109. $claws_addr .= " <attribute uid=\"$time\" name=\"Full Name\" >"
  110. ."$full_name</attribute>\n";
  111. }
  112. if ($tbdata[15] ne "") {
  113. $time++;
  114. $claws_addr .= " <attribute uid=\"$time\" name=\"Home Street Address\" >"
  115. ."$tbdata[15]</attribute>\n";
  116. }
  117. if ($tbdata[16] ne "") {
  118. $time++;
  119. $claws_addr .= " <attribute uid=\"$time\" name=\"Home City Address\" >"
  120. ."$tbdata[16]</attribute>\n";
  121. }
  122. if ($tbdata[17] ne "") {
  123. $time++;
  124. $claws_addr .= " <attribute uid=\"$time\" name=\"Home State Address\" >"
  125. ."$tbdata[17]</attribute>\n";
  126. }
  127. if ($tbdata[18] ne "") {
  128. $time++;
  129. $claws_addr .= " <attribute uid=\"$time\" name=\"Home Zip Address\" >"
  130. ."$tbdata[18]</attribute>\n";
  131. }
  132. if ($tbdata[19] ne "") {
  133. $time++;
  134. $claws_addr .= " <attribute uid=\"$time\" name=\"Home Country Address\" >"
  135. ."$tbdata[19]</attribute>\n";
  136. }
  137. if ($tbdata[9] ne "") {
  138. $time++;
  139. $claws_addr .= " <attribute uid=\"$time\" name=\"Home Phone\" >"
  140. ."$tbdata[9]</attribute>\n";
  141. }
  142. if ($tbdata[10] ne "") {
  143. $time++;
  144. $claws_addr .= " <attribute uid=\"$time\" name=\"Home Fax\" >"
  145. ."$tbdata[10]</attribute>\n";
  146. }
  147. if ($tbdata[11] ne "") {
  148. $time++;
  149. $claws_addr .= " <attribute uid=\"$time\" name=\"Mobile Phone\" >"
  150. ."$tbdata[11]</attribute>\n";
  151. }
  152. if ($tbdata[30] ne "") {
  153. $time++;
  154. $claws_addr .= " <attribute uid=\"$time\" name=\"Personal Homepage\" >"
  155. ."$tbdata[30]</attribute>\n";
  156. }
  157. if ($tbdata[14] ne "") {
  158. $time++;
  159. $claws_addr .= " <attribute uid=\"$time\" name=\"Pager\" >"
  160. ."$tbdata[14]</attribute>\n";
  161. }
  162. if ($tbdata[7] ne "") {
  163. $time++;
  164. $claws_addr .= " <attribute uid=\"$time\" name=\"Bussines Company\" >"
  165. ."$tbdata[7]</attribute>\n";
  166. }
  167. if ($tbdata[8] ne "") {
  168. $time++;
  169. $claws_addr .= " <attribute uid=\"$time\" name=\"Bussines Job Title\" >"
  170. ."$tbdata[8]</attribute>\n";
  171. }
  172. if ($tbdata[20] ne "") {
  173. $time++;
  174. $claws_addr .= " <attribute uid=\"$time\" name=\"Business Department\" >"
  175. ."$tbdata[20]</attribute>\n";
  176. }
  177. if ($tbdata[21] ne "") {
  178. $time++;
  179. $claws_addr .= " <attribute uid=\"$time\" name=\"Business Office\" >"
  180. ."$tbdata[21]</attribute>\n";
  181. }
  182. if ($tbdata[22] ne "") {
  183. $time++;
  184. $claws_addr .= " <attribute uid=\"$time\" name=\"Business Street Address\" >"
  185. ."$tbdata[22]</attribute>\n";
  186. }
  187. if ($tbdata[23] ne "") {
  188. $time++;
  189. $claws_addr .= " <attribute uid=\"$time\" name=\"Business City Address\" >"
  190. ."$tbdata[23]</attribute>\n";
  191. }
  192. if ($tbdata[24] ne "") {
  193. $time++;
  194. $claws_addr .= " <attribute uid=\"$time\" name=\"Business State Address\" >"
  195. ."$tbdata[24]</attribute>\n";
  196. }
  197. if ($tbdata[25] ne "") {
  198. $time++;
  199. $claws_addr .= " <attribute uid=\"$time\" name=\"Business Zip Address\" >"
  200. ."$tbdata[25]</attribute>\n";
  201. }
  202. if ($tbdata[26] ne "") {
  203. $time++;
  204. $claws_addr .= " <attribute uid=\"$time\" name=\"Business Country Address\" >"
  205. ."$tbdata[26]</attribute>\n";
  206. }
  207. if ($tbdata[12] ne "") {
  208. $time++;
  209. $claws_addr .= " <attribute uid=\"$time\" name=\"Business Phone\" >"
  210. ."$tbdata[12]</attribute>\n";
  211. }
  212. if ($tbdata[13] ne "") {
  213. $time++;
  214. $claws_addr .= " <attribute uid=\"$time\" name=\"Business Fax\" >"
  215. ."$tbdata[13]</attribute>\n";
  216. }
  217. if ($tbdata[31] ne "") {
  218. $time++;
  219. $claws_addr .= " <attribute uid=\"$time\" name=\"Business Homepage\" >"
  220. ."$tbdata[31]</attribute>\n";
  221. }
  222. if ($tbdata[27] ne "") {
  223. $time++;
  224. $claws_addr .= " <attribute uid=\"$time\" name=\"Notes\" >"
  225. ."$tbdata[27]</attribute>\n";
  226. }
  227. $claws_addr .= " </attribute-list>\n";
  228. }
  229. # if ( $check > 0 ) {
  230. # $claws_addr .= "</attribute-list>\n";
  231. # }
  232. $claws_addr .= " </person>\n";
  233. $time++;
  234. }
  235. $claws_addr .= "</address-book>\n";
  236. # print new addressbook to file
  237. open (NEWADDR, ">.claws-mail/$new_addrbk");
  238. print NEWADDR $claws_addr;
  239. close NEWADDR;
  240. # add new addressbook to index
  241. open (ADDRIN, "<.claws-mail/addrbook--index.xml") || die("Can't open addrbook--index.xml");
  242. @addrindex_file = <ADDRIN>;
  243. close ADDRIN;
  244. foreach $addrindex_line (@addrindex_file) {
  245. if ($addrindex_line =~ m/<\/book_list>/) {
  246. $rewrite_addrin .= " <book name=\"The Bat! Address Book\" file=\"$new_addrbk\" />\n"
  247. ." </book_list>\n";
  248. } else {
  249. $rewrite_addrin .= "$addrindex_line";
  250. }
  251. }
  252. open (NEWADDRIN, ">.claws-mail/addrbook--index.xml");
  253. print NEWADDRIN "$rewrite_addrin";
  254. close NEWADDRIN;
  255. print "You have sucessfully converted your The Bat! addressbook\n";
  256. print "New addressbook file name: $new_addrbk\n";
  257. print "Total addresses converted: $total_addresses\n";
  258. exit;