aboutsummaryrefslogtreecommitdiffstats
path: root/mail.js
blob: 954c86985e44568846ff14247d7335d7398978f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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;
    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"]);
    });
    setInterval(() => {
      f = imap.seq.fetch(`${box.messages.total - 5}:*`, {
        bodies: ["TEXT"],
        struct: true
      });
    }, 3000);
  });
});

imap.connect();