Skip to content

Instantly share code, notes, and snippets.

@kares
Created March 8, 2012 06:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kares/1999289 to your computer and use it in GitHub Desktop.
Save kares/1999289 to your computer and use it in GitHub Desktop.
Gammu to Android SMS converter. Assumes your backup has been exported as XML.
#!/usr/bin/evn ruby
# (Not just) Nokia 2 Android SMS converter script.
#
# This tool assumes you've backed up your SMS messages from your device using
# Gammu (Wammu) http://wammu.eu/wammu/ in XML format e.g.
#
# * open Wammu and connect your phone: Phone -> Connect
# * retrieve messages (or all) using: Retrieve -> Messages
# * backup as XML using: Backups -> Export messages to XML
#
# The output is writen in a format usable with "SMS Backup & Restore" app.
# https://play.google.com/store/apps/details?id=com.riteshsahu.SMSBackupRestore
#
# Requires a ruby interpreter and the nokogiri gem installed:
# `gem install nokogiri`
#
# Usage: gammusms2android.rb gammu-sms-nokia-export.xml > sms-nokia-import.xml
#
# Install "SMS Backup & Restore" app.
# Copy the created file xml-nokia-import.xml in your Android's /sdcard/SMSBackupRestore.
# Run "SMS Backup & Restore" app and select "Restore", locate the xml file.
require 'date'
require 'rubygems'
require 'nokogiri'
=begin
Sample input XML content :
xml = <<-start
<messages>
<message>
<date>25.02.2012 18:31:04</date>
<dateenc>20120225183104</dateenc>
<text>42 :-P</text>
<telephone>+420123456789</telephone>
<contact>Ferko Suska</contact>
<folder>6</folder>
<stat>Read</stat>
</message>
<message>
<date>25.02.2012 12:22:32</date>
<dateenc>20091128122232</dateenc>
<text>Hey, what's the answer to the ultimate question of life, the universe, and everything ?</text>
<telephone>+420987654321</telephone>
<contact>+420987654321</contact>
<folder>4</folder>
<stat>Sent</stat>
</message>
</messages>
start
=end
xml = File.read ARGV[0]
in_doc = Nokogiri::XML(xml) # <messages>
out_doc = Nokogiri::XML <<-XML
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<?xml-stylesheet type="text/xsl" href="sms.xsl"?>
<smses count="#{in_doc.root.element_children.size}">
</smses>
XML
out = {} # to correctly order the messages
in_doc.root.element_children.each do |message_node|
children = message_node.element_children
date_enc = children.find { |node| node.name == 'dateenc' }.text
text = children.find { |node| node.name == 'text' }.text
telephone = children.find { |node| node.name == 'telephone' }.text
contact = children.find { |node| node.name == 'contact' }.text
stat = children.find { |node| node.name == 'stat' }.text
contact = "(Unknown)" if contact.empty?
t = DateTime.parse(date_enc)
t_millis = t.strftime('%Q')
t_formatted = t.strftime "%b %-d, %Y %-l:%M:%S %p"
status, type = nil, nil
(status = '-1'; type = '1') if stat =~ /Read/i
(status = '0'; type = '2') if stat =~ /Sent/i
raise "unknow status: #{stat}" unless status
out[t] = "<sms protocol=\"0\" address=\"#{telephone}\" date=\"#{t_millis}\" " +
"type=\"#{type}\" subject=\"null\" body=\"#{text}\" toa=\"null\" sc_toa=\"null\" " +
"service_center=\"null\" read=\"1\" status=\"#{status}\" locked=\"0\" " +
"date_sent=\"null\" readable_date=\"#{t_formatted}\" contact_name=\"#{contact}\" />"
end
out.keys.sort.each { |key| out_doc.root << " #{out[key]}\n" }
puts out_doc.to_xml
@HolstenerLiesel
Copy link

Hi, I think this is a great idea! I'm getting an error though:

gammusms2android.rb:73: undefined method text' for nil:NilClass (NoMethodError) from /usr/lib/ruby/vendor_ruby/nokogiri/xml/node_set.rb:239:ineach'
from /usr/lib/ruby/vendor_ruby/nokogiri/xml/node_set.rb:238:in upto' from /usr/lib/ruby/vendor_ruby/nokogiri/xml/node_set.rb:238:ineach'
from ./gammusms2android.rb:68

Any hints?

@Garonenur
Copy link

Hi,

I had the same problem as @HolsteinerLisel, looking at the exported data, the 'sent' messages don't have a 'dateenc' field, so this won't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment