| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Iphone::SMS; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 23840 | use 5.006; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 38 |  | 
| 4 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 9 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 5 | 1 |  |  | 1 |  | 5 | use warnings FATAL => 'all'; | 
|  | 1 |  |  |  |  | 6 |  | 
|  | 1 |  |  |  |  | 49 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 2483 | use DBI; | 
|  | 1 |  |  |  |  | 19260 |  | 
|  | 1 |  |  |  |  | 76 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 1 |  |  | 1 |  | 12 | use Cwd; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 71 |  | 
| 10 | 1 |  |  | 1 |  | 5 | use File::Spec qw(); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 18 |  | 
| 11 | 1 |  |  | 1 |  | 5 | use File::Find; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 61 |  | 
| 12 | 1 |  |  | 1 |  | 5 | use Fcntl qw(:DEFAULT :flock); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 1424 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | =head1 NAME | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | Iphone::SMS - extract sms from itunes backup files | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | =head1 VERSION | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | Version 0.03 | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | =cut | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | Extract your sms in iphone from itunes backup files | 
| 30 |  |  |  |  |  |  | Be sure backup your iphone by itunes first | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | Usage: | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | use Iphone::SMS; | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | my $foo = Iphone::SMS->new([path]); | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | path is the path where itunes backup directory lies | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | or | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | my $foo = Iphone::SMS->new(); | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | it will search the itunes default backup path | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | my $sms = $foo->get_sms() | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =head1 SUBROUTINES/METHODS | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =head2 new | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | create a iphone::sms | 
| 55 |  |  |  |  |  |  | my $foo = Iphone::SMS->new(); | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | =cut | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub new { | 
| 60 | 0 |  |  | 0 | 1 |  | my ($class, $path) = @_; | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 0 | 0 |  |  |  |  | if (not defined $path){ | 
| 63 | 0 | 0 | 0 |  |  |  | if ( $^O =~ /ms/i or $^O eq 'cygwin' ) { | 
|  |  | 0 |  |  |  |  |  | 
| 64 | 0 |  |  |  |  |  | $path = File::Spec->catdir( | 
| 65 |  |  |  |  |  |  | $ENV{'HOME'}, 'AppData', 'Roaming','Apple Computer', 'MobileSync', 'Backup' | 
| 66 |  |  |  |  |  |  | ); | 
| 67 |  |  |  |  |  |  | } elsif ( $^O =~ /darwin/i ) { | 
| 68 | 0 |  |  |  |  |  | $path = File::Spec->catdir( | 
| 69 |  |  |  |  |  |  | $ENV{'HOME'}, 'Library', 'Application Support', 'MobileSync', 'Backup' | 
| 70 |  |  |  |  |  |  | ); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 | 0 |  |  |  |  | if ( not -d $path ) { | 
| 75 | 0 |  |  |  |  |  | print "directory [$path] not exists.\n"; | 
| 76 | 0 |  |  |  |  |  | exit; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 0 |  |  |  |  |  | bless { backup_path => $path }, $class; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | =head2 get_sms | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | get sms message | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | return data: [{ type => 'SMS', number => 'number', message => 'content', time => 'unixtime' }, ...] | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | usage: my $sms = $foo->get_sms() | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =cut | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | sub get_sms { | 
| 93 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 94 | 0 |  |  |  |  |  | my $messages; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | find( { wanted => sub { | 
| 97 | 0 | 0 |  | 0 |  |  | return unless file_is_sqlite($File::Find::name); | 
| 98 |  |  |  |  |  |  | ### $File::Find::name | 
| 99 | 0 | 0 |  |  |  |  | return unless find_message_table($File::Find::name); | 
| 100 | 0 |  |  |  |  |  | push @$messages, @{get_sms_message($File::Find::name)}; | 
|  | 0 |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | }, | 
| 102 | 0 |  |  |  |  |  | follow => 1, | 
| 103 |  |  |  |  |  |  | }, | 
| 104 |  |  |  |  |  |  | $self->{backup_path} | 
| 105 |  |  |  |  |  |  | ); | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 0 |  |  |  |  |  | my @msgs; | 
| 108 | 0 |  |  |  |  |  | for my $text ( @$messages ) { | 
| 109 | 0 |  |  |  |  |  | my ($guid, $content, $time) = @$text; | 
| 110 | 0 |  |  |  |  |  | my ($type, undef, $number) = split(/;/, $guid); | 
| 111 | 0 |  |  |  |  |  | push @msgs, { | 
| 112 |  |  |  |  |  |  | type => $type, | 
| 113 |  |  |  |  |  |  | time => $time, | 
| 114 |  |  |  |  |  |  | message => $content, | 
| 115 |  |  |  |  |  |  | number => $number, | 
| 116 |  |  |  |  |  |  | }; | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 0 |  |  |  |  |  | my @sms = grep { $_->{type} eq 'SMS' } @msgs; | 
|  | 0 |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 0 |  |  |  |  |  | return \@sms; | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | sub file_is_sqlite { | 
| 125 | 0 |  |  | 0 | 0 |  | my $file = shift; | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | # win seems can't open dir | 
| 128 |  |  |  |  |  |  | # and will give permission denied | 
| 129 | 0 | 0 | 0 |  |  |  | return 0 if -d $file or -z $file; | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 0 | 0 |  |  |  |  | sysopen my $fh, $file, O_RDONLY | 
| 132 |  |  |  |  |  |  | or die "can't open $file: $!"; | 
| 133 |  |  |  |  |  |  |  | 
| 134 | 0 |  |  |  |  |  | my $data; | 
| 135 | 0 |  |  |  |  |  | my $length = 20; | 
| 136 | 0 |  |  |  |  |  | sysread $fh, $data, 20; | 
| 137 | 0 |  |  |  |  |  | my $flag = 0; | 
| 138 | 0 | 0 |  |  |  |  | if ( $data =~ /sqlite/i ) { | 
| 139 | 0 |  |  |  |  |  | $flag = 1; | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  |  | 
| 142 | 0 |  |  |  |  |  | close $fh; | 
| 143 | 0 |  |  |  |  |  | return $flag; | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | sub get_sms_message { | 
| 147 | 0 |  |  | 0 | 0 |  | my $dbfile = shift; | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 0 |  |  |  |  |  | my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","",""); | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 0 |  |  |  |  |  | my $sql = qq{ select chat.guid, message.text, message.date + 978307200 as m_date \ | 
| 152 |  |  |  |  |  |  | from chat, message where chat.account_id=message.account_guid order by \ | 
| 153 |  |  |  |  |  |  | chat.guid, message.date | 
| 154 |  |  |  |  |  |  | }; | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | # apple sms time from 2001.01.01 | 
| 157 | 0 |  |  |  |  |  | my $data; | 
| 158 | 0 |  |  |  |  |  | eval { | 
| 159 | 0 |  |  |  |  |  | $data = $dbh->selectall_arrayref($sql); | 
| 160 |  |  |  |  |  |  | }; | 
| 161 |  |  |  |  |  |  |  | 
| 162 | 0 |  |  |  |  |  | $dbh->disconnect(); | 
| 163 | 0 |  |  |  |  |  | return $data; | 
| 164 |  |  |  |  |  |  | } | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | sub find_message_table { | 
| 167 | 0 |  |  | 0 | 0 |  | my $dbfile = shift; | 
| 168 |  |  |  |  |  |  |  | 
| 169 | 0 |  |  |  |  |  | my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","",""); | 
| 170 |  |  |  |  |  |  |  | 
| 171 | 0 |  |  |  |  |  | my $sql = qq{SELECT name FROM sqlite_master WHERE type='table' AND name='message';}; | 
| 172 |  |  |  |  |  |  |  | 
| 173 | 0 |  |  |  |  |  | my $data; | 
| 174 | 0 |  |  |  |  |  | eval { | 
| 175 | 0 |  |  |  |  |  | $data = $dbh->selectall_arrayref($sql); | 
| 176 |  |  |  |  |  |  | }; | 
| 177 |  |  |  |  |  |  |  | 
| 178 | 0 | 0 |  |  |  |  | if ( @$data ) { | 
| 179 | 0 |  |  |  |  |  | return 1; | 
| 180 |  |  |  |  |  |  | } | 
| 181 |  |  |  |  |  |  |  | 
| 182 | 0 |  |  |  |  |  | $dbh->disconnect(); | 
| 183 | 0 |  |  |  |  |  | return 0; | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | =head1 AUTHOR | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | Nianhua Wei, C<<  >> | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | =head1 BUGS | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | Please report any bugs or feature requests to C, or through | 
| 193 |  |  |  |  |  |  | the web interface at L.  I will be notified, and then you'll | 
| 194 |  |  |  |  |  |  | automatically be notified of progress on your bug as I make changes. | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | =head1 SUPPORT | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | perldoc Iphone::SMS | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | You can also look for information at: | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | =over 4 | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | =item * RT: CPAN's request tracker (report bugs here) | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | L | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | L | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | L | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | =item * Search CPAN | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | L | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | =back | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | =head1 ACKNOWLEDGEMENTS | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 233 |  |  |  |  |  |  |  | 
| 234 |  |  |  |  |  |  | Copyright 2013 Nianhua Wei. | 
| 235 |  |  |  |  |  |  |  | 
| 236 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 237 |  |  |  |  |  |  | under the terms of the the Artistic License (2.0). You may obtain a | 
| 238 |  |  |  |  |  |  | copy of the full license at: | 
| 239 |  |  |  |  |  |  |  | 
| 240 |  |  |  |  |  |  | L | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | Any use, modification, and distribution of the Standard or Modified | 
| 243 |  |  |  |  |  |  | Versions is governed by this Artistic License. By using, modifying or | 
| 244 |  |  |  |  |  |  | distributing the Package, you accept this license. Do not use, modify, | 
| 245 |  |  |  |  |  |  | or distribute the Package, if you do not accept this license. | 
| 246 |  |  |  |  |  |  |  | 
| 247 |  |  |  |  |  |  | If your Modified Version has been derived from a Modified Version made | 
| 248 |  |  |  |  |  |  | by someone other than you, you are nevertheless required to ensure that | 
| 249 |  |  |  |  |  |  | your Modified Version complies with the requirements of this license. | 
| 250 |  |  |  |  |  |  |  | 
| 251 |  |  |  |  |  |  | This license does not grant you the right to use any trademark, service | 
| 252 |  |  |  |  |  |  | mark, tradename, or logo of the Copyright Holder. | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | This license includes the non-exclusive, worldwide, free-of-charge | 
| 255 |  |  |  |  |  |  | patent license to make, have made, use, offer to sell, sell, import and | 
| 256 |  |  |  |  |  |  | otherwise transfer the Package with respect to any patent claims | 
| 257 |  |  |  |  |  |  | licensable by the Copyright Holder that are necessarily infringed by the | 
| 258 |  |  |  |  |  |  | Package. If you institute patent litigation (including a cross-claim or | 
| 259 |  |  |  |  |  |  | counterclaim) against any party alleging that the Package constitutes | 
| 260 |  |  |  |  |  |  | direct or contributory patent infringement, then this Artistic License | 
| 261 |  |  |  |  |  |  | to you shall terminate on the date that such litigation is filed. | 
| 262 |  |  |  |  |  |  |  | 
| 263 |  |  |  |  |  |  | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | 
| 264 |  |  |  |  |  |  | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | 
| 265 |  |  |  |  |  |  | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | 
| 266 |  |  |  |  |  |  | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | 
| 267 |  |  |  |  |  |  | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | 
| 268 |  |  |  |  |  |  | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | 
| 269 |  |  |  |  |  |  | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | 
| 270 |  |  |  |  |  |  | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| 271 |  |  |  |  |  |  |  | 
| 272 |  |  |  |  |  |  |  | 
| 273 |  |  |  |  |  |  | =cut | 
| 274 |  |  |  |  |  |  |  | 
| 275 |  |  |  |  |  |  | 1; # End of Iphone::SMS |