gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit600c043d600c043dMerge branch 'v58-day58' into mainIdent600c043d/lib/mail.hl

3.7 KB

  1. // lib/mail.hl — THE MAILER. One hl:smtp Mailer, configured from `.env` beside project.hl
  2. // (SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, SMTP_FROM; the real environment
  3. // outranks the file). A send is non-blocking: the SMTP conversation runs on the
  4. // mailer's thread and its outcome arrives on the event loop (logged below).
  5. //
  6. // IDENT_MAIL_SINK names a file that receives the mail INSTEAD of the SMTP server: one
  7. // line `<address> <code>` is APPENDED per code (invitations: `<sink>.invites`, notification mails: `<sink>.mail`). The dev server and the gate use it, so
  8. // no real mail is sent until SMTP is configured for mail.byrod.in (see README).
  9. //
  10. // Handlers belong to instances: project.hl constructs this file once (`mail = new Mail()`).
  11. import { Mailer } from 'hl:smtp'
  12. import { envString } from './util.hl'
  13. import { writeFile, readFile, exists } from 'hl:fs'
  14. static NL = '
  15. '
  16. // An unconfigured dev setup gets the placeholder host 127.0.0.1 (the Mailer must exist:
  17. // `mailer.deliver()` and the `on mailer.…` handlers below); with the sink set nothing is ever
  18. // sent through it. SMTP_FROM's default is a real sender, not a placeholder — kept (mission 035).
  19. static smtpHost = envString('SMTP_HOST', null)
  20. static mailer = new Mailer(smtpHost != null ? smtpHost : '127.0.0.1', toNumber(envString('SMTP_PORT', '587')), {
  21. user = envString('SMTP_USER', null)
  22. pass = envString('SMTP_PASSWORD', null)
  23. sender = envString('SMTP_FROM', '[email protected]')
  24. })
  25. static sink = envString('IDENT_MAIL_SINK', null)
  26. static sendCode = (email, code) => {
  27. if (sink != null) {
  28. before = exists(sink) ? readFile(sink) : ''
  29. writeFile(sink, before + email + ' ' + code + NL)
  30. return 0
  31. }
  32. if (smtpHost == null) {
  33. console.warn('mail: NOT SENT to ' + email + ' — neither IDENT_MAIL_SINK nor SMTP_HOST is set')
  34. return 0
  35. }
  36. return mailer.send({
  37. to = email
  38. subject = 'Your ident login code: ' + code
  39. text = 'Hi,
  40. your one-time login code for worldapi is ' + code + '
  41. It is valid for 10 minutes and works once. If you did not ask for it, ignore this mail.
  42. '
  43. })
  44. }
  45. // AN INVITATION MAIL (ident#22). With the sink set, the line `<address> <link>` goes to
  46. // `<sink>.invites` (a second file: the code reader of the gates keeps its own format).
  47. static sendInvite = (email, link, appName, project, role, days) => {
  48. if (sink != null) {
  49. f = sink + '.invites'
  50. before = exists(f) ? readFile(f) : ''
  51. writeFile(f, before + email + ' ' + link + NL)
  52. return 0
  53. }
  54. if (smtpHost == null) {
  55. console.warn('mail: invitation NOT SENT to ' + email + ' — neither IDENT_MAIL_SINK nor SMTP_HOST is set')
  56. return 0
  57. }
  58. return mailer.send({
  59. to = email
  60. subject = 'You are invited to ' + project + ' (' + appName + ')'
  61. text = 'Hi,
  62. you are invited to ' + project + ' in ' + appName + ' (role: ' + role + ').
  63. Open this link to join with ident:
  64. ' + link + '
  65. The link works for ' + days + ' days. If you were not expecting it, ignore this mail.
  66. '
  67. })
  68. }
  69. // A NOTIFICATION MAIL (ident#7, lib/delivery.hl: the daily mail at 17:00 and urgent ones at once). With the sink set,
  70. // one JSON line `{ to, subject, text }` per mail goes to `<sink>.mail` (the gates read it; never real mail).
  71. static sendNotificationMail = (email, subject, text) => {
  72. if (sink != null) {
  73. f = sink + '.mail'
  74. before = exists(f) ? readFile(f) : ''
  75. writeFile(f, before + JSON.stringify({ to = email subject = subject text = text }) + NL)
  76. return 0
  77. }
  78. if (smtpHost == null) {
  79. console.warn('mail: notification mail NOT SENT to ' + email + ' — neither IDENT_MAIL_SINK nor SMTP_HOST is set')
  80. return 0
  81. }
  82. return mailer.send({ to = email subject = subject text = text })
  83. }
  84. mailer.deliver()
  85. on mailer.sent(r) { console.log('mail: sent', r.id, r.to, r.message) }
  86. on mailer.failed(r) { console.warn('mail: FAILED', r.id, r.to, r.stage, r.code, r.message) }

Branches

Latest commits

  • 600c043dMerge branch 'v58-day58' into mainIdent
  • 365f8c80|||PR ident: vendor Hybriel day58mre
  • 585126e5Merge branch 'f16-setf' into mainIdent
  • 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
  • a1419d9cMerge branch 'f15-navigate' into mainIdent
  • 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
  • e36f4516Merge branch 'f14-online' into mainIdent
  • 2c5eac91|||PR ident#38 online state without probe timermre
  • bf1980f2Merge branch 'v56-day56' into mainIdent
  • 62ae0adf|||PR ident: vendor Hybriel day56mre
  • 0d18abc4Merge branch 'f13-avatar' into mainIdent
  • 2a99730a|||PR ident#37 avatar in hlmre
  • fa4ed25bMerge branch 'f11-upsert' into mainIdent
  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre
  • 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre
  • 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
  • 991641d4Merge branch 'f10-access' into mainIdent
  • e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
  • df7696dbident#34: tests: access ticks selector quoting fixmre
  • 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre