101 lines
2.5 KiBLFS
JavaScript
101 lines
2.5 KiBLFS
JavaScript
#!/usr/bin/env node
|
|
|
|
import { google } from 'googleapis';
|
|
import minimist from 'minimist';
|
|
import { getAuthClient, parseAccountArg } from './auth/auth-utils.js';
|
|
import { logAction } from './action-logger.js';
|
|
|
|
async function searchEmails(args) {
|
|
// Default to showing 10 results
|
|
const maxResults = args.limit || args.max || 10;
|
|
const query = args.query || args.q || '';
|
|
|
|
// Get authenticated client for specified account (or default)
|
|
const accountId = parseAccountArg(args);
|
|
const oauth2Client = await getAuthClient(accountId);
|
|
|
|
const gmail = google.gmail({ version: 'v1', auth: oauth2Client });
|
|
|
|
// Search messages
|
|
const searchResult = await gmail.users.messages.list({
|
|
userId: 'me',
|
|
q: query,
|
|
maxResults: maxResults
|
|
});
|
|
|
|
if (!searchResult.data.messages) {
|
|
return {
|
|
success: true,
|
|
count: 0,
|
|
messages: [],
|
|
query: query
|
|
};
|
|
}
|
|
|
|
// Get full details for each message
|
|
const messages = await Promise.all(
|
|
searchResult.data.messages.map(async (msg) => {
|
|
const details = await gmail.users.messages.get({
|
|
userId: 'me',
|
|
id: msg.id,
|
|
format: 'metadata',
|
|
metadataHeaders: ['From', 'To', 'Subject', 'Date']
|
|
});
|
|
|
|
const headers = details.data.payload.headers;
|
|
const getHeader = (name) => headers.find(h => h.name === name)?.value || '';
|
|
|
|
return {
|
|
id: msg.id,
|
|
threadId: msg.threadId,
|
|
from: getHeader('From'),
|
|
to: getHeader('To'),
|
|
subject: getHeader('Subject'),
|
|
date: getHeader('Date'),
|
|
snippet: details.data.snippet,
|
|
labels: details.data.labelIds || []
|
|
};
|
|
})
|
|
);
|
|
|
|
return {
|
|
success: true,
|
|
count: messages.length,
|
|
total: searchResult.data.resultSizeEstimate,
|
|
query: query,
|
|
messages: messages,
|
|
account: accountId || 'default'
|
|
};
|
|
}
|
|
|
|
// Main execution
|
|
const args = minimist(process.argv.slice(2));
|
|
const startTime = Date.now();
|
|
|
|
searchEmails(args)
|
|
.then(result => {
|
|
// Log the action
|
|
logAction('search', args, result, {
|
|
account: result.account,
|
|
duration: Date.now() - startTime
|
|
});
|
|
|
|
console.log(JSON.stringify(result, null, 2));
|
|
})
|
|
.catch(error => {
|
|
const errorResult = {
|
|
success: false,
|
|
error: error.message
|
|
};
|
|
|
|
// Log the failed action
|
|
logAction('search', args, errorResult, {
|
|
account: args.account || 'default',
|
|
duration: Date.now() - startTime,
|
|
failed: true
|
|
});
|
|
|
|
console.error(JSON.stringify(errorResult, null, 2));
|
|
process.exit(1);
|
|
});
|