Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -X
- # modified by Zoltan Padrah, 2012, to make it work with contacts stored on SIM,
- # not just contacts from the phone's memory
- # modified by Mihai Secasiu - http://patchlog.com/general/wammu-backup-to-csv/
- # to make it work with UTF-16 encoded files and remove the need to convert from dos to unix
- # of course this assumes that the file is in DOS format
- use strict;
- use Encode;
- use Encode::Guess qw/UTF-16LE UTF16-BE ascii utf8/;
- my $cat;
- my $id;
- my $type;
- my %data;
- my %dataTypes;
- # Print de codes?
- my $codes;
- # Print de raw data
- my $raw;
- # Print de geparste code
- my $parse = 1;
- # read file
- open(my $in, '<:raw', $ARGV[0]) || die "Couldn't open file: $!";
- my $text = do { local $/; <$in> };
- close $in;
- # now decode to use character semantics (no need to specify LE or BE when reading)
- my $content = decode('UTF-16BE', $text);
- my @lines = split /\r\n/, $content;
- foreach my $line (@lines) {
- $line =~ s/[\t\n\r\f]//g;
- $_=$line;
- if ($line =~ m/\[.*/) {
- # start of section "[ .. ]"
- if ($line =~ m/\[Phone.*/) {
- # contact from phone memory
- print STDERR "PHONE\n";
- $cat = "Phone";
- } elsif($line =~ m/\[SIM.*/) {
- # contact from SIM card
- print STDERR "SIM\n";
- $cat = "Phone";
- } elsif($line eq "[Backup]") {
- # backup section, ignore it
- print STDERR "Backup\n";
- $cat = "??";
- } elsif($line eq "[Checksum]") {
- # checksum section, ignore it
- print STDERR "Checksum\n";
- $cat = "??";
- } else {
- # unknown
- print STDERR "WHAT\n";
- $cat = "??";
- }
- } else {
- # data between 2 "[..]" blocks
- if ($cat ne "Phone") {next;}
- my @lijn = split(/ = /);
- if ( $lijn[0] =~ m/.*Location.*/ ) {
- $id = $lijn[1];
- } else {
- if ($type) {
- $data{$id}{$type} = $lijn[1];
- $type = "";
- } elsif ($lijn[1]) {
- $type = $lijn[1];
- $dataTypes{$lijn[1]} = $lijn[1];
- }
- }
- }
- }
- close (MYFILE);
- if ($codes) {
- print "Codes: \n";
- foreach my $tkey (keys %dataTypes) {
- print $tkey . " ";
- }
- print "\n\n\n";
- }
- if ($raw) {
- foreach my $key (keys %data) {
- print $key;
- foreach my $tkey (keys %dataTypes) {
- print $tkey . " " . $data{$key}{$tkey} . "\n";
- }
- }
- }
- if ($parse) {
- # print the first line containing the col titles
- print "Name,Given Name,Additional Name,Family Name,Yomi Name,Given Name Yomi,Additional Name Yomi,Family Name Yomi,Name Prefix,Name Suffix,Initials,Nickname,Short Name,Maiden Name,Birthday,Gender,Location,Billing Information,Directory Server,Mileage,Occupation,Hobby,Sensitivity,Priority,Subject,Notes,Group Membership,E-mail 1 - Type,E-mail 1 - Value,E-mail 2 - Type,E-mail 2 - Value,E-mail 3 - Type,E-mail 3 - Value,E-mail 4 - Type,E-mail 4 - Value,Phone 1 - Type,Phone 1 - Value,Phone 2 - Type,Phone 2 - Value,Phone 3 - Type,Phone 3 - Value,Phone 4 - Type,Phone 4 - Value,Address 1 - Type,Address 1 - Formatted,Address 1 - Street,Address 1 - City,Address 1 - PO Box,Address 1 - Region,Address 1 - Postal Code,Address 1 - Country,Address 1 - Extended Address\n";
- # Parse data
- # Tinker here if all your data isnt in the .csv output
- foreach my $key (keys %data) {
- print "".
- $data{$key}{Name} . "," . #Name,
- $data{$key}{FirstName} . "," . #Given Name,
- "," . #Additional Name,
- $data{$key}{LastName} . "," . #Family Name,
- "," . #Yomi Name,
- "," . #Given Name Yomi,
- "," . #Additional Name Yomi,
- "," . #Family Name Yomi,
- "," . #Name Prefix,
- "," . #Name Suffix,
- "," . #Initials,
- "," . #Nickname,
- "," . #Short Name,
- "," . #Maiden Name,
- "," . #Birthday,
- "," . #Gender,
- "," . #Location,
- "," . #Billing Information,
- "," . #Directory Server,
- "," . #Mileage,
- $data{$key}{Company} . "," . #Occupation,
- "," . #Hobby,
- "," . #Sensitivity,
- "," . #Priority,
- "," . #Subject,
- $data{$key}{Custom1} ;
- print " - " if ($data{$key}{Custom1} && $data{$key}{Note});
- print $data{$key}{Note} . "," . #Notes,
- "," ; #Group Membership,
- print "* " if ($data{$key}{Email});
- print "," . #E-mail 1 - Type,
- $data{$key}{Email} . "," . #E-mail 1 - Value,
- "," . #E-mail 2 - Type,
- "," . #E-mail 2 - Value,
- "," . #E-mail 3 - Type,
- "," . #E-mail 3 - Value,
- "," . #E-mail 4 - Type,
- "," ; #E-mail 4 - Value,
- print "Other" if ($data{$key}{NumberGeneral});
- print "," . #Phone 1 - Type,
- $data{$key}{NumberGeneral} . "," ; #Phone 1 - Value,
- print "Home" if ($data{$key}{NumberHome});
- print "," . #Phone 2 - Type,
- $data{$key}{NumberHome} . "," ; #Phone 2 - Value,
- print "Work" if ($data{$key}{NumberWork});
- print "," . #Phone 3 - Type,
- $data{$key}{NumberWork} . "," ; #Phone 3 - Value,
- print "Mobile" if ($data{$key}{NumberMobile});
- print "," . #Phone 4 - Type,
- $data{$key}{NumberMobile} . "," ; #Phone 4 - Value,
- print "Home" if ($data{$key}{Address});
- print "," . #Address 1 - Type,
- $data{$key}{Address} . "," . #Address 1 - Formatted,
- $data{$key}{Address} . "," . #Address 1 - Street,
- $data{$key}{City} . "," . #Address 1 - City,
- "," . #Address 1 - PO Box,
- "," . #Address 1 - Region,
- $data{$key}{Postal} . "," . #Address 1 - Postal Code,
- $data{$key}{Country} . "," . #Address 1 - Country,
- "," . #Address 1 - Extended Address
- "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement