diff --git a/lib/api/messages.js b/lib/api/messages.js index eaf51ac9..eab9a579 100644 --- a/lib/api/messages.js +++ b/lib/api/messages.js @@ -1,5 +1,6 @@ 'use strict'; +const config = require('wild-config'); const log = require('npmlog'); const Joi = require('../joi'); const MongoPaging = require('mongo-cursor-pagination-node6'); @@ -10,8 +11,16 @@ const consts = require('../consts'); const libbase64 = require('libbase64'); const libqp = require('libqp'); const forward = require('../forward'); +const Maildropper = require('../maildropper'); module.exports = (db, server, messageHandler) => { + let maildrop = new Maildropper({ + db, + zone: config.sender.zone, + collection: config.sender.collection, + gfs: config.sender.gfs + }); + /** * @api {get} /users/:user/mailboxes/:mailbox/messages List messages in a Mailbox * @apiName GetMessages @@ -2167,6 +2176,8 @@ module.exports = (db, server, messageHandler) => { } let forwardData = { + db, + maildrop, parentId: messageData._id, sender: messageData.meta.from, recipient: messageData.meta.to, diff --git a/lib/autoreply.js b/lib/autoreply.js index fe8e27db..1ecadfd8 100644 --- a/lib/autoreply.js +++ b/lib/autoreply.js @@ -2,10 +2,8 @@ const MailComposer = require('nodemailer/lib/mail-composer'); const MessageSplitter = require('./message-splitter'); -const db = require('./db'); const consts = require('./consts'); const errors = require('./errors'); -const maildrop = require('./maildrop'); module.exports = (options, callback) => { if (!options.sender || /mailer-daemon|no-?reply/gi.test(options.sender)) { @@ -13,7 +11,7 @@ module.exports = (options, callback) => { } let curtime = new Date(); - db.database.collection('autoreplies').findOne( + options.db.database.collection('autoreplies').findOne( { user: options.userData._id, start: { @@ -61,7 +59,7 @@ module.exports = (options, callback) => { return callback(null, false); } - db.redis + options.db.redis .multi() // delete all old entries .zremrangebyscore('war:' + autoreply._id, '-inf', Date.now() - consts.MAX_AUTOREPLY_INTERVAL) @@ -113,7 +111,7 @@ module.exports = (options, callback) => { }; let compiler = new MailComposer(data); - let message = maildrop( + let message = options.maildrop( { parentId: options.parentId, reason: 'autoreply', @@ -128,7 +126,7 @@ module.exports = (options, callback) => { } return callback(err, ...args); } - db.database.collection('messagelog').insertOne( + options.db.database.collection('messagelog').insertOne( { id: args[0].id, messageId: args[0].messageId, diff --git a/lib/filter-handler.js b/lib/filter-handler.js index 7ae859d0..da06d398 100644 --- a/lib/filter-handler.js +++ b/lib/filter-handler.js @@ -2,10 +2,10 @@ const log = require('npmlog'); const ObjectID = require('mongodb').ObjectID; -const db = require('./db'); const forward = require('./forward'); const autoreply = require('./autoreply'); const plugins = require('./plugins'); +const Maildropper = require('./maildropper'); const defaultSpamHeaderKeys = [ { @@ -35,13 +35,19 @@ const defaultSpamHeaderKeys = [ class FilterHandler { constructor(options) { - this.database = options.database; - this.users = options.users || options.database; - this.redis = options.redis; + this.db = options.db; this.messageHandler = options.messageHandler; this.spamChecks = options.spamChecks || prepareSpamChecks(defaultSpamHeaderKeys); this.spamHeaderKeys = options.spamHeaderKeys || this.spamChecks.map(check => check.key); + + this.senderEnabled = options.sender.enabled; + this.maildrop = new Maildropper({ + db: this.db, + zone: options.sender.zone, + collection: options.sender.collection, + gfs: options.sender.gfs + }); } getUserData(address, callback) { @@ -83,7 +89,7 @@ class FilterHandler { }; if (collection === 'users') { - return db.users.collection('users').findOne( + return this.db.users.collection('users').findOne( query, { fields @@ -92,14 +98,14 @@ class FilterHandler { ); } - return db.users.collection('addresses').findOne(query, (err, addressData) => { + return this.db.users.collection('addresses').findOne(query, (err, addressData) => { if (err) { return callback(err); } if (!addressData) { return callback(null, false); } - return db.users.collection('users').findOne( + return this.db.users.collection('users').findOne( { _id: addressData.user }, @@ -215,7 +221,7 @@ class FilterHandler { } } - db.database + this.db.database .collection('filters') .find({ user: userData._id @@ -327,6 +333,10 @@ class FilterHandler { }; let forwardMessage = done => { + if (!this.senderEnabled) { + return setImmediate(done); + } + if (userData.forward && !filterActions.get('delete')) { // forward to default recipient only if the message is not deleted (Array.isArray(userData.forward) ? userData.forward : [].concat(userData.forward || [])).forEach(forward => { @@ -364,6 +374,9 @@ class FilterHandler { encryptMessage(userData.encryptForwarded && userData.pubKey, () => { forward( { + db: this.db, + maildrop: this.maildrop, + parentId: prepared.id, userData, sender, @@ -387,6 +400,10 @@ class FilterHandler { }; let sendAutoreply = done => { + if (!this.senderEnabled) { + return setImmediate(done); + } + // never reply to messages marked as spam if (!sender || !userData.autoreply || filterActions.get('spam')) { return setImmediate(done); @@ -394,6 +411,9 @@ class FilterHandler { autoreply( { + db: this.db, + maildrop: this.maildrop, + parentId: prepared.id, userData, sender, diff --git a/lib/forward.js b/lib/forward.js index a1e187c6..c26d6ccc 100644 --- a/lib/forward.js +++ b/lib/forward.js @@ -1,14 +1,6 @@ 'use strict'; -const config = require('wild-config'); -const maildrop = require('./maildrop'); -const db = require('./db'); - module.exports = (options, callback) => { - if (!config.sender.enabled) { - return callback(null, false); - } - let mail = { parentId: options.parentId, reason: 'forward', @@ -21,14 +13,14 @@ module.exports = (options, callback) => { interface: 'forwarder' }; - let message = maildrop(mail, (err, ...args) => { + let message = options.maildrop(mail, (err, ...args) => { if (err || !args[0]) { if (err) { err.code = err.code || 'ERRCOMPOSE'; } return callback(err, ...args); } - db.database.collection('messagelog').insertOne( + options.db.database.collection('messagelog').insertOne( { id: args[0].id, messageId: args[0].messageId, diff --git a/lmtp.js b/lmtp.js index 17afc934..3beb186f 100644 --- a/lmtp.js +++ b/lmtp.js @@ -223,9 +223,8 @@ module.exports = done => { }); filterHandler = new FilterHandler({ - database: db.database, - users: db.users, - redis: db.redis, + db, + sender: config.sender, messageHandler, spamHeaderKeys, spamChecks