clix/publish_atom.lua

Mon, 23 Nov 2020 23:52:31 +0100

author
Kim Alvefur <zash@zash.se>
date
Mon, 23 Nov 2020 23:52:31 +0100
changeset 145
6e05b25c2669
parent 130
11d526d74460
child 168
75e8ca131178
permissions
-rw-r--r--

clix.watch_pep: Move invocation syntax out of short help

So it doesn't clutter the command listing

128
0ae8deb6f75d clix.publish_atom: Import verse
Kim Alvefur <zash@zash.se>
parents: 120
diff changeset
1 local verse = require "verse";
69
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local new_uuid = require "util.uuid".generate;
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local datetime = require "util.datetime".datetime;
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
94
6f038202a502 clix.publish_atom: Move xmlns into a variable
Kim Alvefur <zash@zash.se>
parents: 93
diff changeset
5 local xmlns_atom = "http://www.w3.org/2005/Atom";
97
83cd777fb294 clix.publish_atom: Add a way to attach Activitystrea.ms metadata to the entry
Kim Alvefur <zash@zash.se>
parents: 96
diff changeset
6 local xmlns_activitystreams = "http://activitystrea.ms/spec/1.0/";
94
6f038202a502 clix.publish_atom: Move xmlns into a variable
Kim Alvefur <zash@zash.se>
parents: 93
diff changeset
7
69
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 return function (opts, arg)
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 if opts.short_help then
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 print("Publish an Atom entry to a pubsub node");
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 return;
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 end
130
11d526d74460 clix.publish_atom: Default to XEP-0277 node, require --title or --content
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
13 if opts.help or not (opts.node or opts.title or opts.content) then
69
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 print("clix publish_atom \\");
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 print("","--service=pubsub.shakespeare.lit \\");
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 print("","--node=princely_musings \\");
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 print("","--id=123 \\");
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 print("","--author=Hamlet \\");
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 print("","--title=Soliloquy \\");
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 print("","--summary=\"To be, or not to be: that is the question\"");
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 return 0;
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local function on_connect(conn)
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 -- Required: id, title, updated, author (which must have a name)
95
ea99e6653e18 clix.publish_atom: Fix --id option
Kim Alvefur <zash@zash.se>
parents: 94
diff changeset
25 local entry_id = opts.id or new_uuid();
94
6f038202a502 clix.publish_atom: Move xmlns into a variable
Kim Alvefur <zash@zash.se>
parents: 93
diff changeset
26 local atom_entry = verse.stanza("entry", { xmlns = xmlns_atom})
112
9c4b996bdfd8 clix.publish_atom: Why were there an argument given to :up() ???
Kim Alvefur <zash@zash.se>
parents: 111
diff changeset
27 :tag("id"):text(entry_id):up()
119
d2cb5800ab27 clix.publish_atom: Set type attribute on title, summary and content from --FIELD-type arguments
Kim Alvefur <zash@zash.se>
parents: 113
diff changeset
28 :tag("title", { type = opts["title-type"] }):text(opts.title or ""):up()
69
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 :tag("updated"):text(opts.updated or datetime()):up();
72
0f69e1902f5b clix.publish_atom: Use the $USER environment variable as fallback for author.
Kim Alvefur <zash@zash.se>
parents: 71
diff changeset
30 atom_entry:tag("author"):tag("name"):text(opts.author or os.getenv("USER") or "Unknown author"):up();
69
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 atom_entry:up();
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 if opts.summary then
119
d2cb5800ab27 clix.publish_atom: Set type attribute on title, summary and content from --FIELD-type arguments
Kim Alvefur <zash@zash.se>
parents: 113
diff changeset
33 atom_entry:tag("summary", { type = opts["summary-type"] }):text(opts.summary):up();
69
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 end
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 if opts.content then
119
d2cb5800ab27 clix.publish_atom: Set type attribute on title, summary and content from --FIELD-type arguments
Kim Alvefur <zash@zash.se>
parents: 113
diff changeset
37 atom_entry:tag("content", { type = opts["content-type"] }):text(opts.content):up();
69
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
97
83cd777fb294 clix.publish_atom: Add a way to attach Activitystrea.ms metadata to the entry
Kim Alvefur <zash@zash.se>
parents: 96
diff changeset
40 for opt, optval in pairs(opts) do
129
e805c014efa9 clix.publish_atom: Fix prefix for activitystreams
Kim Alvefur <zash@zash.se>
parents: 128
diff changeset
41 if opt:sub(1,3) == "as_" then
120
3456a6088dc6 clix.publish_atom: Alter Activitystrea.ms argument prefix to --as- and remove IRI prefixing
Kim Alvefur <zash@zash.se>
parents: 119
diff changeset
42 atom_entry:tag(opt:sub(4), { xmlns = xmlns_activitystreams } )
3456a6088dc6 clix.publish_atom: Alter Activitystrea.ms argument prefix to --as- and remove IRI prefixing
Kim Alvefur <zash@zash.se>
parents: 119
diff changeset
43 :text(optval):up();
97
83cd777fb294 clix.publish_atom: Add a way to attach Activitystrea.ms metadata to the entry
Kim Alvefur <zash@zash.se>
parents: 96
diff changeset
44 end
83cd777fb294 clix.publish_atom: Add a way to attach Activitystrea.ms metadata to the entry
Kim Alvefur <zash@zash.se>
parents: 96
diff changeset
45 end
83cd777fb294 clix.publish_atom: Add a way to attach Activitystrea.ms metadata to the entry
Kim Alvefur <zash@zash.se>
parents: 96
diff changeset
46
130
11d526d74460 clix.publish_atom: Default to XEP-0277 node, require --title or --content
Kim Alvefur <zash@zash.se>
parents: 129
diff changeset
47 conn.pubsub(opts.service, opts.node or "urn:xmpp:microblog:0"):publish(entry_id, nil, atom_entry
71
075ecfa46484 clix.publish_atom: Don't quit until the server responds to our publish request.
Kim Alvefur <zash@zash.se>
parents: 69
diff changeset
48 , function(ok)
075ecfa46484 clix.publish_atom: Don't quit until the server responds to our publish request.
Kim Alvefur <zash@zash.se>
parents: 69
diff changeset
49 -- TODO Report success?
075ecfa46484 clix.publish_atom: Don't quit until the server responds to our publish request.
Kim Alvefur <zash@zash.se>
parents: 69
diff changeset
50 conn:close();
075ecfa46484 clix.publish_atom: Don't quit until the server responds to our publish request.
Kim Alvefur <zash@zash.se>
parents: 69
diff changeset
51 end);
69
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 --conn:send(verse.message({ to = opts.to, type = "chat" }) :body(text));
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end
93
8ab81c304073 clix.publish_atom: Don't load pubsub plugin ourself, instead pass to clix_connect
Kim Alvefur <zash@zash.se>
parents: 72
diff changeset
55 clix_connect(opts, on_connect, {"pubsub"});
69
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end

mercurial