package org.meshbay.client.phonesync import java.io.Writer import java.text.SimpleDateFormat import java.util.Date import java.util.Locale import java.util.TimeZone /** One text message, as the platform's SMS provider stores it. */ data class Sms( val id: Long, val address: String, /** Milliseconds, received or sent. */ val date: Long, val dateSent: Long, /** 1 received, 2 sent, 3 draft, 4 outbox, 5 failed, 6 queued. */ val type: Int, val body: String, val read: Int, val status: Int, val locked: Int, val protocol: Int, val subject: String?, val serviceCenter: String?, val contactName: String?, ) /** * Messages written as the `` XML that SMS backup applications read and * restore, so a copy on the node goes back onto a phone with tools that exist. * Pure, so the JVM tests hold the format and its escaping. */ object SmsXml { fun write(messages: List, out: Writer, zone: TimeZone) { val readable = SimpleDateFormat("d MMM yyyy HH:mm:ss", Locale.ROOT).apply { timeZone = zone } out.write("\n") out.write("\n") for (m in messages) { out.write(" \n") } out.write("\n") } /** * A quoted attribute value. Line breaks are kept as character references, * which an attribute would otherwise fold into spaces; characters XML 1.0 * cannot carry at all, even escaped, are left out. */ fun q(text: String): String { val b = StringBuilder(text.length + 2).append('"') for (c in text) when { c == '&' -> b.append("&") c == '<' -> b.append("<") c == '>' -> b.append(">") c == '"' -> b.append(""") c == '\n' -> b.append(" ") c == '\r' -> b.append(" ") c == '\t' -> b.append(" ") c < ' ' || c == '￾' || c == '￿' -> {} else -> b.append(c) } return b.append('"').toString() } }