aboutsummaryrefslogtreecommitdiffstats
path: root/server.c
diff options
context:
space:
mode:
Diffstat (limited to 'server.c')
0 files changed, 0 insertions, 0 deletions
href='#n51'>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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
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);
}

app.post(
  "/contact-sil",
  ah((req, res) => {
    const mailgun = require("mailgun-js")(config.mailgun);
    let data = {
      subject: "Contact form",
      text: JSON.stringify(req.body),
      from: "[email protected]",
      to: "[email protected]"
    };

    mailgun.messages().send(data);
    res.send(req.body);
  })
);

app.post(
  "/contact",
  ah((req, res) => {
    const mailgun = require("mailgun-js")(config.mailgun);
    let data = {
      subject: "Contact form",
      text: JSON.stringify(req.body),
      from: "[email protected]",
      to: "[email protected]"
    };

    mailgun.messages().send(data);
    res.send(req.body);
  })
);

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() {
            let accounts = config.imap.accounts;
            let a = Object.keys(accounts).find(a => buffer.includes(a));
            let user_id = accounts[a];

            if (!emails[seqno] && user_id) {
              let cad = parseFloat(
                buffer
                  .replace(/.*\$(.*)\.\d\d \(CAD\).*/gms, "$1")
                  .replace(",", "")
              );
              emails[seqno] = cad;

              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
                  });

                  let { user } = account;

                  let rate = app.get("rates")["CAD"] * 1.02;
                  let amount = Math.round((cad / rate) * 100000000);

                  await account.increment({ balance: amount }, { transaction });

                  let payment = await db.Payment.create({
                    account_id: account.id,
                    user_id: account.user_id,
                    hash: `Interac deposit: ${cad} 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 });

                  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');
        });

        imap.seq.addFlags(seqno, ["Seen"]);
      });
    }, 10000);
  });
});

imap.connect();