2020-10-14 12:49:18 -07:00
const mailer = require ( 'mailer' ) ;
2021-12-03 13:39:08 -08:00
const fs = require ( 'fs' ) ;
const Suml = require ( 'suml' ) ;
2021-12-03 15:40:08 -08:00
const forbidden = require ( './forbidden' ) ;
2020-10-14 12:49:18 -07:00
2021-12-03 13:39:08 -08:00
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 ( '<path ' , ` <path fill=" ${ color } " ` ) ) ;
const loadSuml = name => new Suml ( ) . parse ( fs . readFileSync ( ` ${ _ _dirname } /../data/ ${ name } .suml ` ) . toString ( ) ) ;
const translations = loadSuml ( 'translations' ) ;
const sendEmail = ( to , subject , body = undefined , html = undefined ) => {
2020-10-14 12:49:18 -07:00
mailer . send ( {
2021-12-03 13:39:08 -08:00
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 ) ;
}
} ) ;
} ;
2021-12-03 15:40:08 -08:00
const terms = ` It is forbidden to post on the Service any Content that might break the law or violate social norms, including but not limited to: ${ forbidden . join ( ', ' ) } `
2021-12-03 13:39:08 -08:00
const templates = {
base : {
subject : ` [[title]] » {{content}} ` ,
html : `
2021-12-04 05:08:26 -08:00
< div style = "margin: 36px auto; width: 100%; max-width: 480px; border: 1px solid #aaa;border-radius: 8px;overflow: hidden;font-family: Helvetica, sans-serif;font-size: 16px;box-shadow: 0 .5rem 1rem rgba(0, 0, 0, .15)" >
2021-12-03 13:39:08 -08:00
< div style = "padding: 16px; padding-top: 10px; background: #f8f8f8; border-bottom: 1px solid #aaa;font-size: 20px;color: ${color};" >
< img src = "${logoEncoded}" style = "height: 24px;width: 24px; position: relative; top: 6px; margin-right: 6px;" alt = "Logo" / >
[ [ title ] ]
< / d i v >
< div style = "padding: 8px 16px; background: #fff;" >
{ { content } }
< / d i v >
< / d i v >
` ,
2020-10-14 12:49:18 -07:00
} ,
2021-12-03 13:39:08 -08:00
notify : {
subject : 'There are entries awaiting moderation' ,
text : 'Entries awaiting moderation:\n\n{{list:stats}}' ,
html : `
< p > Entries awaiting moderation < / p >
< ul > { { list : stats } } < / u l >
` ,
} ,
confirmCode : {
subject : '[[user.login.email.subject]]' ,
text : ` [[user.login.email.instruction]] \n \n {{code}} \n \n [[user.login.email.extra]] ` ,
html : `
< p > [ [ user . login . email . instruction ] ] < / p >
< p style = "border: 1px solid #aaa;border-radius: 8px;overflow: hidden;text-align: center;user-select: all;font-size: 24px; padding:8px;letter-spacing: 8px; font-weight: bold;" > { { code } } < / p >
< p style = "font-size: 12px; color: #777" > [ [ user . login . email . extra ] ] < / p >
` ,
2021-12-03 15:40:08 -08:00
} ,
ban : {
subject : '[[ban.header]]' ,
text : ` [[ban.header]] \n \n [[ban.reason]][[quotation.colon]] %reason% \n \n [[quotation.start]] ${ terms } [[quotation.end]] ` ,
html : `
< p > [ [ ban . header ] ] < / p >
< p > [ [ ban . reason ] ] [ [ quotation . colon ] ] % reason % < / p >
< p style = "font-size: 12px; color: #777" > [ [ quotation . start ] ] $ { terms } [ [ quotation . end ] ] < / p >
` ,
} ,
2021-12-03 13:39:08 -08:00
}
const applyTemplate = ( template , context , params ) => {
template = templates [ template ] [ context ] ;
if ( templates . base [ context ] !== undefined ) {
template = templates . base [ context ] . replace ( '{{content}}' , template ) ;
}
2021-12-03 15:40:08 -08:00
template = template . replace ( /%reason%/g , '{{reason}}' ) ; // TODO
2021-12-03 13:39:08 -08:00
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 => ` <li> ${ s } </li> ` ) . join ( '' )
: value . map ( s => ` - ${ s } ` ) . join ( '\n' ) ;
} else {
return context === 'html'
? Object . keys ( value ) . map ( s => ` <li><strong> ${ s } :</strong> ${ value [ s ] } </li> ` ) . join ( '' )
: Object . keys ( value ) . map ( s => ` - ${ s } : ${ value [ s ] } ` ) . join ( '\n' ) ;
}
2020-10-14 12:49:18 -07:00
}
2021-12-03 13:39:08 -08:00
return params [ key ] ;
2020-10-14 12:49:18 -07:00
} ) ;
2021-12-03 13:39:08 -08:00
return template ;
2020-10-14 12:49:18 -07:00
}
2021-12-03 13:39:08 -08:00
module . exports = ( to , template , params = { } ) => {
sendEmail (
2021-12-03 15:39:23 -08:00
process . env . MAILER _OVERWRITE || to ,
2021-12-03 13:39:08 -08:00
applyTemplate ( template , 'subject' , params ) ,
applyTemplate ( template , 'text' , params ) ,
applyTemplate ( template , 'html' , params ) ,
) ;
} ;