47 lines
1.5 KiB
JavaScript
47 lines
1.5 KiB
JavaScript
exports.register = function () {
|
|
const plugin = this;
|
|
const MessageService = require("../../services/MessageService");
|
|
|
|
plugin.store_message = async function (next, connection) {
|
|
const transaction = connection.transaction;
|
|
if (!transaction) return next();
|
|
|
|
try {
|
|
// Get the full message body as a string
|
|
let body = '';
|
|
|
|
if (transaction.message_stream) {
|
|
// Convert message stream to string
|
|
body = transaction.message_stream.toString();
|
|
}
|
|
|
|
// Get headers safely
|
|
const headers = transaction.header ? transaction.header.headers_decoded : {};
|
|
const subject = transaction.header ? transaction.header.get('subject') : '';
|
|
|
|
const messageData = {
|
|
from: transaction.mail_from.address(),
|
|
to: transaction.rcpt_to.map((addr) => addr.address()).join(", "),
|
|
subject: subject,
|
|
body: body,
|
|
headers: headers,
|
|
};
|
|
|
|
// Debug logging
|
|
connection.logdebug(plugin, 'Message Data:');
|
|
connection.logdebug(plugin, `From: ${messageData.from}`);
|
|
connection.logdebug(plugin, `To: ${messageData.to}`);
|
|
connection.logdebug(plugin, `Subject: ${messageData.subject}`);
|
|
connection.logdebug(plugin, `Body length: ${body.length}`);
|
|
|
|
await MessageService.store(messageData);
|
|
next();
|
|
} catch (error) {
|
|
connection.logerror(plugin, `Failed to store message: ${error.message}`);
|
|
next();
|
|
}
|
|
};
|
|
|
|
// Register hooks - use data_post instead of queue
|
|
plugin.register_hook('data_post', 'store_message');
|
|
}; |