Make plaintext export more compatible with SMS Backup and Restore

This commit adds the contact name and the readable date (and time) to
the plaintext export of every message. That's because that is how SMS
Backup and Restore does it, so this commit makes Signal more compatible.

Closes #6452
// FREEBIE
This commit is contained in:
Gavin Howard 2017-03-27 16:41:16 -06:00 committed by Moxie Marlinspike
parent 512c6dc581
commit 381237a315
2 changed files with 24 additions and 2 deletions

View File

@ -45,6 +45,7 @@ public class PlaintextBackupExporter {
while ((record = reader.getNext()) != null) { while ((record = reader.getNext()) != null) {
XmlBackup.XmlBackupItem item = XmlBackup.XmlBackupItem item =
new XmlBackup.XmlBackupItem(0, record.getIndividualRecipient().getNumber(), new XmlBackup.XmlBackupItem(0, record.getIndividualRecipient().getNumber(),
record.getIndividualRecipient().getName(),
record.getDateReceived(), record.getDateReceived(),
MmsSmsColumns.Types.translateToSystemBaseType(record.getType()), MmsSmsColumns.Types.translateToSystemBaseType(record.getType()),
null, record.getDisplayBody().toString(), null, null, record.getDisplayBody().toString(), null,

View File

@ -11,6 +11,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -18,7 +19,9 @@ public class XmlBackup {
private static final String PROTOCOL = "protocol"; private static final String PROTOCOL = "protocol";
private static final String ADDRESS = "address"; private static final String ADDRESS = "address";
private static final String CONTACT_NAME = "contact_name";
private static final String DATE = "date"; private static final String DATE = "date";
private static final String READABLE_DATE = "readable_date";
private static final String TYPE = "type"; private static final String TYPE = "type";
private static final String SUBJECT = "subject"; private static final String SUBJECT = "subject";
private static final String BODY = "body"; private static final String BODY = "body";
@ -29,6 +32,8 @@ public class XmlBackup {
private static final String SC_TOA = "sc_toa"; private static final String SC_TOA = "sc_toa";
private static final String LOCKED = "locked"; private static final String LOCKED = "locked";
private static final SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
private final XmlPullParser parser; private final XmlPullParser parser;
public XmlBackup(String path) throws XmlPullParserException, FileNotFoundException { public XmlBackup(String path) throws XmlPullParserException, FileNotFoundException {
@ -62,7 +67,9 @@ public class XmlBackup {
if (attributeName.equals(PROTOCOL )) item.protocol = Integer.parseInt(parser.getAttributeValue(i)); if (attributeName.equals(PROTOCOL )) item.protocol = Integer.parseInt(parser.getAttributeValue(i));
else if (attributeName.equals(ADDRESS )) item.address = parser.getAttributeValue(i); else if (attributeName.equals(ADDRESS )) item.address = parser.getAttributeValue(i);
else if (attributeName.equals(CONTACT_NAME )) item.contactName = parser.getAttributeValue(i);
else if (attributeName.equals(DATE )) item.date = Long.parseLong(parser.getAttributeValue(i)); else if (attributeName.equals(DATE )) item.date = Long.parseLong(parser.getAttributeValue(i));
else if (attributeName.equals(READABLE_DATE )) item.readableDate = parser.getAttributeValue(i);
else if (attributeName.equals(TYPE )) item.type = Integer.parseInt(parser.getAttributeValue(i)); else if (attributeName.equals(TYPE )) item.type = Integer.parseInt(parser.getAttributeValue(i));
else if (attributeName.equals(SUBJECT )) item.subject = parser.getAttributeValue(i); else if (attributeName.equals(SUBJECT )) item.subject = parser.getAttributeValue(i);
else if (attributeName.equals(BODY )) item.body = parser.getAttributeValue(i); else if (attributeName.equals(BODY )) item.body = parser.getAttributeValue(i);
@ -80,7 +87,9 @@ public class XmlBackup {
public static class XmlBackupItem { public static class XmlBackupItem {
private int protocol; private int protocol;
private String address; private String address;
private String contactName;
private long date; private long date;
private String readableDate;
private int type; private int type;
private String subject; private String subject;
private String body; private String body;
@ -90,12 +99,14 @@ public class XmlBackup {
public XmlBackupItem() {} public XmlBackupItem() {}
public XmlBackupItem(int protocol, String address, long date, int type, String subject, public XmlBackupItem(int protocol, String address, String contactName, long date, int type,
String body, String serviceCenter, int read, int status) String subject, String body, String serviceCenter, int read, int status)
{ {
this.protocol = protocol; this.protocol = protocol;
this.address = address; this.address = address;
this.contactName = contactName;
this.date = date; this.date = date;
this.readableDate = dateFormatter.format(date);
this.type = type; this.type = type;
this.subject = subject; this.subject = subject;
this.body = body; this.body = body;
@ -112,10 +123,18 @@ public class XmlBackup {
return address; return address;
} }
public String getContactName() {
return contactName;
}
public long getDate() { public long getDate() {
return date; return date;
} }
public String getReadableDate() {
return readableDate;
}
public int getType() { public int getType() {
return type; return type;
} }
@ -172,7 +191,9 @@ public class XmlBackup {
stringBuilder.append(OPEN_TAG_SMS); stringBuilder.append(OPEN_TAG_SMS);
appendAttribute(stringBuilder, PROTOCOL, item.getProtocol()); appendAttribute(stringBuilder, PROTOCOL, item.getProtocol());
appendAttribute(stringBuilder, ADDRESS, escapeXML(item.getAddress())); appendAttribute(stringBuilder, ADDRESS, escapeXML(item.getAddress()));
appendAttribute(stringBuilder, CONTACT_NAME, item.getContactName());
appendAttribute(stringBuilder, DATE, item.getDate()); appendAttribute(stringBuilder, DATE, item.getDate());
appendAttribute(stringBuilder, READABLE_DATE, item.getReadableDate());
appendAttribute(stringBuilder, TYPE, item.getType()); appendAttribute(stringBuilder, TYPE, item.getType());
appendAttribute(stringBuilder, SUBJECT, escapeXML(item.getSubject())); appendAttribute(stringBuilder, SUBJECT, escapeXML(item.getSubject()));
appendAttribute(stringBuilder, BODY, escapeXML(item.getBody())); appendAttribute(stringBuilder, BODY, escapeXML(item.getBody()));