241 lines
7.2 KiB
Perl
241 lines
7.2 KiB
Perl
#!/usr/bin/perl
|
|
|
|
# Script name : tb2sylpheed.pl
|
|
# Script based on : script kmail2sylpheed.pl
|
|
# Script purpose : convert The Bat! addressbook into a Sylpheed addressbook
|
|
# Author : Aleksandar Urosevic aka Urke MMI <urke@gmx.net>
|
|
# Licence : GPL
|
|
#
|
|
# Thanks goes to : Paul Mangan <claws@thewildbeast.co.uk>
|
|
#
|
|
# Usage: Export The Bat! Address Book to CSV file format
|
|
# with all fields selected to YES and then start:
|
|
# tb2sylpheed --tbfile=/full/path/to/thebat/addressbook.csv
|
|
|
|
use Getopt::Long;
|
|
|
|
$tbfile = '';
|
|
|
|
GetOptions("tbfile=s" => \$tbfile);
|
|
|
|
$time = time;
|
|
|
|
# $tbfile = 'thebat-addressbook.csv';
|
|
|
|
$sylph_addr = "<?xml version=\"1.0\" encoding=\"ISO-8859-2\" ?>\n";
|
|
$sylph_addr .= "<address-book name=\"The Bat! Address Book\" >\n";
|
|
|
|
# $new_addrbk = "addrbook-000013.xml";
|
|
|
|
chdir;
|
|
|
|
# check is Sylpheed instrtalled
|
|
|
|
opendir(SYLPHEED, ".sylpheed") || die("Can't oen sylpheed directory\n");
|
|
push(@cached, (readdir(SYLPHEED)));
|
|
closedir(SYLPHEED);
|
|
|
|
# get last existing addressbook filename to set filename for newest addressbook
|
|
|
|
foreach $cached (@cached) {
|
|
if ($cached =~ m/^addrbook/ && $cached =~ m/[0-9].xml$/) {
|
|
push(addr, "$cached");
|
|
}
|
|
}
|
|
|
|
@sorted = sort {$a cmp $b} @addr;
|
|
$last_one = pop(@sorted);
|
|
$last_one =~ s/^addrbook-//;
|
|
$last_one =~ s/.xml$//;
|
|
$last_one++;
|
|
$new_addrbk = "addrbook-"."$last_one".".xml";
|
|
|
|
# opening thebat file in to stack
|
|
|
|
open (TBFILE, "<$tbfile") || die("Can't find the thebat file\n");
|
|
@tblines = <TBFILE>;
|
|
close TBFILE;
|
|
|
|
$dross = shift(@tblines);
|
|
|
|
# create addressbook entry from The Bat! addressbook
|
|
|
|
foreach $tbline (@tblines) {
|
|
(@tbdata) = split(/,/,$tbline);
|
|
foreach $tbdata (@tbdata) {
|
|
$tbdata =~ s/^"//;
|
|
$tbdata =~ s/"$//;
|
|
$tbdata =~ s/"/"/g;
|
|
$tbdata =~ s/&/&/g;
|
|
$tbdata =~ s/'/'/g;
|
|
$tbdata =~ s/</</g;
|
|
$tbdata =~ s/>/>/g;
|
|
}
|
|
$sylph_addr .= " <person uid=\"$time\" first-name=\"$tbdata[1]\""
|
|
." last-name=\"$tbdata[2]\" nick-name=\"$tbdata[5]\""
|
|
." cn=\"$tbdata[0]\" >\n"
|
|
." <address-list>\n";
|
|
$time++;
|
|
$sylph_addr .= " <address uid=\"$time\" alias=\"\" email=\"$tbdata[4]\""
|
|
." remarks=\"$tbdata[5]\" />\n"
|
|
." </address-list>\n";
|
|
if ($tbdata[6] ne "" || $tbdata[7] ne "" || $tbdata[8] ne "" ||
|
|
$tbdata[9] ne "" || $tbdata[10] ne "" || $tbdata[11] ne "" ||
|
|
$tbdata[12] ne "" || $tbdata[13] ne "" || $tbdata[14] ne "" ||
|
|
$tbdata[15] ne "" || $tbdata[16] ne "" || $tbdata[17] ne "" ||
|
|
$tbdata[18] ne "" || $tbdata[19] ne "" || $tbdata[20] ne "" ||
|
|
$tbdata[21] ne "" || $tbdata[22] ne "" || $tbdata[23] ne "" ||
|
|
$tbdata[24] ne "" || $tbdata[25] ne "" || $tbdata[26] ne "" ||
|
|
$tbdata[27] ne "" || $tbdata[30] ne "" || $tbdata[31] ne "") {
|
|
$sylph_addr .= " <attribute-list>\n";
|
|
|
|
if ($tbdata[1] ne "" || $tbdata[2] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Full Name\" >"
|
|
."$tbdata[29] $tbdata[1] $tbdata[3] $tbdata[2] $tbdata[28]</attribute>\n";
|
|
}
|
|
if ($tbdata[15] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Home Street Address\" >"
|
|
."$tbdata[15]</attribute>\n";
|
|
}
|
|
if ($tbdata[16] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Home City Address\" >"
|
|
."$tbdata[16]</attribute>\n";
|
|
}
|
|
if ($tbdata[17] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Home State Address\" >"
|
|
."$tbdata[17]</attribute>\n";
|
|
}
|
|
if ($tbdata[18] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Home Zip Address\" >"
|
|
."$tbdata[18]</attribute>\n";
|
|
}
|
|
if ($tbdata[19] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Home Country Address\" >"
|
|
."$tbdata[19]</attribute>\n";
|
|
}
|
|
if ($tbdata[9] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Home Phone\" >"
|
|
."$tbdata[9]</attribute>\n";
|
|
}
|
|
if ($tbdata[10] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Home Fax\" >"
|
|
."$tbdata[10]</attribute>\n";
|
|
}
|
|
if ($tbdata[11] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Mobile Phone\" >"
|
|
."$tbdata[11]</attribute>\n";
|
|
}
|
|
if ($tbdata[30] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Personal Homepage\" >"
|
|
."$tbdata[30]</attribute>\n";
|
|
}
|
|
if ($tbdata[14] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Pager\" >"
|
|
."$tbdata[14]</attribute>\n";
|
|
}
|
|
if ($tbdata[7] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Bussines Company\" >"
|
|
."$tbdata[7]</attribute>\n";
|
|
}
|
|
if ($tbdata[8] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Bussines Job Title\" >"
|
|
."$tbdata[8]</attribute>\n";
|
|
}
|
|
if ($tbdata[20] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Business Department\" >"
|
|
."$tbdata[20]</attribute>\n";
|
|
}
|
|
if ($tbdata[21] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Business Office\" >"
|
|
."$tbdata[21]</attribute>\n";
|
|
}
|
|
if ($tbdata[22] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Business Street Address\" >"
|
|
."$tbdata[22]</attribute>\n";
|
|
}
|
|
if ($tbdata[23] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Business City Address\" >"
|
|
."$tbdata[23]</attribute>\n";
|
|
}
|
|
if ($tbdata[24] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Business State Address\" >"
|
|
."$tbdata[24]</attribute>\n";
|
|
}
|
|
if ($tbdata[25] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Business Zip Address\" >"
|
|
."$tbdata[25]</attribute>\n";
|
|
}
|
|
if ($tbdata[26] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Business Country Address\" >"
|
|
."$tbdata[26]</attribute>\n";
|
|
}
|
|
if ($tbdata[12] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Business Phone\" >"
|
|
."$tbdata[12]</attribute>\n";
|
|
}
|
|
if ($tbdata[13] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Business Fax\" >"
|
|
."$tbdata[13]</attribute>\n";
|
|
}
|
|
if ($tbdata[31] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Business Homepage\" >"
|
|
."$tbdata[31]</attribute>\n";
|
|
}
|
|
if ($tbdata[27] ne "") {
|
|
$time++;
|
|
$sylph_addr .= " <attribute uid=\"$time\" name=\"Notes\" >"
|
|
."$tbdata[27]</attribute>\n";
|
|
}
|
|
$sylph_addr .= " </attribute-list>\n";
|
|
}
|
|
$sylph_addr .= " </person>\n";
|
|
$time++;
|
|
}
|
|
$sylph_addr .= "</address-book>\n";
|
|
|
|
open (NEWADDR, ">.sylpheed/$new_addrbk");
|
|
print NEWADDR $sylph_addr;
|
|
close NEWADDR;
|
|
|
|
open (ADDRIN, "<.sylpheed/addrbook--index.xml") || die("can't open addrbook--index.xml");
|
|
@addrindex_file = <ADDRIN>;
|
|
close ADDRIN;
|
|
|
|
foreach $addrindex_line (@addrindex_file) {
|
|
if ($addrindex_line =~ m/<\/book_list>/) {
|
|
$rewrite_addrin .= " <book name=\"The Bat! Address Book\" file=\"$new_addrbk\" />\n"
|
|
." </book_list>\n";
|
|
} else {
|
|
$rewrite_addrin .= "$addrindex_line";
|
|
}
|
|
}
|
|
|
|
open (NEWADDRIN, ">.sylpheed/addrbook--index.xml");
|
|
print NEWADDRIN "$rewrite_addrin";
|
|
close NEWADDRIN;
|
|
|
|
print "\nYou have sucessfully converted your The Bat! addressbook\n";
|
|
exit;
|