diff options
| author | Adam Soltys <[email protected]> | 2021-03-19 16:39:49 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2021-03-19 16:39:49 -0700 |
| commit | 0e85dc6ee1caeac4ad12414e7020864a1866cf82 (patch) | |
| tree | fe482817112d01e0319b125dfe232f28dc6803ae /mail.js | |
| parent | 70c8455d9920755b1b925046d46c486a313944c9 (diff) | |
| download | coinos-server-0e85dc6ee1caeac4ad12414e7020864a1866cf82.tar.gz coinos-server-0e85dc6ee1caeac4ad12414e7020864a1866cf82.zip | |
move mail to lib
Diffstat (limited to 'mail.js')
| -rw-r--r-- | mail.js | 168 |
1 files changed, 79 insertions, 89 deletions
@@ -1,109 +1,99 @@ -var fs = require("fs"); -var Imap = require("imap"), +const fs = require("fs"); +const Imap = require("imap"), inspect = require("util").inspect; -var imap = new Imap(config.imap); -const persist = require("./lib/persist"); +const persist = require("./persist"); const emails = persist("data/emails.json"); +let imap = new Imap(config.imap); function openInbox(cb) { imap.openBox("INBOX", true, cb); } -setInterval(() => { - imap.once("ready", function() { - openInbox(function(err, box) { - if (err) throw err; - var f = imap.seq.fetch(`${box.messages.total - 5}:*`, { - bodies: ["TEXT"], - struct: true - }); - f.on("message", function(msg, seqno) { - var prefix = "(#" + seqno + ") "; - msg.on("body", function(stream, info) { - var 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 amount = buffer.replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1"); - emails[seqno] = amount; - console.log( - seqno, - buffer.replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1") - ); - - 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 - }); +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 amount = buffer.replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1"); + emails[seqno] = amount; + console.log( + seqno, + buffer.replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1") + ); - let { user } = account; + 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 + }); - let rate = app.get("rates")["CAD"]; - console.log(rate); + let { user } = account; - await account.increment({ balance: amount }, { transaction }); - let payment = await db.Payment.create({ - account_id: account.id, - user_id: account.user_id, - hash: `Interac deposit: ${amount} CAD`, - amount: Math.round((amount / rate) * 100000000), - 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"]; + console.log(rate); - emit(user.username, "account", account); - emit(user.username, "payment", payment); + await account.increment({ balance: amount }, { transaction }); + let payment = await db.Payment.create({ + account_id: account.id, + user_id: account.user_id, + hash: `Interac deposit: ${amount} CAD`, + amount: Math.round((amount / rate) * 100000000), + currency: "CAD", + rate, + received: true, + tip: 0, + confirmed: true, + address: "", + network: "COINOS" }); - } catch (e) { - console.log(e); - } + payment = payment.get({ plain: true }); + payment.account = account.get({ plain: true }); + + emit(user.username, "account", account); + emit(user.username, "payment", payment); + }); + } catch (e) { + console.log(e); } - }); + } }); - msg.once("end", function() { - // console.log(prefix + 'Finished'); - }); - - imap.seq.addFlags(seqno, ["Seen"]); }); - f.once("error", function(err) { - console.log("Fetch error: " + err); + msg.once("end", function() { + // console.log(prefix + 'Finished'); }); - f.once("end", function() { - imap.end(); - }); - }); - }); - - imap.once("error", function(err) { - console.log(err); - }); - imap.once("end", function() { - console.log("Connection ended"); + imap.seq.addFlags(seqno, ["Seen"]); + }); + setInterval(() => { + f = imap.seq.fetch(`${box.messages.total - 5}:*`, { + bodies: ["TEXT"], + struct: true + }); + }, 3000); }); +}); - imap.connect(); -}, 30000); +imap.connect(); |
