2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
import { createReadStream } from 'fs';
|
|
|
|
import { Transform } from 'stream';
|
|
|
|
import { pipeline } from 'stream/promises';
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
import { SignalService as Proto } from '../protobuf';
|
2023-10-30 16:24:28 +00:00
|
|
|
import protobuf from '../protobuf/wrap';
|
2023-09-14 17:04:48 +00:00
|
|
|
import { normalizeAci } from '../util/normalizeAci';
|
2023-09-28 18:41:45 +00:00
|
|
|
import { isAciString } from '../util/isAciString';
|
2022-11-16 20:18:02 +00:00
|
|
|
import { DurationInSeconds } from '../util/durations';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2023-10-30 16:24:28 +00:00
|
|
|
import type { ContactAvatarType } from '../types/Avatar';
|
|
|
|
import { computeHash } from '../Crypto';
|
|
|
|
import { dropNull } from '../util/dropNull';
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
import Avatar = Proto.ContactDetails.IAvatar;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2022-03-24 17:05:48 +00:00
|
|
|
const { Reader } = protobuf;
|
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
type OptionalFields = {
|
|
|
|
avatar?: Avatar | null;
|
|
|
|
expireTimer?: number | null;
|
|
|
|
number?: string | null;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
|
2022-11-16 20:18:02 +00:00
|
|
|
type MessageWithAvatar<Message extends OptionalFields> = Omit<
|
2021-07-09 19:36:10 +00:00
|
|
|
Message,
|
2023-10-30 16:24:28 +00:00
|
|
|
'avatar' | 'toJSON'
|
2021-07-09 19:36:10 +00:00
|
|
|
> & {
|
2023-10-30 16:24:28 +00:00
|
|
|
avatar?: ContactAvatarType;
|
2022-11-16 20:18:02 +00:00
|
|
|
expireTimer?: DurationInSeconds;
|
2023-10-30 16:24:28 +00:00
|
|
|
number?: string | undefined;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
export type ContactDetailsWithAvatar = MessageWithAvatar<Proto.IContactDetails>;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
export async function parseContactsV2({
|
|
|
|
absolutePath,
|
|
|
|
}: {
|
|
|
|
absolutePath: string;
|
|
|
|
}): Promise<ReadonlyArray<ContactDetailsWithAvatar>> {
|
|
|
|
const logId = 'parseContactsV2';
|
2022-09-13 21:48:09 +00:00
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
const readStream = createReadStream(absolutePath);
|
|
|
|
const parseContactsTransform = new ParseContactsTransform();
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
try {
|
|
|
|
await pipeline(readStream, parseContactsTransform);
|
|
|
|
} catch (error) {
|
|
|
|
try {
|
|
|
|
readStream.close();
|
|
|
|
} catch (cleanupError) {
|
|
|
|
log.error(
|
|
|
|
`${logId}: Failed to clean up after error`,
|
|
|
|
Errors.toLogFormat(cleanupError)
|
|
|
|
);
|
2021-07-09 19:36:10 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
throw error;
|
|
|
|
}
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
readStream.close();
|
2017-09-11 16:50:35 +00:00
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
return parseContactsTransform.contacts;
|
|
|
|
}
|
2022-11-16 20:18:02 +00:00
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
// This transform pulls contacts and their avatars from a stream of bytes. This is tricky,
|
|
|
|
// because the chunk boundaries might fall in the middle of a contact or their avatar.
|
|
|
|
// So we are ready for decodeDelimited() to throw, and to keep activeContact around
|
|
|
|
// while we wait for more chunks to get to the expected avatar size.
|
|
|
|
// Note: exported only for testing
|
|
|
|
export class ParseContactsTransform extends Transform {
|
|
|
|
public contacts: Array<ContactDetailsWithAvatar> = [];
|
|
|
|
|
|
|
|
public activeContact: Proto.ContactDetails | undefined;
|
|
|
|
private unused: Uint8Array | undefined;
|
|
|
|
|
|
|
|
override async _transform(
|
|
|
|
chunk: Buffer | undefined,
|
|
|
|
_encoding: string,
|
|
|
|
done: (error?: Error) => void
|
|
|
|
): Promise<void> {
|
|
|
|
if (!chunk || chunk.byteLength === 0) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
2022-11-16 20:18:02 +00:00
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
try {
|
|
|
|
let data = chunk;
|
|
|
|
if (this.unused) {
|
|
|
|
data = Buffer.concat([this.unused, data]);
|
|
|
|
this.unused = undefined;
|
2020-03-05 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
const reader = Reader.create(data);
|
|
|
|
while (reader.pos < reader.len) {
|
|
|
|
const startPos = reader.pos;
|
|
|
|
|
|
|
|
if (!this.activeContact) {
|
|
|
|
try {
|
|
|
|
this.activeContact = Proto.ContactDetails.decodeDelimited(reader);
|
|
|
|
} catch (err) {
|
|
|
|
// We get a RangeError if there wasn't enough data to read the next record.
|
|
|
|
if (err instanceof RangeError) {
|
|
|
|
// Note: A failed decodeDelimited() does in fact update reader.pos, so we
|
|
|
|
// must reset to startPos
|
|
|
|
this.unused = data.subarray(startPos);
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Something deeper has gone wrong; the proto is malformed or something
|
|
|
|
done(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Something has really gone wrong if the above parsing didn't throw but gave
|
|
|
|
// us nothing back. Let's end the parse.
|
|
|
|
if (!this.activeContact) {
|
|
|
|
done(new Error('ParseContactsTransform: No active contact!'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const attachmentSize = this.activeContact?.avatar?.length ?? 0;
|
|
|
|
if (attachmentSize === 0) {
|
|
|
|
// No avatar attachment for this contact
|
|
|
|
const prepared = prepareContact(this.activeContact);
|
|
|
|
if (prepared) {
|
|
|
|
this.contacts.push(prepared);
|
|
|
|
}
|
|
|
|
this.activeContact = undefined;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const spaceLeftAfterRead = reader.len - (reader.pos + attachmentSize);
|
|
|
|
if (spaceLeftAfterRead >= 0) {
|
|
|
|
// We've read enough data to read the entire attachment
|
|
|
|
const avatarData = reader.buf.slice(
|
|
|
|
reader.pos,
|
|
|
|
reader.pos + attachmentSize
|
|
|
|
);
|
|
|
|
const hash = computeHash(data);
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
const path = await window.Signal.Migrations.writeNewAttachmentData(
|
|
|
|
avatarData
|
|
|
|
);
|
|
|
|
|
|
|
|
const prepared = prepareContact(this.activeContact, {
|
|
|
|
...this.activeContact.avatar,
|
|
|
|
hash,
|
|
|
|
path,
|
|
|
|
});
|
|
|
|
if (prepared) {
|
|
|
|
this.contacts.push(prepared);
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await window.Signal.Migrations.deleteAttachmentData(path);
|
|
|
|
}
|
|
|
|
this.activeContact = undefined;
|
|
|
|
|
|
|
|
reader.skip(attachmentSize);
|
|
|
|
} else {
|
|
|
|
// We have an attachment, but we haven't read enough data yet. We need to
|
|
|
|
// wait for another chunk.
|
|
|
|
this.unused = data.subarray(reader.pos);
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
2022-11-16 20:18:02 +00:00
|
|
|
}
|
2020-03-05 21:14:58 +00:00
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
// No need to push; no downstream consumers!
|
2018-07-21 19:00:08 +00:00
|
|
|
} catch (error) {
|
2023-10-30 16:24:28 +00:00
|
|
|
done(error);
|
|
|
|
return;
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|
2022-08-25 05:04:42 +00:00
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
done();
|
2022-08-25 05:04:42 +00:00
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
function prepareContact(
|
|
|
|
proto: Proto.ContactDetails,
|
|
|
|
avatar?: ContactAvatarType
|
|
|
|
): ContactDetailsWithAvatar | undefined {
|
|
|
|
const expireTimer =
|
|
|
|
proto.expireTimer != null
|
|
|
|
? DurationInSeconds.fromSeconds(proto.expireTimer)
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
// We reject incoming contacts with invalid aci information
|
2023-11-07 20:26:31 +00:00
|
|
|
if (proto.aci && !isAciString(proto.aci)) {
|
2023-10-30 16:24:28 +00:00
|
|
|
log.warn('ParseContactsTransform: Dropping contact with invalid aci');
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2023-09-28 18:41:45 +00:00
|
|
|
return undefined;
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2023-10-30 16:24:28 +00:00
|
|
|
|
2024-02-06 02:30:04 +00:00
|
|
|
const aci = proto.aci
|
|
|
|
? normalizeAci(proto.aci, 'ContactBuffer.aci')
|
|
|
|
: proto.aci;
|
|
|
|
|
2023-10-30 16:24:28 +00:00
|
|
|
const result = {
|
|
|
|
...proto,
|
|
|
|
expireTimer,
|
|
|
|
aci,
|
|
|
|
avatar,
|
|
|
|
number: dropNull(proto.number),
|
|
|
|
};
|
|
|
|
|
|
|
|
return result;
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|