clix/publish_atom.lua

1
local verse = require "verse";
2
local new_uuid = require "prosody.util.uuid".generate;
3
local datetime = require "prosody.util.datetime".datetime;
4
 
5
local xmlns_atom = "http://www.w3.org/2005/Atom";
6
local xmlns_activitystreams = "http://activitystrea.ms/spec/1.0/";
7
 
8
return function (opts, arg)
9
	if opts.short_help then
10
		print("Publish an Atom entry to a pubsub node");
11
		return;
12
	end
13
	if opts.help or not (opts.node or opts.title or opts.content) then
14
		print("clix publish_atom \\");
15
		print("","--service=pubsub.shakespeare.lit \\");
16
		print("","--node=princely_musings \\");
17
		print("","--id=123 \\");
18
		print("","--author=Hamlet \\");
19
		print("","--title=Soliloquy \\");
20
		print("","--summary=\"To be, or not to be: that is the question\"");
21
		return 0;
22
	end
23
	local function on_connect(conn)
24
		-- Required: id, title, updated, author (which must have a name)
25
		local entry_id = opts.id or new_uuid();
26
		local atom_entry = verse.stanza("entry", { xmlns = xmlns_atom})
27
			:tag("id"):text(entry_id):up()
28
			:tag("title", { type = opts["title-type"] }):text(opts.title or ""):up()
29
			:tag("updated"):text(opts.updated or datetime()):up();
30
		atom_entry:tag("author"):tag("name"):text(opts.author or os.getenv("USER") or "Unknown author"):up();
31
		atom_entry:up();
32
		if opts.summary then
33
			atom_entry:tag("summary", { type = opts["summary-type"] }):text(opts.summary):up();
34
		end
35
 
36
		if opts.content then
37
			atom_entry:tag("content", { type = opts["content-type"] }):text(opts.content):up();
38
		end
39
 
40
		for opt, optval in pairs(opts) do
41
			if opt:sub(1,3) == "as_" then
42
				atom_entry:tag(opt:sub(4), { xmlns = xmlns_activitystreams } )
43
					:text(optval):up();
44
			end
45
		end
46
 
47
		conn.pubsub(opts.service, opts.node or "urn:xmpp:microblog:0"):publish(entry_id, nil, atom_entry
48
		, function(ok)
49
			-- TODO Report success?
50
			conn:close();
51
		end);
52
 
53
		--conn:send(verse.message({ to = opts.to, type = "chat" }) :body(text));
54
	end
55
	clix_connect(opts, on_connect, {"pubsub"});
56
end