htdocs/node/newsletter.js
2024-12-24 22:44:26 +00:00

39 lines
2.4 KiB
JavaScript
Executable File

const fs = require("fs")
const { exec } = require("child_process");
const serverPath = require("./serverPath.js")
const members = require("/etc/newsletter_members.js")
const blogs = require(serverPath+"/blogs/blogs.js")
let finalBlogIndex = blogs.length-1
const final_blog = blogs[finalBlogIndex]
const lastBlogIndex = require(serverPath+"/node/previousBlog.js")
if (finalBlogIndex > lastBlogIndex || process.argv[2] == "--test") {
final_blog[1] = final_blog[1].replace(/(?:\r\n|\r|\n)/g, '<br>')
final_blog[1] = final_blog[1].replace(/(?:\t)/g, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
final_blog[1] = final_blog[1].replace(/\(/g,'\\(')
final_blog[1] = final_blog[1].replace(/\)/g,'\\)')
final_blog[1] = final_blog[1].replace(/\&/g,'\\&')
final_blog[1] = final_blog[1].replace(/'/g,"\\'")
let imageAttachments = final_blog[3] ? `${final_blog[3].map(image => `-a ${serverPath}/images/${image}`).join('')}` : '';
console.log(imageAttachments);
//console.log(final_blog[1])
if (process.argv[2] == "--test") {
console.log("test")
let command = `echo "<h1>----TEST POST----</h1><br/><h3>Date created: ${final_blog[2]}</h3><br/>${final_blog[1]}<br/><br/><a href='https://deadvey.com/blog/blogs/${finalBlogIndex}.html'>Original Post</a>" | mutt -e "set content_type=text/html" -s "DeaDvey's Blog (TEST): ${final_blog[0]}" test@deadvey.com ${imageAttachments}`
console.log(`--------------\nFULL COMMAND TO RUN: ${command}\n---------------`)
exec(command)
}
else if (finalBlogIndex > lastBlogIndex) {
console.log("full distribution!!!")
for (let current_member = 0; current_member < members.length; current_member++) {
let command = `echo "<h3>Date created: ${final_blog[2]}</h3><br/>${final_blog[1]}<br/><br/><a href='https://deadvey.com/blog/blogs/${finalBlogIndex}.html'>Original Post</a><br/><a href='mailto:newsletter@deadvey.com?subject=Requesting%20unsubscription%20from%20mailing%20list&body=Please%20unsubscribe%20me%20from%20the%20mailing%20list.'>Unsubscribe</a>" | mutt -e "set content_type=text/html" -s "DeaDvey's Blog: ${final_blog[0]}" ${members[current_member]} ${imageAttachments}`
exec(command)
console.log(`sent to ${members[current_member]}`)
}
}
}
finalBlogIndex = blogs.length-1
fs.writeFileSync(serverPath+"/node/previousBlog.js", `let lastBlog=${finalBlogIndex.toString()}; if (typeof module !== 'undefined' && module.exports) { module.exports = lastBlog; }`);