-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailReader.ts
100 lines (83 loc) · 2.57 KB
/
EmailReader.ts
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
100
import * as Imap from "imap";
export const readEmailsSince = async ({
dateSince,
subject,
emailCredentials
}: {
dateSince: Date;
subject: "ADDRESS_CONFIRMATION" | "SUCCESSFUL_ACCOUNT_CREATION";
emailCredentials: { user: string; password: string; host: string; port: number; tls: boolean }
}) => {
const newLinks: string[] = [];
await new Promise((resolve, reject) => {
const imap = new Imap(emailCredentials);
function openInbox(cb: (error: Error, mailbox: Imap.Box) => void) {
imap.openBox("INBOX", true, cb);
}
imap.once("error", function(err: Error) {
console.log(err);
});
imap.connect();
imap.once("ready", function() {
openInbox((err: Error, box: Imap.Box) => {
if (err) {
throw err;
}
imap.search(["UNSEEN"], function(err, results) {
if (err) throw err;
var f = imap.fetch(results, { bodies: ["HEADER.FIELDS (DATE)", "TEXT"] });
f.on("message", function(msg, seqno) {
let emailBody: Buffer;
let emailDate: Buffer;
msg.on("body", function(stream, info) {
stream.on("data", chunk => {
if (info.which === "TEXT") {
emailBody += chunk;
} else {
emailDate += chunk;
}
});
stream.on("end", () => {
const body = emailBody.toString("utf-8");
const date = emailDate.toString("utf-8");
const dateIndexes = [date.indexOf("undefinedDate: ") + "undefinedDate: ".length, date.indexOf("+0000")];
const utcMailDate = new Date(
new Date(date.substring(dateIndexes[0], dateIndexes[1])).getTime() - new Date().getTimezoneOffset() * 60 * 1000
);
if (subject === "ADDRESS_CONFIRMATION") {
const urlIndex = body.indexOf("https://www.dofus.com/fr/mmorpg/jouer?guid=");
if (urlIndex != -1) {
const url = body
.substr(urlIndex, 300)
.split(" ]")[0]
.replace(/=\r\n/g, "")
.replace(/=3D/g, "=");
if (utcMailDate.getTime() > dateSince.getTime()) {
newLinks.push(url);
}
}
} else {
const urlIndex = body.indexOf("Bienvenue dans la communaut");
if (urlIndex != -1) {
if (utcMailDate.getTime() > dateSince.getTime()) {
newLinks.push("Successful account creation");
}
}
}
});
});
});
f.once("error", function(err) {
console.log("Fetch error: " + err);
reject();
});
f.once("end", function() {
imap.end();
resolve();
});
});
});
});
});
return Array.from(new Set(newLinks));
};