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

107 lines
3.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 packageInfo = require('../../../package');
const imapHandler = require('../handler/imap-handler');
2017-03-06 05:45:50 +08:00
2017-06-03 14:51:58 +08:00
const allowedKeys = ['name', 'version', 'os', 'os-version', 'vendor', 'support-url', 'address', 'date', 'command', 'arguments', 'environment'];
2017-03-06 05:45:50 +08:00
module.exports = {
2017-06-03 14:51:58 +08:00
schema: [
{
name: 'id',
type: ['null', 'array']
}
],
2017-03-06 05:45:50 +08:00
handler(command, callback) {
let clientId = {};
let serverId = {};
let serverIdList = [];
let key = false;
let maxKeyLen = 0;
if (this._server.options.id && typeof this._server.options.id === 'object') {
Object.keys(this._server.options.id).forEach(key => {
serverId[key] = this._server.options.id[key];
});
} else {
serverId.name = packageInfo.name;
serverId.version = packageInfo.version;
serverId.vendor = 'Kreata';
}
// Log ID information proviced by the client
if (Array.isArray(command.attributes[0])) {
command.attributes[0].forEach(val => {
if (key === false) {
key = (val.value || '')
.toString()
.toLowerCase()
.trim();
2017-03-06 05:45:50 +08:00
} else {
if (allowedKeys.indexOf(key) >= 0) {
clientId[key] = (val.value || '').toString();
maxKeyLen = Math.max(maxKeyLen, key.length);
}
key = false;
}
});
2017-06-03 14:51:58 +08:00
this._server.logger.info(
{
2017-05-16 18:57:04 +08:00
tnx: 'id',
cid: this.id
2017-06-03 14:51:58 +08:00
},
'[%s] Client identification data received',
this.id
);
Object.keys(clientId)
.sort((a, b) => allowedKeys.indexOf(a) - allowedKeys.indexOf(b))
.forEach(key => {
this._server.logger.info(
{
tnx: 'id',
cid: this.id
},
'[%s] %s%s: %s',
this.id,
key,
new Array(maxKeyLen - key.length + 1).join(' '),
clientId[key]
);
});
2017-03-06 05:45:50 +08:00
}
// Create response ID serverIdList
if (Object.keys(serverId).length) {
Object.keys(serverId).forEach(key => {
serverIdList.push({
type: 'string',
value: (key || '').toString()
});
serverIdList.push({
type: 'string',
value: (serverId[key] || '').toString()
});
});
}
2017-06-03 14:51:58 +08:00
this.send(
imapHandler.compiler({
tag: '*',
command: 'ID',
attributes: serverIdList.length
? [serverIdList]
: {
type: 'atom',
value: 'NIL'
}
})
);
2017-03-06 05:45:50 +08:00
callback(null, {
response: 'OK'
});
}
};