Advertisement
Soley

Untitled

Mar 5th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. const express = require('express')
  2. const app = express()
  3. const url = require('url');
  4. const crypto = require('crypto');
  5. const port = process.env.PORT || 3000
  6. const fs = require('fs');
  7. const file = '.emails.json'
  8. let h = null
  9. const nodemailer = require('nodemailer');
  10.  
  11. const transporter = nodemailer.createTransport({
  12. service: 'gmail',
  13. auth: {
  14. user: '***@gmail.com',
  15. pass: '***'
  16. }
  17. });
  18.  
  19. const appURL = '<url withoud ending forward slash>'
  20.  
  21. function 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.  
  30. function 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.  
  46. function 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.  
  61. function 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.  
  76. function 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.  
  89. app.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.  
  145. app.listen(port, () => console.log(`App listening on port ${port}!`))
  146.  
  147. function 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.  
  164. function 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.  
  199. function 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.  
  203. function doSomethingGood() {
  204. send('You are a miracle :)', 'Time to do something good!')
  205. }
  206.  
  207. function 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.  
  220. setInterval(() => {
  221. sendDailyEmails()
  222. }, 60 * 1000 * 60);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement