168 lines
5.2 KiB
JavaScript
168 lines
5.2 KiB
JavaScript
const Imap = require("imap");
|
|
const { simpleParser } = require("mailparser");
|
|
const inspect = require("util").inspect;
|
|
const saveMessage = require("./storeMessage").saveMessage;
|
|
const imapConfig = require("./config.json").mail;
|
|
|
|
const fs = require("fs");
|
|
const imap = new Imap({
|
|
user: imapConfig.user,
|
|
password: imapConfig.password,
|
|
tlsOptions: { servername: "imap.gmail.com" },
|
|
host: "imap.gmail.com",
|
|
port: 993,
|
|
tls: true,
|
|
});
|
|
|
|
// reset table;
|
|
|
|
let shouldReset = false;
|
|
// let shouldReset = true;
|
|
|
|
if (shouldReset) {
|
|
|
|
|
|
const { execQuery, execQueryAsync } = require("../sql/bdd");
|
|
const query =
|
|
"SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_schema = 'mail'";
|
|
execQueryAsync(query).then((results) => {
|
|
execQuery("SET FOREIGN_KEY_CHECKS=0");
|
|
results.map((table) => {
|
|
execQuery("DELETE FROM " + table.table_name);
|
|
|
|
});
|
|
});
|
|
return;
|
|
}
|
|
|
|
imap.once("ready", function () {
|
|
const readOnly = true;
|
|
imap.openBox("INBOX", readOnly, (err, box) => {
|
|
// console.log(box); // uidvalidty uidnext, messages total and new
|
|
imap.search(["ALL"], function (err, results) {
|
|
console.log(results[results.length - 1]);
|
|
});
|
|
|
|
const f = imap.fetch(970, {
|
|
size: true,
|
|
struct: true,
|
|
envelope: true,
|
|
});
|
|
// var f = imap.seq.fetch('1:3', {
|
|
// bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
|
|
// struct: true
|
|
// });
|
|
f.on("message", function (msg, seqno) {
|
|
// console.log("Message #%d", seqno);
|
|
// var prefix = "(#" + seqno + ") ";
|
|
// msg.on("body", function (stream, info) {
|
|
// simpleParser(stream, async (err, parsed) => {
|
|
// // find box id;
|
|
// // console.log(parsed)
|
|
// const boxId = 1;
|
|
// saveMessage(parsed, boxId);
|
|
// // console.log(parsed.subject);
|
|
// // fs.writeFileSync("./test.txt", JSON.stringify(parsed));
|
|
// });
|
|
// // console.log(prefix + 'Body');
|
|
// // stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt'));
|
|
// });
|
|
msg.once("attributes", (attrs) => {
|
|
// todo find boxId
|
|
const boxId = 1;
|
|
// console.log(attrs)
|
|
saveMessage(attrs, boxId, imap);
|
|
});
|
|
});
|
|
|
|
f.once("error", function (err) {
|
|
console.log("Fetch error: " + err);
|
|
});
|
|
f.once("end", function () {
|
|
console.log("Done fetching all messages!");
|
|
// imap.end();
|
|
});
|
|
// });
|
|
return;
|
|
// if (err) throw err;
|
|
// const f = imap.seq.fetch('2:2', {
|
|
// bodies: ['HEADER.FIELDS (FROM)','TEXT'],
|
|
// struct: true,
|
|
// envelope: true,
|
|
// extensions: true
|
|
// });
|
|
|
|
// f.on('message', function(msg, seqno) {
|
|
// // console.log('Message #%d', seqno);
|
|
// var prefix = '(#' + seqno + ') ';
|
|
// let attributes = undefined;
|
|
// let body = undefined;
|
|
|
|
// msg.on('body', function(stream, info) {
|
|
// simpleParser(stream, async (err, parsed) => {
|
|
// body = parsed;
|
|
// // console.log(body)
|
|
// if (attributes) {
|
|
// saveMessage(body, attributes);
|
|
// };
|
|
// // console.log(parsed.headers)
|
|
// // const {from, subject, textAsHtml, text} = parsed;
|
|
// // console.log(parsed.attachments)
|
|
// // console.log(prefix + parsed.text)
|
|
// // console.log(parsed.from.value);
|
|
// // console.log(parsed.subject);
|
|
// // console.log(parsed.date)
|
|
// // console.log(parsed.replyTo.value);
|
|
// // console.log(parsed.messageId);
|
|
// // console.log(parsed.html);
|
|
// // console.log(parsed.text);
|
|
// // console.log(parsed.textAsHtml);
|
|
// });
|
|
// });
|
|
|
|
// msg.once('attributes', attrs => {
|
|
// attributes = attrs;
|
|
// console.log(attributes)
|
|
// if (body) {
|
|
// saveMessage(body, attributes);
|
|
// };
|
|
// // console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
|
|
|
|
// });
|
|
|
|
// msg.once('end', function() {
|
|
// console.log(prefix + 'Finished');
|
|
// });
|
|
|
|
// });
|
|
|
|
// f.once('error', function(err) {
|
|
// console.log('Fetch error: ' + err);
|
|
// });
|
|
|
|
// f.once('end', function() {
|
|
// console.log('Done fetching all messages!');
|
|
// imap.end();
|
|
// });
|
|
});
|
|
});
|
|
|
|
imap.once("error", function (err) {
|
|
console.log(err);
|
|
});
|
|
|
|
imap.once("end", function () {
|
|
console.log("Connection ended");
|
|
});
|
|
|
|
imap.connect();
|
|
|
|
function isValidEmail(email) {
|
|
// todo
|
|
return true;
|
|
}
|
|
|
|
module.exports = {
|
|
isValidEmail,
|
|
};
|