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

68 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-03-06 05:45:50 +08:00
'use strict';
2017-06-03 14:51:58 +08:00
const imapTools = require('../imap-tools');
2017-03-06 05:45:50 +08:00
module.exports = {
state: 'Selected',
2017-06-03 14:51:58 +08:00
schema: [
{
name: 'range',
type: 'sequence'
},
{
name: 'mailbox',
type: 'string'
}
],
2017-03-06 05:45:50 +08:00
handler(command, callback) {
let cmd = (command.command || '').toString().toUpperCase();
// Check if COPY method is set
if (typeof this._server.onCopy !== '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();
let mailbox = imapTools.normalizeMailbox(path, !this.acceptUTF8Enabled);
2017-03-06 05:45:50 +08:00
if (!mailbox) {
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 COPY');
2017-06-03 14:51:58 +08:00
this._server.onCopy(
this.selected.mailbox,
{
destination: mailbox,
messages
},
this.session,
(err, success, info) => {
if (err) {
return callback(err);
}
2017-03-06 05:45:50 +08:00
2017-06-03 14:51:58 +08:00
let code = typeof success === 'string'
? success.toUpperCase()
: 'COPYUID ' + info.uidValidity + ' ' + imapTools.packMessageRange(info.sourceUid) + ' ' + imapTools.packMessageRange(info.destinationUid);
2017-03-06 05:45:50 +08:00
2017-06-03 14:51:58 +08:00
callback(null, {
response: success === true ? 'OK' : 'NO',
code
});
}
);
2017-03-06 05:45:50 +08:00
}
};