Refactor multi-flow mail capability boundaries
This commit is contained in:
+60
-13
@@ -32,13 +32,49 @@
|
||||
: HOTMAIL_SERVICE_MODE_LOCAL;
|
||||
}
|
||||
|
||||
function extractVerificationCode(text) {
|
||||
function normalizeRulePatternList(patterns = []) {
|
||||
return Array.isArray(patterns) ? patterns : [];
|
||||
}
|
||||
|
||||
function extractCodeByRulePatterns(text, patterns = []) {
|
||||
const normalizedText = String(text || '');
|
||||
for (const pattern of normalizeRulePatternList(patterns)) {
|
||||
try {
|
||||
const source = String(pattern?.source || '').trim();
|
||||
if (!source) {
|
||||
continue;
|
||||
}
|
||||
const flags = String(pattern?.flags || '').replace(/[^dgimsuvy]/g, '');
|
||||
const match = normalizedText.match(new RegExp(source, flags));
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
for (let index = 1; index < match.length; index += 1) {
|
||||
const candidate = String(match[index] || '').trim();
|
||||
if (candidate) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
if (String(match[0] || '').trim()) {
|
||||
return String(match[0] || '').trim();
|
||||
}
|
||||
} catch (_) {
|
||||
// Ignore invalid runtime rule patterns and continue with other candidates.
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function extractVerificationCode(text, options = {}) {
|
||||
const source = String(text || '');
|
||||
const matchedByRule = extractCodeByRulePatterns(source, options?.codePatterns);
|
||||
if (matchedByRule) return matchedByRule;
|
||||
|
||||
const matchCn = source.match(/(?:代码为|验证码[^0-9]*?)[\s::]*(\d{6})/i);
|
||||
if (matchCn) return matchCn[1];
|
||||
|
||||
const matchOpenAiLogin = source.match(/(?:chatgpt\s+log-?in\s+code|enter\s+this\s+code)[^0-9]{0,24}(\d{6})/i);
|
||||
if (matchOpenAiLogin) return matchOpenAiLogin[1];
|
||||
const matchLoginCode = source.match(/(?:log-?in\s+code|enter\s+this\s+code)[^0-9]{0,24}(\d{6})/i);
|
||||
if (matchLoginCode) return matchLoginCode[1];
|
||||
|
||||
const matchEn = source.match(/code(?:\s+is|[\s:])+(\d{6})/i);
|
||||
if (matchEn) return matchEn[1];
|
||||
@@ -47,7 +83,7 @@
|
||||
return matchStandalone ? matchStandalone[1] : null;
|
||||
}
|
||||
|
||||
function extractVerificationCodeFromMessage(message = {}) {
|
||||
function extractVerificationCodeFromMessage(message = {}, options = {}) {
|
||||
const sender = firstNonEmptyString([
|
||||
message?.from?.emailAddress?.address,
|
||||
message?.sender,
|
||||
@@ -55,7 +91,9 @@
|
||||
]);
|
||||
const subject = firstNonEmptyString([message?.subject]);
|
||||
const preview = firstNonEmptyString([message?.bodyPreview, message?.preview, message?.text]);
|
||||
return extractVerificationCode([subject, preview, sender].filter(Boolean).join(' '));
|
||||
return extractVerificationCode([subject, preview, sender].filter(Boolean).join(' '), {
|
||||
codePatterns: options?.codePatterns,
|
||||
});
|
||||
}
|
||||
|
||||
function getLatestHotmailMessage(messages) {
|
||||
@@ -138,6 +176,10 @@
|
||||
function messageMatchesFilters(message, filters = {}) {
|
||||
const senderFilters = (filters.senderFilters || []).map(normalizeText).filter(Boolean);
|
||||
const subjectFilters = (filters.subjectFilters || []).map(normalizeText).filter(Boolean);
|
||||
const requiredKeywords = (filters.requiredKeywords || []).map(normalizeText).filter(Boolean);
|
||||
const hasSenderFilters = senderFilters.length > 0;
|
||||
const hasSubjectFilters = subjectFilters.length > 0;
|
||||
const hasKeywordHints = requiredKeywords.length > 0;
|
||||
const afterTimestamp = normalizeTimestamp(filters.afterTimestamp);
|
||||
const receivedAt = normalizeTimestamp(message?.receivedDateTime);
|
||||
if (afterTimestamp && receivedAt && receivedAt < afterTimestamp) {
|
||||
@@ -148,20 +190,25 @@
|
||||
const subject = normalizeText(message?.subject);
|
||||
const preview = String(message?.bodyPreview || '');
|
||||
const combinedText = [subject, sender, preview].filter(Boolean).join(' ');
|
||||
const code = extractVerificationCode(combinedText);
|
||||
const code = extractVerificationCode(combinedText, {
|
||||
codePatterns: filters.codePatterns,
|
||||
});
|
||||
const excludedCodes = new Set((filters.excludeCodes || []).filter(Boolean));
|
||||
if (code && excludedCodes.has(code)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const senderMatch = senderFilters.length === 0
|
||||
? true
|
||||
: senderFilters.some((item) => sender.includes(item) || normalizeText(preview).includes(item));
|
||||
const subjectMatch = subjectFilters.length === 0
|
||||
? true
|
||||
: subjectFilters.some((item) => subject.includes(item) || normalizeText(preview).includes(item));
|
||||
const senderMatch = hasSenderFilters
|
||||
? senderFilters.some((item) => sender.includes(item) || normalizeText(preview).includes(item))
|
||||
: false;
|
||||
const subjectMatch = hasSubjectFilters
|
||||
? subjectFilters.some((item) => subject.includes(item) || normalizeText(preview).includes(item))
|
||||
: false;
|
||||
const keywordMatch = hasKeywordHints
|
||||
? requiredKeywords.some((item) => normalizeText(combinedText).includes(item))
|
||||
: false;
|
||||
|
||||
if (!senderMatch && !subjectMatch) {
|
||||
if ((hasSenderFilters || hasSubjectFilters || hasKeywordHints) && !senderMatch && !subjectMatch && !keywordMatch) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user