|
|
|
@ -9,7 +9,6 @@ use Data::UUID;
|
|
|
|
|
use Encode qw(encode decode);
|
|
|
|
|
use HTML::Parse;
|
|
|
|
|
use HTML::FormatText;
|
|
|
|
|
use Test::utf8 qw(is_within_latin_1);
|
|
|
|
|
use URI::Simple;
|
|
|
|
|
use Term::ReadKey;
|
|
|
|
|
use HTTP::DAV;
|
|
|
|
@ -27,7 +26,9 @@ my $conf = {
|
|
|
|
|
user => undef,
|
|
|
|
|
password => undef,
|
|
|
|
|
database => undef,
|
|
|
|
|
filters => []
|
|
|
|
|
filters => [
|
|
|
|
|
["is_company", "=", "False"]
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
dav => {
|
|
|
|
|
url => undef,
|
|
|
|
@ -165,12 +166,10 @@ sub prompt_pass {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my %odoo_uuid = ();
|
|
|
|
|
foreach my $contact_id (@{$odoo->search('res.partner', [])}){
|
|
|
|
|
my $contact_count = 0;
|
|
|
|
|
print "Querying Odoo API for contacts\n";
|
|
|
|
|
foreach my $contact_id (@{$odoo->search('res.partner', $conf->{odoo}->{filters})}){
|
|
|
|
|
my $contact = $odoo->read('res.partner', [ $contact_id ])->[0];
|
|
|
|
|
if (defined $contact->{is_company}){
|
|
|
|
|
print "Skiping $contact->{name} because it's a company\n";
|
|
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $vcard =<<_EOV;
|
|
|
|
|
BEGIN:VCARD
|
|
|
|
@ -186,10 +185,11 @@ _EOV
|
|
|
|
|
$contact->{$_} = HTML::FormatText->new->format(HTML::Parse::parse_html($contact->{$_}));
|
|
|
|
|
$contact->{$_} =~ s/\r?\n/\\n/g;
|
|
|
|
|
}
|
|
|
|
|
if (is_within_latin_1($contact->{$_})){
|
|
|
|
|
$vcard .= "$odoo2vcf->{$_}:" . encode("UTF-8", decode("Latin1", $contact->{$_})) . "\n";
|
|
|
|
|
} else {
|
|
|
|
|
# Check if the field contains non ASCII characters. If it does, it's UTF-8, else, it's Latin1
|
|
|
|
|
if ($contact->{$_} =~ /([^\x{00}-\x{ff}])/){
|
|
|
|
|
$vcard .= "$odoo2vcf->{$_}:" . encode("UTF-8", $contact->{$_}) . "\n";
|
|
|
|
|
} else {
|
|
|
|
|
$vcard .= "$odoo2vcf->{$_}:" . encode("UTF-8", decode("Latin1", $contact->{$_})) . "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -203,8 +203,11 @@ _EOV
|
|
|
|
|
open VCARD, ">$conf->{path}->{workdir}/$uuid.vcf";
|
|
|
|
|
print VCARD $vcard;
|
|
|
|
|
close VCARD;
|
|
|
|
|
$contact_count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $dav_success = 0;
|
|
|
|
|
my $dav_error = 0;
|
|
|
|
|
if (defined $conf->{dav}->{url}){
|
|
|
|
|
my $d = HTTP::DAV->new();
|
|
|
|
|
$d->credentials(
|
|
|
|
@ -237,10 +240,23 @@ if (defined $conf->{dav}->{url}){
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print "Uploading vcards to webdav server $conf->{dav}->{url}\n";
|
|
|
|
|
$d->put( -local => "$conf->{path}->{workdir}/*.vcf" );
|
|
|
|
|
foreach my $vcf (glob("$conf->{path}->{workdir}/*.vcf")){
|
|
|
|
|
if ($d->put( -local => "$vcf" ) == 1){
|
|
|
|
|
$dav_success++;
|
|
|
|
|
} else {
|
|
|
|
|
print "An error occured while uploading $vcf : " . $d->message . "\n";
|
|
|
|
|
$dav_error++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (not $conf->{path}->{keep_vcards}){
|
|
|
|
|
print "Deleting vcards from $conf->{path}->{workdir}\n";
|
|
|
|
|
rmtree($conf->{path}->{workdir});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print "$contact_count contacts found in Odoo\n";
|
|
|
|
|
if (defined $conf->{dav}->{url}){
|
|
|
|
|
print "$dav_success contacts successfuly uploaded to $conf->{dav}->{url}\n";
|
|
|
|
|
print "$dav_error contacts failed while uploading to $conf->{dav}->{url}\n";
|
|
|
|
|
}
|
|
|
|
|