2018-04-04 01:14:31 +00:00
|
|
|
/* global Signal: false */
|
2018-03-09 00:05:03 +00:00
|
|
|
/* global Whisper: false */
|
|
|
|
/* global _: false */
|
|
|
|
/* global textsecure: false */
|
|
|
|
/* global i18n: false */
|
|
|
|
|
|
|
|
/* eslint-env browser */
|
|
|
|
/* eslint-env node */
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
/* eslint-disable no-param-reassign, guard-for-in */
|
2018-03-09 00:05:03 +00:00
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
2018-07-27 01:13:56 +00:00
|
|
|
const { map, fromPairs } = require('lodash');
|
2018-12-13 21:41:42 +00:00
|
|
|
const tar = require('tar');
|
2018-03-13 01:14:24 +00:00
|
|
|
const tmp = require('tmp');
|
|
|
|
const pify = require('pify');
|
|
|
|
const rimraf = require('rimraf');
|
2018-03-09 00:05:03 +00:00
|
|
|
const electronRemote = require('electron').remote;
|
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
const crypto = require('./crypto');
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const { dialog, BrowserWindow } = electronRemote;
|
2018-03-09 00:05:03 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getDirectoryForExport,
|
2018-04-04 01:11:06 +00:00
|
|
|
exportToDirectory,
|
2018-03-09 00:05:03 +00:00
|
|
|
getDirectoryForImport,
|
|
|
|
importFromDirectory,
|
|
|
|
// for testing
|
2018-03-19 19:42:12 +00:00
|
|
|
_sanitizeFileName,
|
|
|
|
_trimFileName,
|
|
|
|
_getExportAttachmentFileName,
|
|
|
|
_getAnonymousAttachmentFileName,
|
|
|
|
_getConversationDirName,
|
|
|
|
_getConversationLoggingName,
|
2018-03-09 00:05:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function stringify(object) {
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
for (const key in object) {
|
|
|
|
const val = object[key];
|
|
|
|
if (val instanceof ArrayBuffer) {
|
|
|
|
object[key] = {
|
|
|
|
type: 'ArrayBuffer',
|
|
|
|
encoding: 'base64',
|
2018-10-18 01:01:21 +00:00
|
|
|
data: crypto.arrayBufferToBase64(val),
|
2018-03-09 00:05:03 +00:00
|
|
|
};
|
|
|
|
} else if (val instanceof Object) {
|
|
|
|
object[key] = stringify(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
function unstringify(object) {
|
|
|
|
if (!(object instanceof Object)) {
|
|
|
|
throw new Error('unstringify expects an object');
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
for (const key in object) {
|
|
|
|
const val = object[key];
|
2018-04-27 21:25:04 +00:00
|
|
|
if (
|
|
|
|
val &&
|
|
|
|
val.type === 'ArrayBuffer' &&
|
|
|
|
val.encoding === 'base64' &&
|
|
|
|
typeof val.data === 'string'
|
|
|
|
) {
|
2018-10-18 01:01:21 +00:00
|
|
|
object[key] = crypto.base64ToArrayBuffer(val.data);
|
2018-03-09 00:05:03 +00:00
|
|
|
} else if (val instanceof Object) {
|
|
|
|
object[key] = unstringify(object[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createOutputStream(writer) {
|
|
|
|
let wait = Promise.resolve();
|
|
|
|
return {
|
|
|
|
write(string) {
|
|
|
|
// eslint-disable-next-line more/no-then
|
2018-04-27 21:25:04 +00:00
|
|
|
wait = wait.then(
|
|
|
|
() =>
|
|
|
|
new Promise(resolve => {
|
|
|
|
if (writer.write(string)) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If write() returns true, we don't need to wait for the drain event
|
|
|
|
// https://nodejs.org/dist/latest-v7.x/docs/api/stream.html#stream_class_stream_writable
|
|
|
|
writer.once('drain', resolve);
|
|
|
|
|
|
|
|
// We don't register for the 'error' event here, only in close(). Otherwise,
|
|
|
|
// we'll get "Possible EventEmitter memory leak detected" warnings.
|
|
|
|
})
|
|
|
|
);
|
2018-03-09 00:05:03 +00:00
|
|
|
return wait;
|
|
|
|
},
|
|
|
|
async close() {
|
|
|
|
await wait;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
writer.once('finish', resolve);
|
|
|
|
writer.once('error', reject);
|
|
|
|
writer.end();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-11 23:59:21 +00:00
|
|
|
async function exportConversationListToFile(parent) {
|
2018-03-09 00:05:03 +00:00
|
|
|
const writer = await createFileAndWriter(parent, 'db.json');
|
2019-02-11 23:59:21 +00:00
|
|
|
return exportConversationList(writer);
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
function writeArray(stream, array) {
|
|
|
|
stream.write('[');
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
for (let i = 0, max = array.length; i < max; i += 1) {
|
|
|
|
if (i > 0) {
|
|
|
|
stream.write(',');
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const item = array[i];
|
|
|
|
|
|
|
|
// We don't back up avatars; we'll get them in a future contact sync or profile fetch
|
|
|
|
const cleaned = _.omit(item, ['avatar', 'profileAvatar']);
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
stream.write(JSON.stringify(stringify(cleaned)));
|
|
|
|
}
|
2018-03-12 21:06:31 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
stream.write(']');
|
|
|
|
}
|
2018-03-12 21:06:31 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
function getPlainJS(collection) {
|
|
|
|
return collection.map(model => model.attributes);
|
|
|
|
}
|
|
|
|
|
2019-02-11 23:59:21 +00:00
|
|
|
async function exportConversationList(fileWriter) {
|
2018-12-13 21:41:42 +00:00
|
|
|
const stream = createOutputStream(fileWriter);
|
|
|
|
|
|
|
|
stream.write('{');
|
|
|
|
|
|
|
|
stream.write('"conversations": ');
|
|
|
|
const conversations = await window.Signal.Data.getAllConversations({
|
|
|
|
ConversationCollection: Whisper.ConversationCollection,
|
2018-03-09 00:05:03 +00:00
|
|
|
});
|
2018-12-13 21:41:42 +00:00
|
|
|
window.log.info(`Exporting ${conversations.length} conversations`);
|
|
|
|
writeArray(stream, getPlainJS(conversations));
|
|
|
|
|
|
|
|
stream.write('}');
|
|
|
|
await stream.close();
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
async function importNonMessages(parent, options) {
|
2018-03-09 00:05:03 +00:00
|
|
|
const file = 'db.json';
|
|
|
|
const string = await readFileAsText(parent, file);
|
2018-10-18 01:01:21 +00:00
|
|
|
return importFromJsonString(string, path.join(parent, file), options);
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function eliminateClientConfigInBackup(data, targetPath) {
|
2019-02-11 23:59:21 +00:00
|
|
|
const cleaned = _.pick(data, 'conversations');
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.info('Writing configuration-free backup file back to disk');
|
2018-03-09 00:05:03 +00:00
|
|
|
try {
|
|
|
|
fs.writeFileSync(targetPath, JSON.stringify(cleaned));
|
|
|
|
} catch (error) {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.error('Error writing cleaned-up backup to disk: ', error.stack);
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 01:47:19 +00:00
|
|
|
async function importConversationsFromJSON(conversations, options) {
|
|
|
|
const { writeNewAttachmentData } = window.Signal.Migrations;
|
|
|
|
const { conversationLookup } = options;
|
|
|
|
|
|
|
|
let count = 0;
|
|
|
|
let skipCount = 0;
|
|
|
|
|
|
|
|
for (let i = 0, max = conversations.length; i < max; i += 1) {
|
|
|
|
const toAdd = unstringify(conversations[i]);
|
|
|
|
const haveConversationAlready =
|
|
|
|
conversationLookup[getConversationKey(toAdd)];
|
|
|
|
|
|
|
|
if (haveConversationAlready) {
|
|
|
|
skipCount += 1;
|
|
|
|
count += 1;
|
|
|
|
// eslint-disable-next-line no-continue
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
const migrated = await window.Signal.Types.Conversation.migrateConversation(
|
|
|
|
toAdd,
|
|
|
|
{
|
|
|
|
writeNewAttachmentData,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await window.Signal.Data.saveConversation(migrated, {
|
|
|
|
Conversation: Whisper.Conversation,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
window.log.info(
|
|
|
|
'Done importing conversations:',
|
|
|
|
'Total count:',
|
|
|
|
count,
|
|
|
|
'Skipped:',
|
|
|
|
skipCount
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
async function importFromJsonString(jsonString, targetPath, options) {
|
2018-03-09 00:05:03 +00:00
|
|
|
options = options || {};
|
|
|
|
_.defaults(options, {
|
|
|
|
forceLightImport: false,
|
|
|
|
conversationLookup: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = {
|
|
|
|
fullImport: true,
|
|
|
|
};
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const importObject = JSON.parse(jsonString);
|
|
|
|
delete importObject.debug;
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
if (!importObject.sessions || options.forceLightImport) {
|
|
|
|
result.fullImport = false;
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
delete importObject.items;
|
|
|
|
delete importObject.signedPreKeys;
|
|
|
|
delete importObject.preKeys;
|
|
|
|
delete importObject.identityKeys;
|
|
|
|
delete importObject.sessions;
|
2018-09-21 01:47:19 +00:00
|
|
|
delete importObject.unprocessed;
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
window.log.info(
|
|
|
|
'This is a light import; contacts, groups and messages only'
|
|
|
|
);
|
|
|
|
}
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// We mutate the on-disk backup to prevent the user from importing client
|
|
|
|
// configuration more than once - that causes lots of encryption errors.
|
2019-02-11 23:59:21 +00:00
|
|
|
// This of course preserves the true data: conversations.
|
2018-10-18 01:01:21 +00:00
|
|
|
eliminateClientConfigInBackup(importObject, targetPath);
|
2018-10-31 16:44:18 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const storeNames = _.keys(importObject);
|
|
|
|
window.log.info('Importing to these stores:', storeNames.join(', '));
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
// Special-case conversations key here, going to SQLCipher
|
|
|
|
const { conversations } = importObject;
|
|
|
|
const remainingStoreNames = _.without(
|
|
|
|
storeNames,
|
|
|
|
'conversations',
|
2019-02-11 23:59:21 +00:00
|
|
|
'unprocessed',
|
|
|
|
'groups' // in old data sets, but no longer included in database schema
|
2018-10-18 01:01:21 +00:00
|
|
|
);
|
|
|
|
await importConversationsFromJSON(conversations, options);
|
|
|
|
|
|
|
|
const SAVE_FUNCTIONS = {
|
|
|
|
identityKeys: window.Signal.Data.createOrUpdateIdentityKey,
|
|
|
|
items: window.Signal.Data.createOrUpdateItem,
|
|
|
|
preKeys: window.Signal.Data.createOrUpdatePreKey,
|
|
|
|
sessions: window.Signal.Data.createOrUpdateSession,
|
|
|
|
signedPreKeys: window.Signal.Data.createOrUpdateSignedPreKey,
|
|
|
|
};
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
await Promise.all(
|
|
|
|
_.map(remainingStoreNames, async storeName => {
|
|
|
|
const save = SAVE_FUNCTIONS[storeName];
|
|
|
|
if (!_.isFunction(save)) {
|
|
|
|
throw new Error(
|
|
|
|
`importFromJsonString: Didn't have save function for store ${storeName}`
|
2018-03-09 00:05:03 +00:00
|
|
|
);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.log.info(`Importing items for store ${storeName}`);
|
|
|
|
const toImport = importObject[storeName];
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
if (!toImport || !toImport.length) {
|
|
|
|
window.log.info(`No items in ${storeName} store`);
|
2018-10-31 16:44:18 +00:00
|
|
|
return;
|
2018-10-26 17:52:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
for (let i = 0, max = toImport.length; i < max; i += 1) {
|
|
|
|
const toAdd = unstringify(toImport[i]);
|
2019-02-11 23:59:21 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await save(toAdd);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
window.log.info(
|
|
|
|
'Done importing to store',
|
|
|
|
storeName,
|
|
|
|
'Total count:',
|
2019-02-11 23:59:21 +00:00
|
|
|
toImport.length
|
2018-10-18 01:01:21 +00:00
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
window.log.info('DB import complete');
|
|
|
|
return result;
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createDirectory(parent, name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-03-19 19:42:12 +00:00
|
|
|
const sanitized = _sanitizeFileName(name);
|
2018-03-09 00:05:03 +00:00
|
|
|
const targetDir = path.join(parent, sanitized);
|
2018-03-13 01:14:24 +00:00
|
|
|
if (fs.existsSync(targetDir)) {
|
|
|
|
resolve(targetDir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
fs.mkdir(targetDir, error => {
|
2018-03-09 00:05:03 +00:00
|
|
|
if (error) {
|
2018-03-13 01:14:24 +00:00
|
|
|
reject(error);
|
|
|
|
return;
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 01:14:24 +00:00
|
|
|
resolve(targetDir);
|
2018-03-09 00:05:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createFileAndWriter(parent, name) {
|
2018-04-27 21:25:04 +00:00
|
|
|
return new Promise(resolve => {
|
2018-03-19 19:42:12 +00:00
|
|
|
const sanitized = _sanitizeFileName(name);
|
2018-03-09 00:05:03 +00:00
|
|
|
const targetPath = path.join(parent, sanitized);
|
|
|
|
const options = {
|
|
|
|
flags: 'wx',
|
|
|
|
};
|
|
|
|
return resolve(fs.createWriteStream(targetPath, options));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function readFileAsText(parent, name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const targetPath = path.join(parent, name);
|
|
|
|
fs.readFile(targetPath, 'utf8', (error, string) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(string);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
// Buffer instances are also Uint8Array instances, but they might be a view
|
|
|
|
// https://nodejs.org/docs/latest/api/buffer.html#buffer_buffers_and_typedarray
|
|
|
|
const toArrayBuffer = nodeBuffer =>
|
|
|
|
nodeBuffer.buffer.slice(
|
|
|
|
nodeBuffer.byteOffset,
|
|
|
|
nodeBuffer.byteOffset + nodeBuffer.byteLength
|
|
|
|
);
|
|
|
|
|
2018-03-13 01:14:24 +00:00
|
|
|
function readFileAsArrayBuffer(targetPath) {
|
2018-03-09 00:05:03 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// omitting the encoding to get a buffer back
|
|
|
|
fs.readFile(targetPath, (error, buffer) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
return resolve(toArrayBuffer(buffer));
|
2018-03-09 00:05:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
function _trimFileName(filename) {
|
2018-03-09 00:05:03 +00:00
|
|
|
const components = filename.split('.');
|
|
|
|
if (components.length <= 1) {
|
|
|
|
return filename.slice(0, 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
const extension = components[components.length - 1];
|
|
|
|
const name = components.slice(0, components.length - 1);
|
|
|
|
if (extension.length > 5) {
|
|
|
|
return filename.slice(0, 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${name.join('.').slice(0, 24)}.${extension}`;
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
function _getExportAttachmentFileName(message, index, attachment) {
|
2018-03-09 00:05:03 +00:00
|
|
|
if (attachment.fileName) {
|
2018-03-19 19:42:12 +00:00
|
|
|
return _trimFileName(attachment.fileName);
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let name = attachment.id;
|
|
|
|
|
|
|
|
if (attachment.contentType) {
|
|
|
|
const components = attachment.contentType.split('/');
|
2018-04-27 21:25:04 +00:00
|
|
|
name += `.${
|
|
|
|
components.length > 1 ? components[1] : attachment.contentType
|
|
|
|
}`;
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
function _getAnonymousAttachmentFileName(message, index) {
|
2018-03-13 01:14:24 +00:00
|
|
|
if (!index) {
|
|
|
|
return message.id;
|
|
|
|
}
|
|
|
|
return `${message.id}-${index}`;
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
async function readEncryptedAttachment(dir, attachment, name, options) {
|
2018-03-19 19:42:12 +00:00
|
|
|
options = options || {};
|
2018-04-04 02:47:23 +00:00
|
|
|
const { key } = options;
|
2018-03-19 19:42:12 +00:00
|
|
|
|
2018-04-19 22:17:01 +00:00
|
|
|
const sanitizedName = _sanitizeFileName(name);
|
|
|
|
const targetPath = path.join(dir, sanitizedName);
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-03-13 01:14:24 +00:00
|
|
|
if (!fs.existsSync(targetPath)) {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.warn(`Warning: attachment ${sanitizedName} not found`);
|
2018-03-13 01:14:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
const data = await readFileAsArrayBuffer(targetPath);
|
|
|
|
|
2018-04-04 02:47:23 +00:00
|
|
|
const isEncrypted = !_.isUndefined(key);
|
2018-04-20 21:55:33 +00:00
|
|
|
|
2018-04-04 02:47:23 +00:00
|
|
|
if (isEncrypted) {
|
2018-12-13 21:41:42 +00:00
|
|
|
attachment.data = await crypto.decryptAttachment(
|
|
|
|
key,
|
|
|
|
attachment.path,
|
|
|
|
data
|
|
|
|
);
|
2018-03-19 19:42:12 +00:00
|
|
|
} else {
|
|
|
|
attachment.data = data;
|
|
|
|
}
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
async function writeQuoteThumbnail(attachment, options) {
|
|
|
|
if (!attachment || !attachment.thumbnail || !attachment.thumbnail.path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const { dir, message, index, key, newKey } = options;
|
|
|
|
const filename = `${_getAnonymousAttachmentFileName(
|
2018-03-19 19:42:12 +00:00
|
|
|
message,
|
2018-04-27 21:25:04 +00:00
|
|
|
index
|
2018-12-13 21:41:42 +00:00
|
|
|
)}-quote-thumbnail`;
|
2018-03-13 01:14:24 +00:00
|
|
|
const target = path.join(dir, filename);
|
2018-04-20 21:55:33 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
await writeEncryptedAttachment(target, attachment.thumbnail.path, {
|
2018-04-20 21:55:33 +00:00
|
|
|
key,
|
|
|
|
newKey,
|
|
|
|
filename,
|
|
|
|
dir,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
async function writeQuoteThumbnails(quotedAttachments, options) {
|
2018-04-20 21:55:33 +00:00
|
|
|
const { name } = options;
|
|
|
|
|
|
|
|
try {
|
2018-04-27 21:25:04 +00:00
|
|
|
await Promise.all(
|
2018-12-13 21:41:42 +00:00
|
|
|
_.map(quotedAttachments, (attachment, index) =>
|
|
|
|
writeQuoteThumbnail(
|
2018-04-27 21:25:04 +00:00
|
|
|
attachment,
|
|
|
|
Object.assign({}, options, {
|
|
|
|
index,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2018-04-20 21:55:33 +00:00
|
|
|
} catch (error) {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.error(
|
2018-04-20 21:55:33 +00:00
|
|
|
'writeThumbnails: error exporting conversation',
|
|
|
|
name,
|
|
|
|
':',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
throw error;
|
2018-03-13 01:14:24 +00:00
|
|
|
}
|
2018-04-20 21:55:33 +00:00
|
|
|
}
|
2018-03-13 01:14:24 +00:00
|
|
|
|
2018-04-20 21:55:33 +00:00
|
|
|
async function writeAttachment(attachment, options) {
|
2018-12-13 21:41:42 +00:00
|
|
|
if (!_.isString(attachment.path)) {
|
|
|
|
throw new Error('writeAttachment: attachment.path was not a string!');
|
|
|
|
}
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const { dir, message, index, key, newKey } = options;
|
2018-04-20 21:55:33 +00:00
|
|
|
const filename = _getAnonymousAttachmentFileName(message, index);
|
|
|
|
const target = path.join(dir, filename);
|
2018-04-04 01:15:15 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
await writeEncryptedAttachment(target, attachment.path, {
|
2018-04-20 21:55:33 +00:00
|
|
|
key,
|
|
|
|
newKey,
|
|
|
|
filename,
|
|
|
|
dir,
|
|
|
|
});
|
2018-12-13 21:41:42 +00:00
|
|
|
|
|
|
|
if (attachment.thumbnail && _.isString(attachment.thumbnail.path)) {
|
|
|
|
const thumbnailName = `${_getAnonymousAttachmentFileName(
|
|
|
|
message,
|
|
|
|
index
|
|
|
|
)}-thumbnail`;
|
|
|
|
const thumbnailTarget = path.join(dir, thumbnailName);
|
|
|
|
await writeEncryptedAttachment(thumbnailTarget, attachment.thumbnail.path, {
|
|
|
|
key,
|
|
|
|
newKey,
|
|
|
|
filename: thumbnailName,
|
|
|
|
dir,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachment.screenshot && _.isString(attachment.screenshot.path)) {
|
|
|
|
const screenshotName = `${_getAnonymousAttachmentFileName(
|
|
|
|
message,
|
|
|
|
index
|
|
|
|
)}-screenshot`;
|
|
|
|
const screenshotTarget = path.join(dir, screenshotName);
|
|
|
|
await writeEncryptedAttachment(
|
|
|
|
screenshotTarget,
|
|
|
|
attachment.screenshot.path,
|
|
|
|
{
|
|
|
|
key,
|
|
|
|
newKey,
|
|
|
|
filename: screenshotName,
|
|
|
|
dir,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
async function writeAttachments(attachments, options) {
|
2018-03-19 19:42:12 +00:00
|
|
|
const { name } = options;
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const promises = _.map(attachments, (attachment, index) =>
|
|
|
|
writeAttachment(
|
|
|
|
attachment,
|
|
|
|
Object.assign({}, options, {
|
|
|
|
index,
|
|
|
|
})
|
|
|
|
)
|
2018-03-13 01:14:24 +00:00
|
|
|
);
|
2018-03-09 00:05:03 +00:00
|
|
|
try {
|
|
|
|
await Promise.all(promises);
|
|
|
|
} catch (error) {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.error(
|
2018-03-09 00:05:03 +00:00
|
|
|
'writeAttachments: error exporting conversation',
|
|
|
|
name,
|
|
|
|
':',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
async function writeAvatar(contact, options) {
|
|
|
|
const { avatar } = contact || {};
|
|
|
|
if (!avatar || !avatar.avatar || !avatar.avatar.path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-27 16:32:31 +00:00
|
|
|
const { dir, message, index, key, newKey } = options;
|
|
|
|
const name = _getAnonymousAttachmentFileName(message, index);
|
|
|
|
const filename = `${name}-contact-avatar`;
|
|
|
|
const target = path.join(dir, filename);
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
await writeEncryptedAttachment(target, avatar.avatar.path, {
|
2018-04-27 16:32:31 +00:00
|
|
|
key,
|
|
|
|
newKey,
|
|
|
|
filename,
|
|
|
|
dir,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function writeContactAvatars(contact, options) {
|
|
|
|
const { name } = options;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Promise.all(
|
2018-12-13 21:41:42 +00:00
|
|
|
_.map(contact, (item, index) =>
|
2018-04-27 16:32:31 +00:00
|
|
|
writeAvatar(
|
|
|
|
item,
|
|
|
|
Object.assign({}, options, {
|
|
|
|
index,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (error) {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.error(
|
2018-04-27 16:32:31 +00:00
|
|
|
'writeContactAvatars: error exporting conversation',
|
|
|
|
name,
|
|
|
|
':',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-16 03:03:56 +00:00
|
|
|
async function writePreviewImage(preview, options) {
|
|
|
|
const { image } = preview || {};
|
|
|
|
if (!image || !image.path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { dir, message, index, key, newKey } = options;
|
|
|
|
const name = _getAnonymousAttachmentFileName(message, index);
|
|
|
|
const filename = `${name}-preview`;
|
|
|
|
const target = path.join(dir, filename);
|
|
|
|
|
|
|
|
await writeEncryptedAttachment(target, image.path, {
|
|
|
|
key,
|
|
|
|
newKey,
|
|
|
|
filename,
|
|
|
|
dir,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function writePreviews(preview, options) {
|
|
|
|
const { name } = options;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Promise.all(
|
|
|
|
_.map(preview, (item, index) =>
|
|
|
|
writePreviewImage(
|
|
|
|
item,
|
|
|
|
Object.assign({}, options, {
|
|
|
|
index,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
window.log.error(
|
|
|
|
'writePreviews: error exporting conversation',
|
|
|
|
name,
|
|
|
|
':',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
async function writeEncryptedAttachment(target, source, options = {}) {
|
2018-04-27 21:25:04 +00:00
|
|
|
const { key, newKey, filename, dir } = options;
|
2018-04-20 21:55:33 +00:00
|
|
|
|
|
|
|
if (fs.existsSync(target)) {
|
|
|
|
if (newKey) {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.info(`Deleting attachment ${filename}; key has changed`);
|
2018-04-20 21:55:33 +00:00
|
|
|
fs.unlinkSync(target);
|
|
|
|
} else {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.info(`Skipping attachment ${filename}; already exists`);
|
2018-04-20 21:55:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const { readAttachmentData } = Signal.Migrations;
|
|
|
|
const data = await readAttachmentData(source);
|
|
|
|
const ciphertext = await crypto.encryptAttachment(key, source, data);
|
2018-04-20 21:55:33 +00:00
|
|
|
|
|
|
|
const writer = await createFileAndWriter(dir, filename);
|
|
|
|
const stream = createOutputStream(writer);
|
|
|
|
stream.write(Buffer.from(ciphertext));
|
|
|
|
await stream.close();
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
function _sanitizeFileName(filename) {
|
2018-03-09 00:05:03 +00:00
|
|
|
return filename.toString().replace(/[^a-z0-9.,+()'#\- ]/gi, '_');
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
async function exportConversation(conversation, options = {}) {
|
2018-04-27 21:25:04 +00:00
|
|
|
const { name, dir, attachmentsDir, key, newKey } = options;
|
2018-12-13 21:41:42 +00:00
|
|
|
|
2018-03-13 01:14:24 +00:00
|
|
|
if (!name) {
|
|
|
|
throw new Error('Need a name!');
|
|
|
|
}
|
|
|
|
if (!dir) {
|
|
|
|
throw new Error('Need a target directory!');
|
|
|
|
}
|
|
|
|
if (!attachmentsDir) {
|
|
|
|
throw new Error('Need an attachments directory!');
|
|
|
|
}
|
2018-03-19 19:42:12 +00:00
|
|
|
if (!key) {
|
|
|
|
throw new Error('Need a key to encrypt with!');
|
|
|
|
}
|
2018-03-13 01:14:24 +00:00
|
|
|
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.info('exporting conversation', name);
|
2018-03-09 00:05:03 +00:00
|
|
|
const writer = await createFileAndWriter(dir, 'messages.json');
|
2018-12-13 21:41:42 +00:00
|
|
|
const stream = createOutputStream(writer);
|
|
|
|
stream.write('{"messages":[');
|
2018-03-13 01:14:24 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const CHUNK_SIZE = 50;
|
|
|
|
let count = 0;
|
|
|
|
let complete = false;
|
|
|
|
|
|
|
|
// We're looping from the most recent to the oldest
|
|
|
|
let lastReceivedAt = Number.MAX_VALUE;
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
while (!complete) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
const collection = await window.Signal.Data.getMessagesByConversation(
|
|
|
|
conversation.id,
|
|
|
|
{
|
|
|
|
limit: CHUNK_SIZE,
|
|
|
|
receivedAt: lastReceivedAt,
|
|
|
|
MessageCollection: Whisper.MessageCollection,
|
|
|
|
}
|
2018-03-09 00:05:03 +00:00
|
|
|
);
|
2018-12-13 21:41:42 +00:00
|
|
|
const messages = getPlainJS(collection);
|
|
|
|
|
|
|
|
for (let i = 0, max = messages.length; i < max; i += 1) {
|
|
|
|
const message = messages[i];
|
|
|
|
if (count > 0) {
|
|
|
|
stream.write(',');
|
|
|
|
}
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
count += 1;
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
// skip message if it is disappearing, no matter the amount of time left
|
|
|
|
if (message.expireTimer) {
|
|
|
|
// eslint-disable-next-line no-continue
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const { attachments } = message;
|
|
|
|
// eliminate attachment data from the JSON, since it will go to disk
|
|
|
|
// Note: this is for legacy messages only, which stored attachment data in the db
|
|
|
|
message.attachments = _.map(attachments, attachment =>
|
|
|
|
_.omit(attachment, ['data'])
|
2018-03-09 00:05:03 +00:00
|
|
|
);
|
2018-12-13 21:41:42 +00:00
|
|
|
// completely drop any attachments in messages cached in error objects
|
|
|
|
// TODO: move to lodash. Sadly, a number of the method signatures have changed!
|
|
|
|
message.errors = _.map(message.errors, error => {
|
|
|
|
if (error && error.args) {
|
|
|
|
error.args = [];
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
2018-12-13 21:41:42 +00:00
|
|
|
if (error && error.stack) {
|
|
|
|
error.stack = '';
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
2018-12-13 21:41:42 +00:00
|
|
|
return error;
|
|
|
|
});
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const jsonString = JSON.stringify(stringify(message));
|
|
|
|
stream.write(jsonString);
|
|
|
|
|
|
|
|
if (attachments && attachments.length > 0) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await writeAttachments(attachments, {
|
|
|
|
dir: attachmentsDir,
|
|
|
|
name,
|
|
|
|
message,
|
|
|
|
key,
|
|
|
|
newKey,
|
2018-03-12 21:06:31 +00:00
|
|
|
});
|
2018-12-13 21:41:42 +00:00
|
|
|
}
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const quoteThumbnails = message.quote && message.quote.attachments;
|
|
|
|
if (quoteThumbnails && quoteThumbnails.length > 0) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await writeQuoteThumbnails(quoteThumbnails, {
|
|
|
|
dir: attachmentsDir,
|
|
|
|
name,
|
|
|
|
message,
|
|
|
|
key,
|
|
|
|
newKey,
|
|
|
|
});
|
|
|
|
}
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const { contact } = message;
|
|
|
|
if (contact && contact.length > 0) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await writeContactAvatars(contact, {
|
|
|
|
dir: attachmentsDir,
|
|
|
|
name,
|
|
|
|
message,
|
|
|
|
key,
|
|
|
|
newKey,
|
|
|
|
});
|
|
|
|
}
|
2019-01-16 03:03:56 +00:00
|
|
|
|
|
|
|
const { preview } = message;
|
|
|
|
if (preview && preview.length > 0) {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await writePreviews(preview, {
|
|
|
|
dir: attachmentsDir,
|
|
|
|
name,
|
|
|
|
message,
|
|
|
|
key,
|
|
|
|
newKey,
|
|
|
|
});
|
|
|
|
}
|
2018-12-13 21:41:42 +00:00
|
|
|
}
|
2018-04-20 21:55:33 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const last = messages.length > 0 ? messages[messages.length - 1] : null;
|
|
|
|
if (last) {
|
|
|
|
lastReceivedAt = last.received_at;
|
|
|
|
}
|
2018-04-27 16:32:31 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
if (messages.length < CHUNK_SIZE) {
|
|
|
|
complete = true;
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
stream.write(']}');
|
|
|
|
await stream.close();
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Goals for directory names:
|
|
|
|
// 1. Human-readable, for easy use and verification by user (names not just ids)
|
|
|
|
// 2. Sorted just like the list of conversations in the left-pan (active_at)
|
|
|
|
// 3. Disambiguated from other directories (active_at, truncated name, id)
|
2018-03-19 19:42:12 +00:00
|
|
|
function _getConversationDirName(conversation) {
|
|
|
|
const name = conversation.active_at || 'inactive';
|
2018-03-09 00:05:03 +00:00
|
|
|
if (conversation.name) {
|
|
|
|
return `${name} (${conversation.name.slice(0, 30)} ${conversation.id})`;
|
|
|
|
}
|
|
|
|
return `${name} (${conversation.id})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Goals for logging names:
|
|
|
|
// 1. Can be associated with files on disk
|
|
|
|
// 2. Adequately disambiguated to enable debugging flow of execution
|
|
|
|
// 3. Can be shared to the web without privacy concerns (there's no global redaction
|
|
|
|
// logic for group ids, so we do it manually here)
|
2018-03-19 19:42:12 +00:00
|
|
|
function _getConversationLoggingName(conversation) {
|
|
|
|
let name = conversation.active_at || 'inactive';
|
2018-03-09 00:05:03 +00:00
|
|
|
if (conversation.type === 'private') {
|
|
|
|
name += ` (${conversation.id})`;
|
|
|
|
} else {
|
|
|
|
name += ` ([REDACTED_GROUP]${conversation.id.slice(-3)})`;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
async function exportConversations(options) {
|
2018-03-13 01:14:24 +00:00
|
|
|
options = options || {};
|
2018-04-27 21:25:04 +00:00
|
|
|
const { messagesDir, attachmentsDir, key, newKey } = options;
|
2018-03-13 01:14:24 +00:00
|
|
|
|
|
|
|
if (!messagesDir) {
|
2018-12-13 21:41:42 +00:00
|
|
|
throw new Error('Need a messages directory!');
|
2018-03-13 01:14:24 +00:00
|
|
|
}
|
|
|
|
if (!attachmentsDir) {
|
2018-12-13 21:41:42 +00:00
|
|
|
throw new Error('Need an attachments directory!');
|
2018-03-13 01:14:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const collection = await window.Signal.Data.getAllConversations({
|
|
|
|
ConversationCollection: Whisper.ConversationCollection,
|
2018-03-09 00:05:03 +00:00
|
|
|
});
|
2018-12-13 21:41:42 +00:00
|
|
|
const conversations = collection.models;
|
|
|
|
|
|
|
|
for (let i = 0, max = conversations.length; i < max; i += 1) {
|
|
|
|
const conversation = conversations[i];
|
|
|
|
const dirName = _getConversationDirName(conversation);
|
|
|
|
const name = _getConversationLoggingName(conversation);
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
const dir = await createDirectory(messagesDir, dirName);
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await exportConversation(conversation, {
|
|
|
|
name,
|
|
|
|
dir,
|
|
|
|
attachmentsDir,
|
|
|
|
key,
|
|
|
|
newKey,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
window.log.info('Done exporting conversations!');
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 23:13:21 +00:00
|
|
|
function getDirectory(options = {}) {
|
2018-03-09 00:05:03 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const browserWindow = BrowserWindow.getFocusedWindow();
|
|
|
|
const dialogOptions = {
|
|
|
|
title: options.title,
|
|
|
|
properties: ['openDirectory'],
|
|
|
|
buttonLabel: options.buttonLabel,
|
|
|
|
};
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
dialog.showOpenDialog(browserWindow, dialogOptions, directory => {
|
2018-03-09 00:05:03 +00:00
|
|
|
if (!directory || !directory[0]) {
|
|
|
|
const error = new Error('Error choosing directory');
|
|
|
|
error.name = 'ChooseError';
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(directory[0]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDirContents(dir) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.readdir(dir, (err, files) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
files = _.map(files, file => path.join(dir, file));
|
|
|
|
|
|
|
|
resolve(files);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-27 16:32:31 +00:00
|
|
|
async function loadAttachments(dir, getName, options) {
|
2018-03-19 19:42:12 +00:00
|
|
|
options = options || {};
|
|
|
|
const { message } = options;
|
|
|
|
|
2018-04-27 16:32:31 +00:00
|
|
|
await Promise.all(
|
2018-12-13 21:41:42 +00:00
|
|
|
_.map(message.attachments, async (attachment, index) => {
|
2018-04-27 16:32:31 +00:00
|
|
|
const name = getName(message, index, attachment);
|
2018-12-13 21:41:42 +00:00
|
|
|
|
|
|
|
await readEncryptedAttachment(dir, attachment, name, options);
|
|
|
|
|
|
|
|
if (attachment.thumbnail && _.isString(attachment.thumbnail.path)) {
|
|
|
|
const thumbnailName = `${name}-thumbnail`;
|
|
|
|
await readEncryptedAttachment(
|
|
|
|
dir,
|
|
|
|
attachment.thumbnail,
|
|
|
|
thumbnailName,
|
|
|
|
options
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachment.screenshot && _.isString(attachment.screenshot.path)) {
|
|
|
|
const screenshotName = `${name}-screenshot`;
|
|
|
|
await readEncryptedAttachment(
|
|
|
|
dir,
|
|
|
|
attachment.screenshot,
|
|
|
|
screenshotName,
|
|
|
|
options
|
|
|
|
);
|
|
|
|
}
|
2018-04-27 16:32:31 +00:00
|
|
|
})
|
|
|
|
);
|
2018-04-20 21:55:33 +00:00
|
|
|
|
|
|
|
const quoteAttachments = message.quote && message.quote.attachments;
|
2018-04-27 16:32:31 +00:00
|
|
|
await Promise.all(
|
|
|
|
_.map(quoteAttachments, (attachment, index) => {
|
|
|
|
const thumbnail = attachment && attachment.thumbnail;
|
|
|
|
if (!thumbnail) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-04-20 21:55:33 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const name = `${getName(message, index)}-quote-thumbnail`;
|
|
|
|
return readEncryptedAttachment(dir, thumbnail, name, options);
|
2018-04-27 16:32:31 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const { contact } = message;
|
|
|
|
await Promise.all(
|
|
|
|
_.map(contact, (item, index) => {
|
|
|
|
const avatar = item && item.avatar && item.avatar.avatar;
|
|
|
|
if (!avatar) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = `${getName(message, index)}-contact-avatar`;
|
2018-12-13 21:41:42 +00:00
|
|
|
return readEncryptedAttachment(dir, avatar, name, options);
|
2018-04-27 16:32:31 +00:00
|
|
|
})
|
|
|
|
);
|
2018-04-20 21:55:33 +00:00
|
|
|
|
2019-01-16 03:03:56 +00:00
|
|
|
const { preview } = message;
|
|
|
|
await Promise.all(
|
|
|
|
_.map(preview, (item, index) => {
|
|
|
|
const image = item && item.image;
|
|
|
|
if (!image) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = `${getName(message, index)}-preview`;
|
|
|
|
return readEncryptedAttachment(dir, image, name, options);
|
|
|
|
})
|
|
|
|
);
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
function saveMessage(message) {
|
|
|
|
return saveAllMessages([message]);
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
async function saveAllMessages(rawMessages) {
|
2018-04-04 01:14:31 +00:00
|
|
|
if (rawMessages.length === 0) {
|
2018-07-25 22:02:27 +00:00
|
|
|
return;
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
try {
|
|
|
|
const { writeMessageAttachments, upgradeMessageSchema } = Signal.Migrations;
|
|
|
|
const importAndUpgrade = async message =>
|
|
|
|
upgradeMessageSchema(await writeMessageAttachments(message));
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-07-25 22:02:27 +00:00
|
|
|
const messages = await Promise.all(rawMessages.map(importAndUpgrade));
|
2018-03-09 00:05:03 +00:00
|
|
|
|
|
|
|
const { conversationId } = messages[0];
|
|
|
|
|
2018-08-03 04:12:27 +00:00
|
|
|
await window.Signal.Data.saveMessages(messages, {
|
|
|
|
forceSave: true,
|
|
|
|
});
|
2018-07-25 22:02:27 +00:00
|
|
|
|
|
|
|
window.log.info(
|
|
|
|
'Saved',
|
|
|
|
messages.length,
|
|
|
|
'messages for conversation',
|
|
|
|
// Don't know if group or private conversation, so we blindly redact
|
|
|
|
`[REDACTED]${conversationId.slice(-3)}`
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
window.log.error(
|
|
|
|
'saveAllMessages error',
|
|
|
|
error && error.message ? error.message : error
|
|
|
|
);
|
|
|
|
}
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// To reduce the memory impact of attachments, we make individual saves to the
|
|
|
|
// database for every message with an attachment. We load the attachment for a
|
|
|
|
// message, save it, and only then do we move on to the next message. Thus, every
|
|
|
|
// message with attachments needs to be removed from our overall message save with the
|
|
|
|
// filter() call.
|
2018-10-18 01:01:21 +00:00
|
|
|
async function importConversation(dir, options) {
|
2018-03-09 00:05:03 +00:00
|
|
|
options = options || {};
|
|
|
|
_.defaults(options, { messageLookup: {} });
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const { messageLookup, attachmentsDir, key } = options;
|
2018-03-13 01:14:24 +00:00
|
|
|
|
2018-03-09 00:05:03 +00:00
|
|
|
let conversationId = 'unknown';
|
|
|
|
let total = 0;
|
|
|
|
let skipped = 0;
|
|
|
|
let contents;
|
|
|
|
|
|
|
|
try {
|
|
|
|
contents = await readFileAsText(dir, 'messages.json');
|
|
|
|
} catch (error) {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.error(
|
|
|
|
`Warning: could not access messages.json in directory: ${dir}`
|
|
|
|
);
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let promiseChain = Promise.resolve();
|
|
|
|
|
|
|
|
const json = JSON.parse(contents);
|
|
|
|
if (json.messages && json.messages.length) {
|
2018-04-27 21:25:04 +00:00
|
|
|
conversationId = `[REDACTED]${(json.messages[0].conversationId || '').slice(
|
|
|
|
-3
|
|
|
|
)}`;
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
total = json.messages.length;
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const messages = _.filter(json.messages, message => {
|
2018-03-09 00:05:03 +00:00
|
|
|
message = unstringify(message);
|
|
|
|
|
|
|
|
if (messageLookup[getMessageKey(message)]) {
|
|
|
|
skipped += 1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-23 22:36:47 +00:00
|
|
|
const hasAttachments = message.attachments && message.attachments.length;
|
2018-04-27 21:25:04 +00:00
|
|
|
const hasQuotedAttachments =
|
|
|
|
message.quote &&
|
|
|
|
message.quote.attachments &&
|
2018-04-23 22:36:47 +00:00
|
|
|
message.quote.attachments.length > 0;
|
2018-07-25 22:02:27 +00:00
|
|
|
const hasContacts = message.contact && message.contact.length;
|
2019-01-16 03:03:56 +00:00
|
|
|
const hasPreviews = message.preview && message.preview.length;
|
2018-04-23 22:36:47 +00:00
|
|
|
|
2019-01-16 03:03:56 +00:00
|
|
|
if (hasAttachments || hasQuotedAttachments || hasContacts || hasPreviews) {
|
2018-03-13 01:14:24 +00:00
|
|
|
const importMessage = async () => {
|
|
|
|
const getName = attachmentsDir
|
2018-03-19 19:42:12 +00:00
|
|
|
? _getAnonymousAttachmentFileName
|
|
|
|
: _getExportAttachmentFileName;
|
2018-04-27 21:25:04 +00:00
|
|
|
const parentDir =
|
|
|
|
attachmentsDir || path.join(dir, message.received_at.toString());
|
2018-03-19 19:42:12 +00:00
|
|
|
|
|
|
|
await loadAttachments(parentDir, getName, {
|
|
|
|
message,
|
|
|
|
key,
|
|
|
|
});
|
2018-10-18 01:01:21 +00:00
|
|
|
return saveMessage(message);
|
2018-03-09 00:05:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// eslint-disable-next-line more/no-then
|
2018-03-13 01:14:24 +00:00
|
|
|
promiseChain = promiseChain.then(importMessage);
|
2018-03-09 00:05:03 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
await saveAllMessages(messages);
|
2018-03-09 00:05:03 +00:00
|
|
|
|
|
|
|
await promiseChain;
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.info(
|
2018-03-09 00:05:03 +00:00
|
|
|
'Finished importing conversation',
|
|
|
|
conversationId,
|
|
|
|
'Total:',
|
|
|
|
total,
|
|
|
|
'Skipped:',
|
|
|
|
skipped
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
async function importConversations(dir, options) {
|
2018-03-09 00:05:03 +00:00
|
|
|
const contents = await getDirContents(dir);
|
|
|
|
let promiseChain = Promise.resolve();
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
_.forEach(contents, conversationDir => {
|
2018-03-09 00:05:03 +00:00
|
|
|
if (!fs.statSync(conversationDir).isDirectory()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const loadConversation = () => importConversation(conversationDir, options);
|
2018-03-09 00:05:03 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line more/no-then
|
2018-03-13 01:14:24 +00:00
|
|
|
promiseChain = promiseChain.then(loadConversation);
|
2018-03-09 00:05:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return promiseChain;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMessageKey(message) {
|
|
|
|
const ourNumber = textsecure.storage.user.getNumber();
|
|
|
|
const source = message.source || ourNumber;
|
|
|
|
if (source === ourNumber) {
|
|
|
|
return `${source} ${message.timestamp}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sourceDevice = message.sourceDevice || 1;
|
|
|
|
return `${source}.${sourceDevice} ${message.timestamp}`;
|
|
|
|
}
|
2018-09-21 01:47:19 +00:00
|
|
|
async function loadMessagesLookup() {
|
|
|
|
const array = await window.Signal.Data.getAllMessageIds();
|
|
|
|
return fromPairs(map(array, item => [getMessageKey(item), true]));
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getConversationKey(conversation) {
|
|
|
|
return conversation.id;
|
|
|
|
}
|
2018-09-21 01:47:19 +00:00
|
|
|
async function loadConversationLookup() {
|
|
|
|
const array = await window.Signal.Data.getAllConversationIds();
|
|
|
|
return fromPairs(map(array, item => [getConversationKey(item), true]));
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getDirectoryForExport() {
|
2018-08-27 23:13:21 +00:00
|
|
|
return getDirectory();
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
async function compressArchive(file, targetDir) {
|
|
|
|
const items = fs.readdirSync(targetDir);
|
|
|
|
return tar.c(
|
|
|
|
{
|
|
|
|
gzip: true,
|
|
|
|
file,
|
2018-03-13 01:14:24 +00:00
|
|
|
cwd: targetDir,
|
2018-12-13 21:41:42 +00:00
|
|
|
},
|
|
|
|
items
|
|
|
|
);
|
|
|
|
}
|
2018-03-13 01:14:24 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
async function decompressArchive(file, targetDir) {
|
|
|
|
return tar.x({
|
|
|
|
file,
|
|
|
|
cwd: targetDir,
|
2018-03-13 01:14:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
function writeFile(targetPath, contents) {
|
|
|
|
return pify(fs.writeFile)(targetPath, contents);
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
// prettier-ignore
|
|
|
|
const UNIQUE_ID = new Uint8Array([
|
|
|
|
1, 3, 4, 5, 6, 7, 8, 11,
|
|
|
|
23, 34, 1, 34, 3, 5, 45, 45,
|
|
|
|
1, 3, 4, 5, 6, 7, 8, 11,
|
|
|
|
23, 34, 1, 34, 3, 5, 45, 45,
|
|
|
|
]);
|
2018-03-19 19:42:12 +00:00
|
|
|
async function encryptFile(sourcePath, targetPath, options) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
const { key } = options;
|
|
|
|
if (!key) {
|
|
|
|
throw new Error('Need key to do encryption!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const plaintext = await readFileAsArrayBuffer(sourcePath);
|
2018-12-13 21:41:42 +00:00
|
|
|
const ciphertext = await crypto.encryptFile(key, UNIQUE_ID, plaintext);
|
|
|
|
return writeFile(targetPath, Buffer.from(ciphertext));
|
2018-03-19 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function decryptFile(sourcePath, targetPath, options) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
const { key } = options;
|
|
|
|
if (!key) {
|
|
|
|
throw new Error('Need key to do encryption!');
|
|
|
|
}
|
|
|
|
|
2018-04-04 02:47:53 +00:00
|
|
|
const ciphertext = await readFileAsArrayBuffer(sourcePath);
|
2018-12-13 21:41:42 +00:00
|
|
|
const plaintext = await crypto.decryptFile(key, UNIQUE_ID, ciphertext);
|
2018-03-19 19:42:12 +00:00
|
|
|
return writeFile(targetPath, Buffer.from(plaintext));
|
|
|
|
}
|
|
|
|
|
2018-03-13 01:14:24 +00:00
|
|
|
function createTempDir() {
|
|
|
|
return pify(tmp.dir)();
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteAll(pattern) {
|
|
|
|
return pify(rimraf)(pattern);
|
|
|
|
}
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const ARCHIVE_NAME = 'messages.tar.gz';
|
2018-06-16 00:31:16 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
async function exportToDirectory(directory, options) {
|
2019-02-19 20:14:05 +00:00
|
|
|
const env = window.getEnvironment();
|
|
|
|
if (env !== 'test') {
|
|
|
|
throw new Error('export is only supported in test mode');
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:42:12 +00:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
if (!options.key) {
|
|
|
|
throw new Error('Encrypted backup requires a key to encrypt with!');
|
|
|
|
}
|
|
|
|
|
|
|
|
let stagingDir;
|
|
|
|
let encryptionDir;
|
2018-03-09 00:05:03 +00:00
|
|
|
try {
|
2018-03-19 19:42:12 +00:00
|
|
|
stagingDir = await createTempDir();
|
|
|
|
encryptionDir = await createTempDir();
|
2018-03-13 01:14:24 +00:00
|
|
|
|
|
|
|
const attachmentsDir = await createDirectory(directory, 'attachments');
|
|
|
|
|
2019-02-11 23:59:21 +00:00
|
|
|
await exportConversationListToFile(stagingDir);
|
2018-04-27 21:25:04 +00:00
|
|
|
await exportConversations(
|
|
|
|
Object.assign({}, options, {
|
|
|
|
messagesDir: stagingDir,
|
|
|
|
attachmentsDir,
|
|
|
|
})
|
|
|
|
);
|
2018-03-13 01:14:24 +00:00
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const archivePath = path.join(directory, ARCHIVE_NAME);
|
|
|
|
await compressArchive(archivePath, stagingDir);
|
|
|
|
await encryptFile(archivePath, path.join(directory, ARCHIVE_NAME), options);
|
2018-03-09 00:05:03 +00:00
|
|
|
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.info('done backing up!');
|
2018-03-13 01:14:24 +00:00
|
|
|
return directory;
|
2018-03-09 00:05:03 +00:00
|
|
|
} catch (error) {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.error(
|
2018-03-19 19:42:12 +00:00
|
|
|
'The backup went wrong!',
|
2018-03-09 00:05:03 +00:00
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
throw error;
|
2018-03-13 01:14:24 +00:00
|
|
|
} finally {
|
2018-03-19 19:42:12 +00:00
|
|
|
if (stagingDir) {
|
|
|
|
await deleteAll(stagingDir);
|
|
|
|
}
|
|
|
|
if (encryptionDir) {
|
|
|
|
await deleteAll(encryptionDir);
|
2018-03-13 01:14:24 +00:00
|
|
|
}
|
2018-03-09 00:05:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDirectoryForImport() {
|
|
|
|
const options = {
|
|
|
|
title: i18n('importChooserTitle'),
|
|
|
|
};
|
|
|
|
return getDirectory(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function importFromDirectory(directory, options) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const lookups = await Promise.all([
|
2018-10-18 01:01:21 +00:00
|
|
|
loadMessagesLookup(),
|
|
|
|
loadConversationLookup(),
|
2018-03-09 00:05:03 +00:00
|
|
|
]);
|
2019-02-11 23:59:21 +00:00
|
|
|
const [messageLookup, conversationLookup] = lookups;
|
2018-03-09 00:05:03 +00:00
|
|
|
options = Object.assign({}, options, {
|
|
|
|
messageLookup,
|
|
|
|
conversationLookup,
|
|
|
|
});
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const archivePath = path.join(directory, ARCHIVE_NAME);
|
|
|
|
if (fs.existsSync(archivePath)) {
|
2019-02-19 20:14:05 +00:00
|
|
|
const env = window.getEnvironment();
|
|
|
|
if (env !== 'test') {
|
|
|
|
throw new Error('import is only supported in test mode');
|
|
|
|
}
|
|
|
|
|
2018-03-13 01:14:24 +00:00
|
|
|
// we're in the world of an encrypted, zipped backup
|
2018-03-19 19:42:12 +00:00
|
|
|
if (!options.key) {
|
2018-04-27 21:25:04 +00:00
|
|
|
throw new Error(
|
|
|
|
'Importing an encrypted backup; decryption key is required!'
|
|
|
|
);
|
2018-03-19 19:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let stagingDir;
|
|
|
|
let decryptionDir;
|
2018-03-13 01:14:24 +00:00
|
|
|
try {
|
2018-03-19 19:42:12 +00:00
|
|
|
stagingDir = await createTempDir();
|
|
|
|
decryptionDir = await createTempDir();
|
|
|
|
|
2018-03-13 01:14:24 +00:00
|
|
|
const attachmentsDir = path.join(directory, 'attachments');
|
|
|
|
|
2018-12-13 21:41:42 +00:00
|
|
|
const decryptedArchivePath = path.join(decryptionDir, ARCHIVE_NAME);
|
|
|
|
await decryptFile(archivePath, decryptedArchivePath, options);
|
|
|
|
await decompressArchive(decryptedArchivePath, stagingDir);
|
2018-03-13 01:14:24 +00:00
|
|
|
|
|
|
|
options = Object.assign({}, options, {
|
|
|
|
attachmentsDir,
|
|
|
|
});
|
2018-10-18 01:01:21 +00:00
|
|
|
const result = await importNonMessages(stagingDir, options);
|
|
|
|
await importConversations(stagingDir, Object.assign({}, options));
|
2018-03-13 01:14:24 +00:00
|
|
|
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.info('Done importing from backup!');
|
2018-03-13 01:14:24 +00:00
|
|
|
return result;
|
|
|
|
} finally {
|
2018-03-19 19:42:12 +00:00
|
|
|
if (stagingDir) {
|
|
|
|
await deleteAll(stagingDir);
|
|
|
|
}
|
|
|
|
if (decryptionDir) {
|
|
|
|
await deleteAll(decryptionDir);
|
2018-03-13 01:14:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const result = await importNonMessages(directory, options);
|
|
|
|
await importConversations(directory, options);
|
2018-03-13 01:14:24 +00:00
|
|
|
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.info('Done importing!');
|
2018-03-09 00:05:03 +00:00
|
|
|
return result;
|
|
|
|
} catch (error) {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.error(
|
2018-03-19 19:42:12 +00:00
|
|
|
'The import went wrong!',
|
2018-03-09 00:05:03 +00:00
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|