const mailer = require('mailer'); const fs = require('fs'); const Suml = require('suml'); const forbidden = require('./forbidden'); const color = '#C71585'; const logo = fs.readFileSync(__dirname + '/../node_modules/@fortawesome/fontawesome-pro/svgs/light/tags.svg').toString('utf-8'); const logoEncoded = 'data:image/svg+xml,' + encodeURIComponent(logo.replace(' new Suml().parse(fs.readFileSync(`${__dirname}/../data/${name}.suml`).toString()); const translations = loadSuml('translations'); const sendEmail = (to, subject, body = undefined, html = undefined) => { mailer.send({ host: process.env.MAILER_HOST, port: parseInt(process.env.MAILER_PORT), ssl: parseInt(process.env.MAILER_PORT) === 465, authentication: 'login', username: process.env.MAILER_USER, password: process.env.MAILER_PASS, from: process.env.MAILER_FROM, to, subject, body, html, }, function(err){ if (err) { console.error(err); } }); }; const terms = translations.terms && translations.terms.content ? (translations.terms.content.content.violations + ' ' + Object.values(translations.terms.content.content.violationsExamples).join(', ')) : ''; const templates = { base: { subject: `[[title]] ยป {{content}}`, html: `
Logo [[title]]
{{content}}
`, }, notify: { subject: 'There are entries awaiting moderation', text: 'Entries awaiting moderation:\n\n{{list:stats}}', html: `

Entries awaiting moderation

`, }, confirmCode: { subject: '[[user.login.email.subject]]', text: `[[user.login.email.instruction]]\n\n{{code}}\n\n[[user.login.email.extra]]`, html: `

[[user.login.email.instruction]]

{{code}}

[[user.login.email.extra]]

`, }, ban: { subject: '[[ban.header]]', text: `[[ban.header]]\n\n[[ban.reason]][[quotation.colon]] %reason%\n\n[[quotation.start]]${terms}[[quotation.end]]`, html: `

[[ban.header]]

[[ban.reason]][[quotation.colon]] %reason%

[[quotation.start]]${terms}[[quotation.end]]

`, }, } const applyTemplate = (template, context, params) => { template = templates[template][context]; if (templates.base[context] !== undefined) { template = templates.base[context].replace('{{content}}', template); } template = template.replace(/%reason%/g, '{{reason}}'); // TODO template = template.replace(/\[\[([^\]]+)]]/g, m => { let x = translations; for (let part of m.substring(2, m.length - 2).split('.')) { x = x[part]; } return x; }); template = template.replace(/{{([^}]+)}}/g, m => { const key = m.substring(2, m.length - 2); if (key.startsWith('list:')) { const value = params[key.substring(5)]; if (Array.isArray(value)) { return context === 'html' ? value.map(s => `
  • ${s}
  • `).join('') : value.map(s => ` - ${s}`).join('\n'); } else { return context === 'html' ? Object.keys(value).map(s => `
  • ${s}: ${value[s]}
  • `).join('') : Object.keys(value).map(s => ` - ${s}: ${value[s]}`).join('\n'); } } return params[key]; }); return template; } module.exports = (to, template, params = {}) => { sendEmail( process.env.MAILER_OVERWRITE || to, applyTemplate(template, 'subject', params), applyTemplate(template, 'text', params), applyTemplate(template, 'html', params), ); };