2017-03-06 22:13:40 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const config = require('config');
|
|
|
|
const restify = require('restify');
|
|
|
|
const log = require('npmlog');
|
|
|
|
const Joi = require('joi');
|
|
|
|
const bcrypt = require('bcryptjs');
|
2017-03-21 06:07:23 +08:00
|
|
|
const tools = require('./lib/tools');
|
2017-03-27 15:36:45 +08:00
|
|
|
const MessageHandler = require('./lib/message-handler');
|
|
|
|
const db = require('./lib/db');
|
2017-04-03 18:39:39 +08:00
|
|
|
const ObjectID = require('mongodb').ObjectID;
|
2017-04-03 21:59:04 +08:00
|
|
|
const libqp = require('libqp');
|
|
|
|
const libbase64 = require('libbase64');
|
2017-03-06 22:13:40 +08:00
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
const server = restify.createServer({
|
|
|
|
name: 'Wild Duck API',
|
|
|
|
formatters: {
|
|
|
|
'application/json': (req, res, body, cb) => cb(null, JSON.stringify(body, null, 2)),
|
|
|
|
'text/html': (req, res, body, cb) => cb(null, body)
|
|
|
|
}
|
|
|
|
});
|
2017-03-06 22:13:40 +08:00
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
let messageHandler;
|
2017-03-06 22:13:40 +08:00
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
server.use(restify.queryParser());
|
2017-03-06 22:13:40 +08:00
|
|
|
server.use(restify.bodyParser({
|
|
|
|
maxBodySize: 0,
|
|
|
|
mapParams: true,
|
|
|
|
mapFiles: false,
|
|
|
|
overrideParams: false
|
|
|
|
}));
|
|
|
|
|
|
|
|
server.post('/user/create', (req, res, next) => {
|
2017-03-27 15:36:45 +08:00
|
|
|
res.charSet('utf-8');
|
|
|
|
|
2017-03-06 22:13:40 +08:00
|
|
|
const schema = Joi.object().keys({
|
2017-03-25 04:26:04 +08:00
|
|
|
username: Joi.string().alphanum().lowercase().min(3).max(30).required(),
|
2017-03-27 04:58:05 +08:00
|
|
|
password: Joi.string().min(3).max(100).required(),
|
2017-04-12 16:32:57 +08:00
|
|
|
quota: Joi.number().default(config.maxStorage * (1024 * 1024))
|
2017-03-06 22:13:40 +08:00
|
|
|
});
|
|
|
|
|
2017-03-21 06:07:23 +08:00
|
|
|
const result = Joi.validate({
|
2017-03-25 04:26:04 +08:00
|
|
|
username: req.params.username,
|
2017-03-27 15:36:45 +08:00
|
|
|
password: req.params.password,
|
|
|
|
quota: req.params.quota
|
2017-03-21 06:07:23 +08:00
|
|
|
}, schema, {
|
2017-03-06 22:13:40 +08:00
|
|
|
abortEarly: false,
|
|
|
|
convert: true,
|
|
|
|
allowUnknown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
res.json({
|
|
|
|
error: result.error.message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2017-03-25 04:26:04 +08:00
|
|
|
let username = result.value.username;
|
2017-03-06 22:13:40 +08:00
|
|
|
let password = result.value.password;
|
2017-03-27 15:36:45 +08:00
|
|
|
let quota = result.value.quota;
|
2017-03-06 22:13:40 +08:00
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
db.database.collection('users').findOne({
|
2017-03-06 22:13:40 +08:00
|
|
|
username
|
2017-03-25 04:26:04 +08:00
|
|
|
}, (err, userData) => {
|
2017-03-06 22:13:40 +08:00
|
|
|
if (err) {
|
2017-03-22 16:30:10 +08:00
|
|
|
res.json({
|
2017-03-06 22:13:40 +08:00
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
username
|
|
|
|
});
|
2017-03-22 16:30:10 +08:00
|
|
|
return next();
|
2017-03-06 22:13:40 +08:00
|
|
|
}
|
2017-03-25 04:26:04 +08:00
|
|
|
if (userData) {
|
2017-03-22 16:30:10 +08:00
|
|
|
res.json({
|
2017-03-06 22:13:40 +08:00
|
|
|
error: 'This username already exists',
|
|
|
|
username
|
|
|
|
});
|
2017-03-22 16:30:10 +08:00
|
|
|
return next();
|
2017-03-06 22:13:40 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 04:26:04 +08:00
|
|
|
// Insert
|
|
|
|
let hash = bcrypt.hashSync(password, 11);
|
2017-03-27 15:36:45 +08:00
|
|
|
db.database.collection('users').insertOne({
|
2017-03-25 04:26:04 +08:00
|
|
|
username,
|
|
|
|
password: hash,
|
|
|
|
address: false,
|
2017-03-27 04:58:05 +08:00
|
|
|
storageUsed: 0,
|
2017-03-27 15:36:45 +08:00
|
|
|
quota,
|
2017-03-25 04:26:04 +08:00
|
|
|
created: new Date()
|
|
|
|
}, (err, result) => {
|
2017-03-06 22:13:40 +08:00
|
|
|
if (err) {
|
2017-03-22 16:30:10 +08:00
|
|
|
res.json({
|
2017-03-06 22:13:40 +08:00
|
|
|
error: 'MongoDB Error: ' + err.message,
|
2017-03-25 04:26:04 +08:00
|
|
|
username
|
2017-03-22 16:30:10 +08:00
|
|
|
});
|
|
|
|
return next();
|
2017-03-06 22:13:40 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 04:26:04 +08:00
|
|
|
let user = result.insertedId;
|
|
|
|
|
|
|
|
// create folders for user
|
|
|
|
let uidValidity = Math.floor(Date.now() / 1000);
|
2017-03-27 15:36:45 +08:00
|
|
|
db.database.collection('mailboxes').insertMany([{
|
2017-03-25 04:26:04 +08:00
|
|
|
user,
|
|
|
|
path: 'INBOX',
|
|
|
|
uidValidity,
|
|
|
|
uidNext: 1,
|
|
|
|
modifyIndex: 0,
|
2017-03-30 18:25:42 +08:00
|
|
|
subscribed: true,
|
|
|
|
flags: []
|
2017-03-25 04:26:04 +08:00
|
|
|
}, {
|
|
|
|
user,
|
|
|
|
path: 'Sent Mail',
|
|
|
|
specialUse: '\\Sent',
|
|
|
|
uidValidity,
|
|
|
|
uidNext: 1,
|
|
|
|
modifyIndex: 0,
|
2017-03-30 18:25:42 +08:00
|
|
|
subscribed: true,
|
|
|
|
flags: []
|
2017-03-25 04:26:04 +08:00
|
|
|
}, {
|
|
|
|
user,
|
|
|
|
path: 'Trash',
|
|
|
|
specialUse: '\\Trash',
|
|
|
|
uidValidity,
|
|
|
|
uidNext: 1,
|
|
|
|
modifyIndex: 0,
|
2017-03-30 18:25:42 +08:00
|
|
|
subscribed: true,
|
|
|
|
flags: []
|
2017-03-25 04:26:04 +08:00
|
|
|
}, {
|
|
|
|
user,
|
|
|
|
path: 'Junk',
|
|
|
|
specialUse: '\\Junk',
|
|
|
|
uidValidity,
|
|
|
|
uidNext: 1,
|
|
|
|
modifyIndex: 0,
|
2017-03-30 18:25:42 +08:00
|
|
|
subscribed: true,
|
|
|
|
flags: []
|
2017-03-25 04:26:04 +08:00
|
|
|
}], {
|
|
|
|
w: 1,
|
|
|
|
ordered: false
|
|
|
|
}, err => {
|
2017-03-06 22:13:40 +08:00
|
|
|
if (err) {
|
2017-03-22 16:30:10 +08:00
|
|
|
res.json({
|
2017-03-06 22:13:40 +08:00
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
username
|
|
|
|
});
|
2017-03-22 16:30:10 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2017-03-25 04:26:04 +08:00
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
username
|
2017-03-22 16:30:10 +08:00
|
|
|
});
|
2017-03-25 04:26:04 +08:00
|
|
|
|
|
|
|
return next();
|
2017-03-22 16:30:10 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-25 04:26:04 +08:00
|
|
|
server.post('/user/address/create', (req, res, next) => {
|
2017-03-27 15:36:45 +08:00
|
|
|
res.charSet('utf-8');
|
|
|
|
|
2017-03-22 16:30:10 +08:00
|
|
|
const schema = Joi.object().keys({
|
2017-03-25 04:26:04 +08:00
|
|
|
username: Joi.string().alphanum().lowercase().min(3).max(30).required(),
|
|
|
|
address: Joi.string().email().required(),
|
|
|
|
main: Joi.boolean().truthy(['Y', 'true', 'yes', 1]).optional()
|
2017-03-22 16:30:10 +08:00
|
|
|
});
|
|
|
|
|
2017-03-25 04:26:04 +08:00
|
|
|
let username = req.params.username;
|
|
|
|
let address = req.params.address;
|
|
|
|
let main = req.params.main;
|
2017-03-22 16:30:10 +08:00
|
|
|
|
|
|
|
const result = Joi.validate({
|
2017-03-25 04:26:04 +08:00
|
|
|
username,
|
|
|
|
address: (address || '').replace(/[\u0080-\uFFFF]/g, 'x'),
|
|
|
|
main
|
2017-03-22 16:30:10 +08:00
|
|
|
}, schema, {
|
|
|
|
abortEarly: false,
|
|
|
|
convert: true,
|
|
|
|
allowUnknown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
res.json({
|
|
|
|
error: result.error.message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2017-03-25 04:26:04 +08:00
|
|
|
username = result.value.username;
|
|
|
|
address = tools.normalizeAddress(address);
|
|
|
|
main = result.value.main;
|
2017-03-22 16:30:10 +08:00
|
|
|
|
2017-03-25 04:26:04 +08:00
|
|
|
if (address.indexOf('+') >= 0) {
|
2017-03-22 16:30:10 +08:00
|
|
|
res.json({
|
|
|
|
error: 'Address can not contain +'
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
db.database.collection('users').findOne({
|
2017-03-25 04:26:04 +08:00
|
|
|
username
|
|
|
|
}, (err, userData) => {
|
2017-03-22 16:30:10 +08:00
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
2017-03-25 04:26:04 +08:00
|
|
|
username
|
2017-03-22 16:30:10 +08:00
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
2017-03-25 04:26:04 +08:00
|
|
|
if (!userData) {
|
2017-03-22 16:30:10 +08:00
|
|
|
res.json({
|
|
|
|
error: 'This user does not exist',
|
2017-03-25 04:26:04 +08:00
|
|
|
username
|
2017-03-22 16:30:10 +08:00
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
db.database.collection('addresses').findOne({
|
2017-03-25 04:26:04 +08:00
|
|
|
address
|
|
|
|
}, (err, addressData) => {
|
2017-03-22 16:30:10 +08:00
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
2017-03-25 04:26:04 +08:00
|
|
|
username,
|
|
|
|
address
|
2017-03-22 16:30:10 +08:00
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
2017-03-25 04:26:04 +08:00
|
|
|
if (addressData) {
|
2017-03-22 16:30:10 +08:00
|
|
|
res.json({
|
|
|
|
error: 'This email address already exists',
|
2017-03-25 04:26:04 +08:00
|
|
|
username,
|
|
|
|
address
|
2017-03-22 16:30:10 +08:00
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert alias address to email address registry
|
2017-03-27 15:36:45 +08:00
|
|
|
db.database.collection('addresses').insertOne({
|
2017-03-25 04:26:04 +08:00
|
|
|
user: userData._id,
|
|
|
|
address,
|
2017-03-22 16:30:10 +08:00
|
|
|
created: new Date()
|
2017-03-25 04:26:04 +08:00
|
|
|
}, err => {
|
2017-03-22 16:30:10 +08:00
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
2017-03-25 04:26:04 +08:00
|
|
|
address
|
2017-03-22 16:30:10 +08:00
|
|
|
});
|
|
|
|
return next();
|
2017-03-06 22:13:40 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 04:26:04 +08:00
|
|
|
let done = () => {
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
username,
|
|
|
|
address
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
};
|
2017-03-06 22:13:40 +08:00
|
|
|
|
2017-03-25 04:26:04 +08:00
|
|
|
if (!userData.address || main) {
|
|
|
|
// register this address as the default address for that user
|
2017-03-27 15:36:45 +08:00
|
|
|
return db.database.collection('users').findOneAndUpdate({
|
2017-03-25 04:26:04 +08:00
|
|
|
_id: userData._id
|
|
|
|
}, {
|
|
|
|
$set: {
|
|
|
|
address
|
|
|
|
}
|
|
|
|
}, {}, done);
|
|
|
|
}
|
|
|
|
|
|
|
|
done();
|
2017-03-06 22:13:40 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
server.post('/user/quota', (req, res, next) => {
|
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
|
|
|
username: Joi.string().alphanum().lowercase().min(3).max(30).required(),
|
2017-04-12 16:32:57 +08:00
|
|
|
quota: Joi.number().min(0).optional(),
|
|
|
|
recipients: Joi.number().min(0).max(1000000).optional()
|
2017-03-27 15:36:45 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const result = Joi.validate({
|
|
|
|
username: req.params.username,
|
2017-04-12 16:32:57 +08:00
|
|
|
quota: req.params.quota,
|
|
|
|
recipients: req.params.recipients
|
2017-03-27 15:36:45 +08:00
|
|
|
}, schema, {
|
|
|
|
abortEarly: false,
|
|
|
|
convert: true,
|
|
|
|
allowUnknown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
res.json({
|
|
|
|
error: result.error.message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let username = result.value.username;
|
|
|
|
let quota = result.value.quota;
|
2017-04-12 16:32:57 +08:00
|
|
|
let recipients = result.value.recipients;
|
|
|
|
|
|
|
|
let $set = {};
|
|
|
|
if (quota) {
|
|
|
|
$set.quota = quota;
|
|
|
|
}
|
|
|
|
if (recipients) {
|
|
|
|
$set.recipients = recipients;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!quota && !recipients) {
|
|
|
|
res.json({
|
|
|
|
error: 'Nothing was updated'
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
2017-03-27 15:36:45 +08:00
|
|
|
|
|
|
|
db.database.collection('users').findOneAndUpdate({
|
|
|
|
username
|
|
|
|
}, {
|
2017-04-12 16:32:57 +08:00
|
|
|
$set
|
|
|
|
}, {
|
|
|
|
returnOriginal: false
|
2017-03-27 15:36:45 +08:00
|
|
|
}, (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result || !result.value) {
|
|
|
|
res.json({
|
|
|
|
error: 'This user does not exist',
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
username,
|
2017-04-12 16:32:57 +08:00
|
|
|
quota: Number(result.value.quota) || 0,
|
|
|
|
recipients: Number(result.value.recipients) || 0
|
2017-03-27 15:36:45 +08:00
|
|
|
});
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-30 01:06:09 +08:00
|
|
|
server.post('/user/quota/reset', (req, res, next) => {
|
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
|
|
|
username: Joi.string().alphanum().lowercase().min(3).max(30).required()
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = Joi.validate({
|
|
|
|
username: req.params.username
|
|
|
|
}, schema, {
|
|
|
|
abortEarly: false,
|
|
|
|
convert: true,
|
|
|
|
allowUnknown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
res.json({
|
|
|
|
error: result.error.message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let username = result.value.username;
|
|
|
|
|
|
|
|
db.database.collection('users').findOne({
|
|
|
|
username
|
|
|
|
}, (err, user) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
res.json({
|
|
|
|
error: 'This user does not exist',
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// calculate mailbox size by aggregating the size's of all messages
|
|
|
|
db.database.collection('messages').aggregate([{
|
|
|
|
$match: {
|
|
|
|
user: user._id
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
$group: {
|
|
|
|
_id: {
|
|
|
|
user: '$user'
|
|
|
|
},
|
|
|
|
storageUsed: {
|
|
|
|
$sum: '$size'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}], {
|
|
|
|
cursor: {
|
|
|
|
batchSize: 1
|
|
|
|
}
|
|
|
|
}).toArray((err, result) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let storageUsed = result && result[0] && result[0].storageUsed || 0;
|
|
|
|
|
|
|
|
// update quota counter
|
|
|
|
db.database.collection('users').findOneAndUpdate({
|
|
|
|
_id: user._id
|
|
|
|
}, {
|
|
|
|
$set: {
|
|
|
|
storageUsed: Number(storageUsed) || 0
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
returnOriginal: false
|
|
|
|
}, (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result || !result.value) {
|
|
|
|
res.json({
|
|
|
|
error: 'This user does not exist',
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
username,
|
2017-04-01 19:49:58 +08:00
|
|
|
previousStorageUsed: user.storageUsed,
|
|
|
|
storageUsed: Number(result.value.storageUsed) || 0
|
2017-03-30 01:06:09 +08:00
|
|
|
});
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
server.post('/user/password', (req, res, next) => {
|
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
|
|
|
username: Joi.string().alphanum().lowercase().min(3).max(30).required(),
|
|
|
|
password: Joi.string().min(3).max(100).required()
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = Joi.validate({
|
|
|
|
username: req.params.username,
|
|
|
|
password: req.params.password
|
|
|
|
}, schema, {
|
|
|
|
abortEarly: false,
|
|
|
|
convert: true,
|
|
|
|
allowUnknown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
res.json({
|
|
|
|
error: result.error.message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let username = result.value.username;
|
|
|
|
let password = result.value.password;
|
|
|
|
|
|
|
|
db.database.collection('users').findOneAndUpdate({
|
|
|
|
username
|
|
|
|
}, {
|
|
|
|
$set: {
|
|
|
|
password: bcrypt.hashSync(password, 11)
|
|
|
|
}
|
|
|
|
}, (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result || !result.value) {
|
|
|
|
res.json({
|
|
|
|
error: 'This user does not exist',
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
username
|
|
|
|
});
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
server.get('/user', (req, res, next) => {
|
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
|
|
|
username: Joi.string().alphanum().lowercase().min(3).max(30).required()
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = Joi.validate({
|
|
|
|
username: req.query.username
|
|
|
|
}, schema, {
|
|
|
|
abortEarly: false,
|
|
|
|
convert: true,
|
|
|
|
allowUnknown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
res.json({
|
|
|
|
error: result.error.message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let username = result.value.username;
|
|
|
|
|
|
|
|
db.database.collection('users').findOne({
|
|
|
|
username
|
|
|
|
}, (err, userData) => {
|
2017-03-06 22:13:40 +08:00
|
|
|
if (err) {
|
2017-03-27 15:36:45 +08:00
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if (!userData) {
|
|
|
|
res.json({
|
|
|
|
error: 'This user does not exist',
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
2017-03-06 22:13:40 +08:00
|
|
|
}
|
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
db.database.collection('addresses').find({
|
|
|
|
user: userData._id
|
|
|
|
}).sort({
|
|
|
|
address: 1
|
|
|
|
}).toArray((err, addresses) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
2017-03-06 22:13:40 +08:00
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
if (!addresses) {
|
|
|
|
addresses = [];
|
2017-03-06 22:13:40 +08:00
|
|
|
}
|
2017-03-27 15:36:45 +08:00
|
|
|
|
2017-04-12 16:32:57 +08:00
|
|
|
db.redis.multi().
|
|
|
|
get('wdr:' + userData._id.toString()).
|
|
|
|
ttl('wdr:' + userData._id.toString()).
|
|
|
|
exec((err, result) => {
|
|
|
|
if (err) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
let recipients = Number(userData.recipients) || 0;
|
|
|
|
let recipientsSent = Number(result && result[0]) || 0;
|
|
|
|
let recipientsTtl = Number(result && result[1]) || 0;
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
username,
|
|
|
|
|
|
|
|
quota: Number(userData.quota) || config.maxStorage * 1024 * 1024,
|
|
|
|
storageUsed: Math.max(Number(userData.storageUsed) || 0, 0),
|
|
|
|
|
|
|
|
recipients,
|
|
|
|
recipientsSent,
|
|
|
|
|
|
|
|
recipientsLimited: recipients ? recipients <= recipientsSent : false,
|
|
|
|
recipientsTtl: recipientsTtl >= 0 ? recipientsTtl : false,
|
|
|
|
|
|
|
|
addresses: addresses.map(address => ({
|
|
|
|
id: address._id.toString(),
|
|
|
|
address: address.address,
|
|
|
|
main: address.address === userData.address,
|
|
|
|
created: address.created
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
return next();
|
2017-03-27 15:36:45 +08:00
|
|
|
});
|
2017-03-06 22:13:40 +08:00
|
|
|
});
|
2017-03-27 15:36:45 +08:00
|
|
|
});
|
|
|
|
});
|
2017-03-06 22:13:40 +08:00
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
server.get('/user/mailboxes', (req, res, next) => {
|
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
|
|
|
username: Joi.string().alphanum().lowercase().min(3).max(30).required()
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = Joi.validate({
|
|
|
|
username: req.query.username
|
|
|
|
}, schema, {
|
|
|
|
abortEarly: false,
|
|
|
|
convert: true,
|
|
|
|
allowUnknown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
res.json({
|
|
|
|
error: result.error.message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let username = result.value.username;
|
|
|
|
|
|
|
|
db.database.collection('users').findOne({
|
|
|
|
username
|
|
|
|
}, (err, userData) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if (!userData) {
|
|
|
|
res.json({
|
|
|
|
error: 'This user does not exist',
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
db.database.collection('mailboxes').find({
|
|
|
|
user: userData._id
|
|
|
|
}).toArray((err, mailboxes) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
username
|
|
|
|
});
|
|
|
|
return next();
|
2017-03-06 22:13:40 +08:00
|
|
|
}
|
2017-03-27 15:36:45 +08:00
|
|
|
|
|
|
|
if (!mailboxes) {
|
|
|
|
mailboxes = [];
|
|
|
|
}
|
|
|
|
|
2017-03-30 01:06:09 +08:00
|
|
|
let priority = {
|
|
|
|
Inbox: 1,
|
|
|
|
Sent: 2,
|
|
|
|
Junk: 3,
|
|
|
|
Trash: 4
|
|
|
|
};
|
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
username,
|
|
|
|
mailboxes: mailboxes.map(mailbox => ({
|
|
|
|
id: mailbox._id.toString(),
|
|
|
|
path: mailbox.path,
|
2017-03-30 01:06:09 +08:00
|
|
|
special: mailbox.path === 'INBOX' ? 'Inbox' : (mailbox.specialUse ? mailbox.specialUse.replace(/^\\/, '') : false)
|
2017-03-27 15:36:45 +08:00
|
|
|
})).sort((a, b) => {
|
|
|
|
if (a.special && !b.special) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-03-30 01:06:09 +08:00
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
if (b.special && !a.special) {
|
|
|
|
return 1;
|
|
|
|
}
|
2017-03-30 01:06:09 +08:00
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
if (a.special && b.special) {
|
|
|
|
return (priority[a.special] || 5) - (priority[b.special] || 5);
|
|
|
|
}
|
2017-03-30 01:06:09 +08:00
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
return a.path.localeCompare(b.path);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-04-03 21:59:04 +08:00
|
|
|
// FIXME: if listing a page after the last one then there is no prev URL
|
|
|
|
// Probably should detect the last page the same way the first one is detected
|
|
|
|
server.get('/mailbox/:id', (req, res, next) => {
|
2017-03-27 15:36:45 +08:00
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
2017-04-03 21:59:04 +08:00
|
|
|
id: Joi.string().hex().lowercase().length(24).required(),
|
|
|
|
before: Joi.number().default(0),
|
|
|
|
after: Joi.number().default(0),
|
|
|
|
size: Joi.number().min(1).max(50).default(20)
|
2017-03-27 15:36:45 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const result = Joi.validate({
|
2017-04-03 21:59:04 +08:00
|
|
|
id: req.params.id,
|
|
|
|
before: req.params.before,
|
|
|
|
after: req.params.after,
|
|
|
|
size: req.params.size
|
2017-03-27 15:36:45 +08:00
|
|
|
}, schema, {
|
|
|
|
abortEarly: false,
|
|
|
|
convert: true,
|
|
|
|
allowUnknown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
res.json({
|
|
|
|
error: result.error.message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = result.value.id;
|
2017-04-03 21:59:04 +08:00
|
|
|
let before = result.value.before;
|
|
|
|
let after = result.value.after;
|
|
|
|
let size = result.value.size;
|
2017-03-27 15:36:45 +08:00
|
|
|
|
2017-04-03 21:59:04 +08:00
|
|
|
db.database.collection('mailboxes').findOne({
|
|
|
|
_id: new ObjectID(id)
|
|
|
|
}, (err, mailbox) => {
|
2017-03-27 15:36:45 +08:00
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
id
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
2017-04-03 21:59:04 +08:00
|
|
|
if (!mailbox) {
|
|
|
|
res.json({
|
|
|
|
error: 'This mailbox does not exist',
|
|
|
|
id
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
2017-03-27 15:36:45 +08:00
|
|
|
|
2017-04-03 21:59:04 +08:00
|
|
|
let query = {
|
|
|
|
mailbox: mailbox._id
|
|
|
|
};
|
|
|
|
let reverse = false;
|
2017-04-04 18:30:38 +08:00
|
|
|
let sort = [
|
|
|
|
['uid', -1]
|
|
|
|
];
|
2017-04-03 21:59:04 +08:00
|
|
|
|
|
|
|
if (req.params.before) {
|
|
|
|
query.uid = {
|
|
|
|
$lt: before
|
|
|
|
};
|
|
|
|
} else if (req.params.after) {
|
|
|
|
query.uid = {
|
|
|
|
$gt: after
|
|
|
|
};
|
2017-04-04 18:30:38 +08:00
|
|
|
sort = [
|
|
|
|
['uid', 1]
|
|
|
|
];
|
2017-04-03 21:59:04 +08:00
|
|
|
reverse = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
db.database.collection('messages').findOne({
|
|
|
|
mailbox: mailbox._id
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
uid: true
|
|
|
|
},
|
2017-04-04 18:30:38 +08:00
|
|
|
sort: [
|
|
|
|
['uid', -1]
|
|
|
|
]
|
2017-04-03 21:59:04 +08:00
|
|
|
}, (err, entry) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
id
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
mailbox: {
|
|
|
|
id: mailbox._id,
|
|
|
|
path: mailbox.path
|
|
|
|
},
|
|
|
|
next: false,
|
|
|
|
prev: false,
|
|
|
|
messages: []
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let newest = entry.uid;
|
|
|
|
|
2017-04-04 01:07:21 +08:00
|
|
|
db.database.collection('messages').findOne({
|
|
|
|
mailbox: mailbox._id
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
uid: true
|
|
|
|
},
|
2017-04-04 18:30:38 +08:00
|
|
|
sort: [
|
|
|
|
['uid', 1]
|
|
|
|
]
|
2017-04-04 01:07:21 +08:00
|
|
|
}, (err, entry) => {
|
2017-04-03 21:59:04 +08:00
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
id
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2017-04-04 01:07:21 +08:00
|
|
|
if (!entry) {
|
|
|
|
res.json({
|
|
|
|
error: 'Unexpected result'
|
|
|
|
});
|
|
|
|
return next();
|
2017-04-03 21:59:04 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 01:07:21 +08:00
|
|
|
let oldest = entry.uid;
|
|
|
|
|
|
|
|
db.database.collection('messages').find(query, {
|
|
|
|
uid: true,
|
|
|
|
mailbox: true,
|
2017-04-13 16:35:39 +08:00
|
|
|
idate: true,
|
2017-04-04 01:07:21 +08:00
|
|
|
headers: true,
|
|
|
|
hasAttachments: true,
|
|
|
|
intro: true
|
|
|
|
}).sort(sort).limit(size).toArray((err, messages) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
id
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
2017-04-03 21:59:04 +08:00
|
|
|
|
2017-04-04 01:07:21 +08:00
|
|
|
if (reverse) {
|
|
|
|
messages = messages.reverse();
|
2017-04-03 21:59:04 +08:00
|
|
|
}
|
2017-04-04 01:07:21 +08:00
|
|
|
|
|
|
|
let nextPage = false;
|
|
|
|
let prevPage = false;
|
|
|
|
|
|
|
|
if (messages.length) {
|
|
|
|
if (after || before) {
|
|
|
|
prevPage = messages[0].uid;
|
|
|
|
if (prevPage >= newest) {
|
|
|
|
prevPage = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (messages.length >= size) {
|
|
|
|
nextPage = messages[messages.length - 1].uid;
|
|
|
|
if (nextPage < oldest) {
|
|
|
|
nextPage = false;
|
|
|
|
}
|
2017-04-03 21:59:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 01:07:21 +08:00
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
mailbox: {
|
|
|
|
id: mailbox._id,
|
|
|
|
path: mailbox.path
|
|
|
|
},
|
|
|
|
next: nextPage ? '/mailbox/' + id + '?before=' + nextPage + '&size=' + size : false,
|
|
|
|
prev: prevPage ? '/mailbox/' + id + '?after=' + prevPage + '&size=' + size : false,
|
|
|
|
messages: messages.map(message => {
|
|
|
|
let response = {
|
|
|
|
id: message._id,
|
2017-04-13 16:35:39 +08:00
|
|
|
date: message.idate,
|
2017-04-04 01:07:21 +08:00
|
|
|
hasAttachments: message.hasAttachments,
|
|
|
|
intro: message.intro
|
|
|
|
};
|
|
|
|
|
|
|
|
message.headers.forEach(entry => {
|
|
|
|
if (['subject', 'from', 'to', 'cc', 'bcc'].includes(entry.key)) {
|
|
|
|
response[entry.key] = entry.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
});
|
2017-04-03 21:59:04 +08:00
|
|
|
|
2017-04-04 01:07:21 +08:00
|
|
|
return next();
|
|
|
|
});
|
2017-04-03 21:59:04 +08:00
|
|
|
});
|
2017-03-06 22:13:40 +08:00
|
|
|
});
|
2017-03-27 15:36:45 +08:00
|
|
|
});
|
2017-04-03 18:39:39 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
server.get('/message/:id', (req, res, next) => {
|
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
|
|
|
id: Joi.string().hex().lowercase().length(24).required(),
|
|
|
|
mailbox: Joi.string().hex().lowercase().length(24).optional()
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = Joi.validate({
|
|
|
|
id: req.params.id,
|
|
|
|
mailbox: req.params.mailbox
|
|
|
|
}, schema, {
|
|
|
|
abortEarly: false,
|
|
|
|
convert: true,
|
|
|
|
allowUnknown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
res.json({
|
|
|
|
error: result.error.message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = result.value.id;
|
|
|
|
let mailbox = result.value.mailbox;
|
|
|
|
|
|
|
|
let query = {
|
|
|
|
_id: new ObjectID(id)
|
|
|
|
};
|
2017-03-27 15:36:45 +08:00
|
|
|
|
2017-04-03 18:39:39 +08:00
|
|
|
if (mailbox) {
|
|
|
|
query.mailbox = new ObjectID(mailbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
db.database.collection('messages').findOne(query, {
|
|
|
|
mailbox: true,
|
|
|
|
headers: true,
|
|
|
|
html: true,
|
|
|
|
text: true,
|
|
|
|
attachments: true,
|
2017-04-13 16:35:39 +08:00
|
|
|
idate: true,
|
2017-04-03 18:39:39 +08:00
|
|
|
flags: true
|
|
|
|
}, (err, message) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
id
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if (!message) {
|
|
|
|
res.json({
|
|
|
|
error: 'This message does not exist',
|
|
|
|
id
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
message: {
|
|
|
|
id,
|
|
|
|
mailbox: message.mailbox,
|
|
|
|
headers: message.headers,
|
2017-04-13 16:35:39 +08:00
|
|
|
date: message.idate,
|
2017-04-03 18:39:39 +08:00
|
|
|
flags: message.flags,
|
|
|
|
text: message.text,
|
|
|
|
html: message.html,
|
|
|
|
attachments: message.attachments
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
2017-03-27 15:36:45 +08:00
|
|
|
});
|
|
|
|
|
2017-04-03 21:59:04 +08:00
|
|
|
server.get('/message/:id/raw', (req, res, next) => {
|
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
|
|
|
id: Joi.string().hex().lowercase().length(24).required(),
|
|
|
|
mailbox: Joi.string().hex().lowercase().length(24).optional()
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = Joi.validate({
|
|
|
|
id: req.params.id,
|
|
|
|
mailbox: req.params.mailbox
|
|
|
|
}, schema, {
|
|
|
|
abortEarly: false,
|
|
|
|
convert: true,
|
|
|
|
allowUnknown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
res.json({
|
|
|
|
error: result.error.message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = result.value.id;
|
|
|
|
let mailbox = result.value.mailbox;
|
|
|
|
|
|
|
|
let query = {
|
|
|
|
_id: new ObjectID(id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (mailbox) {
|
|
|
|
query.mailbox = new ObjectID(mailbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
db.database.collection('messages').findOne(query, {
|
|
|
|
mimeTree: true,
|
|
|
|
size: true
|
|
|
|
}, (err, message) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
id
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if (!message) {
|
|
|
|
res.json({
|
|
|
|
error: 'This message does not exist',
|
|
|
|
id
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let response = messageHandler.indexer.rebuild(message.mimeTree);
|
|
|
|
if (!response || response.type !== 'stream' || !response.value) {
|
|
|
|
res.json({
|
|
|
|
error: 'Can not fetch message',
|
|
|
|
id
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'message/rfc822'
|
|
|
|
});
|
|
|
|
response.value.pipe(res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
server.get('/message/:message/attachment/:attachment', (req, res, next) => {
|
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
|
|
|
message: Joi.string().hex().lowercase().length(24).required(),
|
|
|
|
attachment: Joi.string().hex().lowercase().length(24).required()
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = Joi.validate({
|
|
|
|
message: req.params.message,
|
|
|
|
attachment: req.params.attachment
|
|
|
|
}, schema, {
|
|
|
|
abortEarly: false,
|
|
|
|
convert: true,
|
|
|
|
allowUnknown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
res.json({
|
|
|
|
error: result.error.message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let message = result.value.message;
|
|
|
|
let attachment = result.value.attachment;
|
|
|
|
|
|
|
|
let query = {
|
|
|
|
_id: new ObjectID(attachment),
|
|
|
|
'metadata.messages': new ObjectID(message)
|
|
|
|
};
|
|
|
|
|
|
|
|
db.database.collection('attachments.files').findOne(query, (err, messageData) => {
|
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
attachment,
|
|
|
|
message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
if (!messageData) {
|
|
|
|
res.json({
|
|
|
|
error: 'This message does not exist',
|
|
|
|
attachment,
|
|
|
|
message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
res.writeHead(200, {
|
2017-04-10 02:43:22 +08:00
|
|
|
'Content-Type': messageData.contentType || 'application/octet-stream'
|
2017-04-03 21:59:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
let attachmentStream = messageHandler.indexer.gridstore.createReadStream(messageData._id);
|
|
|
|
|
|
|
|
attachmentStream.once('error', err => res.emit('error', err));
|
|
|
|
|
|
|
|
if (messageData.metadata.transferEncoding === 'base64') {
|
|
|
|
attachmentStream.pipe(new libbase64.Decoder()).pipe(res);
|
|
|
|
} else if (messageData.metadata.transferEncoding === 'quoted-printable') {
|
|
|
|
attachmentStream.pipe(new libqp.Decoder()).pipe(res);
|
|
|
|
} else {
|
|
|
|
attachmentStream.pipe(res);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
server.del('/message/:id', (req, res, next) => {
|
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
|
|
|
id: Joi.string().hex().lowercase().length(24).required(),
|
|
|
|
mailbox: Joi.string().hex().lowercase().length(24).optional()
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = Joi.validate({
|
|
|
|
id: req.params.id,
|
|
|
|
mailbox: req.params.mailbox
|
|
|
|
}, schema, {
|
|
|
|
abortEarly: false,
|
|
|
|
convert: true,
|
|
|
|
allowUnknown: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
res.json({
|
|
|
|
error: result.error.message
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
let id = result.value.id;
|
|
|
|
let mailbox = result.value.mailbox;
|
|
|
|
|
|
|
|
let query = {
|
|
|
|
_id: new ObjectID(id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (mailbox) {
|
|
|
|
query.mailbox = new ObjectID(mailbox);
|
|
|
|
}
|
|
|
|
|
2017-04-10 22:12:47 +08:00
|
|
|
messageHandler.del({
|
|
|
|
query
|
|
|
|
}, (err, success) => {
|
2017-04-03 21:59:04 +08:00
|
|
|
if (err) {
|
|
|
|
res.json({
|
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
id
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success,
|
|
|
|
id
|
|
|
|
});
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
module.exports = done => {
|
2017-04-13 16:35:39 +08:00
|
|
|
if (!config.imap.enabled) {
|
|
|
|
return setImmediate(() => done(null, false));
|
|
|
|
}
|
|
|
|
|
2017-03-27 15:36:45 +08:00
|
|
|
let started = false;
|
|
|
|
|
|
|
|
messageHandler = new MessageHandler(db.database);
|
|
|
|
|
|
|
|
server.on('error', err => {
|
|
|
|
if (!started) {
|
|
|
|
started = true;
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
log.error('API', err);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(config.api.port, config.api.host, () => {
|
|
|
|
if (started) {
|
|
|
|
return server.close();
|
|
|
|
}
|
|
|
|
started = true;
|
|
|
|
log.info('API', 'Server listening on %s:%s', config.api.host || '0.0.0.0', config.api.port);
|
|
|
|
done(null, server);
|
2017-03-06 22:13:40 +08:00
|
|
|
});
|
|
|
|
};
|