aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mail.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2021-03-22 00:52:12 -0400
committerAdam Soltys <[email protected]>2021-03-22 00:52:12 -0400
commitcfd79bd3f47772f5190893f213fb79abae57cf22 (patch)
tree21b7af34cd54b99b34e7dc9b72666d04e73dd707 /lib/mail.js
parent4fa6b3aeca3730a9c8235a001cf6862ce7ce8e0c (diff)
downloadcoinos-server-cfd79bd3f47772f5190893f213fb79abae57cf22.tar.gz
coinos-server-cfd79bd3f47772f5190893f213fb79abae57cf22.zip
mail server
Diffstat (limited to 'lib/mail.js')
-rw-r--r--lib/mail.js145
1 files changed, 77 insertions, 68 deletions
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);
});
});