diff options
| -rw-r--r-- | index.js | 2 | ||||
| -rw-r--r-- | lib/mail.js | 96 | ||||
| -rw-r--r-- | lib/persist.js | 32 | ||||
| -rw-r--r-- | mail.js | 168 |
4 files changed, 194 insertions, 104 deletions
@@ -66,7 +66,7 @@ require("./lib/notifications"); require("./lib/register"); require("./lib/send"); -require("./mail"); +require("./lib/mail"); require("./routes/assets"); require("./routes/invoices"); diff --git a/lib/mail.js b/lib/mail.js new file mode 100644 index 00000000..353954f1 --- /dev/null +++ b/lib/mail.js @@ -0,0 +1,96 @@ +const fs = require("fs"); +const Imap = require("imap"), + inspect = require("util").inspect; + +const persist = require("./persist"); +const emails = persist("data/emails.json"); + +let imap = new Imap(config.imap); +function openInbox(cb) { + imap.openBox("INBOX", true, cb); +} + +imap.once("ready", function() { + openInbox(function(err, box) { + if (err) throw err; + setInterval(() => { + 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") + ); + + 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 { user } = account; + + let rate = app.get("rates")["CAD"]; + console.log(rate); + + 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 }); + + 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"]); + }); + }, 3000); + }); +}); + +imap.connect(); diff --git a/lib/persist.js b/lib/persist.js index 24d00693..54e7e209 100644 --- a/lib/persist.js +++ b/lib/persist.js @@ -1,18 +1,22 @@ -fs = require("fs"); +const fs = require("fs"); module.exports = (path, initial = {}) => { - try { - return require("../" + path); - } catch { - return new Proxy(initial, { - set(obj, prop, value) { - fs.writeFileSync( - path, - JSON.stringify({ ...obj, [prop]: value }, null, 2), - "utf-8" - ); - return Reflect.set(...arguments); - } - }); + if (!initial || !Object.keys(initial).length) { + try { + initial = require("../" + path); + } catch(e) {} } + + // console.log("initial", path, initial); + + return new Proxy(initial, { + set(obj, prop, value) { + fs.writeFileSync( + path, + JSON.stringify({ ...obj, [prop]: value }, null, 2), + "utf-8" + ); + return Reflect.set(...arguments); + } + }); }; @@ -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(); |
