In the past I've been using Gammu /
Wammu to connect my mobile to my computer. One of
the features I liked most, was the sms-export. It allows you to export
into the native-gammu-format, into an .mbox file or directly upload it
to an IMAP server. I kept exporting them as .mbox so I could reimporting
it in Thunderbird using ImportExportTools (MboxImport
enhanced).
Recently I've switched to my precious 'Motorola Milestone', which runs
Android. So time to lookout for a new solution, since
Gammu / Wammu don't
support Android.
So after a while, I stumbled over SMS Backup &
Restore, which
exports it to an XML. So now we have all the text-messages in an XML,
but what can we do with it..? Well, I always liked XML, because it was
human-readable and there are enough converters available for every kind
of XML, but not this time... So since it seems to be an easy task, let's
parse the xml on our own and convert it to something else! :-D
Forget it.. I was hoping for some nice cli-utility to help me out, but
wasn't able to find something to fullfill my needs. So after hours and
hours of googling without succes, I just stumbled over this 'Perl XML
Quickstart
Tutorial'.
After I quickly read through it and got some working code, I've put some
more Perl-code together (see below), which parses the XML and spits an
.mbox file out on the other side.
I'm not really fluent in Perl, so I'm open for criticism. ;-)
Code
[cc lang="perl" lines="-1"]
#!/usr/bin/perl
#########################################################
# Author: Raphael Hoegger
# Source: http://pfuender.net/?p=175
# License: This file is licensed under the GPL v2.
# Latest change: 2010.07.13 23:03:14 CEST
# Version: 1.00
#########################################################
## Import our libraries
use XML::Twig;
use Time::CTime;
## Variables
my \$file = 'sms.xml';
my \$OWNER_NUMBER = '+41XXXXXXXXX';
my \$OWNER_NAME = 'Your Name';
my \$twig = XML::Twig->new();
\$twig->parsefile(\$file);
my \$root = \$twig->root;
## Loop it for all the sms'es
foreach my \$sms (\$root->children('sms')){
my \$FROM = \$sms->att('address');
my \$FROM_NAME = \$sms->att('contact_name');
my \$RDATE = \$sms->att('readable_date');
my \$EPOCH = \$sms->att('date');
my \$DATE = localtime(\$EPOCH/1000); ## java-epoch = perl-epoch/1000
-- Java calculates the difference between January 1, 1970 and now in ms,
whereas perl uses seconds, so factor 1000
my \$TYPE = \$sms->att('type'); ## 1=received 2=send
my \$BODY = \$sms->att('body');
my \$SUBJECT = substr(\$BODY, 0, 50); ## Shorten the subject to 50
characters
## Header for received sms
if (\$TYPE == 1) {
print "From \$FROM \$DATE\n";
print "To: \$OWNER_NAME <\$OWNER_NUMBER>\n";
print "From: \$FROM_NAME <\$FROM>\n";
}
## Header for sent sms
if (\$TYPE == 2) {
print "From \$OWNER_NUMBER \$DATE\n";
print "To: \$FROM_NAME <\$FROM>\n";
print "From: \$OWNER_NAME <\$OWNER_NUMBER>\n";
}
print "Subject: \$SUBJECT\n";
print "Date: \$DATE +0200\n";
print "\n\$BODY\n\n\n";
}
[/cc]
I think the code should be selfexplaining, so no comments except the ones inline.
Usage
[cc]
|| user@workstation \~ ||\$ tree
.
├── smsbakxml2mbox.perl
└── sms.xml
|| user@workstation \~ ||\$ ./smsbakxml2mbox.perl > out.mbox
|| user@workstation \~ ||\$ head out.mbox
From +41XXXXXXXXX Thu Apr 01 08:00:00 2010
To: Your Friend <+41XXXXXXXXX>
From: Your Name <+41XXXXXXXXX>
Subject: test-message
Date: Thu Apr 01 08:00:00 2010 +0200
test-message
|| user@workstation \~ ||\$
[/cc]
Thanks for reading! Questions/Suggestions in the comments :-D
Cheers,
Raphi