hg2pubsub.lua

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