hg2pubsub.lua

Fri, 16 Dec 2011 23:42:24 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 16 Dec 2011 23:42:24 +0000
changeset 6
f54217e38365
parent 3
e6d20e578df3
permissions
-rw-r--r--

README: Fix example service name (thanks Florob)

0
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Command-line arguments
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local config_file, server, node, branch, base_link = unpack(arg, 1, 5);
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local config = assert(io.open(config_file));
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local jid = assert(config:read("*l"), "no JID in config")
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local password = assert(config:read("*l"), "no password in config")
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local verse = require "verse";
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 require "verse.client";
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local xmppstream = require "util.xmppstream";
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local hgstream_handlers = {
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 stream_ns = "";
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 stream_tag = "log";
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 streamopened = function (sess) sess.notopen = nil; end;
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 streamclosed = function () end;
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 };
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 function hgstream_handlers.handlestanza(sess, commit)
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local atom = verse.stanza("entry", { xmlns = "http://www.w3.org/2005/Atom" });
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local id = commit.attr.node;
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 atom:tag("id"):text(id):up();
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local author = commit:get_child("author");
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 atom:tag("author")
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 :tag("name"):text(author:get_text()):up()
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 :tag("email"):text(author.attr.email):up()
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 :up();
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 atom:tag("published"):text(commit:get_child("date"):get_text()):up();
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 atom:tag("title"):text(commit:get_child("msg"):get_text()):up();
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 atom:tag("link", { rel = "alternate", href = base_link..id:sub(1,12) }):up();
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 sess.conn.pubsub:publish(server, node, branch, atom);
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local conn = verse.new();
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 conn:add_plugin("disco");
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 conn:add_plugin("pubsub");
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 conn:hook("stanza", print, 1000);
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 conn:hook("stanza-out", print, 1000);
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 conn:hook("ready", function ()
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local hgstream = xmppstream.new({ notopen = true, conn = conn }, hgstream_handlers);
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 for line in io.lines() do
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 hgstream:feed(line);
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 conn:close();
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end);
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
3
e6d20e578df3 Fix for exit code
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
57 local function exit() os.exit(); end
e6d20e578df3 Fix for exit code
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
58 conn:hook("authentication-failure", exit);
e6d20e578df3 Fix for exit code
Matthew Wild <mwild1@gmail.com>
parents: 2
diff changeset
59 conn:hook("disconnected", exit);
0
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 conn:connect_client(jid, password);
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
5db077e371bf Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 verse.loop();

mercurial