forked from basnijholt/iOSMessageExport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iOSMessages.pm
executable file
·130 lines (108 loc) · 3.44 KB
/
iOSMessages.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package iOSMessages;
use DBI;
use Data::Dumper;
use Digest::SHA1 qw(sha1 sha1_hex sha1_base64);
sub new
{
my ($class, $params) = @_;
my $self = {
_backup_directory => $params->{backup_directory},
_sms_db_filename => '3d/3d0d7e5fb2ce288813306e4d4636395e047a3d28',
_sms_db => undef,
_messages => {},
_attachments => {}
};
unless (-d $self->{_backup_directory}){
print "direcotry " . $self->{_backup_directory} . "\n";
die 'Directory does not exist';
}
bless $self, $class;
$self->_generate_messages_hash();
return $self;
}
sub get_messages{
my ($self) = @_;
return $self->{_messages};
}
sub get_attachments{
my ($self) = @_;
return $self->{_attachments};
}
# Internal methods
sub _db {
my ($self) = @_;
return $self->{_sms_db} if ($self->{_sms_db});
$self->{_sms_db} = DBI->connect(
"dbi:SQLite:dbname=".$self->{_backup_directory}.$self->{_sms_db_filename},
"",
"",
{ RaiseError => 1 },
) or die $DBI::errstr;
return $self->{_sms_db};
}
sub _generate_messages_hash {
my ($self) = @_;
my $dbh = $self->_db;
my $query = qq|
SELECT
m.rowid as RowID,
h.id AS UniqueID,
CASE is_from_me
WHEN 0 THEN "received"
WHEN 1 THEN "sent"
ELSE "Unknown"
END as Type,
CASE
WHEN date > 0 then TIME(date + 978307200, 'unixepoch', 'localtime')
ELSE NULL
END as Time,
CASE
WHEN date > 0 THEN strftime('%Y%m%d', date + 978307200, 'unixepoch', 'localtime')
ELSE NULL
END as Date,
CASE
WHEN date > 0 THEN date + 978307200
ELSE NULL
END as Epoch,
text as Text,
maj.attachment_id AS AttachmentID
FROM message m
LEFT JOIN handle h ON h.rowid = m.handle_id
LEFT JOIN message_attachment_join maj
ON maj.message_id = m.rowid
ORDER BY UniqueID, Date, Time|;
my $sth = $dbh->prepare($query);
$sth->execute();
my $tempMessages = {};
while (my $text = $sth->fetchrow_hashref){
if (my $uniqueID = $text->{'UniqueID'}) {
my $uniqueID = $text->{'UniqueID'};
if ($date = $text->{'Date'}) {
push @{$tempMessages->{$uniqueID}->{$date}}, $text;
}
if ($text->{'AttachmentID'}) {
$self->_process_mms($text->{'AttachmentID'});
}
}
}
$self->{_messages} = $tempMessages;
}
sub _process_mms {
my ($self, $attachment_id) = @_;
my $dbh = $self->{_sms_db};
my $query = qq|SELECT * FROM attachment WHERE ROWID = ?|;
my $sth = $dbh->prepare($query);
$sth->execute($attachment_id);
my $attachment = $sth->fetchrow_hashref();
my $filepath = $attachment->{filename};
(my $filename = $filepath) =~ s{(.*)/(.*)}{$2}xms;
$filepath =~ s#^~/#MediaDomain-#;
my $sha1_filename = sha1_hex($filepath);
my $sha1_filename = substr($sha1_filename, 0, 2) . '/' . $sha1_filename;
$self->{_attachments}->{$attachment_id} = {
sha1_filename => $sha1_filename,
filename => $filename,
mime_type => $attachment->{mime_type}
};
}
1;