1 import { PACKAGE_TYPE } from '@proton/shared/lib/mail/mailSettings';
3 import { MIME_TYPES, PGP_SCHEMES } from '../../constants';
4 import type { Message } from '../../interfaces/mail/Message';
5 import type { EncryptionPreferences } from '../encryptionPreferences';
6 import { isEO } from '../messages';
8 const { SEND_PM, SEND_EO, SEND_CLEAR, SEND_PGP_INLINE, SEND_PGP_MIME } = PACKAGE_TYPE;
11 * Logic for determining the PGP scheme to be used when sending to an email address.
12 * The API expects a package type.
14 export const getPGPScheme = (
15 { encrypt, sign, scheme, isInternal }: Pick<EncryptionPreferences, 'encrypt' | 'sign' | 'scheme' | 'isInternal'>,
16 message?: Partial<Message>
21 if (!encrypt && isEO(message)) {
25 return scheme === PGP_SCHEMES.PGP_INLINE ? SEND_PGP_INLINE : SEND_PGP_MIME;
30 export const getPGPSchemeAndMimeType = (
36 mimeType: prefMimeType,
37 }: Pick<EncryptionPreferences, 'encrypt' | 'sign' | 'scheme' | 'mimeType' | 'isInternal'>,
38 message?: Partial<Message>
39 ): { pgpScheme: PACKAGE_TYPE; mimeType: MIME_TYPES } => {
40 const messageMimeType = message?.MIMEType as MIME_TYPES;
41 const pgpScheme = getPGPScheme({ encrypt, sign, scheme, isInternal }, message);
43 if (sign && [SEND_PGP_INLINE, SEND_PGP_MIME].includes(pgpScheme)) {
44 const enforcedMimeType = pgpScheme === SEND_PGP_INLINE ? MIME_TYPES.PLAINTEXT : MIME_TYPES.MIME;
45 return { pgpScheme, mimeType: enforcedMimeType };
48 // If sending EO, respect the MIME type of the composer, since it will be what the API returns when retrieving the message.
49 // If plain text is selected in the composer and the message is not signed, send in plain text
50 if (pgpScheme === SEND_EO || messageMimeType === MIME_TYPES.PLAINTEXT) {
51 return { pgpScheme, mimeType: messageMimeType || prefMimeType };
54 return { pgpScheme, mimeType: prefMimeType || messageMimeType };