clix/publish_atom.lua

Sun, 19 Feb 2012 23:51:05 +0100

author
Kim Alvefur <zash@zash.se>
date
Sun, 19 Feb 2012 23:51:05 +0100
changeset 72
0f69e1902f5b
parent 71
075ecfa46484
child 93
8ab81c304073
permissions
-rw-r--r--

clix.publish_atom: Use the $USER environment variable as fallback for author.

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
1 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
2 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
3
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 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
5 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
6 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
7 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
8 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
9 --if (#arg == 0 or opts.help) then -- why?
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 if opts.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
11 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
12 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
13 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
14 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
15 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
16 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
17 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
18 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
19 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
20 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
21 conn:add_plugin"pubsub";
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 -- Required: id, title, updated, author (which must have a name)
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 entry_id = opts.entry_id or ("clix-"..new_uuid());
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 local atom_entry = verse.stanza("entry", { xmlns="http://www.w3.org/2005/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
25 :tag("id"):text(entry_id):up(id)
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 :tag("title"):text(opts.title or ""):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
27 :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
28 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
29 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
30 if opts.summary 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
31 atom_entry:tag("summary"):text(opts.summary):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 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
33
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 if opts.content 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
35 atom_entry:tag("content"):text(opts.content):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
36 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
37
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
38 conn.pubsub(opts.service, opts.node):publish(entry_id, nil, atom_entry
075ecfa46484 clix.publish_atom: Don't quit until the server responds to our publish request.
Kim Alvefur <zash@zash.se>
parents: 69
diff changeset
39 , 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
40 -- 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
41 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
42 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
43
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 --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
45 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
46 clix_connect(opts, on_connect);
4b65a9a0229b clix.publish_atom: Publish Atom entries into PubSub nodes. (Thanks MattJ for the unfinished code)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 end

mercurial