File

mod_pubsub_forgejo/mod_pubsub_forgejo.lua @ 6305:1c62edeb9147

mod_pastebin: Update Readme diff --git a/mod_pastebin/README.md b/mod_pastebin/README.md --- a/mod_pastebin/README.md +++ b/mod_pastebin/README.md @@ -37,12 +37,14 @@ For example: Pastes will be available by default at `http://<your-prosody>:5280/pastebin/` by default. -In Prosody 0.9 and later this can be changed with [HTTP -settings](https://prosody.im/doc/http). +Ports and path can be changed with [HTTP +settings](https://prosody.im/doc/http), for example like: -In 0.8 and older this can be changed with `pastebin_ports` (see below), -or you can forward another external URL from your web server to Prosody, -use `pastebin_url` to set that URL. +``` {.lua} + http_paths = { + pastebin = "/$host-paste"; + } +``` # Discovery @@ -82,27 +84,16 @@ The line and character tresholds are adv pastebin_line_threshold The maximum number of lines a message may have before it is sent to the pastebin. (default 4 lines) pastebin_trigger A string of characters (e.g. "!paste ") which if detected at the start of a message, always sends the message to the pastebin, regardless of length. (default: not set) pastebin_expire_after Number of hours after which to expire (remove) a paste, defaults to 24. Set to 0 to store pastes permanently on disk. - pastebin_ports List of ports to run the HTTP server on, same format as mod_httpserver's http_ports[^1] - pastebin_url Base URL to display for pastebin links, must end with / and redirect to Prosody's built-in HTTP server[^2] # Compatibility - ------ ------- - trunk Works + ------ --------------------- + trunk Works as of 25-06-13 + 13 Works 0.12 Works - 0.11 Works - 0.10 Works - 0.9 Works - 0.8 Works - ------ ------- + ------ --------------------- # Todo - Maximum paste length - Web interface to submit pastes? - -[^1]: As of Prosody 0.9, `pastebin_ports` is replaced by `http_ports`, - see [Prosody HTTP server documentation](https://prosody.im/doc/http) - -[^2]: See also - [http_external_url](https://prosody.im/doc/http#external_url)
author Menel <menel@snikket.de>
date Fri, 13 Jun 2025 11:39:58 +0200
parent 6203:131b8bfbefb4
line wrap: on
line source

module:depends("http")
local pubsub_service = module:depends("pubsub").service

local st = require "util.stanza"
local json = require "util.json"
local hashes = require "util.hashes"
local from_hex = require"util.hex".from
local hmacs = {
	sha1 = hashes.hmac_sha1,
	sha256 = hashes.hmac_sha256,
	sha384 = hashes.hmac_sha384,
	sha512 = hashes.hmac_sha512
}

local format = module:require "format"
local default_templates = module:require "templates"

-- configuration
local forgejo_secret = module:get_option("forgejo_secret")

local default_node = module:get_option("forgejo_node", "forgejo")
local node_prefix = module:get_option_string("forgejo_node_prefix", "forgejo/")
local node_mapping = module:get_option_string("forgejo_node_mapping")
local forgejo_actor = module:get_option_string("forgejo_actor") or true

local skip_commitless_push = module:get_option_boolean(
				                             "forgejo_skip_commitless_push", true)
local custom_templates = module:get_option("forgejo_templates")

local forgejo_templates = default_templates

if custom_templates ~= nil then
	for k, v in pairs(custom_templates) do forgejo_templates[k] = v end
end

-- used for develoment, should never be set in prod!
local insecure = module:get_option_boolean("forgejo_insecure", false)
-- validation
if not insecure then assert(forgejo_secret, "Please set 'forgejo_secret'") end

local error_mapping = {
	["forbidden"] = 403,
	["item-not-found"] = 404,
	["internal-server-error"] = 500,
	["conflict"] = 409
}

local function verify_signature(secret, body, signature)
	if insecure then return true end
	if not signature then return false end
	local algo, digest = signature:match("^([^=]+)=(%x+)")
	if not algo then return false end
	local hmac = hmacs[algo]
	if not algo then return false end
	return hmac(secret, body) == from_hex(digest)
end

function handle_POST(event)
	local request, response = event.request, event.response

	if not verify_signature(forgejo_secret, request.body,
	                        request.headers.x_hub_signature) then
		module:log("debug", "Signature validation failed")
		return 401
	end

	local data = json.decode(request.body)
	if not data then
		response.status_code = 400
		return "Invalid JSON. From you of all people..."
	end

	local forgejo_event = request.headers.x_forgejo_event or data.object_kind

	if skip_commitless_push and forgejo_event == "push" and data.total_commits == 0 then
		module:log("debug", "Skipping push event with 0 commits")
		return 501
	end

	if forgejo_templates[forgejo_event] == nil then
		module:log("debug", "Unsupported forgejo event %q", forgejo_event)
		return 501
	end

	local item = format(data, forgejo_templates[forgejo_event])

	if item == nil then
		module:log("debug", "Formatter returned nil for event %q", forgejo_event)
		return 501
	end

	local node = default_node
	if node_mapping then node = node_prefix .. data.repository[node_mapping] end

	create_node(node)

	local ok, err = pubsub_service:publish(node, forgejo_actor, item.attr.id, item)
	if not ok then return error_mapping[err] or 500 end

	response.status_code = 202
	return "Thank you forgejo.\n" .. tostring(item:indent(1, " "))
end

module:provides("http", {route = {POST = handle_POST}})

function create_node(node)
	if not pubsub_service.nodes[node] then
		local ok, err = pubsub_service:create(node, true)
		if not ok then
			module:log("error", "Error creating node: %s", err)
		else
			module:log("debug", "Node %q created", node)
		end
	end
end