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

178 lines
5.2 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 imapHandler = require('../handler/imap-handler');
const imapTools = require('../imap-tools');
const utf7 = require('utf7').imap;
2017-03-06 05:45:50 +08:00
// tag LIST (SPECIAL-USE) "" "%" RETURN (SPECIAL-USE)
module.exports = {
state: ['Authenticated', 'Selected'],
2017-06-03 14:51:58 +08:00
schema: [
{
name: 'selection',
type: ['array'],
optional: true
},
{
name: 'reference',
type: 'string'
},
{
name: 'mailbox',
type: 'string'
},
{
name: 'return',
type: 'atom',
optional: true
},
{
name: 'return',
type: 'array',
optional: true
}
],
2017-03-06 05:45:50 +08:00
handler(command, callback) {
let filterSpecialUseFolders = false;
let filterSpecialUseFlags = false;
let reference;
let mailbox;
let arrPos = 0;
// (SPECIAL-USE)
if (Array.isArray(command.attributes[0])) {
if (command.attributes[0].length) {
2017-06-03 14:51:58 +08:00
if (
command.attributes[0].length === 1 &&
command.attributes[0][0].type === 'ATOM' &&
command.attributes[0][0].value.toUpperCase() === 'SPECIAL-USE'
) {
2017-03-06 05:45:50 +08:00
filterSpecialUseFolders = true;
} else {
return callback(new Error('Invalid argument provided for LIST'));
}
}
arrPos++;
}
// ""
2017-06-03 14:51:58 +08:00
reference = Buffer.from((command.attributes[arrPos] && command.attributes[arrPos].value) || '', 'binary').toString();
2017-03-06 05:45:50 +08:00
arrPos++;
// "%"
2017-06-03 14:51:58 +08:00
mailbox = Buffer.from((command.attributes[arrPos] && command.attributes[arrPos].value) || '', 'binary').toString();
2017-03-06 05:45:50 +08:00
arrPos++;
// RETURN (SPECIAL-USE)
if (arrPos < command.attributes.length) {
if (command.attributes[arrPos].type === 'ATOM' && command.attributes[arrPos].value.toUpperCase() === 'RETURN') {
arrPos++;
2017-06-03 14:51:58 +08:00
if (
Array.isArray(command.attributes[arrPos]) &&
command.attributes[arrPos].length === 1 &&
command.attributes[arrPos][0].type === 'ATOM' &&
command.attributes[arrPos][0].value.toUpperCase() === 'SPECIAL-USE'
) {
2017-03-06 05:45:50 +08:00
filterSpecialUseFlags = true;
} else {
return callback(new Error('Invalid argument provided for LIST'));
}
} else {
return callback(new Error('Invalid argument provided for LIST'));
}
}
// Check if LIST method is set
if (typeof this._server.onList !== 'function') {
return callback(null, {
response: 'NO',
message: 'LIST not implemented'
});
}
let query = imapTools.normalizeMailbox(reference + mailbox, !this.acceptUTF8Enabled);
2017-03-06 05:45:50 +08:00
let listResponse = (err, list) => {
if (err) {
return callback(err);
}
imapTools.filterFolders(imapTools.generateFolderListing(list), query).forEach(folder => {
if (!folder) {
return;
}
if (filterSpecialUseFolders && !folder.specialUse) {
return;
}
let response = {
tag: '*',
command: 'LIST',
attributes: []
};
let flags = [];
if (!filterSpecialUseFlags) {
flags = flags.concat(folder.flags || []);
}
flags = flags.concat(folder.specialUse || []);
2017-06-03 14:51:58 +08:00
response.attributes.push(
flags.map(flag => ({
type: 'atom',
value: flag
}))
);
2017-03-06 05:45:50 +08:00
response.attributes.push('/');
let path = folder.path;
if (!this.acceptUTF8Enabled) {
path = utf7.encode(path);
} else {
path = Buffer.from(path);
}
response.attributes.push(path);
2017-03-06 05:45:50 +08:00
this.send(imapHandler.compiler(response));
});
callback(null, {
response: 'OK'
});
};
2017-05-16 20:23:08 +08:00
if (!mailbox && !filterSpecialUseFlags) {
2017-03-06 05:45:50 +08:00
// return delimiter only
2017-05-16 20:23:08 +08:00
let response = {
tag: '*',
command: 'LIST',
attributes: [
2017-06-03 14:51:58 +08:00
[
{
type: 'atom',
value: '\\Noselect'
}
],
'/',
'/'
2017-05-16 20:23:08 +08:00
]
};
this.send(imapHandler.compiler(response));
return callback(null, {
response: 'OK'
});
2017-03-06 05:45:50 +08:00
}
// Do folder listing
// Concat reference and mailbox. No special reference handling whatsoever
this._server.onList(query, this.session, listResponse);
}
};