clix/publish_atom.lua

changeset 122
1dfd28db10bd
parent 120
3456a6088dc6
child 128
0ae8deb6f75d
equal deleted inserted replaced
103:45843df81db0 122:1dfd28db10bd
1 local new_uuid = require "util.uuid".generate; 1 local new_uuid = require "util.uuid".generate;
2 local datetime = require "util.datetime".datetime; 2 local datetime = require "util.datetime".datetime;
3 3
4 local xmlns_atom = "http://www.w3.org/2005/Atom"; 4 local xmlns_atom = "http://www.w3.org/2005/Atom";
5 local xmlns_activitystreams = "http://activitystrea.ms/spec/1.0/"; 5 local xmlns_activitystreams = "http://activitystrea.ms/spec/1.0/";
6 local activitystreams_schema_base = "http://activitystrea.ms/schema/1.0/";
7 6
8 return function (opts, arg) 7 return function (opts, arg)
9 if opts.short_help then 8 if opts.short_help then
10 print("Publish an Atom entry to a pubsub node"); 9 print("Publish an Atom entry to a pubsub node");
11 return; 10 return;
12 end 11 end
13 --if (#arg == 0 or opts.help) then -- why? 12 if opts.help or not opts.node then
14 if opts.help then
15 print("clix publish_atom \\"); 13 print("clix publish_atom \\");
16 print("","--service=pubsub.shakespeare.lit \\"); 14 print("","--service=pubsub.shakespeare.lit \\");
17 print("","--node=princely_musings \\"); 15 print("","--node=princely_musings \\");
18 print("","--id=123 \\"); 16 print("","--id=123 \\");
19 print("","--author=Hamlet \\"); 17 print("","--author=Hamlet \\");
23 end 21 end
24 local function on_connect(conn) 22 local function on_connect(conn)
25 -- Required: id, title, updated, author (which must have a name) 23 -- Required: id, title, updated, author (which must have a name)
26 local entry_id = opts.id or new_uuid(); 24 local entry_id = opts.id or new_uuid();
27 local atom_entry = verse.stanza("entry", { xmlns = xmlns_atom}) 25 local atom_entry = verse.stanza("entry", { xmlns = xmlns_atom})
28 :tag("id"):text(entry_id):up(id) 26 :tag("id"):text(entry_id):up()
29 :tag("title"):text(opts.title or ""):up() 27 :tag("title", { type = opts["title-type"] }):text(opts.title or ""):up()
30 :tag("updated"):text(opts.updated or datetime()):up(); 28 :tag("updated"):text(opts.updated or datetime()):up();
31 atom_entry:tag("author"):tag("name"):text(opts.author or os.getenv("USER") or "Unknown author"):up(); 29 atom_entry:tag("author"):tag("name"):text(opts.author or os.getenv("USER") or "Unknown author"):up();
32 atom_entry:up(); 30 atom_entry:up();
33 atom_entry:tag("source")
34 :tag("id"):text(opts.node):up()
35 :tag("link", { rel = "self", href = opts.node }):up()
36 :up();
37 if opts.summary then 31 if opts.summary then
38 atom_entry:tag("summary"):text(opts.summary):up(); 32 atom_entry:tag("summary", { type = opts["summary-type"] }):text(opts.summary):up();
39 end 33 end
40 34
41 if opts.content then 35 if opts.content then
42 atom_entry:tag("content"):text(opts.content):up(); 36 atom_entry:tag("content", { type = opts["content-type"] }):text(opts.content):up();
43 end 37 end
44 38
45 for opt, optval in pairs(opts) do 39 for opt, optval in pairs(opts) do
46 if opt:match"^as_" then 40 if opt:match"^as%-" then
47 atom_entry:tag(opt:sub(4):gsub("_", "-"), { xmlns = xmlns_activitystreams } ) 41 atom_entry:tag(opt:sub(4), { xmlns = xmlns_activitystreams } )
48 :text(activitystreams_schema_base..optval):up(); 42 :text(optval):up();
49 end 43 end
50 end 44 end
51 45
52 conn.pubsub(opts.service, opts.node):publish(entry_id, nil, atom_entry 46 conn.pubsub(opts.service, opts.node):publish(entry_id, nil, atom_entry
53 , function(ok) 47 , function(ok)

mercurial