ident
All repositories: gitoria
3.9 KB
// lib/mail.hl — THE MAILER. One hl:smtp Mailer, configured from `.env` beside project.hl// (SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, SMTP_FROM; the real environment// outranks the file). A send is non-blocking: the SMTP conversation runs on the// mailer's thread and its outcome arrives on the event loop (logged below).//// IDENT_MAIL_SINK names a file that receives the mail INSTEAD of the SMTP server: one// line `<address> <code>` is APPENDED per code (invitations: `<sink>.invites`, notification mails: `<sink>.mail`). The dev server and the gate use it, so// no real mail is sent until SMTP is configured for mail.byrod.in (see README).//// Handlers belong to instances: project.hl constructs this file once (`mail = new Mail()`).import { Mailer } from 'hl:smtp'import { env } from 'hl:proc'import { writeFile, readFile, exists } from 'hl:fs'static NL = ''static setting = (name, fallback) => { return env(name) != null && env(name) != '' ? env(name) : fallback }// An unconfigured dev setup gets the placeholder host 127.0.0.1 (the Mailer must exist:// `mailer.deliver()` and the `on mailer.…` handlers below); with the sink set nothing is ever// sent through it. SMTP_FROM's default is a real sender, not a placeholder — kept (mission 035).static smtpHost = setting('SMTP_HOST', null)static mailer = new Mailer(smtpHost != null ? smtpHost : '127.0.0.1', toNumber(setting('SMTP_PORT', '587')), {user = setting('SMTP_USER', null)pass = setting('SMTP_PASSWORD', null)sender = setting('SMTP_FROM', '[email protected]')})static sink = env('IDENT_MAIL_SINK') != null && env('IDENT_MAIL_SINK') != '' ? env('IDENT_MAIL_SINK') : nullstatic sendCode = (email, code) => {if (sink != null) {before = exists(sink) ? readFile(sink) : ''writeFile(sink, before + email + ' ' + code + NL)return 0}if (smtpHost == null) {console.warn('mail: NOT SENT to ' + email + ' — neither IDENT_MAIL_SINK nor SMTP_HOST is set')return 0}return mailer.send({to = emailsubject = 'Your ident login code: ' + codetext = 'Hi,your one-time login code for worldapi is ' + code + 'It is valid for 10 minutes and works once. If you did not ask for it, ignore this mail.'})}// AN INVITATION MAIL (ident#22). With the sink set, the line `<address> <link>` goes to// `<sink>.invites` (a second file: the code reader of the gates keeps its own format).static sendInvite = (email, link, appName, project, role, days) => {if (sink != null) {f = sink + '.invites'before = exists(f) ? readFile(f) : ''writeFile(f, before + email + ' ' + link + NL)return 0}if (smtpHost == null) {console.warn('mail: invitation NOT SENT to ' + email + ' — neither IDENT_MAIL_SINK nor SMTP_HOST is set')return 0}return mailer.send({to = emailsubject = 'You are invited to ' + project + ' (' + appName + ')'text = 'Hi,you are invited to ' + project + ' in ' + appName + ' (role: ' + role + ').Open this link to join with ident:' + link + 'The link works for ' + days + ' days. If you were not expecting it, ignore this mail.'})}// A NOTIFICATION MAIL (ident#7, lib/delivery.hl: the daily mail at 17:00 and urgent ones at once). With the sink set,// one JSON line `{ to, subject, text }` per mail goes to `<sink>.mail` (the gates read it; never real mail).static sendNotificationMail = (email, subject, text) => {if (sink != null) {f = sink + '.mail'before = exists(f) ? readFile(f) : ''writeFile(f, before + JSON.stringify({ to = email subject = subject text = text }) + NL)return 0}if (smtpHost == null) {console.warn('mail: notification mail NOT SENT to ' + email + ' — neither IDENT_MAIL_SINK nor SMTP_HOST is set')return 0}return mailer.send({ to = email subject = subject text = text })}mailer.deliver()on mailer.sent(r) { console.log('mail: sent', r.id, r.to, r.message) }on mailer.failed(r) { console.warn('mail: FAILED', r.id, r.to, r.stage, r.code, r.message) }
Branches
Latest commits
- 7276a008ident mission 013: re-vendor Hybriel master f9614007 (#200, #202 mpackdb copy fix); gates and e2e rerunmre
- e13e30c3ident mission 013: report, mission, repro, LOGmre
- eff734f3ident mission 013: in-place sign-out also closes the code step (no leftover code form); checksmre
- b7a61546ident mission 013: e2e — in-place main-menu mark kept as a known gapmre
- 59b43d81ident mission 013: sign out everywhere keeps this browser's own (signed-out) session so the page goes on in placemre
- 716e14ffident mission 013: browser gate — Bob signs out everywhere (no second code request)mre
- 5885f1c9ident mission 013 step 2: stopgap reload removed (in-place sign-in/out reach the shell via emit, notices back, route-marked main menu), gates updatedmre
- 734ac0a9ident mission 013 step 1: re-vendor Hybriel ce6f2e0a (plugins whole; bin/ is git-ignored)mre
- b37875ccident mission 012 part 3: real-Chrome fixes (brand 44 px, identity form width), e2e walk, report, logmre
- f989c2ebident mission 012: stopgap reload after in-place sign-in/sign-out (until hybriel#193), gates wait for cold start, welcome end reloadsmre
- 443291d4ident mission 012: /start on its own shell (no account), browser gate 152/0, reportmre
- 021dd0fcident mission 012: browser gate waits cold start (IDENT_COLD_MS), header selectors, e2e checks; shell events from a page do not reach it (probe), current not marked, account-bar link 44 pxmre
- 97f77b07ident mission 012 (WIP): onto the layouts theme dbe3de7, shell on AppHeader/MainMenu/UserMenu/Sidebar + ContentFirst, styles cut, check-theme gatemre
- 8e4f435bpaths: projects moved into /media/STORAGE/projects/WorldAPI/mre
- fa288f63ident mission 011: report wording (Intl zone names)mre
- 961fae01ident mission 011: report, LOG, STATUSmre
- 65348039ident#7: delivery gate — console check split at the gate's own server restart (49 checks)mre
- dfa7b783ident#7: README (Delivery, env, gate, files, data), deploy.sh runs tests/delivery.mjsmre
- ab6331f4ident#7: tests/delivery.mjs — fake clock gate: Vienna/New York/Tokyo/UTC 17:00, urgent, mail off, restart, DST days, zone changemre
- 7869f57bident#7: delivery — the daily mail at 17:00 in the user's zone (digests table, once per local day, test clock IDENT_TEST_CLOCK), urgent mailed at once, push hookmre