From 01574b8967ff0332373c411a992eded5360c2ac0 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Tue, 11 Aug 2020 11:12:40 +0200 Subject: [PATCH] new method for imap --- back/methods/imap-time-control/checkInbox.js | 129 +++++++++++++ package-lock.json | 183 ++++++++++++++++++- package.json | 2 + 3 files changed, 309 insertions(+), 5 deletions(-) create mode 100644 back/methods/imap-time-control/checkInbox.js diff --git a/back/methods/imap-time-control/checkInbox.js b/back/methods/imap-time-control/checkInbox.js new file mode 100644 index 000000000..ff0b0a46e --- /dev/null +++ b/back/methods/imap-time-control/checkInbox.js @@ -0,0 +1,129 @@ +const Imap = require('imap'); + +module.exports = Self => { + Self.remoteMethod('checkInbox', { + description: 'Check an email inbox and process it', + accessType: 'READ', + returns: + { + arg: 'body', + type: 'file', + root: true + }, + http: { + path: `/checkInbox`, + verb: 'GET' + } + }); + + Self.checkInbox = async() => { + let imapConfig = await Self.app.models.WorkerTimeControlParams.findOne(); + let imap = new Imap({ + user: imapConfig.mailUser, + password: imapConfig.mailPass, + host: imapConfig.mailHost, + port: 993, + tls: true + }); + let isEmailOk; + let emailText = ''; + + function openInbox(cb) { + imap.openBox('INBOX', true, cb); + } + + imap.once('ready', function() { + openInbox(function(err, box) { + if (err) throw err; + let f = imap.seq.fetch('1:3', { + bodies: ['HEADER.FIELDS (FROM SUBJECT)', '1'], + struct: true + }); + f.on('message', function(msg, seqno) { + isEmailOk = false; + msg.on('body', function(stream, info) { + let buffer = ''; + stream.on('data', function(chunk) { + buffer = chunk.toString('utf8'); + if (info.which === '1') { + emailText = buffer.split('\n')[0]; + emailText = emailText.toUpperCase(); + emailText = emailText.replace(/\s/g, ''); + if (emailText === 'OK') + isEmailOk = true; // isEmailOk + } + }); + stream.once('end', function() { + if (info.which === 'HEADER.FIELDS (FROM SUBJECT)') { + console.log('isEmailOk', isEmailOk); + if (isEmailOk) { + emailConfirm(buffer); + imap.seq.move(seqno, 'exito', function(err) { + console.log('Move correcte: ' + err); + console.log('Move error: ' + seqno); + }); + } else { + emailReply(buffer); + imap.seq.move(seqno, 'error', function(err) { + console.log('Move error: ' + err); + console.log('Move error: ' + seqno); + }); + } + } + }); + }); + }); + // msg.on('attributes', function(attrs) { + // console.log('ATRIBUTO', attrs.struct[0]); + // console.log('ATRIBUTO', attrs.struct[0].params); + // }); + }); + }); + + imap.connect(); + return 'pepinillos'; + }; + async function getUser(workerEmail) { + let firstPosition = workerEmail.search('<') + 1; + let lastPosition = workerEmail.search('@') - firstPosition; + let userName = workerEmail.substr(firstPosition, lastPosition); + let user = await Self.app.models.Account.findOne({where: {name: userName}}); + return user; + } + + async function getEmailDate(subject) { + let date = subject.match(/\d+/g); + return date; + } + + async function emailConfirm(buffer) { + let now = new Date(); + let from = JSON.stringify(Imap.parseHeader(buffer).from); + let subject = JSON.stringify(Imap.parseHeader(buffer).subject); + + let timeControlDate = await getEmailDate(subject); + let week = timeControlDate[0]; + let year = timeControlDate[1]; + let user = await getUser(from); + + let workerMail = await Self.app.models.WorkerTimeControlMail.findOne({ + where: { + week: week, + year: year, + worker: user.id + } + }); + await workerMail.updateAttributes({ + updated: now, + state: 'CONFIRMED' + }); + // MOVER A CARPETA EXITO + } + async function emailReply(subject, workerEmail) { + console.log('bbbbb'); + + // INSERT EN VN.MAIL + // UPDATE EN WORKERTIMECONTROLMAIL + // MOVER A CARPETA ERROR + } +}; diff --git a/package-lock.json b/package-lock.json index 37af78e3a..8bd4ddb07 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8795,6 +8795,11 @@ "iconv-lite": "~0.4.13" } }, + "encoding-japanese": { + "version": "1.0.30", + "resolved": "https://registry.npmjs.org/encoding-japanese/-/encoding-japanese-1.0.30.tgz", + "integrity": "sha512-bd/DFLAoJetvv7ar/KIpE3CNO8wEuyrt9Xuw6nSMiZ+Vrz/Q21BPsMHvARL2Wz6IKHKXgb+DWZqtRg1vql9cBg==" + }, "end-of-stream": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.1.tgz", @@ -12288,8 +12293,7 @@ "he": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz", - "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", - "dev": true + "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==" }, "helmet": { "version": "3.21.2", @@ -12489,6 +12493,17 @@ } } }, + "html-to-text": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/html-to-text/-/html-to-text-5.1.1.tgz", + "integrity": "sha512-Bci6bD/JIfZSvG4s0gW/9mMKwBRoe/1RWLxUME/d6WUSZCdY7T60bssf/jFf7EYXRyqU4P5xdClVqiYU0/ypdA==", + "requires": { + "he": "^1.2.0", + "htmlparser2": "^3.10.1", + "lodash": "^4.17.11", + "minimist": "^1.2.0" + } + }, "html-webpack-plugin": { "version": "4.0.0-beta.11", "resolved": "https://registry.npmjs.org/html-webpack-plugin/-/html-webpack-plugin-4.0.0-beta.11.tgz", @@ -12724,6 +12739,38 @@ "integrity": "sha1-SMptcvbGo68Aqa1K5odr44ieKwk=", "dev": true }, + "imap": { + "version": "0.8.19", + "resolved": "https://registry.npmjs.org/imap/-/imap-0.8.19.tgz", + "integrity": "sha1-NniHOTSrCc6mukh0HyhNoq9Z2NU=", + "requires": { + "readable-stream": "1.1.x", + "utf7": ">=1.0.2" + }, + "dependencies": { + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, + "readable-stream": { + "version": "1.1.14", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" + } + } + }, "import-fresh": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.1.0.tgz", @@ -13488,7 +13535,7 @@ "jasmine-spec-reporter": { "version": "4.2.1", "resolved": "https://registry.npmjs.org/jasmine-spec-reporter/-/jasmine-spec-reporter-4.2.1.tgz", - "integrity": "sha512-FZBoZu7VE5nR7Nilzy+Np8KuVIOxF4oXDPDknehCYBDE080EnlPu0afdZNmpGDBRCUBv3mj5qgqCRmk6W/K8vg==", + "integrity": "sha1-HWMq7ANBZwrTJPkrqEtLMrNeniI=", "dev": true, "requires": { "colors": "1.1.2" @@ -18481,6 +18528,32 @@ "type-check": "~0.3.2" } }, + "libbase64": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/libbase64/-/libbase64-1.2.1.tgz", + "integrity": "sha512-l+nePcPbIG1fNlqMzrh68MLkX/gTxk/+vdvAb388Ssi7UuUN31MI44w4Yf33mM3Cm4xDfw48mdf3rkdHszLNew==" + }, + "libmime": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/libmime/-/libmime-5.0.0.tgz", + "integrity": "sha512-2Bm96d5ktnE217Ib1FldvUaPAaOst6GtZrsxJCwnJgi9lnsoAKIHyU0sae8rNx6DNYbjdqqh8lv5/b9poD8qOg==", + "requires": { + "encoding-japanese": "1.0.30", + "iconv-lite": "0.6.2", + "libbase64": "1.2.1", + "libqp": "1.1.0" + }, + "dependencies": { + "iconv-lite": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.2.tgz", + "integrity": "sha512-2y91h5OpQlolefMPmUlivelittSWy0rP+oYVpn6A7GwVHNE8AWzoYOBNmlwks3LobaJxgHCYZAnyNo2GgpNRNQ==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + } + } + } + }, "liboneandone": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/liboneandone/-/liboneandone-1.2.0.tgz", @@ -18490,6 +18563,11 @@ "request": "^2.74.0" } }, + "libqp": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/libqp/-/libqp-1.1.0.tgz", + "integrity": "sha1-9ebgatdLeU+1tbZpiL9yjvHe2+g=" + }, "liftoff": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/liftoff/-/liftoff-3.1.0.tgz", @@ -18512,6 +18590,14 @@ "integrity": "sha1-HADHQ7QzzQpOgHWPe2SldEDZ/wA=", "dev": true }, + "linkify-it": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-3.0.2.tgz", + "integrity": "sha512-gDBO4aHNZS6coiZCKVhSNh43F9ioIL4JwRjLZPkoLIY4yZFwg264Y5lu2x6rb1Js42Gh6Yqm2f6L2AJcnkzinQ==", + "requires": { + "uc.micro": "^1.0.1" + } + }, "load-json-file": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz", @@ -19258,6 +19344,68 @@ "yallist": "^3.0.2" } }, + "mailparser": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/mailparser/-/mailparser-2.8.0.tgz", + "integrity": "sha512-Ja5H6/I1NKIFdFJF5HdZkaMGCN8F+1lu3870ZmrabkHQshhrPk4OWeEMiF0OVo82+6XJqemJPCg6pqgXaj3B6Q==", + "requires": { + "encoding-japanese": "1.0.30", + "he": "1.2.0", + "html-to-text": "5.1.1", + "iconv-lite": "0.6.2", + "libmime": "5.0.0", + "linkify-it": "3.0.2", + "mailsplit": "5.0.0", + "nodemailer": "6.4.10", + "tlds": "1.207.0" + }, + "dependencies": { + "iconv-lite": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.2.tgz", + "integrity": "sha512-2y91h5OpQlolefMPmUlivelittSWy0rP+oYVpn6A7GwVHNE8AWzoYOBNmlwks3LobaJxgHCYZAnyNo2GgpNRNQ==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + } + }, + "nodemailer": { + "version": "6.4.10", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.4.10.tgz", + "integrity": "sha512-j+pS9CURhPgk6r0ENr7dji+As2xZiHSvZeVnzKniLOw1eRAyM/7flP0u65tCnsapV8JFu+t0l/5VeHsCZEeh9g==" + } + } + }, + "mailsplit": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/mailsplit/-/mailsplit-5.0.0.tgz", + "integrity": "sha512-HeXA0eyCKBtZqbr7uoeb3Nn2L7VV8Vm27x6/YBb0ZiNzRzLoNS2PqRgGYADwh0cBzLYtqddq40bSSirqLO2LGw==", + "requires": { + "libbase64": "1.2.1", + "libmime": "4.2.1", + "libqp": "1.1.0" + }, + "dependencies": { + "iconv-lite": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.5.0.tgz", + "integrity": "sha512-NnEhI9hIEKHOzJ4f697DMz9IQEXr/MMJ5w64vN2/4Ai+wRnvV7SBrL0KLoRlwaKVghOc7LQ5YkPLuX146b6Ydw==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "libmime": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/libmime/-/libmime-4.2.1.tgz", + "integrity": "sha512-09y7zjSc5im1aNsq815zgo4/G3DnIzym3aDOHsGq4Ee5vrX4PdgQRybAsztz9Rv0NhO+J5C0llEUloa3sUmjmA==", + "requires": { + "encoding-japanese": "1.0.30", + "iconv-lite": "0.5.0", + "libbase64": "1.2.1", + "libqp": "1.1.0" + } + } + } + }, "make-dir": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-2.1.0.tgz", @@ -24464,6 +24612,11 @@ "setimmediate": "^1.0.4" } }, + "tlds": { + "version": "1.207.0", + "resolved": "https://registry.npmjs.org/tlds/-/tlds-1.207.0.tgz", + "integrity": "sha512-k7d7Q1LqjtAvhtEOs3yN14EabsNO8ZCoY6RESSJDB9lst3bTx3as/m1UuAeCKzYxiyhR1qq72ZPhpSf+qlqiwg==" + }, "tmp": { "version": "0.0.33", "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz", @@ -24748,6 +24901,11 @@ "is-typedarray": "^1.0.0" } }, + "uc.micro": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz", + "integrity": "sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==" + }, "uglify-js": { "version": "3.4.10", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.4.10.tgz", @@ -25088,6 +25246,21 @@ "integrity": "sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ==", "dev": true }, + "utf7": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/utf7/-/utf7-1.0.2.tgz", + "integrity": "sha1-lV9JCq5lO6IguUVqCod2wZk2CZE=", + "requires": { + "semver": "~5.3.0" + }, + "dependencies": { + "semver": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.3.0.tgz", + "integrity": "sha1-myzl094C0XxgEq0yaqa00M9U+U8=" + } + } + }, "utf8-bytes": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/utf8-bytes/-/utf8-bytes-0.0.1.tgz", @@ -25877,7 +26050,7 @@ }, "globby": { "version": "6.1.0", - "resolved": "http://registry.npmjs.org/globby/-/globby-6.1.0.tgz", + "resolved": "https://registry.npmjs.org/globby/-/globby-6.1.0.tgz", "integrity": "sha1-9abXDoOV4hyFj7BInWTfAkJNUGw=", "dev": true, "requires": { @@ -25890,7 +26063,7 @@ "dependencies": { "pify": { "version": "2.3.0", - "resolved": "http://registry.npmjs.org/pify/-/pify-2.3.0.tgz", + "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=", "dev": true } diff --git a/package.json b/package.json index f9d291467..b407bdfef 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "fs-extra": "^5.0.0", "helmet": "^3.21.2", "i18n": "^0.8.4", + "imap": "^0.8.19", "loopback": "^3.26.0", "loopback-boot": "^2.27.1", "loopback-component-explorer": "^6.5.0", @@ -23,6 +24,7 @@ "loopback-connector-mysql": "^5.4.3", "loopback-connector-remote": "^3.4.1", "loopback-context": "^3.4.0", + "mailparser": "^2.8.0", "md5": "^2.2.1", "object-diff": "0.0.4", "object.pick": "^1.3.0",