Add multi-account OAuth, Obsidian integration, product assets, and test tooling
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
174
src/imap.ts
174
src/imap.ts
@@ -1,6 +1,6 @@
|
||||
import { ImapFlow } from 'imapflow';
|
||||
|
||||
export type Account = 'yahoo' | 'fetcherpay' | 'garfield' | 'sales' | 'leads' | 'founder';
|
||||
export type Account = 'yahoo' | 'fetcherpay' | 'garfield' | 'sales' | 'leads' | 'founder' | 'gmail';
|
||||
|
||||
const FETCHERPAY_IMAP_HOST = process.env['FETCHERPAY_IMAP_HOST'] ?? 'mail.fetcherpay.com';
|
||||
const FETCHERPAY_IMAP_PORT = parseInt(process.env['FETCHERPAY_IMAP_PORT'] ?? '30993');
|
||||
@@ -42,6 +42,16 @@ function getConfig(account: Account = 'yahoo') {
|
||||
process.env['FOUNDER_EMAIL'] as string,
|
||||
process.env['FOUNDER_PASSWORD'] as string,
|
||||
);
|
||||
case 'gmail':
|
||||
return {
|
||||
host: 'imap.gmail.com',
|
||||
port: 993,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: process.env['GMAIL_EMAIL'] as string,
|
||||
pass: process.env['GMAIL_APP_PASSWORD'] as string,
|
||||
},
|
||||
};
|
||||
default:
|
||||
return {
|
||||
host: 'imap.mail.yahoo.com',
|
||||
@@ -73,6 +83,7 @@ export interface MessageSummary {
|
||||
date: string;
|
||||
seen: boolean;
|
||||
size: number;
|
||||
folder: string;
|
||||
}
|
||||
|
||||
export interface FullMessage {
|
||||
@@ -86,48 +97,142 @@ export interface FullMessage {
|
||||
seen: boolean;
|
||||
}
|
||||
|
||||
export async function searchMessages(query: string, maxResults = 20, account: Account = 'yahoo'): Promise<MessageSummary[]> {
|
||||
return withClient(account, async (client) => {
|
||||
await client.mailboxOpen('INBOX');
|
||||
function parseSearchCriteria(query: string): object {
|
||||
const trimmed = query.trim();
|
||||
if (!trimmed) return { all: true };
|
||||
|
||||
const criteria = query
|
||||
? { or: [{ subject: query }, { from: query }] }
|
||||
: { all: true };
|
||||
// Parse quoted and unquoted tokens like from:x, subject:y, to:z
|
||||
const tokens: { key: string; value: string }[] = [];
|
||||
const regex = /(\w+):("([^"]*)"|([^\s]+))/g;
|
||||
let m: RegExpExecArray | null;
|
||||
let lastIndex = 0;
|
||||
while ((m = regex.exec(trimmed)) !== null) {
|
||||
tokens.push({ key: m[1].toLowerCase(), value: m[3] ?? m[4] });
|
||||
lastIndex = regex.lastIndex;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const uids = await client.search(criteria as any, { uid: true });
|
||||
const uidList: number[] = Array.isArray(uids) ? uids : [];
|
||||
const recentUids = uidList.slice(-maxResults).reverse();
|
||||
const remaining = trimmed.slice(lastIndex).trim();
|
||||
if (remaining) {
|
||||
tokens.push({ key: 'keyword', value: remaining });
|
||||
}
|
||||
|
||||
if (recentUids.length === 0) return [];
|
||||
if (tokens.length === 0) {
|
||||
return { or: [{ subject: trimmed }, { from: trimmed }] };
|
||||
}
|
||||
|
||||
const messages: MessageSummary[] = [];
|
||||
for await (const msg of client.fetch(recentUids, {
|
||||
envelope: true,
|
||||
flags: true,
|
||||
size: true,
|
||||
}, { uid: true })) {
|
||||
const env = msg.envelope;
|
||||
messages.push({
|
||||
uid: msg.uid,
|
||||
messageId: env?.messageId ?? '',
|
||||
subject: env?.subject ?? '(no subject)',
|
||||
from: env?.from?.[0]
|
||||
? `${env.from[0].name ?? ''} <${env.from[0].address ?? ''}>`.trim()
|
||||
: '',
|
||||
date: env?.date?.toISOString() ?? '',
|
||||
seen: msg.flags?.has('\\Seen') ?? false,
|
||||
size: msg.size ?? 0,
|
||||
});
|
||||
const parts: object[] = [];
|
||||
for (const t of tokens) {
|
||||
switch (t.key) {
|
||||
case 'from':
|
||||
parts.push({ from: t.value });
|
||||
break;
|
||||
case 'subject':
|
||||
parts.push({ subject: t.value });
|
||||
break;
|
||||
case 'to':
|
||||
parts.push({ to: t.value });
|
||||
break;
|
||||
case 'after':
|
||||
case 'since': {
|
||||
const d = new Date(t.value);
|
||||
if (!isNaN(d.getTime())) parts.push({ since: d });
|
||||
break;
|
||||
}
|
||||
case 'before': {
|
||||
const d = new Date(t.value);
|
||||
if (!isNaN(d.getTime())) parts.push({ before: d });
|
||||
break;
|
||||
}
|
||||
case 'keyword':
|
||||
default:
|
||||
parts.push({ or: [{ subject: t.value }, { from: t.value }] });
|
||||
break;
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
if (parts.length === 1) return parts[0];
|
||||
return { and: parts };
|
||||
}
|
||||
|
||||
async function searchInFolder(
|
||||
client: ImapFlow,
|
||||
folder: string,
|
||||
query: string,
|
||||
maxResults: number,
|
||||
account: Account
|
||||
): Promise<MessageSummary[]> {
|
||||
await client.mailboxOpen(folder);
|
||||
|
||||
const criteria = account === 'gmail'
|
||||
? { gmailraw: query }
|
||||
: parseSearchCriteria(query);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const uids = await client.search(criteria as any, { uid: true });
|
||||
const uidList: number[] = Array.isArray(uids) ? uids : [];
|
||||
const recentUids = uidList.slice(-maxResults).reverse();
|
||||
|
||||
if (recentUids.length === 0) return [];
|
||||
|
||||
const messages: MessageSummary[] = [];
|
||||
for await (const msg of client.fetch(recentUids, {
|
||||
envelope: true,
|
||||
flags: true,
|
||||
size: true,
|
||||
}, { uid: true })) {
|
||||
const env = msg.envelope;
|
||||
messages.push({
|
||||
uid: msg.uid,
|
||||
messageId: env?.messageId ?? '',
|
||||
subject: env?.subject ?? '(no subject)',
|
||||
from: env?.from?.[0]
|
||||
? `${env.from[0].name ?? ''} <${env.from[0].address ?? ''}>`.trim()
|
||||
: '',
|
||||
date: env?.date?.toISOString() ?? '',
|
||||
seen: msg.flags?.has('\\Seen') ?? false,
|
||||
size: msg.size ?? 0,
|
||||
folder,
|
||||
});
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
export async function searchMessages(
|
||||
query: string,
|
||||
maxResults = 20,
|
||||
account: Account = 'yahoo',
|
||||
folder?: string
|
||||
): Promise<MessageSummary[]> {
|
||||
return withClient(account, async (client) => {
|
||||
const foldersToSearch: string[] = [];
|
||||
|
||||
if (folder) {
|
||||
foldersToSearch.push(folder);
|
||||
} else if (account === 'gmail') {
|
||||
foldersToSearch.push('INBOX');
|
||||
} else {
|
||||
foldersToSearch.push('INBOX');
|
||||
}
|
||||
|
||||
for (const f of foldersToSearch) {
|
||||
const results = await searchInFolder(client, f, query, maxResults, account);
|
||||
if (results.length > 0) return results;
|
||||
}
|
||||
|
||||
// Fallback for Gmail: search All Mail if INBOX was empty
|
||||
if (account === 'gmail' && !folder) {
|
||||
const allMailResults = await searchInFolder(client, '[Gmail]/All Mail', query, maxResults, account);
|
||||
if (allMailResults.length > 0) return allMailResults;
|
||||
}
|
||||
|
||||
return [];
|
||||
});
|
||||
}
|
||||
|
||||
export async function readMessage(uid: number, account: Account = 'yahoo'): Promise<FullMessage> {
|
||||
export async function readMessage(uid: number, account: Account = 'yahoo', folder = 'INBOX'): Promise<FullMessage> {
|
||||
return withClient(account, async (client) => {
|
||||
console.log(`[imap] readMessage uid=${uid} account=${account}`);
|
||||
await client.mailboxOpen('INBOX');
|
||||
console.log(`[imap] readMessage uid=${uid} account=${account} folder=${folder}`);
|
||||
await client.mailboxOpen(folder);
|
||||
console.log(`[imap] mailbox opened, fetching uid=${uid}`);
|
||||
|
||||
let result: FullMessage | null = null;
|
||||
@@ -195,6 +300,7 @@ export async function getProfile(account: Account = 'yahoo'): Promise<{ email: s
|
||||
sales: process.env['SALES_EMAIL'] ?? '',
|
||||
leads: process.env['LEADS_EMAIL'] ?? '',
|
||||
founder: process.env['FOUNDER_EMAIL'] ?? '',
|
||||
gmail: process.env['GMAIL_EMAIL'] ?? '',
|
||||
};
|
||||
const email = emailMap[account] ?? '';
|
||||
return { email, name: email.split('@')[0], account };
|
||||
|
||||
Reference in New Issue
Block a user