Files
dcrouter/ts/mail/core/classes.rulemanager.ts

195 lines
6.8 KiB
TypeScript
Raw Normal View History

2025-05-08 01:13:54 +00:00
import * as plugins from '../../plugins.js';
import { EmailService } from '../services/classes.emailservice.js';
import { logger } from '../../logger.js';
import { Email, type IEmailOptions } from './classes.email.js';
2024-02-15 20:30:38 +01:00
export class RuleManager {
2024-02-16 13:28:40 +01:00
public emailRef: EmailService;
public smartruleInstance = new plugins.smartrule.SmartRule<Email>();
2024-02-15 20:30:38 +01:00
2024-02-16 13:28:40 +01:00
constructor(emailRefArg: EmailService) {
2024-02-15 20:30:38 +01:00
this.emailRef = emailRefArg;
2025-05-21 00:12:49 +00:00
// Register handler for incoming emails if email server is enabled
if (this.emailRef.unifiedEmailServer) {
this.setupIncomingHandler();
}
2024-02-15 20:30:38 +01:00
}
/**
2025-05-21 00:12:49 +00:00
* Set up handler for incoming emails via the UnifiedEmailServer
*/
2025-05-21 00:12:49 +00:00
private setupIncomingHandler() {
// Use UnifiedEmailServer events for incoming emails
const incomingDir = './received';
2025-05-21 00:12:49 +00:00
// The UnifiedEmailServer raises events for incoming emails
// For backward compatibility, also watch the directory
this.watchIncomingEmails(incomingDir);
}
2024-02-15 20:30:38 +01:00
/**
* 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
2025-05-21 00:12:49 +00:00
// 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);
2025-05-21 00:12:49 +00:00
await this.handleIncomingEmail(filePath);
}
});
*/
2025-05-21 00:12:49 +00:00
// Set up event listener on UnifiedEmailServer if available
if (this.emailRef.unifiedEmailServer) {
this.emailRef.unifiedEmailServer.on('emailProcessed', (email: Email, mode, rule) => {
2025-05-21 00:12:49 +00:00
// Process email through rule system
this.smartruleInstance.makeDecision(email);
2025-05-21 00:12:49 +00:00
});
}
}
2024-02-15 20:30:38 +01:00
/**
2025-05-21 00:12:49 +00:00
* Handle incoming email received via email server
*/
2025-05-21 00:12:49 +00:00
public async handleIncomingEmail(emailPath: string) {
try {
2025-05-21 00:12:49 +00:00
// 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 an Email object with the parsed content
const fromAddress = Array.isArray(parsedContent.from)
? parsedContent.from[0]?.text || 'unknown@example.com'
: parsedContent.from?.text || 'unknown@example.com';
const toAddress = Array.isArray(parsedContent.to)
? parsedContent.to[0]?.text || 'unknown@example.com'
: parsedContent.to?.text || 'unknown@example.com';
const fetchedEmail = new Email({
from: fromAddress,
to: toAddress,
2025-05-21 00:12:49 +00:00
subject: parsedContent.subject || '',
text: parsedContent.text || '',
html: parsedContent.html || undefined
2025-05-21 00:12:49 +00:00
});
console.log('=======================');
2025-05-21 00:12:49 +00:00
console.log('Received a mail:');
console.log(`From: ${fetchedEmail.from}`);
console.log(`Subject: ${fetchedEmail.subject}`);
console.log('^^^^^^^^^^^^^^^^^^^^^^^');
2024-02-15 20:30:38 +01:00
logger.log(
'info',
`email from ${fetchedEmail.from} with subject '${fetchedEmail.subject}'`,
{
eventType: 'receivedEmail',
2025-05-21 00:12:49 +00:00
provider: 'unified',
email: {
from: fetchedEmail.from,
subject: fetchedEmail.subject,
},
}
);
2024-02-15 20:30:38 +01:00
// Process with rules
this.smartruleInstance.makeDecision(fetchedEmail);
} catch (error) {
2025-05-21 00:12:49 +00:00
logger.log('error', `Failed to process incoming email: ${error.message}`, {
eventType: 'emailError',
2025-05-21 00:12:49 +00:00
provider: 'unified',
error: error.message
});
}
2024-02-15 20:30:38 +01:00
}
public async init() {
// Setup email rules
2024-02-15 20:30:38 +01:00
await this.createForwards();
}
/**
* creates the default forwards
*/
public async createForwards() {
const forwards: { originalToAddress: string[]; forwardedToAddress: string[] }[] = [];
2024-02-15 20:30:38 +01:00
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 (emailArg: Email) => {
2024-02-15 20:30:38 +01:00
const matched = forward.originalToAddress.reduce<boolean>((prevValue, currentValue) => {
return emailArg.to.some(to => to.includes(currentValue)) || prevValue;
2024-02-15 20:30:38 +01:00
}, false);
if (matched) {
console.log('Forward rule matched');
console.log(forward);
return 'apply-continue';
} else {
return 'continue';
}
},
async (emailArg: Email) => {
2024-02-15 20:30:38 +01:00
forward.forwardedToAddress.map(async (toArg) => {
const forwardedEmail = new Email({
from: 'forwarder@mail.lossless.one',
to: toArg,
subject: `Forwarded mail for '${emailArg.to.join(', ')}'`,
html:
2024-02-15 20:30:38 +01:00
`
<div style="background: #CCC; padding: 10px; border-radius: 3px;">
<div><b>Original Sender:</b></div>
<div>${emailArg.from}</div>
2024-02-15 20:30:38 +01:00
<div><b>Original Recipient:</b></div>
<div>${emailArg.to.join(', ')}</div>
2024-02-15 20:30:38 +01:00
<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>${emailArg.getSubject()}</div>
2024-02-15 20:30:38 +01:00
<div><b>The original body can be found below.</b></div>
</div>
` + emailArg.getBody(true),
text: `Forwarded mail from ${emailArg.from} to ${emailArg.to.join(', ')}\n\n${emailArg.getBody()}`,
attachments: emailArg.attachments
2024-02-15 20:30:38 +01:00
});
// Use the EmailService's sendEmail method to send with the appropriate provider
await this.emailRef.sendEmail(forwardedEmail);
2024-02-15 20:30:38 +01:00
console.log(`forwarded mail to ${toArg}`);
logger.log(
'info',
`email from ${emailArg.from} to ${toArg} with subject '${emailArg.getSubject()}'`,
2024-02-15 20:30:38 +01:00
{
eventType: 'forwardedEmail',
email: {
from: emailArg.from,
to: emailArg.to.join(', '),
2024-02-15 20:30:38 +01:00
forwardedTo: toArg,
subject: emailArg.subject,
2024-02-15 20:30:38 +01:00
},
}
);
});
}
);
}
}
}