clix/publish_atom.lua

Sun, 15 Nov 2020 20:45:50 +0100

author
Kim Alvefur <zash@zash.se>
date
Sun, 15 Nov 2020 20:45:50 +0100
changeset 140
8815232cbbeb
parent 130
11d526d74460
child 168
75e8ca131178
permissions
-rw-r--r--

clix.watch_pep: Take PEP node as positional argument

Thanks MattJ

local verse = require "verse";
local new_uuid = require "util.uuid".generate;
local datetime = require "util.datetime".datetime;

local xmlns_atom = "http://www.w3.org/2005/Atom";
local xmlns_activitystreams = "http://activitystrea.ms/spec/1.0/";

return function (opts, arg)
	if opts.short_help then
		print("Publish an Atom entry to a pubsub node");
		return;
	end
	if opts.help or not (opts.node or opts.title or opts.content) then
		print("clix publish_atom \\");
		print("","--service=pubsub.shakespeare.lit \\");
		print("","--node=princely_musings \\");
		print("","--id=123 \\");
		print("","--author=Hamlet \\");
		print("","--title=Soliloquy \\");
		print("","--summary=\"To be, or not to be: that is the question\"");
		return 0;
	end
	local function on_connect(conn)
		-- Required: id, title, updated, author (which must have a name)
		local entry_id = opts.id or new_uuid();
		local atom_entry = verse.stanza("entry", { xmlns = xmlns_atom})
			:tag("id"):text(entry_id):up()
			:tag("title", { type = opts["title-type"] }):text(opts.title or ""):up()
			:tag("updated"):text(opts.updated or datetime()):up();
		atom_entry:tag("author"):tag("name"):text(opts.author or os.getenv("USER") or "Unknown author"):up();
		atom_entry:up();
		if opts.summary then
			atom_entry:tag("summary", { type = opts["summary-type"] }):text(opts.summary):up();
		end

		if opts.content then
			atom_entry:tag("content", { type = opts["content-type"] }):text(opts.content):up();
		end

		for opt, optval in pairs(opts) do
			if opt:sub(1,3) == "as_" then
				atom_entry:tag(opt:sub(4), { xmlns = xmlns_activitystreams } )
					:text(optval):up();
			end
		end

		conn.pubsub(opts.service, opts.node or "urn:xmpp:microblog:0"):publish(entry_id, nil, atom_entry
		, function(ok)
			-- TODO Report success?
			conn:close();
		end);

		--conn:send(verse.message({ to = opts.to, type = "chat" }) :body(text));
	end
	clix_connect(opts, on_connect, {"pubsub"});
end

mercurial