diff --git a/Dockerfile b/Dockerfile index 00a3712..5e2930c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,7 +28,6 @@ RUN set -eux &&\ Data::UUID \ HTML::Parse \ HTML::FormatText \ - Test::utf8 \ URI::Simple \ HTTP::DAV \ Term::ReadKey \ diff --git a/README.md b/README.md index 8e6e2e1..03f8113 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,6 @@ The following perl modules are needed to run it - Encode - HTML::Parse - HTML::FormatText -- Test::utf8 - URI::Simple - Term::ReadKey - HTTP::DAV diff --git a/odoo2carddav b/odoo2carddav index cbd5f81..fc2c8d5 100755 --- a/odoo2carddav +++ b/odoo2carddav @@ -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; @@ -167,6 +166,7 @@ sub prompt_pass { } my %odoo_uuid = (); +my $contact_count = 0; foreach my $contact_id (@{$odoo->search('res.partner', $conf->{odoo}->{filters})}){ my $contact = $odoo->read('res.partner', [ $contact_id ])->[0]; @@ -184,10 +184,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"; } } @@ -201,6 +202,7 @@ _EOV open VCARD, ">$conf->{path}->{workdir}/$uuid.vcf"; print VCARD $vcard; close VCARD; + $countact_count++; } if (defined $conf->{dav}->{url}){ @@ -235,10 +237,23 @@ if (defined $conf->{dav}->{url}){ } print "Uploading vcards to webdav server $conf->{dav}->{url}\n"; - $d->put( -local => "$conf->{path}->{workdir}/*.vcf" ); + my $dav_success = 0; + my $dav_error = 0; + foreach my $vcf (glob("$conf->{path}->{workdir}/*.vcf")){ + if ($d->put( -local => "$vcf" ) == 0){ + $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"; +print "$dav_success contacts successfuly uploaded to $conf->{dav}->{url}\n"; +print "$dav_error contacts failed while uploading to $conf->{dav}->{url}\n";