· 7 years ago · Mar 05, 2019, 08:54 PM
1const express = require('express')
2const app = express()
3const url = require('url');
4const crypto = require('crypto');
5const port = process.env.PORT || 3000
6const fs = require('fs');
7const file = '.emails.json'
8let h = null
9const nodemailer = require('nodemailer');
10
11const transporter = nodemailer.createTransport({
12 service: 'gmail',
13 auth: {
14 user: '***@gmail.com',
15 pass: '***'
16 }
17});
18
19const appURL = '<url withoud ending forward slash>'
20
21function addToFile(email) {
22 const json = JSON.parse(fs.readFileSync(file))
23 const hash = crypto.createHash('md5').update(email + Math.random()).digest("hex")
24 const finished = 0
25 const isActive = true
26 json[email] = { hash, finished, isActive }
27 fs.writeFileSync(file, JSON.stringify(json));
28}
29
30function enableEmail(email) {
31 const json = JSON.parse(fs.readFileSync(file))
32 const keys = Object.keys(json)
33 for (let index = 0; index < keys.length; index++) {
34 const to = keys[index];
35 if (to === email) {
36 const row = json[email]
37 json[email] = { ...row, isActive: true }
38 fs.writeFileSync(file, JSON.stringify(json));
39 return true
40 }
41 }
42 return false
43}
44
45
46function disableEmail(hash) {
47 const json = JSON.parse(fs.readFileSync(file))
48 const keys = Object.keys(json)
49 for (let index = 0; index < keys.length; index++) {
50 const to = keys[index];
51 const row = json[to]
52 if (row.hash === hash) {
53 json[to] = { ...row, isActive: false }
54 fs.writeFileSync(file, JSON.stringify(json));
55 return true
56 }
57 }
58 return false
59}
60
61function incFinished(hash) {
62 const json = JSON.parse(fs.readFileSync(file))
63 const keys = Object.keys(json)
64 for (let index = 0; index < keys.length; index++) {
65 const to = keys[index];
66 const row = json[to]
67 if (row.hash === hash) {
68 json[to] = { ...row, finished: Number(row.finished) + 1 }
69 fs.writeFileSync(file, JSON.stringify(json));
70 return json[to].finished
71 }
72 }
73 return false
74}
75
76function removeFromFile(hash) {
77 const json = JSON.parse(fs.readFileSync(file))
78 const keys = Object.keys(json)
79 for (let index = 0; index < keys.length; index++) {
80 const tmp = json[keys[index]];
81 if (tmp.hash === hash) {
82 delete json[keys[index]]
83 break
84 }
85 }
86 fs.writeFileSync(file, JSON.stringify(json));
87}
88
89app.get('/', (req, res) => {
90 const url_parts = url.parse(req.url, true);
91 const query = url_parts.query;
92
93 if (query.exit) {
94 res.send('App terminated')
95 process.exit()
96 return
97 }
98
99 if (query.add) {
100 if (enableEmail(query.add)) {
101 res.send('Sending mail enabled.')
102 } else {
103 addToFile(query.add)
104 res.send('Added to the list.')
105 }
106 return
107 }
108
109 if (query.finished) {
110 const points = incFinished(query.finished)
111 if (points) {
112 res.send(`
113 <meta name="viewport" content="width=device-width, initial-scale=1.0">
114 <h2>Well done!</h2>
115 <br/>
116 <h3>You have earned +1 point!</h3>
117 <br/>
118 <br/>
119 <hr/>
120 <p>You already have <b>${points}</b> point${points > 1 ? 's' : ''}!</p>
121 `)
122 return
123 }
124 }
125
126 if (query.disable && disableEmail(query.disable)) {
127 res.send('Sending mail disabled.')
128 }
129
130 if (query.remove) {
131 removeFromFile(query.remove)
132 res.send('Removed from the list.')
133 return
134 }
135
136 if (query.send) {
137 doSomethingGood()
138 res.send('Sent to all emails')
139 return
140 }
141
142 res.send('Unknown command. Use only send, exit, add, remove, and disable endpoints')
143})
144
145app.listen(port, () => console.log(`App listening on port ${port}!`))
146
147function sendEmail(to, subject, html) {
148 const mailOptions = {
149 from: 'a.life.with.discipline@gmail.com',
150 to,
151 subject,
152 html
153 };
154
155 transporter.sendMail(mailOptions, function (error, info) {
156 if (error) {
157 console.log(error);
158 } else {
159 console.log('Email sent: ' + info.response);
160 }
161 });
162}
163
164function send(subject, text) {
165 const json = JSON.parse(fs.readFileSync(file))
166 const keys = Object.keys(json)
167 for (let index = 0; index < keys.length; index++) {
168 const to = keys[index];
169 const { hash } = json[to];
170
171 const html = `
172 <h2>${text}</h2>
173
174 <hr />
175
176 <a style="
177 font-size: 14px;
178 color: #ffffff;
179 background: #FF9800;
180 padding: 10px 15px;
181 border-radius: 4px;
182 text-decoration: none;
183 margin: 50px 0;
184 display: inline-block;" href="${appURL}/?finished=${hash}">FInished it!</a>
185
186 <hr />
187
188 <a style="font-size: 11px;" href="${appURL}/?disable=${hash}">Disable emails</a>
189 <br/>
190 <a style="font-size: 11px;" href="${appURL}/?add=${to}">Enable emails</a>
191 <br/>
192 <a style="font-size: 11px;" href="${appURL}/?remove=${hash}">Disable and remove email</a>
193 `
194
195 sendEmail(to, subject, html)
196 }
197}
198
199function sendGoodMorning() {
200 send('Start a wonderful day', 'Today will be a facinating day. Do lots of great things and be proud of yourself, because you deserve it. Soley')
201}
202
203function doSomethingGood() {
204 send('You are a miracle :)', 'Time to do something good!')
205}
206
207function sendDailyEmails() {
208 console.log('Checking...')
209 const H = new Date().getHours()
210 if (h !== H) {
211 h = H;
212 if (H === 9) sendGoodMorning()
213 if (H >= 10 && H <= 18) {
214 doSomethingGood()
215 console.log('Time to do something!')
216 }
217 }
218}
219
220setInterval(() => {
221 sendDailyEmails()
222}, 60 * 1000 * 60);