* chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0 Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 66.0.0 to 68.0.0. - [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases) - [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v66.0.0...v68.0.0) --- updated-dependencies: - dependency-name: eslint-plugin-unicorn dependency-version: 68.0.0 dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * style: fix no-useless-coercion * style: fix prefer-boolean-return * style: fix no-useless-logical-operand * style: fix prefer-single-replace * style: fix consistent-conditional-object-spread * style: fix no-useless-else * style: fix prefer-unicode-code-point-escapes * style: fix operator-assignment * style: fix prefer-continue * style: fix prefer-math-constants * style: fix prefer-hoisting-branch-code * style: fix prefer-array-slice * style: fix consistent-class-member-order * style: fix no-return-array-push * style: fix prefer-promise-with-resolvers * style: fix no-array-concat-in-loop * style: fix no-loop-iterable-mutation * style: fix no-invalid-argument-count * style: fix no-duplicate-if-branches * style: fix prefer-else-if * style: fix no-unreadable-for-of-expression * style: fix logical-assignment-operators * style: fix default-export-style * style: revert false positve in prefer-type-literal-last in #22274 * style: fix arrow-body-style * style: add v68 recommended preset * style: refactor GraphQL queries to use template literals for better readability --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Tony <TonyRL@users.noreply.github.com>
112 lines
3.5 KiB
TypeScript
112 lines
3.5 KiB
TypeScript
import { ImapFlow } from 'imapflow';
|
|
import { simpleParser } from 'mailparser';
|
|
|
|
import { config } from '@/config';
|
|
import ConfigNotFoundError from '@/errors/types/config-not-found';
|
|
import type { Route } from '@/types';
|
|
import cache from '@/utils/cache';
|
|
import logger from '@/utils/logger';
|
|
import { parseDate } from '@/utils/parse-date';
|
|
|
|
export const route: Route = {
|
|
path: '/imap/:email/:folder{.+}?',
|
|
name: 'Unknown',
|
|
maintainers: [],
|
|
handler,
|
|
};
|
|
|
|
async function handler(ctx) {
|
|
const { email, folder = 'INBOX' } = ctx.req.param();
|
|
const { limit = 10 } = ctx.req.query();
|
|
const mailConfig = {
|
|
username: email,
|
|
port: 993,
|
|
...Object.fromEntries(new URLSearchParams(config.email.config[email.replaceAll(/[.@]/g, '_')])),
|
|
};
|
|
|
|
if (!mailConfig.username || !mailConfig.password || !mailConfig.host || !mailConfig.port) {
|
|
throw new ConfigNotFoundError('Email Inbox RSS is disabled due to the lack of <a href="https://docs.rsshub.app/deploy/#route-specific-configurations">relevant config</a>');
|
|
}
|
|
|
|
const client = new ImapFlow({
|
|
host: mailConfig.host,
|
|
port: Number.parseInt(mailConfig.port),
|
|
secure: true,
|
|
auth: {
|
|
user: mailConfig.username,
|
|
pass: mailConfig.password,
|
|
},
|
|
proxy: config.proxyUri, // Note: socks5h is not supported
|
|
logger: {
|
|
debug: (log) => logger.debug(log.msg),
|
|
info: (log) => logger.info(log.msg),
|
|
warn: (log) => logger.warn(log.msg),
|
|
error: (log) => logger.error(log?.msg),
|
|
},
|
|
});
|
|
|
|
try {
|
|
await client.connect();
|
|
} catch (error) {
|
|
throw new Error(error.responseText, { cause: error });
|
|
}
|
|
|
|
/**
|
|
[
|
|
{
|
|
// https://imapflow.com/global.html#FetchMessageObject
|
|
seq: Number,
|
|
uid: Number,
|
|
envelope: {
|
|
// https://imapflow.com/global.html#MessageEnvelopeObject
|
|
},
|
|
id: 'md5-like-hash-string',
|
|
source: Buffer,
|
|
}
|
|
]
|
|
*/
|
|
const mails = [];
|
|
const lock = await client.getMailboxLock(folder);
|
|
try {
|
|
const messages = client.fetch(`${Math.max(client.mailbox.exists - limit + 1, 1)}:*`, { envelope: true, source: true, uid: true });
|
|
for await (const message of messages) {
|
|
mails.push(message);
|
|
}
|
|
} finally {
|
|
lock.release();
|
|
}
|
|
|
|
const items = await Promise.all(
|
|
mails.map((item) =>
|
|
cache.tryGet(`mail:${email}:${item.envelope.messageId}`, async () => {
|
|
const parsed = await simpleParser(item.source);
|
|
|
|
let description = parsed.html || parsed.textAsHtml;
|
|
if (parsed.attachments.length) {
|
|
description += `<h3>Attachments (${parsed.attachments.length})</h3>`;
|
|
for (const attachment of parsed.attachments) {
|
|
description += `<p>${attachment.filename}</p>`;
|
|
}
|
|
}
|
|
|
|
return {
|
|
title: item.envelope.subject,
|
|
description,
|
|
pubDate: parseDate(item.envelope.date),
|
|
author: parsed.from.text,
|
|
guid: `mail:${email}:${item.envelope.messageId}`,
|
|
};
|
|
})
|
|
)
|
|
);
|
|
|
|
await client.logout();
|
|
|
|
return {
|
|
title: `${email}'s Inbox${folder === 'INBOX' ? '' : ` - ${folder}`}`,
|
|
link: `https://${email.split('@', 2)[1]}`,
|
|
item: items,
|
|
allowEmpty: true,
|
|
};
|
|
}
|