n8n-nodes-imap-ai
Version:
Simplified IMAP node for n8n with AI-agent support. Clean and modular email and mailbox management for automation workflows.
179 lines • 6.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParameterValidator = exports.ImapConnectionManager = exports.SearchQueryParser = void 0;
class SearchQueryParser {
static parse(query) {
if (!query.trim()) {
return { all: true };
}
const trimmedQuery = query.trim().toLowerCase();
if (trimmedQuery.includes(' and ') || trimmedQuery.includes(' & ')) {
return this.parseMultipleCriteria(query, 'AND');
}
if (trimmedQuery.includes(' or ') || trimmedQuery.includes(' | ')) {
return this.parseMultipleCriteria(query, 'OR');
}
if (trimmedQuery.startsWith('from:')) {
return { from: query.substring(5).trim() };
}
if (trimmedQuery.startsWith('to:')) {
return { to: query.substring(3).trim() };
}
if (trimmedQuery.startsWith('subject:')) {
return { subject: query.substring(8).trim() };
}
if (trimmedQuery.startsWith('body:')) {
return { body: query.substring(5).trim() };
}
if (trimmedQuery.startsWith('text:')) {
return { text: query.substring(5).trim() };
}
if (trimmedQuery.startsWith('since:')) {
const dateStr = query.substring(6).trim();
const date = this.parseDate(dateStr);
return date ? { since: date } : { subject: query };
}
if (trimmedQuery.startsWith('before:')) {
const dateStr = query.substring(7).trim();
const date = this.parseDate(dateStr);
return date ? { before: date } : { subject: query };
}
if (trimmedQuery === 'unread' || trimmedQuery === 'unseen') {
return { unseen: true };
}
if (trimmedQuery === 'read' || trimmedQuery === 'seen') {
return { seen: true };
}
if (trimmedQuery === 'flagged' || trimmedQuery === 'important') {
return { flagged: true };
}
if (trimmedQuery === 'unflagged') {
return { unflagged: true };
}
if (trimmedQuery === 'answered' || trimmedQuery === 'replied') {
return { answered: true };
}
if (trimmedQuery === 'unanswered') {
return { unanswered: true };
}
if (trimmedQuery.startsWith('larger:')) {
const size = Number.parseInt(query.substring(7).trim());
return !isNaN(size) ? { larger: size } : { subject: query };
}
if (trimmedQuery.startsWith('smaller:')) {
const size = Number.parseInt(query.substring(8).trim());
return !isNaN(size) ? { smaller: size } : { subject: query };
}
return { text: query.trim() };
}
static parseMultipleCriteria(query, operator) {
const separator = operator === 'AND'
? query.includes(' and ')
? ' and '
: ' & '
: query.includes(' or ')
? ' or '
: ' | ';
const parts = query.split(separator).map((part) => part.trim());
const criteria = parts.map((part) => this.parse(part));
if (operator === 'AND') {
return { and: criteria };
}
else {
return { or: criteria };
}
}
static parseDate(dateStr) {
const cleanDateStr = dateStr.trim();
if (cleanDateStr === 'today') {
return new Date();
}
if (cleanDateStr === 'yesterday') {
const date = new Date();
date.setDate(date.getDate() - 1);
return date;
}
if (cleanDateStr.endsWith('h') || cleanDateStr.endsWith('hours')) {
const hours = Number.parseInt(cleanDateStr);
if (!isNaN(hours)) {
const date = new Date();
date.setTime(date.getTime() - (hours * 60 * 60 * 1000));
return date;
}
}
if (cleanDateStr.endsWith('m') || cleanDateStr.endsWith('min') || cleanDateStr.endsWith('minutes')) {
const minutes = Number.parseInt(cleanDateStr);
if (!isNaN(minutes)) {
const date = new Date();
date.setTime(date.getTime() - (minutes * 60 * 1000));
return date;
}
}
if (cleanDateStr.endsWith('d') || cleanDateStr.endsWith('days')) {
const days = Number.parseInt(cleanDateStr);
if (!isNaN(days)) {
const date = new Date();
date.setDate(date.getDate() - days);
return date;
}
}
const date = new Date(cleanDateStr);
return isNaN(date.getTime()) ? null : date;
}
}
exports.SearchQueryParser = SearchQueryParser;
class ImapConnectionManager {
static async ensureConnection(client) {
if (!client.usable) {
await client.connect();
}
}
static async safeClose(client) {
try {
if (client.usable) {
await client.logout();
}
}
catch (error) {
}
}
}
exports.ImapConnectionManager = ImapConnectionManager;
class ParameterValidator {
static validateUid(uid) {
if (!uid || uid.trim() === '') {
throw new Error('Email UID is required and cannot be empty');
}
}
static validateMailbox(mailbox) {
const mailboxName = this.extractMailboxName(mailbox);
if (!mailboxName || mailboxName.trim() === '') {
throw new Error('Folder name is required and cannot be empty');
}
}
static extractMailboxName(mailbox) {
if (typeof mailbox === 'string') {
return mailbox;
}
if (mailbox && typeof mailbox === 'object' && mailbox.value) {
return mailbox.value;
}
throw new Error('Invalid mailbox parameter format');
}
static validateLimit(limit) {
if (limit < 1) {
return 50;
}
if (limit > 1000) {
return 1000;
}
return limit;
}
static validateSearchQuery(query) {
if (query && query.length > 1000) {
throw new Error('Search query is too long (max 1000 characters)');
}
}
}
exports.ParameterValidator = ParameterValidator;
//# sourceMappingURL=helpers.js.map