Annotate

tools/migration/migrator/jabberd14.lua @ 4417:33c149394dcb

migrator/jabberd14: Support for reading jabberd14 spool files.
author Waqas Hussain <waqas20@gmail.com>
date Sat, 19 Nov 2011 21:12:23 +0500
child 4445:0434eb77d18c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4417
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local lfs = require "lfs";
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 local lxp = require "lxp";
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 local st = require "util.stanza";
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local os_getenv = os.getenv;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 local io_open = io.open;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 local assert = assert;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 local ipairs = ipairs;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local coroutine = coroutine;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local print = print;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 module "jabberd14"
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local function is_dir(path) return lfs.attributes(path, "mode") == "directory"; end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local function is_file(path) return lfs.attributes(path, "mode") == "file"; end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 local function clean_path(path)
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 return path:gsub("\\", "/"):gsub("//+", "/"):gsub("^~", os_getenv("HOME") or "~");
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local parse_xml = (function()
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 local ns_prefixes = {
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 ["http://www.w3.org/XML/1998/namespace"] = "xml";
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 };
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local ns_separator = "\1";
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local ns_pattern = "^([^"..ns_separator.."]*)"..ns_separator.."?(.*)$";
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 return function(xml)
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 local handler = {};
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 local stanza = st.stanza("root");
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 function handler:StartElement(tagname, attr)
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 local curr_ns,name = tagname:match(ns_pattern);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 if name == "" then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 curr_ns, name = "", curr_ns;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 if curr_ns ~= "" then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 attr.xmlns = curr_ns;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 for i=1,#attr do
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 local k = attr[i];
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 attr[i] = nil;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 local ns, nm = k:match(ns_pattern);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 if nm ~= "" then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 ns = ns_prefixes[ns];
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 if ns then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 attr[ns..":"..nm] = attr[k];
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 attr[k] = nil;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 stanza:tag(name, attr);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 function handler:CharacterData(data)
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 stanza:text(data);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 function handler:EndElement(tagname)
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 stanza:up();
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 local parser = lxp.new(handler, "\1");
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 local ok, err, line, col = parser:parse(xml);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 if ok then ok, err, line, col = parser:parse(); end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 --parser:close();
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 if ok then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 return stanza.tags[1];
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 else
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 return ok, err.." (line "..line..", col "..col..")";
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 end;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 end)();
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 local function load_xml(path)
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 if path then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 local f, err = io_open(path);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 if not f then return f, err; end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 local data = f:read("*a");
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 f:close();
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 if data then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 return parse_xml(data);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 local function load_spool_file(host, filename, path)
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 local xml = load_xml(path);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 if not xml then return; end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 local register_element = xml:get_child("query", "jabber:iq:register");
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 local username_element = register_element and register_element:get_child("username", "jabber:iq:register");
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 local password_element = register_element and register_element:get_child("password", "jabber:iq:auth");
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 local username = username_element and username_element:get_text();
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 local password = password_element and password_element:get_text();
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 if not username then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 print("[warn] Missing /xdb/{jabber:iq:register}register/username> in file "..filename)
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 return;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 elseif username..".xml" ~= filename then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 print("[warn] Missing /xdb/{jabber:iq:register}register/username does not match filename "..filename);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 return;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 local userdata = {
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 user = username;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 host = host;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 stores = {};
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 };
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 local stores = userdata.stores;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 stores.accounts = { password = password };
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 for i=1,#xml.tags do
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 local tag = xml.tags[i];
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 local xname = (tag.attr.xmlns or "")..":"..tag.name;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 if tag.attr.j_private_flag == "1" and tag.attr.xmlns then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 -- Private XML
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 stores.private = stores.private or {};
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 tag.attr.j_private_flag = nil;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 stores.private[tag.attr.xmlns] = st.preserialize(tag);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 elseif xname == "jabber:iq:auth:password" then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 if stores.accounts.password ~= tag:get_text() then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 if password then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 print("[warn] conflicting passwords")
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 else
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 stores.accounts.password = tag:get_text();
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 elseif xname == "jabber:iq:register:query" then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 -- already processed
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 elseif xname == "jabber:xdb:nslist:foo" then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 -- ignore
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 elseif xname == "jabber:iq:auth:0k:zerok" then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 -- ignore
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 elseif xname == "jabber:iq:roster:query" then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 -- Roster
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 local roster = {};
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 local subscription_types = { from = true, to = true, both = true, none = true };
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 for _,item_element in ipairs(tag.tags) do
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 assert(item_element.name == "item");
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 assert(item_element.attr.jid);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 assert(subscription_types[item_element.attr.subscription]);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 assert((item_element.attr.ask or "subscribe") == "subscribe")
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 if item_element.name == "item" then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 local groups = {};
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 for _,group_element in ipairs(item_element.tags) do
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 assert(group_element.name == "group");
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 groups[group_element:get_text()] = true;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 local item = {
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 name = item_element.attr.name;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 subscription = item_element.attr.subscription;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 ask = item_element.attr.ask;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 groups = groups;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 };
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 roster[item_element.attr.jid] = item;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152 stores.roster = roster;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 elseif xname == "jabber:iq:last:query" then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 -- Last activity
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 elseif xname == "jabber:x:offline:foo" then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 -- Offline messages
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 elseif xname == "vcard-temp:vCard" then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 -- vCards
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 stores.vcard = st.preserialize(tag);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 else
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 print("[warn] Unknown tag: "..xname);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 return userdata;
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 local function loop_over_users(path, host, cb)
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 for file in lfs.dir(path) do
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 if file:match("%.xml$") then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 local user = load_spool_file(host, file, path.."/"..file);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 if user then cb(user); end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 local function loop_over_hosts(path, cb)
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 for host in lfs.dir(path) do
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 if host ~= "." and host ~= ".." and is_dir(path.."/"..host) then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 loop_over_users(path.."/"..host, host, cb);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 function reader(input)
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184 local path = clean_path(assert(input.path, "no input.path specified"));
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 assert(is_dir(path), "input.path is not a directory");
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187 if input.host then
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 return coroutine.wrap(function() loop_over_users(input.path, input.host, coroutine.yield) end);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
189 else
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
190 return coroutine.wrap(function() loop_over_hosts(input.path, coroutine.yield) end);
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
191 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 end
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193
33c149394dcb migrator/jabberd14: Support for reading jabberd14 spool files.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 return _M;