started with filters

This commit is contained in:
Andris Reinman 2017-04-15 21:59:27 +03:00
parent dd2d43eed4
commit f9a28e1e60
5 changed files with 104 additions and 40 deletions

View file

@ -446,7 +446,7 @@ Response includes the following fields
- **id** is the message ID
- **date** is the date when this message was received
- **hasAttachments** is a boolean that indicates if this messages has attachments or not
- **ha** is a boolean that indicates if this messages has attachments or not
- **intro** includes the first 256 characters from the message
- **subject** is the message title
- **from** is the From: field
@ -469,7 +469,7 @@ The response for successful listing should look like this:
{
"id": "58e25243ab71621c3890417e",
"date": "2017-04-03T13:46:44.226Z",
"hasAttachments": true,
"ha": true,
"intro": "Welcome to Ryan Finnie's MIME torture test. This message was designed to introduce a couple of the newer features of MIME-aware MUAs, features that have come around since the days of the original MIME torture test. Just to be clear, this message SUPPLEMENT…",
"subject": "ryan finnie's mime torture test v1.0",
"from": "ryan finnie <rfinnie@domain.dom>",

4
api.js
View file

@ -854,7 +854,7 @@ server.get('/mailbox/:id', (req, res, next) => {
mailbox: true,
idate: true,
headers: true,
hasAttachments: true,
ha: true,
intro: true
}).sort(sort).limit(size).toArray((err, messages) => {
if (err) {
@ -899,7 +899,7 @@ server.get('/mailbox/:id', (req, res, next) => {
let response = {
id: message._id,
date: message.idate,
hasAttachments: message.hasAttachments,
ha: message.ha,
intro: message.intro
};

View file

@ -147,10 +147,10 @@
"draft": 1
}
}, {
"name": "has_attachments",
"name": "has_attachment",
"key": {
"mailbox": 1,
"hasAttachments": 1
"ha": 1
}
}]
}, {

View file

@ -235,9 +235,9 @@ class MessageHandler {
if (maildata.attachments && maildata.attachments.length) {
message.attachments = maildata.attachments;
message.hasAttachments = true;
message.ha = true;
} else {
message.hasAttachments = false;
message.ha = false;
}
let maxTextLength = 300 * 1024;

128
lmtp.js
View file

@ -153,6 +153,49 @@ const serverOptions = {
let mailboxQueryKey = 'path';
let mailboxQueryValue = 'INBOX';
let filters = [
// example filter
{
query: {
headers: {
from: 'abc',
to: 'def',
subject: 'ghi'
},
text: 'jkl',
// positive: must have attachments, negative: no attachments
ha: 1,
// positive: larger than size, netaive: smaller than abs(size)
size: 10
},
action: {
// mark message as seen
seen: true,
// mark message as flagged
flag: true,
// set mailbox ID
mailbox: 'aaaaa',
// positive spam, negative ham
spam: 1,
// if true, delete message
delete: false
}
}
].concat(spamHeader ? {
query: {
headers: {
[spamHeader]: 'Yes'
}
},
action: {
spam: true
}
} : []);
let filterResults = checkFilters(prepared, filters);
// TODO: apply filter result
// apply filters
if (spamHeader) {
for (let i = prepared.headers.length - 1; i >= 0; i--) {
@ -250,44 +293,65 @@ module.exports = done => {
done(null, server);
});
};
/*
function generateReceivedHeader(session, queueId, hostname, recipient) {
let origin = session.remoteAddress ? '[' + session.remoteAddress + ']' : '';
let originhost = session.clientHostname && session.clientHostname.charAt(0) !== '[' ? session.clientHostname : false;
origin = [].concat(origin || []).concat(originhost || []);
if (origin.length > 1) {
origin = '(' + origin.join(' ') + ')';
} else {
origin = origin.join(' ').trim() || 'localhost';
function checkFilters(prepared, filters) {
if (!filters || !filters.length) {
return false;
}
let value = '' +
// from ehlokeyword
'from' + (session.hostNameAppearsAs ? ' ' + session.hostNameAppearsAs : '') +
// [1.2.3.4]
' ' + origin +
(originhost ? '\r\n' : '') +
for (let i = 0; i < filters.length; i++) {
let filter = filters[i];
// by smtphost
' by ' + hostname +
// prepare filter data
let headerFilters = new Map();
if (filter.headers) {
Object.keys(filter.headers).forEach(key => {
headerFilters.set(key, (filter.headers[key] || '').toString().toLowerCase());
});
}
// with ESMTP
' with ' + session.transmissionType +
// id 12345678
' id ' + queueId +
'\r\n' +
// check headers
if (headerFilters.size) {
let headerMatches = new Set();
for (let j = prepared.headers.length - 1; j >= 0; j--) {
let header = prepared.headers[j];
if (headerFilters.has(header.key) && header.value.indexOf(headerFilters.get(header.key)) >= 0) {
headerMatches.add(header.key);
}
}
if (headerMatches.size < headerFilters.size) {
// not enough matches
continue;
}
}
// for <receiver@example.com>
' for <' + recipient + '>' +
// (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256)
(session.tlsOptions ? '\r\n (version=' + session.tlsOptions.version + ' cipher=' + session.tlsOptions.name + ')' : '') +
if (filter.ha) {
// FIXME: there is no prepared.maildata :(
let hasAttachments = prepared.maildata && prepared.maildata.attachments && prepared.maildata.attachments.length;
if (hasAttachments && filter.ha < 0) {
continue;
}
}
';' +
'\r\n' +
if (filter.size) {
let messageSize = prepared.size;
let filterSize = Math.abs(filter.size);
// negative value means "less than", positive means "more than"
if (filter.size < 0 && messageSize > filterSize) {
continue;
}
if (filter.size > 0 && messageSize < filterSize) {
continue;
}
}
// Wed, 03 Aug 2016 11:32:07 +0000
' ' + new Date().toUTCString().replace(/GMT/, '+0000');
return value;
if (filter.text) {
// TODO: check against plaintext version of the message
}
// we reached the end of the filter, so this means we have a match
return filter.action;
}
return false;
}
*/