Advertisement
Guest User

wammu2csv - work with SIM

a guest
Jul 17th, 2012
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.51 KB | None | 0 0
  1. #!/usr/bin/perl -X
  2.  
  3. # modified by Zoltan Padrah, 2012, to make it work with contacts stored on SIM,
  4. # not just contacts from the phone's memory
  5.  
  6. # modified by Mihai Secasiu - http://patchlog.com/general/wammu-backup-to-csv/
  7. # to make it work with UTF-16 encoded files and remove the need to convert from dos to unix
  8. # of course this assumes that the file is in DOS format
  9.  
  10. use strict;
  11. use Encode;
  12. use Encode::Guess qw/UTF-16LE UTF16-BE ascii utf8/;
  13.  
  14. my $cat;
  15. my $id;
  16. my $type;
  17. my %data;
  18. my %dataTypes;
  19.  
  20. # Print de codes?
  21. my $codes;
  22. # Print de raw data
  23. my $raw;
  24. # Print de geparste code
  25. my $parse = 1;
  26.  
  27. # read file
  28. open(my $in, '<:raw', $ARGV[0]) || die "Couldn't open file: $!";
  29. my $text = do { local $/; <$in> };
  30. close $in;
  31.  
  32. # now decode to use character semantics (no need to specify LE or BE when reading)
  33. my $content = decode('UTF-16BE', $text);
  34. my @lines = split /\r\n/, $content;
  35.  
  36.  
  37. foreach my $line (@lines) {
  38.  
  39.   $line =~ s/[\t\n\r\f]//g;
  40.   $_=$line;
  41.  
  42.   if ($line =~ m/\[.*/) {
  43.     # start of section "[ .. ]"
  44.     if ($line =~ m/\[Phone.*/) {
  45.       # contact from phone memory
  46.       print STDERR "PHONE\n";
  47.       $cat = "Phone";
  48.     } elsif($line =~ m/\[SIM.*/) {
  49.       # contact from SIM card
  50.       print STDERR "SIM\n";
  51.       $cat = "Phone";
  52.     } elsif($line eq "[Backup]") {
  53.       # backup section, ignore it
  54.       print STDERR "Backup\n";
  55.       $cat = "??";
  56.     } elsif($line eq "[Checksum]") {
  57.       # checksum section, ignore it
  58.       print STDERR "Checksum\n";
  59.       $cat = "??";
  60.     } else {
  61.       # unknown
  62.       print STDERR "WHAT\n";
  63.       $cat = "??";
  64.     }
  65.   } else {
  66.     # data between 2 "[..]" blocks
  67.     if ($cat ne "Phone") {next;}
  68.     my @lijn = split(/ = /);
  69.  
  70.     if ( $lijn[0] =~ m/.*Location.*/ ) {
  71.       $id = $lijn[1];
  72.     } else {
  73.  
  74.       if ($type) {
  75.         $data{$id}{$type} = $lijn[1];
  76.         $type = "";
  77.       } elsif ($lijn[1]) {
  78.         $type = $lijn[1];
  79.         $dataTypes{$lijn[1]} = $lijn[1];
  80.       }
  81.     }
  82.   }
  83. }
  84.  
  85. close (MYFILE);
  86.  
  87. if ($codes) {
  88.   print "Codes: \n";
  89.   foreach my $tkey (keys %dataTypes) {
  90.     print $tkey . " ";
  91.   }
  92.   print "\n\n\n";
  93. }
  94.  
  95.  
  96. if ($raw) {
  97.   foreach my $key (keys %data) {
  98.     print $key;
  99.     foreach my $tkey (keys %dataTypes) {
  100.       print $tkey . " " . $data{$key}{$tkey} . "\n";
  101.     }
  102.   }
  103. }
  104.  
  105.  
  106. if ($parse) {
  107.   # print the first line containing the col titles
  108.   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";
  109.  
  110.   # Parse data
  111.   # Tinker here if all your data isnt in the .csv output
  112.   foreach my $key (keys %data) {
  113.     print "".
  114.       $data{$key}{Name} . "," . #Name,
  115.       $data{$key}{FirstName} . "," . #Given Name,
  116.       "," . #Additional Name,
  117.       $data{$key}{LastName} . "," . #Family Name,
  118.       "," . #Yomi Name,
  119.       "," . #Given Name Yomi,
  120.       "," . #Additional Name Yomi,
  121.       "," . #Family Name Yomi,
  122.       "," . #Name Prefix,
  123.       "," . #Name Suffix,
  124.       "," . #Initials,
  125.       "," . #Nickname,
  126.       "," . #Short Name,
  127.       "," . #Maiden Name,
  128.       "," . #Birthday,
  129.       "," . #Gender,
  130.       "," . #Location,
  131.       "," . #Billing Information,
  132.       "," . #Directory Server,
  133.       "," . #Mileage,
  134.       $data{$key}{Company} . "," . #Occupation,
  135.       "," . #Hobby,
  136.       "," . #Sensitivity,
  137.       "," . #Priority,
  138.       "," . #Subject,
  139.       $data{$key}{Custom1} ;
  140.     print " - " if ($data{$key}{Custom1} && $data{$key}{Note});
  141.     print $data{$key}{Note} . "," . #Notes,
  142.       "," ; #Group Membership,
  143.     print "* " if ($data{$key}{Email});
  144.     print "," . #E-mail 1 - Type,
  145.       $data{$key}{Email} . "," . #E-mail 1 - Value,
  146.       "," . #E-mail 2 - Type,
  147.       "," . #E-mail 2 - Value,
  148.       "," . #E-mail 3 - Type,
  149.       "," . #E-mail 3 - Value,
  150.       "," . #E-mail 4 - Type,
  151.       "," ; #E-mail 4 - Value,
  152.     print "Other" if ($data{$key}{NumberGeneral});
  153.     print "," . #Phone 1 - Type,
  154.       $data{$key}{NumberGeneral} . "," ; #Phone 1 - Value,
  155.     print "Home" if ($data{$key}{NumberHome});
  156.     print "," . #Phone 2 - Type,
  157.       $data{$key}{NumberHome} . "," ; #Phone 2 - Value,
  158.     print "Work" if ($data{$key}{NumberWork});
  159.     print "," . #Phone 3 - Type,
  160.       $data{$key}{NumberWork} . "," ; #Phone 3 - Value,
  161.     print "Mobile" if ($data{$key}{NumberMobile});
  162.     print "," . #Phone 4 - Type,
  163.       $data{$key}{NumberMobile} . "," ; #Phone 4 - Value,
  164.     print "Home" if ($data{$key}{Address});
  165.     print "," . #Address 1 - Type,
  166.       $data{$key}{Address} . "," . #Address 1 - Formatted,
  167.       $data{$key}{Address} . "," . #Address 1 - Street,
  168.       $data{$key}{City} . "," . #Address 1 - City,
  169.       "," . #Address 1 - PO Box,
  170.       "," . #Address 1 - Region,
  171.       $data{$key}{Postal} . "," . #Address 1 - Postal Code,
  172.       $data{$key}{Country} . "," . #Address 1 - Country,
  173.       "," . #Address 1 - Extended Address
  174.       "\n";
  175.   }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement