From 443a6331e6792f4758d5a3b83ba2576e6897138d Mon Sep 17 00:00:00 2001 From: Robin Malley Date: Mon, 20 Feb 2023 05:06:36 +0000 Subject: [PATCH] Continue expanding addon system --- conf/smr.conf.in | 1 + src/lua/endpoints/download_get.lua | 19 +++++++++++++ src/lua/hooks.lua | 45 ++++++++++++++++++++++++++++++ src/pages/read.etlua.in | 17 +++++++---- 4 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 src/lua/hooks.lua diff --git a/conf/smr.conf.in b/conf/smr.conf.in index 277597f..1f5ede4 100644 --- a/conf/smr.conf.in +++ b/conf/smr.conf.in @@ -156,6 +156,7 @@ domain * { validate qs:get story v_storyid validate qs:get pwd v_hex_128 + validate qs:get fmt v_any } route /_preview { diff --git a/src/lua/endpoints/download_get.lua b/src/lua/endpoints/download_get.lua index c2b1af2..18c86b5 100644 --- a/src/lua/endpoints/download_get.lua +++ b/src/lua/endpoints/download_get.lua @@ -5,6 +5,7 @@ local db = require("db") local queries = require("queries") local util = require("util") local pages = require("pages") +local api = require("hooks") local stmnt_download local oldconfigure = configure @@ -13,6 +14,24 @@ function configure(...) return oldconfigure(...) end +local oldget = api.get.page_reader +api.get.page_reader = function(env) + local ret = oldget(env) + local button = { + endpoint = string.format("https://%s/_download",env.domain), + method = "GET", + fields = { + story = env.short, + }, + text = "Download TXT" + } + if env.unlisted then + button.fields.pwd = env.hashstr + end + table.insert(ret,button) + return ret +end + local function download_get(req) local host = http_request_get_host(req) local path = http_request_get_path(req) diff --git a/src/lua/hooks.lua b/src/lua/hooks.lua new file mode 100644 index 0000000..cef8daf --- /dev/null +++ b/src/lua/hooks.lua @@ -0,0 +1,45 @@ +--[[ +Global functions that smr exposes that can be overriden by addons +]] + +local api = {} + +-- Called before any request processing. Returning true "traps" the request, and +-- does not continue calling smr logic, or logic of any other addons. +api.pre_request = function(req) end + +-- Called after smr request processing. Returning true "traps" the request, and +-- does not continue calling any other addon logic. This will not stop smr from +-- responding to the request, since by this time http_request_response() has +-- already been called. +api.post_request = function(req) end + +-- Called during startup of the worker process +-- Failures in this function will prevent kore from starting. +-- Return value is ignored. +api.worker_init = function() end + +-- Called during shutdown of the worker process +-- Failures in this function cause other addon hooks to this function to be skipped. +-- Return value is ignored. +api.worker_shutdown = function() end + +-- The following are tables and their options: +-- "buttonspec" - specifies a button to display on the front end, has the fields: +-- .endpoint - the url to go to when the button is pressed +-- .method - the HTTP method to use to call the endpoint +-- .fields - key/value pairs to send as arguments when calling the endpoint +-- These are usually "hidden" fields for a form. +-- .text - The text that displays on the button + +-- Called to display configuration as html. +api.get = { + -- returns an array of buttonspec, displayed at the top of a story, + -- only for the logged in owner of a story. + page_owner = function(env) return {} end, + + -- returns an array of buttonspec, displayed at the bottom of a story + page_reader = function(env) return {} end, +} + +return api diff --git a/src/pages/read.etlua.in b/src/pages/read.etlua.in index d095ea0..9f9afec 100644 --- a/src/pages/read.etlua.in +++ b/src/pages/read.etlua.in @@ -37,13 +37,18 @@

<%= views %> Hits

-
- - <% if unlisted then %> - +
+<% for _, spec in ipairs(api.get.page_reader(getfenv(1))) do %> +
+ <% for key, value in pairs(spec.fields) do %> + <% end %> - - + + +
+<% end %> +
+ <% if not show_comments then -%>