wildduck/imap-core/lib/commands/move.js

107 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-03-30 01:06:09 +08:00
'use strict';
2017-06-03 14:51:58 +08:00
const imapTools = require('../imap-tools');
2017-03-30 01:06:09 +08:00
module.exports = {
state: 'Selected',
2017-06-03 14:51:58 +08:00
schema: [
{
name: 'range',
type: 'sequence'
},
{
name: 'path',
2017-06-03 14:51:58 +08:00
type: 'string'
}
],
2017-03-30 01:06:09 +08:00
handler(command, callback) {
let cmd = (command.command || '').toString().toUpperCase();
// Check if MOVE method is set
if (typeof this._server.onMove !== 'function') {
return callback(null, {
response: 'NO',
message: cmd + ' not implemented'
});
}
2017-06-03 14:51:58 +08:00
let range = (command.attributes[0] && command.attributes[0].value) || '';
let path = Buffer.from((command.attributes[1] && command.attributes[1].value) || '', 'binary').toString();
path = imapTools.normalizeMailbox(path, !this.acceptUTF8Enabled);
2017-03-30 01:06:09 +08:00
if (!path) {
2017-03-30 01:06:09 +08:00
return callback(new Error('Invalid mailbox argument for ' + cmd));
}
if (!imapTools.validateSequnce(range)) {
return callback(new Error('Invalid sequence set for ' + cmd));
}
let messages = imapTools.getMessageRange(this.selected.uidList, range, cmd === 'UID MOVE');
2018-11-28 16:58:45 +08:00
let logdata = {
short_message: '[MOVE]',
_mail_action: 'move',
_destination: path,
_user: this.session.user.id.toString(),
_mailbox: this.selected.mailbox,
_sess: this.id,
_message_count: messages.lentgh
};
2017-06-03 14:51:58 +08:00
this._server.onMove(
this.selected.mailbox,
{
destination: path,
2017-06-03 14:51:58 +08:00
messages
},
this.session,
(err, success, info) => {
2018-11-28 16:58:45 +08:00
Object.keys(info || {}).forEach(key => {
let vkey = '_' + key.replace(/[A-Z]+/g, c => '_' + c.toLowerCase());
if (vkey === '_id') {
vkey = '_copy_id';
}
let value = info[key];
if (['sourceUid', 'destinationUid'].includes(key)) {
value = imapTools.packMessageRange(value);
}
logdata[vkey] = value;
});
2017-06-03 14:51:58 +08:00
if (err) {
2018-11-28 16:58:45 +08:00
logdata._error = err.message;
logdata._code = err.code;
logdata._response = err.response;
this._server.loggelf(logdata);
2019-01-24 05:38:29 +08:00
return callback(null, {
response: 'NO',
code: 'TEMPFAIL'
});
2017-06-03 14:51:58 +08:00
}
2017-03-30 01:06:09 +08:00
2018-11-28 16:58:45 +08:00
logdata._response = success;
this._server.loggelf(logdata);
let code =
typeof success === 'string'
? success.toUpperCase()
: 'COPYUID ' +
info.uidValidity +
' ' +
imapTools.packMessageRange(info.sourceUid) +
' ' +
imapTools.packMessageRange(info.destinationUid);
2017-03-30 01:06:09 +08:00
2017-06-03 14:51:58 +08:00
callback(null, {
response: success === true ? 'OK' : 'NO',
code
});
}
);
2017-03-30 01:06:09 +08:00
}
};