-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
70 lines (57 loc) · 2.06 KB
/
index.js
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
const notifier = require('mail-notifier');
const axios = require('axios');
const logger = require('tracer').colorConsole({ format: '[{{timestamp}} {{title}}] (in {{file}}:{{line}}) {{message}}', level: process.env.LOG_LEVEL || 'info' });
const usernames = process.env.IMAP_USERNAMES.split(",");
const passwords = process.env.IMAP_PASSWORDS.split(",");
const githubURL = process.env.DISCORD_WH_URL;
const imapMarkSeen = process.env.IMAP_MARK_SEEN === "false" ? false : true;
const startTime = new Date();
const triggerWebhook = (username, mail) => {
logger.debug(mail)
const description = mail.text === undefined ? "" : mail.text.substring(0, 1000);
if (!imapMarkSeen && mail.date < startTime) {
return;
}
require("axios").post(githubURL, {
content: "",
username: username,
embeds: [
{
title: mail.headers.subject,
author: {
name: mail.headers.from
},
description: description
}
]
}).then((response) => {
}).catch((err) => {
logger.error("Error triggering Webhook:" + err);
});
}
logger.info("Log level: " + process.env.LOG_LEVEL || "info")
logger.info("IMAP Host: " + process.env.IMAP_HOST);
logger.info("IMAP Port: " + process.env.IMAP_PORT || 141);
logger.info("IMAP Check TLS: " + (process.env.IMAP_TLS || "true"));
logger.info("IMAP Check TLS: " + (process.env.IMAP_MARK_SEEN || "false"));
usernames.forEach((username, index) => {
const imap = {
user: username,
password: passwords[index],
host: process.env.IMAP_HOST,
port: process.env.IMAP_PORT || 143,
tls: process.env.IMAP_TLS === "true" ? true : false,
autotls: 'always',
markSeen: imapMarkSeen
};
notifier(imap)
.on('connected',() => {
logger.info("Connected " + username + ". Listening for emails.");
})
.on('error', (e) => {
logger.error("Failed to connect with " + username, e)
throw e;
})
.on('mail', (mail) => triggerWebhook(username, mail))
.start();
})