#!/usr/bin/perl -w use strict; use lib "$ENV{HOME}/lib/perl"; use DBI; my $datafile="/home/sweh/etc/contacts.db"; my $oldfile="/home/sweh/etc/.contacts.db.old"; my $tmpfile="/home/sweh/etc/.contacts.tmp"; my $remote_file="/data/data/com.motorola.blur.providers.contacts/databases/contacts2.db"; my $phone="droid3"; my $old=3600; my $INDENT=20; my %phonebook_types = ( 1 => 'Home', 2 => 'Mobile', 3 => 'Work', 4 => 'Work fax', 5 => 'Home fax', 6 => 'Pager', 7 => 'Other', ); my %address_types = ( 1 => 'Home', 2 => 'Work', 3 => 'Other', ); sub fix_type($$%) { my ($type,$extra,%lookup)=@_; my $display_type=$lookup{$type}; if (!$display_type && $extra) { $type="Z"; $display_type=$extra } if (!$type) { $display_type="Unknown($type)"; $type=99; } return "$type:$display_type"; } my $WANT=join(" ",@ARGV) || ''; if ( -w "/etc/passwd" ) { print "Not checking for new data since running as root\n"; } else { my $need_copy=0; if (! -f $datafile) { $need_copy=1; } else { my $s=(stat($datafile))[9]; if (time()-$s > $old) { $need_copy=1; } } if ($need_copy) { # Is the phone on the network? system("ping -q -c 1 -W 1 $phone >/dev/null 2>&1"); if ($? == 0) { system("scp $phone:$remote_file $tmpfile"); } if ( -f "$tmpfile" ) { unlink($oldfile); rename($datafile,$oldfile); rename($tmpfile,$datafile); } } if (! -f $datafile) { print "No contact data\n"; exit; } } # Connect to datafile! my $dbh = DBI->connect( "dbi:SQLite:$datafile" ) || die "Cannot connect: $DBI::errstr\n"; sub do_run($) { my ($sql)=@_; my $sth=$dbh->prepare($sql) || die "Bad SQL: $DBI::errstr\n"; $sth->execute || die "$DBI::errstr\n"; return $sth; } # First, get a list of the person IDs that match type 6 (name) or type 8 # (nickname) my %ids=(); my $sth=do_run("select raw_contact_id from data where data1 like '%$WANT%'"); ## and mimetype_id in (6,8)"); while (my $res= $sth->fetchrow_hashref()) { $ids{$res->{raw_contact_id}}=1; } undef $sth; # Now get all the data we need for these IDs; my $wantid=join(",",keys %ids); $sth=do_run("select mimetype_id,raw_contact_id,data1,data2,data3,data4,data5,data6,data7,data8,data9,data10,data11,data12,data13,data14,data15 from data where raw_contact_id in ($wantid) and mimetype_id in (1,2,3,5,6,7,8,11,16)"); my %data=(); while (my $res= $sth->fetchrow_hashref()) { my $mime=$res->{mimetype_id}; my $id=$res->{raw_contact_id}; my $content=$res->{data1}; my $type=$res->{data2} || ''; my $extra=$res->{data3} || ''; next unless $content; if ($mime == 1) { $type=fix_type($type,$extra,%address_types); push(@{$data{$id}{email}{$type}},$content); } if ($mime == 2) { $type=fix_type($type,$extra,%address_types); push(@{$data{$id}{im}{$type}},$content); } elsif ($mime == 3) { $type=fix_type($type,$extra,%address_types); push(@{$data{$id}{postal}{$type}},$content); } elsif ($mime == 5) { $type=fix_type($type,$extra,%phonebook_types); push(@{$data{$id}{phone}{"$type"}},$content); } elsif ($mime == 6) { $data{$id}{name}=$content; } elsif ($mime == 7) { $content=""; $content = $res->{data1} if $res->{data1}; $content .= "\n" . $res->{data3} if $res->{data3}; $content .= "\n" . $res->{data4} if $res->{data4}; $content .= "\n" . $res->{data5} if $res->{data5}; $content .= "\n" . $res->{data6} if $res->{data6}; $content .= "\n" . $res->{data7} if $res->{data7}; $content .= "\n" . $res->{data8} if $res->{data8}; $content .= "\n" . $res->{data9} if $res->{data9}; $content .= "\n" . $res->{data10} if $res->{data10}; $content .= "\n" . $res->{data11} if $res->{data11}; $content .= "\n" . $res->{data12} if $res->{data12}; $content .= "\n" . $res->{data13} if $res->{data13}; $content .= "\n" . $res->{data14} if $res->{data14}; $content .= "\n" . $res->{data15} if $res->{data15}; $data{$id}{org}=$content; } elsif ($mime == 8) { $data{$id}{nickname}=$content; } elsif ($mime == 11) { $data{$id}{website}=$content; } elsif ($mime == 16) { $content=~s/[\r\n\s]+$//mg; $data{$id}{note}=$content; } } undef $sth; # Finished with the database; $dbh->disconnect; my %names=(); foreach my $id (keys %data) { my $name=$data{$id}{name} || ''; my $nick=$data{$id}{nickname} || ''; $nick=" ($nick)" if $nick; $name.=$nick; $names{$name}{$id}=1; } sub fmt($$$) { my ($i,$l,$c)=@_; $l=~s/^[^:]*://; printf("%${i}s: ",$l); my @c=split(/[\r\n]/,$c); print join("\n". (" "x$i) . " ",@c) . "\n"; } sub dump_data($$$%) { my ($id,$title,$field,%data)=@_; my @d=sort keys %{ $data{$id}{$field} }; if (@d) { print " $title:\n"; foreach my $type (@d) { foreach my $d (@{$data{$id}{$field}{$type}}) { fmt($INDENT,$type,$d); } } } } my $first=1; foreach my $name (sort { lc($a) cmp lc($b) } keys %names) { print "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n" unless $first; $first=0; foreach my $id (sort keys %{$names{$name}}) { my $notes=$data{$id}{note} || ''; my $org=$data{$id}{org} || ''; print "$name\n"; dump_data($id,"Email addresses","email",%data); dump_data($id,"Instant Messanger","im",%data); dump_data($id,"Phone Numbers","phone",%data); dump_data($id,"Postal Addresses","postal",%data); if ($org || $notes) { print "Other details:\n"; fmt($INDENT," Organisation",$org) if $org; fmt($INDENT," Notes",$notes) if $notes; } } }