Initial commit

Tue, 21 Dec 2010 15:15:40 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 21 Dec 2010 15:15:40 +0000
changeset 0
5db077e371bf
child 1
09dedb9bd52e

Initial commit

hg2pubsub.lua file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hg2pubsub.lua	Tue Dec 21 15:15:40 2010 +0000
@@ -0,0 +1,64 @@
+-- Command-line arguments
+local config_file, server, node, branch, base_link = unpack(arg, 1, 5);
+
+local config = assert(io.open(config_file));
+
+local jid = assert(config:read("*l"), "no JID in config")
+local password = assert(config:read("*l"), "no password in config")
+
+local verse = require "verse";
+require "verse.client";
+local xmppstream = require "util.xmppstream";
+
+local hgstream_handlers = {
+	stream_ns = "";
+	stream_tag = "log";
+	streamopened = function (sess) sess.notopen = nil; end;
+	streamclosed = function () end;
+};
+
+function hgstream_handlers.handlestanza(sess, commit)
+	local atom = verse.stanza("entry", { xmlns = "http://www.w3.org/2005/Atom" });
+	
+	local id = commit.attr.node;
+	atom:tag("id"):text(id):up();
+	
+	local author = commit:get_child("author");
+	atom:tag("author")
+		:tag("name"):text(author:get_text()):up()
+		:tag("email"):text(author.attr.email):up()
+	:up();
+	
+	atom:tag("published"):text(commit:get_child("date"):get_text()):up();
+	
+	atom:tag("title"):text(commit:get_child("msg"):get_text()):up();
+	
+	atom:tag("link", { rel = "alternate", href = base_link..id:sub(1,12) }):up();
+	
+	sess.conn.pubsub:publish(server, node, branch, atom);
+end
+
+local conn = verse.new();
+
+conn:add_plugin("disco");
+conn:add_plugin("pubsub");
+
+conn:hook("stanza", print, 1000);
+conn:hook("stanza-out", print, 1000);
+
+conn:hook("ready", function ()
+	local hgstream = xmppstream.new({ notopen = true, conn = conn }, hgstream_handlers);
+	for line in io.lines() do
+		hgstream:feed(line);
+	end
+	conn:close();
+end);
+
+conn:hook("disconnected", function ()
+	os.exit();
+end);
+
+conn.connect_host = "localhost";
+conn:connect_client(jid, password);
+
+verse.loop();

mercurial