changeset 4:a17e400ddaa1

add email
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 21 Jun 2022 11:58:27 -0600
parents fc3ee39d7764
children 2f20b11affdd
files src/lib/Mail.luan src/lib/Shared.luan src/login.html.luan src/private/admin.html.luan src/private/admin/config.html.luan src/private/admin/mail_config.html.luan
diffstat 6 files changed, 183 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
diff -r fc3ee39d7764 -r a17e400ddaa1 src/lib/Mail.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/Mail.luan	Tue Jun 21 11:58:27 2022 -0600
@@ -0,0 +1,38 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local parse = Luan.parse or error()
+local Luan_mail = require "luan:mail/Mail.luan"
+local Db = require "site:/lib/Db.luan"
+
+
+local Mail = {}
+
+function Mail.get()
+	local doc = Db.get_document("type:mail")
+	return doc and doc.config
+end
+
+function Mail.sender()
+	local text = Mail.get()
+	if text == nil then
+		return nil
+	end
+	local info = parse(text)
+	return Luan_mail.sender(info)
+end
+
+function Mail.set(text)
+	if text == nil then
+		Db.delete("type:mail")
+	else
+		do  -- test
+			local info = parse(text)
+			Luan_mail.sender(info)
+		end
+		local doc = Db.get_document("type:mail") or {type="mail"}
+		doc.config = text
+		Db.save(doc)
+	end
+end
+
+return Mail
diff -r fc3ee39d7764 -r a17e400ddaa1 src/lib/Shared.luan
--- a/src/lib/Shared.luan	Sun Jun 19 20:47:31 2022 -0600
+++ b/src/lib/Shared.luan	Tue Jun 21 11:58:27 2022 -0600
@@ -41,6 +41,14 @@
 <%
 end
 
+function Shared.admin_header()
+%>
+		<div header>
+			<a href="/private/admin.html">Admin</a>
+		</div>
+<%
+end
+
 function Shared.base_url()
 	return Http.request.scheme.."://"..Http.request.headers["host"]
 end
diff -r fc3ee39d7764 -r a17e400ddaa1 src/login.html.luan
--- a/src/login.html.luan	Sun Jun 19 20:47:31 2022 -0600
+++ b/src/login.html.luan	Tue Jun 21 11:58:27 2022 -0600
@@ -5,6 +5,7 @@
 local Html = require "luan:Html.luan"
 local url_encode = Html.url_encode or error()
 local Io = require "luan:Io.luan"
+local output_of = Io.output_of or error()
 local Http = require "luan:http/Http.luan"
 local Shared = require "site:/lib/Shared.luan"
 local head = Shared.head or error()
@@ -14,6 +15,7 @@
 local Forum = require "site:/lib/Forum.luan"
 local forum_title = Forum.title or error()
 local User = require "site:/lib/User.luan"
+local Mail = require "site:/lib/Mail.luan"
 local Db = require "site:/lib/Db.luan"
 local run_in_transaction = Db.run_in_transaction or error()
 
@@ -85,10 +87,20 @@
 		end)
 	elseif password == nil then
 		local user = User.get_or_create_by_email(email)
+		local mailer = Mail.sender() or error "mail not configured"
+		mailer.send{
+			From = forum_title.." <support@freedit.org>"
+			To = user.email
+			Subject = "Login"
+			body = output_of(function() %>
+Login or register by clicking this link:
+
+<%=base_url()%>/login.html?email=<%=url_encode(email)%>&password=<%=user.password%>
+<%			end)
+		}
 		page(function()
 %>
 			<p>We have sent you an email.  Please check your email to login or register.</p>
-			<p>hack - <a href="<%=base_url()%>/login.html?email=<%=url_encode(email)%>&password=<%=user.password%>">link</a></p>
 <%
 		end)
 	elseif name == nil then
