Guest User

Untitled

a guest
Jun 20th, 2018
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. var Promise = require("bluebird");
  2. var Imap = require('imap'),
  3. inspect = require('util').inspect;
  4. let buffer = '';
  5. let OTPCode = '';
  6. let OTPRegex = /\d{8,}/;
  7.  
  8. var imap = new Imap({
  9. user: "example@gmail.com",
  10. password: "etc",
  11. host: "mail.google.net",
  12. port: 993,
  13. tls: true,
  14. connTimeout: 10000, // Default by node-imap
  15. authTimeout: 5000, // Default by node-imap,
  16. debug: null, // Or your custom function with only one incoming argument. Default: null
  17. tlsOptions: { rejectUnauthorized: false },
  18. mailbox: "INBOX", // mailbox to monitor
  19. searchFilter: ["UNSEEN", "FLAGGED"], // the search filter being used after an IDLE notification has been retrieved
  20. markSeen: true,
  21. fetchUnreadOnStart: false,
  22. mailParserOptions: { streamAttachments: false },
  23. attachments: false,
  24. attachmentOptions: { directory: "attachments/" }
  25. });
  26.  
  27. function openInbox(cb) {
  28. imap.openBox('INBOX', false, cb);
  29. }
  30.  
  31. imap.once('ready', function () {
  32. openInbox(function (err, box) {
  33. if (err) throw err;
  34. imap.search(['UNSEEN', ['SUBJECT', 'Requested LexisNexis(R) One-Time Passcode (OTP)']], function (err, results) {
  35. if (err) throw err;
  36. var f = imap.fetch(results, { bodies: '1', markSeen: true });
  37. f.on('message', function (msg, seqno) {
  38. msg.on('body', function (stream, info) {
  39. stream.on('data', function (chunk) {
  40. buffer += chunk.toString('utf8');
  41. });
  42. stream.once('end', function () {
  43. if (info.which === '1') {
  44. var res = buffer.match(OTPRegex);
  45. OTPCode = res[0];
  46. console.log(OTPCode);
  47. }
  48. });
  49. });
  50. msg.once('attributes', function (attrs) {
  51. });
  52. msg.once('end', function () {
  53. });
  54. });
  55. f.once('error', function (err) {
  56. console.log('Fetch error.');
  57. });
  58. f.once('end', function () {
  59. console.log('Done fetching OTP codes.');
  60. imap.end();
  61. });
  62. });
  63. });
  64. });
  65.  
  66. imap.once('error', function (err) {
  67. console.log('An error occured fetching the OTP code.');
  68. });
  69.  
  70. imap.once('end', function () {
  71. console.log('Connection terminated.');
  72. });
  73.  
  74.  
  75. module.exports = {
  76. fetchOTP: async function() {
  77. imap.connect();
  78. return OTPCode.toString();
  79.  
  80. }
  81. }
Add Comment
Please, Sign In to add comment