Skip to content

Commit

Permalink
fix: fixed attachment issues
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Dec 11, 2023
1 parent 4811f25 commit 299d62e
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 78 deletions.
5 changes: 1 addition & 4 deletions helpers/attachment-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ class AttachmentStorage {
);
}

async get(mimeTree, hash) {
const instance = mimeTree[Symbol.for('instance')];
const session = mimeTree[Symbol.for('session')];

async get(mimeTree, hash, instance, session) {
if (!instance) throw new TypeError('Instance is not defined');
if (!session) throw new TypeError('Session is not defined');

Expand Down
73 changes: 35 additions & 38 deletions helpers/get-query-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@ const libmime = require('libmime');
const Indexer = require('./indexer');

// eslint-disable-next-line complexity, max-params
function getQueryResponse(query, message, options, instance, session) {
function getQueryResponse(query, message, options = {}, instance, session) {
options = options || {};

if (!instance) throw new TypeError('Instance is missing');
if (!session) throw new TypeError('Session is missing');

// for optimization purposes try to use cached mimeTree etc. if available
// If these values are missing then generate these when first time required
// So if the query is for (UID FLAGS) then mimeTree is never generated
let { mimeTree } = message;
const indexer = new Indexer(options);

// NOTE: we bind a few symbols so we don't have to rewrite everything
if (mimeTree) {
mimeTree[Symbol.for('instance')] = instance;
mimeTree[Symbol.for('session')] = session;
}

// generate response object
const values = [];
for (const item of query) {
Expand Down Expand Up @@ -71,8 +68,6 @@ function getQueryResponse(query, message, options, instance, session) {
} else {
if (!mimeTree) {
mimeTree = indexer.parseMimeTree(message.raw);
mimeTree[Symbol.for('instance')] = instance;
mimeTree[Symbol.for('session')] = session;
}

value = indexer.getBodyStructure(mimeTree);
Expand Down Expand Up @@ -121,8 +116,6 @@ function getQueryResponse(query, message, options, instance, session) {
} else {
if (!mimeTree) {
mimeTree = indexer.parseMimeTree(message.raw);
mimeTree[Symbol.for('instance')] = instance;
mimeTree[Symbol.for('session')] = session;
}

value = indexer.getEnvelope(mimeTree);
Expand Down Expand Up @@ -195,11 +188,9 @@ function getQueryResponse(query, message, options, instance, session) {
case 'rfc822': {
if (!mimeTree) {
mimeTree = indexer.parseMimeTree(message.raw);
mimeTree[Symbol.for('instance')] = instance;
mimeTree[Symbol.for('session')] = session;
}

value = indexer.getContents(mimeTree);
value = indexer.getContents(mimeTree, false, {}, instance, session);
break;
}

Expand All @@ -209,8 +200,6 @@ function getQueryResponse(query, message, options, instance, session) {
} else {
if (!mimeTree) {
mimeTree = indexer.parseMimeTree(message.raw);
mimeTree[Symbol.for('instance')] = instance;
mimeTree[Symbol.for('session')] = session;
}

value = indexer.getSize(mimeTree);
Expand All @@ -223,8 +212,6 @@ function getQueryResponse(query, message, options, instance, session) {
// Equivalent to BODY[HEADER]
if (!mimeTree) {
mimeTree = indexer.parseMimeTree(message.raw);
mimeTree[Symbol.for('session')] = session;
mimeTree[Symbol.for('instance')] = instance;
}

value = [mimeTree.header || []].flat().join('\r\n') + '\r\n\r\n';
Expand All @@ -235,14 +222,18 @@ function getQueryResponse(query, message, options, instance, session) {
// Equivalent to BODY[TEXT]
if (!mimeTree) {
mimeTree = indexer.parseMimeTree(message.raw);
mimeTree[Symbol.for('instance')] = instance;
mimeTree[Symbol.for('session')] = session;
}

value = indexer.getContents(mimeTree, {
path: '',
type: 'text'
});
value = indexer.getContents(
mimeTree,
{
path: '',
type: 'text'
},
{},
instance,
session
);
break;
}

Expand All @@ -252,35 +243,41 @@ function getQueryResponse(query, message, options, instance, session) {
// BODY
if (!mimeTree) {
mimeTree = indexer.parseMimeTree(message.raw);
mimeTree[Symbol.for('instance')] = instance;
mimeTree[Symbol.for('session')] = session;
}

value = indexer.getBody(mimeTree);
} else if (item.path === '' && item.type === 'content') {
// BODY[]
if (!mimeTree) {
mimeTree = indexer.parseMimeTree(message.raw);
mimeTree[Symbol.for('instance')] = instance;
mimeTree[Symbol.for('session')] = session;
}

value = indexer.getContents(mimeTree, false, {
startFrom: item.partial && item.partial.startFrom,
maxLength: item.partial && item.partial.maxLength
});
value = indexer.getContents(
mimeTree,
false,
{
startFrom: item.partial && item.partial.startFrom,
maxLength: item.partial && item.partial.maxLength
},
instance,
session
);
} else {
// BODY[SELECTOR]
if (!mimeTree) {
mimeTree = indexer.parseMimeTree(message.raw);
mimeTree[Symbol.for('instance')] = instance;
mimeTree[Symbol.for('session')] = session;
}

value = indexer.getContents(mimeTree, item, {
startFrom: item.partial && item.partial.startFrom,
maxLength: item.partial && item.partial.maxLength
});
value = indexer.getContents(
mimeTree,
item,
{
startFrom: item.partial && item.partial.startFrom,
maxLength: item.partial && item.partial.maxLength
},
instance,
session
);
}

if (item.partial) {
Expand Down
22 changes: 0 additions & 22 deletions helpers/imap/on-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,6 @@ async function getMessages(instance, session, server, opts = {}) {
// eslint-disable-next-line no-await-in-loop
const message = await convertResult(Messages, result, projection);

// NOTE: we bind a few symbols so we don't have to rewrite everything
if (message.mimeTree) {
message.mimeTree[Symbol.for('instance')] = instance;
message.mimeTree[Symbol.for('session')] = session;
}

server.logger.debug('fetched message', {
result,
message,
Expand Down Expand Up @@ -248,14 +242,6 @@ async function getMessages(instance, session, server, opts = {}) {
})
);

//
// security safeguard
//
if (message.mimeTree) {
delete message.mimeTree[Symbol.for('instance')];
delete message.mimeTree[Symbol.for('session')];
}

const data = formatResponse.call(session, 'FETCH', message.uid, {
query: options.query,
values
Expand Down Expand Up @@ -329,14 +315,6 @@ async function getMessages(instance, session, server, opts = {}) {
})
);

//
// security safeguard
//
if (message.mimeTree) {
delete message.mimeTree[Symbol.for('instance')];
delete message.mimeTree[Symbol.for('session')];
}

const data = formatResponse.call(session, 'FETCH', message.uid, {
query: options.query,
values
Expand Down
Loading

0 comments on commit 299d62e

Please sign in to comment.