diff -r fc3ee39d7764 -r a17e400ddaa1 src/private/admin.html.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/private/admin.html.luan	Tue Jun 21 11:58:27 2022 -0600
@@ -0,0 +1,33 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local Shared = require "site:/lib/Shared.luan"
+local head = Shared.head or error()
+local header = Shared.admin_header or error()
+
+
+return function()
+	Io.stdout = Http.response.text_writer()
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title>Admin</title>
+	</head>
+	<body>
+<%		header() %>
+		<div content>
+			<h1>Admin</h1>
+			<p><a href="/">Home</a></p>
+			<p><a href="admin/config.html">Configure Forum</a></p>
+			<p><a href="admin/mail_config.html">Configure Mail</a></p>
+
+			<h2>Tools</h2>
+			<p><a href="tools/lucene.html">lucene</a></p>
+		</div>
+	</body>
+</html>
+<%
+end
diff -r fc3ee39d7764 -r a17e400ddaa1 src/private/admin/config.html.luan
--- a/src/private/admin/config.html.luan	Sun Jun 19 20:47:31 2022 -0600
+++ b/src/private/admin/config.html.luan	Tue Jun 21 11:58:27 2022 -0600
@@ -10,12 +10,16 @@
 local test_as_init = Http.test_as_init or error()
 local Run = require "luan:http/tools/Run.luan"
 local Config = require "site:/lib/Config.luan"
+local Shared = require "site:/lib/Shared.luan"
+local head = Shared.head or error()
+local header = Shared.admin_header or error()
 local Logging = require "luan:logging/Logging.luan"
-local logger = Logging.logger "admin/Config.html"
+local logger = Logging.logger "admin/config.html"
 
 
 return function()
 	Io.stdout = Http.response.text_writer()
+	local updated = false
 	if Http.request.method == "POST" then
 		local config = Http.request.parameters.config or error()
 		try
@@ -26,17 +30,15 @@
 		end
 		Config.set(config)
 		reset_luan()
-		%>Updated<%
-		return
+		updated = true
 	end
 %>
 <!doctype html>
 <html>
 	<head>
+<%		head() %>
 		<title>FreedIt - Configure Forum</title>
 		<style>
-			@import "/site.css";
-
 			textarea {
 				width: 90%;
 				height: 20em;
@@ -49,16 +51,22 @@
 		</script>
 	</head>
 	<body>
-		<h1>Configure Forum</h1>
-
-		<form method=post>
-			<p>
-				Configuration in <a href="http://www.luan.software/">Luan</a>
-				- <a href="javascript:restoreDefault()">restore default</a>
-			</p>
-			<p><textarea name=config autofocus><%= html_encode(Config.text) %></textarea></p>
-			<p><input type=submit><p>
-		</form>
+<%		header() %>
+		<div content>
+			<h1>Configure Forum</h1>
+<%	if updated then %>
+			<p>Updated</p>
+<%	else %>
+			<form method=post>
+				<p>
+					Configuration in <a href="http://www.luan.software/">Luan</a>
+					- <a href="javascript:restoreDefault()">restore default</a>
+				</p>
+				<p><textarea name=config autofocus><%= html_encode(Config.text) %></textarea></p>
+				<p><input type=submit><p>
+			</form>
+<%	end %>
+		</div>
 	</body>
 </html>
 <%
diff -r fc3ee39d7764 -r a17e400ddaa1 src/private/admin/mail_config.html.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/private/admin/mail_config.html.luan	Tue Jun 21 11:58:27 2022 -0600
@@ -0,0 +1,68 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local stringify = Luan.stringify or error()
+local String = require "luan:String.luan"
+local trim = String.trim or error()
+local Html = require "luan:Html.luan"
+local html_encode = Html.encode or error()
+local Parsers = require "luan:Parsers.luan"
+local json_string = Parsers.json_string or error()
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local Mail = require "site:/lib/Mail.luan"
+local Shared = require "site:/lib/Shared.luan"
+local head = Shared.head or error()
+local header = Shared.admin_header or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "admin/mail_config.html"
+
+--[[
+local default = stringify{
+	host = "smtpcorp.com"
+	username = "xxx"
+	password = "xxx"
+	port = 2525
+}
+]]
+
+return function()
+	Io.stdout = Http.response.text_writer()
+	local updated = false
+	if Http.request.method == "POST" then
+		local config = Http.request.parameters.config or error()
+		if trim(config) == "" then
+			config = nil
+		end
+		Mail.set(config)
+		updated = true
+	end
+%>
+<!doctype html>
+<html>
+	<head>
+<%		head() %>
+		<title>FreedIt - Configure Mail</title>
+		<style>
+			textarea {
+				width: 20em;
+				height: 10em;
+			}
+		</style>
+	</head>
+	<body>
+<%		header() %>
+		<div content>
+			<h1>Configure Mail</h1>
+<%	if updated then %>
+			<p>Updated</p>
+<%	else %>
+			<form method=post>
+				<p><textarea name=config autofocus><%= html_encode(Mail.get() or "") %></textarea></p>
+				<p><input type=submit><p>
+			</form>
+<%	end %>
+		</div>
+	</body>
+</html>
+<%
+end