From cfd79bd3f47772f5190893f213fb79abae57cf22 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 22 Mar 2021 00:52:12 -0400 Subject: mail server --- lib/mail.js | 145 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 77 insertions(+), 68 deletions(-) (limited to 'lib/mail.js') diff --git a/lib/mail.js b/lib/mail.js index 02c7517d..4ddb02fe 100644 --- a/lib/mail.js +++ b/lib/mail.js @@ -13,85 +13,94 @@ function openInbox(cb) { imap.once("ready", function() { openInbox(function(err, box) { if (err) throw err; - let f = imap.seq.fetch(`${box.messages.total - 5}:*`, { - bodies: ["TEXT"], - struct: true - }); - f.on("message", function(msg, seqno) { - const prefix = "(#" + seqno + ") "; - msg.on("body", function(stream, info) { - let buffer = ""; - stream.on("data", function(chunk) { - //stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt', {flags:'a+'})); - buffer += chunk.toString("utf8"); - }); - stream.once("end", async function() { - if (!emails[seqno] && buffer.includes(config.imap.secret)) { - let cad = parseFloat(buffer.replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1").replace(",", "")); - emails[seqno] = cad; + setInterval(() => { + let f = imap.seq.fetch(`${box.messages.total - 5}:*`, { + bodies: ["TEXT"], + struct: true + }); - try { - await db.transaction(async transaction => { - const account = await db.Account.findOne({ - where: { - user_id: config.imap.user_id, - asset: config.liquid.btcasset, - pubkey: null - }, - include: { - model: db.User, - as: "user" - }, - lock: transaction.LOCK.UPDATE, - transaction - }); + f.on("message", function(msg, seqno) { + const prefix = "(#" + seqno + ") "; + msg.on("body", function(stream, info) { + let buffer = ""; + stream.on("data", function(chunk) { + //stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt', {flags:'a+'})); + buffer += chunk.toString("utf8"); + }); + stream.once("end", async function() { + let accounts = config.imap.accounts; + let user_id = accounts[Object.keys(accounts).find(a => buffer.includes(a))]; - let { user } = account; + if (!emails[seqno] && user_id) { + let cad = parseFloat( + buffer + .replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1") + .replace(",", "") + ); + emails[seqno] = cad; - let rate = app.get("rates")["CAD"]; - let amount = Math.round((cad / rate) * 100000000) + try { + await db.transaction(async transaction => { + const account = await db.Account.findOne({ + where: { + user_id, + asset: config.liquid.btcasset, + pubkey: null + }, + include: { + model: db.User, + as: "user" + }, + lock: transaction.LOCK.UPDATE, + transaction + }); - await account.increment({ balance: amount }, { transaction }); + let { user } = account; - let payment = await db.Payment.create({ - account_id: account.id, - user_id: account.user_id, - hash: `Interac deposit: ${amount} CAD`, - amount, - currency: "CAD", - rate, - received: true, - tip: 0, - confirmed: true, - address: "", - network: "COINOS" - }); - payment = payment.get({ plain: true }); - payment.account = account.get({ plain: true }); + let rate = app.get("rates")["CAD"]; + let amount = Math.round((cad / rate) * 100000000); + + await account.increment({ balance: amount }, { transaction }); - emit(user.username, "account", account); - emit(user.username, "payment", payment); + let payment = await db.Payment.create({ + account_id: account.id, + user_id: account.user_id, + hash: `Interac deposit: ${amount} CAD`, + amount, + currency: "CAD", + rate, + received: true, + tip: 0, + confirmed: true, + address: "", + network: "COINOS" + }); + payment = payment.get({ plain: true }); + payment.account = account.get({ plain: true }); - l.info("interac deposit detected for ", user.username, cad, amount); - }); - } catch (e) { - console.log(e); + emit(user.username, "account", account); + emit(user.username, "payment", payment); + + l.info( + "interac deposit detected for", + user.username, + cad, + amount + ); + }); + } catch (e) { + console.log(e); + } } - } + }); + }); + msg.once("end", function() { + // console.log(prefix + 'Finished'); }); - }); - msg.once("end", function() { - // console.log(prefix + 'Finished'); - }); - imap.seq.addFlags(seqno, ["Seen"]); - }); - setInterval(() => { - f = imap.seq.fetch(`${box.messages.total - 5}:*`, { - bodies: ["TEXT"], - struct: true + imap.seq.addFlags(seqno, ["Seen"]); }); - }, 3000); + }, 10000); }); }); -- cgit v1.2.3