wildduck/lib/handlers/on-append.js

64 lines
1.9 KiB
JavaScript
Raw Normal View History

'use strict';
const db = require('../db');
// APPEND mailbox (flags) date message
module.exports = (server, messageHandler) => (path, flags, date, raw, session, callback) => {
server.logger.debug(
{
tnx: 'append',
cid: session.id
},
'[%s] Appending message to "%s"',
session.id,
path
);
db.users.collection('users').findOne({
_id: session.user.id
2017-08-03 20:26:44 +08:00
}, (err, userData) => {
if (err) {
return callback(err);
}
2017-08-03 20:26:44 +08:00
if (!userData) {
return callback(new Error('User not found'));
}
2017-08-03 20:26:44 +08:00
if (userData.quota && userData.storageUsed > userData.quota) {
return callback(false, 'OVERQUOTA');
}
2017-08-03 20:26:44 +08:00
messageHandler.encryptMessage(userData.encryptMessages ? userData.pubKey : false, raw, (err, encrypted) => {
if (!err && encrypted) {
raw = encrypted;
}
messageHandler.add(
{
user: session.user.id,
path,
meta: {
source: 'IMAP',
2017-10-20 18:43:44 +08:00
from: '',
to: [session.user.address || session.user.username],
origin: session.remoteAddress,
transtype: 'APPEND',
time: new Date()
2017-08-03 20:26:44 +08:00
},
session,
date,
flags,
raw
},
2017-08-03 20:26:44 +08:00
(err, status, data) => {
if (err) {
if (err.imapResponse) {
return callback(null, err.imapResponse);
}
return callback(err);
}
2017-08-03 20:26:44 +08:00
callback(null, status, data);
}
2017-08-03 20:26:44 +08:00
);
});
});
};