]> git.somenet.org - pub/jan/scripts.git/blob - gammu2android-sms.pl
Script to convert gammu sms backups to "sms backup and restore" format.
[pub/jan/scripts.git] / gammu2android-sms.pl
1 #!/usr/bin/perl
2
3 # Copyright (C) 2009 Thilo-Alexander Ginkel <thilo@ginkel.com>
4 # Copyright 2015 by Jan Vales <jan@jvales.net> (Someone <someone@somenet.org>)
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 use Date::Parse;
20 use HTML::Entities;
21
22 print <<HEADER;
23 <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
24 <smses>
25 HEADER
26
27 while (<>) {
28         chomp;
29         s/^(.*)\r$/$1/;
30
31         if (/^DateTime = (.*)/) {
32                 $time = str2time($1);
33         } elsif (/^Number = "(.*)"/) {
34                 $address = $1;
35         } elsif (/^UDH = 050003(.*)(\p{XDigit}{2})/) {
36                 $msgid = $1;
37                 $seqno = $2;
38         } elsif (/^State = (.*)/) {
39                 if ($1 eq "Sent") {
40                         $type = 2;
41                 } elsif ($1 eq "Read") {
42                         $type = 1;
43                 }
44         } elsif (/^Text\d+ = (.*)/) {
45                 my $encoded = $1;
46                 $encoded =~ s/([0-9A-F]{4})/&utf8(hex($1))/gei;
47                 $text .= $encoded;
48                 # print $text, "\n";
49         } elsif (/^$/ and defined($address)) {
50                 unless ($msgid) {
51                         print_message($address, $time, $type, $text);
52                 } else {
53                         my $msg = {};
54                         my $parts = {};
55
56                         $id = $address . $msgid;
57
58                         if (defined($multipart{$id})) {
59                                 $msg = $multipart{$id};
60                         } else {
61                                 $msg->{'date'} = $time;
62                                 $msg->{'address'} = $address;
63                                 $msg->{'type'} = $type;
64                         }
65
66                         if (defined($msg->{'parts'})) {
67                                 $parts = $msg->{'parts'};
68                         }
69
70                         $parts->{$seqno} = $text;
71
72                         $msg->{'parts'} = $parts;
73                         $multipart{$id} = $msg;
74                 }
75
76                 $time ++;
77                 $address = undef;
78                 $msgid = undef;
79                 $seqno = undef;
80                 $type = undef;
81                 $text = undef;
82         }
83 }
84
85 # output any multipart messages
86 foreach $msg (values %multipart) {
87         my $body;
88         my $parts = $msg->{'parts'};
89         foreach $seqno (sort keys %$parts) {
90                 $body .= $parts->{$seqno};
91         }
92         print_message($msg->{'address'}, $msg->{'date'}, $msg->{'type'}, $body);
93 }
94
95 print "</smses>\n";
96
97 sub utf8 { local($_)=@_;
98     return $_ < 0x80 ? chr($_) : 
99         $_ < 0x800 ? chr($_>>6&0x3F|0xC0) . chr($_&0x3F|0x80) :
100             chr($_>>12&0x0F|0xE0).chr($_>>6&0x3F|0x80).chr($_&0x3F|0x80);
101 }
102
103 sub print_message {
104         my ($address, $date, $type, $text) = @_;
105         $text = encode_entities($text, "<>&'\"");
106     $text =~s/\n/&#10;/g;
107     $text =~s/\r//g;
108         print "<sms protocol=\"0\" address=\"" . encode_entities($address, "<>&'\"") . "\" date=\"${date}000\" type=\"$type\" subject=\"null\" body=\"".$text."\" toa=\"null\" sc_toa=\"null\" service_center=\"\" read=\"1\" status=\"-1\" />\n";
109