Notices


Reply
Thread Tools
Posts: 1 | Thanked: 0 times | Joined on Feb 2010
#1
Hi there,

So I made a conventionscript to convert SMS that i exported from Windows mobile via Jeyo mobile companion and to be able to import them with rmoravciks smstools

The script depends on the Perl-module XML::Twig (libxml-twig-perl in Debian)

So the way to do this is something like:
  1. Install XML::Twig on a computer (win32 or linux)
  2. Save the code underneith as a script on that computer
  3. Backup SMS-folders (Inbox, Sent) via Jeyo
  4. Run the script with the xml-filenames as arguments and redirect output to a new file: perl script.pl inbox.xml >> inbox.csv
  5. Follow instructions on smstools and install on your N900
  6. Backup your SMS on your N900
  7. Copy the csv-file to your N900-device and run it according to smstools instructions

I hope that this helps someone!


Code:
#!/usr/bin/perl

use XML::Twig;
my $fn = $ARGV[0];

my $twig = XML::Twig->new(twig_handlers => { item => \&itemhandler });
$twig->parsefile("$fn");

sub itemhandler {
    my $t = shift;
    my $item = shift;

    my $sender = $item->first_child_text('sender') || "";
    my $receiver = $item->first_child_text('receiver') || "";

    my $date = $item->first_child_text('delivertime');
    my $text = $item->first_child_text('subject');

    # if sender is set -> there was a sender -> it was sent to us
    my $type = $sender ? "deliver" : "submit";

    if($sender =~ /(\+\d+)/) {
        $sender = $1;
    }

    if($receiver =~ /\<(\+?\d+)/) {
        $receiver = $1;
    }

    $date =~ s/\-/\./g;
    $date =~ s/\:\d\d$//;

    unless((!$sender && !$receiver) || ($text =~ /\n/)) {
        print qq!sms|$type|! . ($type eq "deliver" ? qq!"$sender"|""! : qq!""|"$receiver"!) . qq!|""|"$date"|""|"$text"!;
        print "\n";
   }
}
 
Posts: 1 | Thanked: 0 times | Joined on Feb 2010
#2
Nice use of XML::Twig.

Since I am a born nitpicker, a few minor comments on the code:

Code:
 $twig->parsefile("$fn");
no need to quote the variable here, $twig->parsefile($fn); is fine (I told you I liked nitpicking!)

Code:
my $sender = $item->first_child_text('sender') || "";
I usually write this as my $sender = $item->field('sender') || "";. It's really a question of personal preference, but I like the shorter version, and I don't know if you know about it.

Code:
unless((!$sender && !$receiver) || ($text =~ /\n/))
This one is actually the one that prompted me to comment on your code. Complex unless expressions are often hard to understand, and quickly become a maintenance problem. I believe this is equivalent to if( ($sender || $receiver) && $text !~ /\n/), but you may want to double check it.

I hope that helps

Thanks

--
mirod
 
Posts: 5 | Thanked: 0 times | Joined on Nov 2009
#3
Originally Posted by fluffis View Post
...
I hope that this helps someone!
...
Absolutly, this sounds awsome. I will test this today when Im back home.
 
Posts: 5 | Thanked: 0 times | Joined on Nov 2009
#4
fluffis, thanks for the script, I had no problems to create the csv-file. I have also tried to import one message to the N900, it worked but the message appears first in the message-log even that it is older than the other ones, but I guess that has to do with limitations in "smsimporter" or the N900.
 
Reply

Thread Tools

 
Forum Jump


All times are GMT. The time now is 15:06.