dcrouter/ts/mail/core/classes.rulemanager.ts
2025-05-21 00:12:49 +00:00

200 lines
7.3 KiB
TypeScript

import * as plugins from '../../plugins.js';
import { EmailService } from '../services/classes.emailservice.js';
import { logger } from '../../logger.js';
export class RuleManager {
public emailRef: EmailService;
public smartruleInstance = new plugins.smartrule.SmartRule<
plugins.smartmail.Smartmail<any>
>();
constructor(emailRefArg: EmailService) {
this.emailRef = emailRefArg;
// Register handler for incoming emails if email server is enabled
if (this.emailRef.unifiedEmailServer) {
this.setupIncomingHandler();
}
}
/**
* Set up handler for incoming emails via the UnifiedEmailServer
*/
private setupIncomingHandler() {
// Use UnifiedEmailServer events for incoming emails
const incomingDir = './received';
// The UnifiedEmailServer raises events for incoming emails
// For backward compatibility, also watch the directory
this.watchIncomingEmails(incomingDir);
}
/**
* Watch directory for incoming emails (conceptual implementation)
*/
private watchIncomingEmails(directory: string) {
console.log(`Watching for incoming emails in: ${directory}`);
// Conceptual - in a real implementation, set up proper file watching
// or use UnifiedEmailServer events for incoming emails
/*
// Example using a file watcher:
const watcher = plugins.fs.watch(directory, async (eventType, filename) => {
if (eventType === 'rename' && filename.endsWith('.eml')) {
const filePath = plugins.path.join(directory, filename);
await this.handleIncomingEmail(filePath);
}
});
*/
// Set up event listener on UnifiedEmailServer if available
if (this.emailRef.unifiedEmailServer) {
this.emailRef.unifiedEmailServer.on('emailProcessed', (email, mode, rule) => {
// Process email through rule system
// Convert Email to Smartmail format
// Convert Email object to Smartmail format
const smartmail = new plugins.smartmail.Smartmail({
// Use standard fields
from: email.from,
subject: email.subject || '',
body: email.text || email.html || ''
});
// Process with rules
this.smartruleInstance.makeDecision(smartmail);
});
}
}
/**
* Handle incoming email received via email server
*/
public async handleIncomingEmail(emailPath: string) {
try {
// Process the email file using direct file access or access through UnifiedEmailServer
// For compatibility with existing code, we'll make a basic assumption about structure
const emailContent = await plugins.fs.promises.readFile(emailPath, 'utf8');
// Parse the email content into proper format
const parsedContent = await plugins.mailparser.simpleParser(emailContent);
// Create a Smartmail object with the parsed content
const fetchedSmartmail = new plugins.smartmail.Smartmail({
// Use standardized fields that are always available
body: parsedContent.text || parsedContent.html || '',
subject: parsedContent.subject || '',
// Use a default from address if not present
from: parsedContent.from?.text || 'unknown@example.com'
});
console.log('=======================');
console.log('Received a mail:');
console.log(`From: ${fetchedSmartmail.options?.from || 'unknown'}`);
console.log(`Subject: ${fetchedSmartmail.options?.subject || 'no subject'}`);
console.log('^^^^^^^^^^^^^^^^^^^^^^^');
logger.log(
'info',
`email from ${fetchedSmartmail.options?.from || 'unknown'} with subject '${fetchedSmartmail.options?.subject || 'no subject'}'`,
{
eventType: 'receivedEmail',
provider: 'unified',
email: {
from: fetchedSmartmail.options?.from || 'unknown',
subject: fetchedSmartmail.options?.subject || 'no subject',
},
}
);
// Process with rules
this.smartruleInstance.makeDecision(fetchedSmartmail);
} catch (error) {
logger.log('error', `Failed to process incoming email: ${error.message}`, {
eventType: 'emailError',
provider: 'unified',
error: error.message
});
}
}
public async init() {
// Setup email rules
await this.createForwards();
}
/**
* creates the default forwards
*/
public async createForwards() {
const forwards: { originalToAddress: string[]; forwardedToAddress: string[] }[] = [];
console.log(`${forwards.length} forward rules configured:`);
for (const forward of forwards) {
console.log(forward);
}
for (const forward of forwards) {
this.smartruleInstance.createRule(
10,
async (smartmailArg) => {
const matched = forward.originalToAddress.reduce<boolean>((prevValue, currentValue) => {
return smartmailArg.options.creationObjectRef.To.includes(currentValue) || prevValue;
}, false);
if (matched) {
console.log('Forward rule matched');
console.log(forward);
return 'apply-continue';
} else {
return 'continue';
}
},
async (smartmailArg: plugins.smartmail.Smartmail<any>) => {
forward.forwardedToAddress.map(async (toArg) => {
const forwardedSmartMail = new plugins.smartmail.Smartmail({
body:
`
<div style="background: #CCC; padding: 10px; border-radius: 3px;">
<div><b>Original Sender:</b></div>
<div>${smartmailArg.options.creationObjectRef.From}</div>
<div><b>Original Recipient:</b></div>
<div>${smartmailArg.options.creationObjectRef.To}</div>
<div><b>Forwarded to:</b></div>
<div>${forward.forwardedToAddress.reduce<string>((pVal, cVal) => {
return `${pVal ? pVal + ', ' : ''}${cVal}`;
}, null)}</div>
<div><b>Subject:</b></div>
<div>${smartmailArg.getSubject()}</div>
<div><b>The original body can be found below.</b></div>
</div>
` + smartmailArg.getBody(),
from: 'forwarder@mail.lossless.one',
subject: `Forwarded mail for '${smartmailArg.options.creationObjectRef.To}'`,
});
for (const attachment of smartmailArg.attachments) {
forwardedSmartMail.addAttachment(attachment);
}
// Use the EmailService's sendEmail method to send with the appropriate provider
await this.emailRef.sendEmail(forwardedSmartMail, toArg);
console.log(`forwarded mail to ${toArg}`);
logger.log(
'info',
`email from ${
smartmailArg.options.creationObjectRef.From
} to ${toArg} with subject '${smartmailArg.getSubject()}'`,
{
eventType: 'forwardedEmail',
email: {
from: smartmailArg.options.creationObjectRef.From,
to: smartmailArg.options.creationObjectRef.To,
forwardedTo: toArg,
subject: smartmailArg.options.creationObjectRef.Subject,
},
}
);
});
}
);
}
}
}