mew2claws-mail.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/perl
  2. # * Copyright 2007 Jérôme Lelong <jerome.lelong@gmail.com>
  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 3 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 : mew2claws-mail.pl
  18. ## script purpose : convert a Mew addressbook into a Claws Mail addressbook
  19. ## This script assumes your Mew addressbook is Latin-1 encoded and not
  20. ## unicode. In this latter case, you will have to hack this script a
  21. ## little.
  22. use Getopt::Long;
  23. ## Process the command line options
  24. ## the program expects one argument: the Mew addressbook file
  25. my $help=0;
  26. my $mewfile='';
  27. GetOptions("mew-addressbook=s" => \$mewfile,
  28. "help" => \$help);
  29. if ($help==1)
  30. {
  31. print("usage : perl mew2claws-mail.pl [--help] [--mew-addressbook=file] \n");
  32. print("\t--help: displays this help\n");
  33. print("\t--mew-addressbook=file : file is the filename of your Mew addressbook\n");
  34. exit 0;
  35. }
  36. if ($mewfile ne '' && !-f $mewfile)
  37. {
  38. print("file $mewfile does not exists\n");
  39. exit 1;
  40. }
  41. $time=time;
  42. $claws_addr='';
  43. $home = glob("~");
  44. $clawsdir=`claws-mail --config-dir`;
  45. chomp($clawsdir);
  46. $clawsdir = $home . '/' . $clawsdir . '/' . 'addrbook/';
  47. opendir(CLAWS, $clawsdir) || die("Can't open $clawsdir directory\n");
  48. push(@cached,(readdir(CLAWS)));
  49. closedir(CLAWS);
  50. ## find the first availabel name for a new addressbook in claws-mail
  51. foreach $cached (@cached)
  52. {
  53. if ($cached =~ m/^addrbook/ && $cached =~ m/[0-9].xml$/)
  54. {
  55. push(@addr, "$cached");
  56. }
  57. }
  58. @sorted = sort {$a cmp $b} @addr;
  59. $last_one = pop(@sorted);
  60. $last_one =~ s/^addrbook-//;
  61. $last_one =~ s/.xml$//;
  62. $last_one++;
  63. $new_addrbk = "addrbook-"."$last_one".".xml";
  64. open (MEWFILE, "<$mewfile") || die("Can't find the Mew addressbook file\n");
  65. @mewentries = <MEWFILE>;
  66. close MEWFILE;
  67. $claws_addr .= "<?xml version=\"1.0\" encoding=\"ISO8859-1\" ?>\n"
  68. . "<address-book name=\"Mew Address Book\" >";
  69. chomp(@mewentries);
  70. foreach $line (@mewentries)
  71. {
  72. $line =~ s/ *\t/ /g;
  73. $line =~ s/ *$//g;
  74. (@fields) = split(/ +/,$line);
  75. $nickname= shift(@fields);
  76. @emails=();
  77. $alias='';
  78. $firstname='';
  79. $lastname='';
  80. while (1)
  81. {
  82. $field = shift(@fields);
  83. if ($field =~ m/@/)
  84. {
  85. $field =~ s/,$//;
  86. push(@emails, $field);
  87. } else
  88. {
  89. unshift(@fields, $field);
  90. last;
  91. }
  92. }
  93. $alias = shift(@fields);
  94. if ($alias eq "\*")
  95. {
  96. print($alias . "\n");
  97. $alias='';
  98. }
  99. $firstname=shift(@fields); $firstname =~ s/"//g;
  100. foreach (@fields)
  101. {
  102. $lastname .= "$_ ";
  103. }
  104. $lastname =~ s/"//g;
  105. $lastname =~ s/ *$//g;
  106. $claws_addr .= " <person uid=\"$time\" first-name=\"$firstname\""
  107. ." last-name=\"$lastname\" nick-name=\"$nickname\""
  108. ." cn=\"$firstname $lastname\" >\n"
  109. ." <address-list>\n";
  110. $time++;
  111. foreach $email (@emails)
  112. {
  113. $claws_addr .= " <address uid=\"$time\" alias=\"$alias\" email=\"$email\""
  114. ." remarks=\"\" />\n";
  115. $time++;
  116. }
  117. $claws_addr .= " </address-list>\n"
  118. . " <attribute-list>\n"
  119. . " </attribute-list>\n";
  120. $claws_addr .= " </person>\n";
  121. $time++;
  122. }
  123. $claws_addr .= "</address-book>\n";
  124. open (NEWADDR, ">$clawsdir/$new_addrbk") ;
  125. print NEWADDR ($claws_addr);
  126. close NEWADDR;
  127. open (ADDRIN, "<$clawsdir/addrbook--index.xml") || die("can't open addrbook--index.xml");
  128. @addrindex_file = <ADDRIN>;
  129. close ADDRIN;
  130. foreach $addrindex_line (@addrindex_file)
  131. {
  132. if ($addrindex_line =~ m/<book name=\"Mew Address Book\"/)
  133. {
  134. print("An entry already exists for \"Mew Address Book\", you may duplicate it\n");
  135. print("Continuing anyway...\n");
  136. }
  137. if ($addrindex_line =~ m/<\/book_list>/)
  138. {
  139. $rewrite_addrin .= " <book name=\"Mew Address Book\" file=\"$new_addrbk\" />\n"
  140. ." </book_list>\n";
  141. } else
  142. {
  143. $rewrite_addrin .= "$addrindex_line";
  144. }
  145. }
  146. open (NEWADDRIN, ">$clawsdir/addrbook--index.xml");
  147. print NEWADDRIN "$rewrite_addrin";
  148. close NEWADDRIN;
  149. print "\nYou have sucessfully converted your Mew addressbook\n";
  150. exit;