Skip to content

Instantly share code, notes, and snippets.

@ujifgc
Last active February 22, 2024 13:14
Show Gist options
  • Save ujifgc/5033937 to your computer and use it in GitHub Desktop.
Save ujifgc/5033937 to your computer and use it in GitHub Desktop.
This tiny script converts SMS text message backup from VMG to XML. Formats are tested on transferring Kies bada backup to Android SMS Backup & Restore.
require 'cgi'
class String
def extract( token )
self.scan( /#{token}:(.*)/ ).flatten.first
end
end
puts "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n<smses>\n"
data = File.binread 'kiesTotalSMS.vmg'
vmsgs = data.scan(/BEGIN:VMSG\n(.*?)END:VMSG\n/m).flatten
vmsgs.each do |vmsg|
sms = {}
begin
sms[:address] = vmsg.extract( 'TEL.*' )
next if sms[:address].nil?
sms[:body] = vmsg.extract( 'TEXT;ENCODING=QUOTED-PRINTABLE' )
next if sms[:body].nil?
sms[:body] = CGI.escapeHTML(sms[:body].unpack("M")[0].force_encoding('utf-8').gsub(/[\r\n\s]+/, ' ').strip)
sms[:date] = Time.new *vmsg.extract( 'Date' ).chomp('Z').split('.').map(&:to_i), '+00:00'
sms[:date] = sms[:date].localtime.to_i.to_s
sms[:type] = { 'SENTBOX' => '2', 'INBOX' => '1' }[vmsg.extract( 'X-IRMC-BOX' )]
puts "<sms protocol=\"0\" address=\"#{sms[:address]}\" date=\"#{sms[:date]}000\" type=\"#{sms[:type]}\" subject=\"null\" body=\"#{sms[:body]}\" toa=\"null\" sc_toa=\"null\" service_center=\"\" read=\"1\" status=\"-1\" />\n";
end
end
puts '</smses>\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment