Implement basic mailing list mirroring #57

This commit is contained in:
Federico Ceratto 2015-06-21 17:13:00 +01:00
commit 087dbfd60a
2 changed files with 20 additions and 2 deletions

View file

@ -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"),
@ -490,6 +491,10 @@ template writeToDb(c, cr, setPostId: expr) =
if setPostId:
c.postId = retID.int
template sendToMailingList(c) =
# send comment to a mailing list (if configured to do so)
discard sendMailToMailingList(c.config, c.username, c.email, subject, content)
proc edit(c: var TForumData, postId: int): bool =
checkLogin(c)
if c.isPreview:
@ -530,6 +535,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 +545,11 @@ proc reply(c: var TForumData): bool =
exec(db, sql"update thread set modified = DATETIME('now') where id = ?",
$c.threadId)
sendToMailingList(c)
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 +564,7 @@ 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')")
sendToMailingList(c)
result = true
proc login(c: var TForumData, name, pass: string): bool =

View file

@ -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.