Changeset

50:e5cdd8e93e05

clix.export: XEP-0227 format account exporter
author Kim Alvefur <zash@zash.se>
date Mon, 14 Mar 2011 17:29:01 +0100
parents 49:64e637041347
children 51:e6a408eaa825
files clix/export.lua
diffstat 1 files changed, 75 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clix/export.lua	Mon Mar 14 17:29:01 2011 +0100
@@ -0,0 +1,75 @@
+short_opts.i = "interactive";
+local split_jid = require "util.jid".split;
+local timer = require "util.timer";
+
+local xmlns_pie = "urn:xmpp:pie:0";
+local xmlns_nodes = {
+	{ "query", "jabber:iq:roster" },
+	{ "query", "jabber:iq:privacy" },
+	{ "vCard", "vcard-temp" },
+}
+local xmlns_private = "jabber:iq:private";
+local xmlns_private_nodes = {
+	{ "storage", "storage:bookmarks" },
+}
+
+local function on_connect(conn)
+	local export = verse.stanza("server-data", { xmlns = xmlns_pie })
+		:tag("host", { jid = conn.host })
+			:tag("user", { name = conn.username, password = conn.password });
+	local out = 0;
+
+	for i = 1,#xmlns_nodes do
+		out = out + 1;
+		local node = xmlns_nodes[i];
+		print(("requesting %s*%s"):format(node[2], node[1]));
+		conn:send_iq(verse.iq{type="get"}
+			:tag(node[1], { xmlns = node[2] })
+		, function(stanza)
+			print(("got %s*%s"):format(node[2], node[1]));
+			local query = stanza:get_child(node[1], node[2]);
+			if query then
+				export:add_child(query):up();
+			end
+			out = out - 1;
+		end);
+	end
+
+	for i = 1,#xmlns_private_nodes do
+		out = out + 1;
+		local node = xmlns_private_nodes[i];
+		print(("requesting private node %s*%s"):format(node[2], node[1]));
+		conn:send_iq(verse.iq{type="get"}
+			:tag("query", { xmlns = xmlns_private })
+				:tag(node[1], { xmlns = node[2] })
+		, function(stanza)
+			print(("got private %s*%s"):format(node[2], node[1]));
+			local query = stanza:get_child("query", xmlns_private);
+			if query then
+				export:add_child(query):up();
+			end
+			out = out - 1;
+		end);
+	end
+
+	timer.add_task(1, function ()
+		if out > 0 then
+			return 1
+		end
+		print("<?xml version='1.0' encoding='UTF-8'?>");
+		print(export);
+		conn:close();
+	end)
+end
+
+return function (opts, arg)
+	if opts.short_help then
+		print("Export your account");
+		return;
+	end
+	if opts.help then
+		return 0;
+	end
+	clix_connect(opts, on_connect);
+end
+