diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb03e3e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +*.log diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..26fc37e --- /dev/null +++ b/AUTHORS @@ -0,0 +1,2 @@ +Bjarke Walling (https://bjarkewalling.com) +Khalid Rehioui (https://github.com/javascipt) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ccd29de --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2018 Bjarke WALLING, Khalid REHIOUI + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3630fad --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ + +# is-autoreply + +Returns `true` if email is an autoreply according to the email headers. + +Please [open an issue](https://github.com/walling/is-autoreply/issues) if an email isn't properly detected as autoreply :) + + +## Installation + +npm install --save is-autoreply + + +## Programmatic usage + +```js +const isAutoreply = require('is-autoreply'); +const emailHeaders = { 'X-Auto-Response-Suppress': 'OOF' }; + +if (isAutoreply(emailHeaders)) { + console.log('The email is an autoreply'); +} +``` + +## License + +[MIT](LICENSE) diff --git a/index.js b/index.js new file mode 100644 index 0000000..ccc5887 --- /dev/null +++ b/index.js @@ -0,0 +1,38 @@ + +/** + * Convert header names to lower case and normalize value to string. + * @param {Object} headers - Key/value object of headers + * @return {Object} - Normalized headers + */ +const normalizedHeaders = (headers) => { + var normalized = {}; + Object.keys(headers).forEach((key) => { + normalized[key.toLowerCase()] = "" + [headers[key]]; + }); + return normalized; +} + +/** + * Check if email is autoreply according to headers. + * @param {Object} headers - Email headers as key/value object + * @return {boolean} - True if autoreply, false otherwise + */ +module.exports = (headers) => { + // TODO: maybe if headers is a string, we can parse it as email headers using a NPM module + + headers = normalizedHeaders(headers); + + // Detections according to + if ('auto-submitted' in headers && headers['auto-submitted'].toLowerCase() !== 'no') { + return true; + } + // TODO: add more here + + // Detecting MS Exchange/Outlook according to + if ('x-auto-response-suppress' in headers && headers['x-auto-response-suppress'].toLowerCase() !== 'none') { + return true; + } + + // By default email is not autoreply + return false; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..581a495 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "is-autoreply", + "version": "0.1.0", + "description": "Detect if email is an autoreply according to the email headers.", + "main": "index.js", + "scripts": { + "test": "node test" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/walling/is-autoreply.git" + }, + "keywords": [ + "email", + "autoreply", + "auto-reply", + "headers", + "autoresponse", + "auto-reponse", + "autoresponder", + "auto-reponder" + ], + "author": "Bjarke Walling (https://bjarkewalling.com)", + "contributors": [ + "Bjarke Walling (https://bjarkewalling.com)", + "Khalid Rehioui (https://github.com/javascipt)" + ], + "license": "MIT", + "bugs": { + "url": "https://github.com/walling/is-autoreply/issues" + }, + "homepage": "https://github.com/walling/is-autoreply#readme" +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..74a06d8 --- /dev/null +++ b/test.js @@ -0,0 +1,21 @@ + +const isAutoreply = require('.'); +const assert = require('assert'); + +// Generally email is not autoreply +assert.ok(!isAutoreply({})); + +// Test Auto-Submitted header +assert.ok(!isAutoreply({ 'Auto-Submitted': 'No' })); +assert.ok(!isAutoreply({ 'auto-submitted': 'no' })); +assert.ok(isAutoreply({ 'Auto-Submitted': 'Yes' })); +assert.ok(isAutoreply({ 'auto-submitted': 'yes' })); +assert.ok(isAutoreply({ 'auto-submitted': '' })); + +// Test X-Auto-Response-Suppress header +assert.ok(!isAutoreply({ 'X-Auto-Response-Suppress': 'None' })); +assert.ok(!isAutoreply({ 'x-auto-response-suppress': 'none' })); +assert.ok(isAutoreply({ 'X-Auto-Response-Suppress': 'OOF' })); +assert.ok(isAutoreply({ 'x-auto-response-suppress': 'oof' })); +assert.ok(isAutoreply({ 'x-auto-response-suppress': 'RN, NRN' })); +assert.ok(isAutoreply({ 'x-auto-response-suppress': '' }));