diff --git a/forum.nim b/forum.nim index dbf3d8f..c5c8f13 100644 --- a/forum.nim +++ b/forum.nim @@ -483,6 +483,7 @@ template setPreviewData(c: expr) {.immediate, dirty.} = c.currentPost.content = content template writeToDb(c, cr, setPostId: expr) = + # insert a comment in the DB let retID = insertID(db, crud(cr, "post", "author", "ip", "header", "content", "thread"), c.userId, c.req.ip, subject, content, $c.threadId, "") discard tryExec(db, crud(cr, "post_fts", "id", "header", "content"), @@ -530,6 +531,7 @@ proc edit(c: var TForumData, postId: int): bool = result = true proc reply(c: var TForumData): bool = + # reply to an existing thread checkLogin(c) retrPost(c) if c.isPreview: @@ -539,9 +541,12 @@ proc reply(c: var TForumData): bool = exec(db, sql"update thread set modified = DATETIME('now') where id = ?", $c.threadId) + asyncCheck sendMailToMailingList(c.config, c.username, c.email, + subject, content) result = true proc newThread(c: var TForumData): bool = + # create new conversation thread (permanent or transient) const query = sql"insert into thread(name, views, modified) values (?, 0, DATETIME('now'))" checkLogin(c) retrPost(c) @@ -556,6 +561,8 @@ proc newThread(c: var TForumData): bool = writeToDb(c, crCreate, false) discard tryExec(db, sql"insert into post_fts(post_fts) values('optimize')") discard tryExec(db, sql"insert into post_fts(thread_fts) values('optimize')") + asyncCheck sendMailToMailingList(c.config, c.username, c.email, + subject, content) result = true proc login(c: var TForumData, name, pass: string): bool = diff --git a/utils.nim b/utils.nim index 4d3e1c5..44a8de4 100644 --- a/utils.nim +++ b/utils.nim @@ -6,6 +6,7 @@ type smtpPort: int smtpUser: string smtpPassword: string + mlistAddress: string proc loadConfig*(filename = getCurrentDir() / "forum.json"): Config = result = Config(smtpAddress: "localhost", smtpPort: 25, smtpUser: "", @@ -16,10 +17,11 @@ proc loadConfig*(filename = getCurrentDir() / "forum.json"): Config = result.smtpPort = root["smtpPort"].getNum(25).int result.smtpUser = root["smtpUser"].getStr("") result.smtpPassword = root["smtpPassword"].getStr("") + result.mlistAddress = root["mlistAddress"].getStr("") except: echo("[WARNING] Couldn't read config file: ./forum.json") -proc sendMail(config: Config, subject, message, recipient: string) {.async.} = +proc sendMail(config: Config, subject, message, recipient: string, from_addr = "forum@nim-lang.org") {.async.} = var client = newAsyncSmtp(config.smtpAddress, Port(config.smtpPort)) await client.connect() if config.smtpUser.len > 0: @@ -29,9 +31,16 @@ proc sendMail(config: Config, subject, message, recipient: string) {.async.} = let encoded = createMessage(subject, message, toList, @[], []) - await client.sendMail("forum@nim-lang.org", toList, + await client.sendMail(from_addr, toList, $encoded) +proc sendMailToMailingList*(config: Config, username, user_email_addr, subject, message: string) {.async.} = + # send message to a mailing list + let from_addr = "$# <$#>" % [username, user_email_addr] + + if config.mlistAddress != "": + await sendMail(config, subject, message, config.mlistAddress, from_addr=from_addr) + proc sendPassReset*(config: Config, email, user, resetUrl: string) {.async.} = let message = """Hello $1, A password reset has been requested for your account on the Nim Forum.