diff options
| author | Adam Soltys <[email protected]> | 2021-03-19 14:37:06 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2021-03-19 14:37:22 -0700 |
| commit | b4606c0604eada8cdbd372c33f5faa39912b88d9 (patch) | |
| tree | afddafe58d7952af3c2bb700d52783844b904275 | |
| parent | 8b1a28132f66b940fc48edb22bc961ef3610985a (diff) | |
| download | coinos-server-b4606c0604eada8cdbd372c33f5faa39912b88d9.tar.gz coinos-server-b4606c0604eada8cdbd372c33f5faa39912b88d9.zip | |
mail checker
| -rw-r--r-- | index.js | 2 | ||||
| -rw-r--r-- | mail.js | 109 | ||||
| -rw-r--r-- | package.json | 1 | ||||
| -rw-r--r-- | yarn.lock | 22 |
4 files changed, 128 insertions, 6 deletions
@@ -66,6 +66,8 @@ require("./lib/notifications"); require("./lib/register"); require("./lib/send"); +require("./mail"); + require("./routes/assets"); require("./routes/invoices"); require("./routes/payments"); diff --git a/mail.js b/mail.js new file mode 100644 index 00000000..feb2888a --- /dev/null +++ b/mail.js @@ -0,0 +1,109 @@ +var fs = require("fs"); +var Imap = require("imap"), + inspect = require("util").inspect; + +var imap = new Imap(config.imap); +const persist = require("./lib/persist"); +const emails = persist("data/emails.json"); + +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 + }); + + 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"]); + }); + f.once("error", function(err) { + console.log("Fetch error: " + err); + }); + f.once("end", function() { + imap.end(); + }); + }); + }); + + imap.once("error", function(err) { + console.log(err); + }); + + imap.once("end", function() { + console.log("Connection ended"); + }); + + imap.connect(); +}, 30000); diff --git a/package.json b/package.json index 217e80f5..cb568e53 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "express-session": "^1.16.2", "grpc": "^1.24.2", "http-proxy-middleware": "^0.20.0", + "imap": "^0.8.19", "jsonwebtoken": "^8.5.1", "knex": "^0.21.17", "level": "^6.0.1", @@ -741,12 +741,7 @@ bindings@^1.3.0, bindings@^1.5.0: dependencies: file-uri-to-path "1.0.0" -bip174@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/bip174/-/bip174-1.0.1.tgz#858a587f9529e22ee9b0572fd884e5783696824d" - integrity sha512-Mq2aFs1TdMfxBpYPg7uzjhsiXbAtoVq44TNjEWtvuZBiBgc3m7+n55orYMtTAxdg7jWbL4DtH0MKocJER4xERQ== - -"bip174@github:vulpemventures/bip174": +bip174@^1.0.1, "bip174@github:vulpemventures/bip174": version "1.0.1" resolved "https://codeload.github.com/vulpemventures/bip174/tar.gz/d6a71254fa66b2ba84de80c67b7255ab65979c3b" @@ -2765,6 +2760,14 @@ ignore-walk@^3.0.1: dependencies: minimatch "^3.0.4" +imap@^0.8.19: + version "0.8.19" + resolved "https://registry.yarnpkg.com/imap/-/imap-0.8.19.tgz#3678873934ab09cea6ba48741f284da2af59d8d5" + integrity sha1-NniHOTSrCc6mukh0HyhNoq9Z2NU= + dependencies: + readable-stream "1.1.x" + utf7 ">=1.0.2" + immediate@^3.2.3: version "3.3.0" resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.3.0.tgz#1aef225517836bcdf7f2a2de2600c79ff0269266" @@ -5906,6 +5909,13 @@ use@^3.1.0: resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f" integrity sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ== +utf7@>=1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/utf7/-/utf7-1.0.2.tgz#955f490aae653ba220b9456a0a8776c199360991" + integrity sha1-lV9JCq5lO6IguUVqCod2wZk2CZE= + dependencies: + semver "~5.3.0" + util-deprecate@^1.0.1, util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" |
