tools/ejabberd2prosody.lua
author Waqas Hussain <waqas20@gmail.com>
Thu, 04 Dec 2008 23:40:15 +0500
changeset 547 265c4b8f0a8a
parent 519 cccd610a0ef9
child 606 f8f1534a4e06
permissions -rwxr-xr-x
Changed the ejabberd import script to use util.serialization
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
485
f8456f0da769 Make ejabberd2prosody.lua a little more cross-platform :)
Matthew Wild <mwild1@gmail.com>
parents: 484
diff changeset
     1
#!/usr/bin/env lua
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
     2
-- Prosody IM v0.1
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
     3
-- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
     4
-- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
     5
-- 
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
     6
-- This program is free software; you can redistribute it and/or
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
     7
-- modify it under the terms of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
     8
-- as published by the Free Software Foundation; either version 2
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
     9
-- of the License, or (at your option) any later version.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
    10
-- 
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
    11
-- This program is distributed in the hope that it will be useful,
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
    12
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
    13
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
    14
-- GNU General Public License for more details.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
    15
-- 
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
    16
-- You should have received a copy of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
    17
-- along with this program; if not, write to the Free Software
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
    18
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
    19
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
    20
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 489
diff changeset
    21
489
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    22
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    23
require "erlparse";
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    24
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    25
package.path = package.path ..";../?.lua";
547
265c4b8f0a8a Changed the ejabberd import script to use util.serialization
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
    26
local serialize = require "util.serialization".serialize;
489
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    27
local st = require "util.stanza";
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    28
package.loaded["util.logger"] = {init = function() return function() end; end}
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    29
local dm = require "util.datamanager"
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    30
local data_path = "data";
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    31
dm.set_data_path(data_path);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    32
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    33
local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    34
local _mkdir = {}
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    35
function mkdir(path)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    36
	path = path:gsub("/", path_separator);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    37
	--print("mkdir",path);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    38
	local x = io.popen("mkdir "..path.." 2>&1"):read("*a");
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    39
end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    40
function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    41
function getpath(username, host, datastore, ext)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    42
	ext = ext or "dat";
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    43
	if username then
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    44
		return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    45
	elseif host then
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    46
		return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    47
	else
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    48
		return format("%s/%s.%s", data_path, datastore, ext);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    49
	end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    50
end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    51
function mkdirs(host)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    52
	if not _mkdir[host] then
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    53
		local host_dir = string.format("%s/%s", data_path, encode(host));
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    54
		mkdir(host_dir);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    55
		mkdir(host_dir.."/accounts");
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    56
		mkdir(host_dir.."/vcard");
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    57
		mkdir(host_dir.."/roster");
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    58
		mkdir(host_dir.."/private");
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    59
		mkdir(host_dir.."/offline");
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    60
		_mkdir[host] = true;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    61
	end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    62
end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    63
mkdir(data_path);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    64
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    65
function build_stanza(tuple, stanza)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    66
	if tuple[1] == "xmlelement" then
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    67
		local name = tuple[2];
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    68
		local attr = {};
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    69
		for _, a in ipairs(tuple[3]) do attr[a[1]] = a[2]; end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    70
		local up;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    71
		if stanza then stanza:tag(name, attr); up = true; else stanza = st.stanza(name, attr); end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    72
		for _, a in ipairs(tuple[4]) do build_stanza(a, stanza); end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    73
		if up then stanza:up(); else return stanza end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    74
	elseif tuple[1] == "xmlcdata" then
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    75
		stanza:text(tuple[2]);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    76
	else
547
265c4b8f0a8a Changed the ejabberd import script to use util.serialization
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
    77
		error("unknown element type: "..serialize(tuple));
489
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    78
	end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    79
end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    80
function build_time(tuple)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    81
	local Megaseconds,Seconds,Microseconds = unpack(tuple);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    82
	return Megaseconds * 1000000 + Seconds;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    83
end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    84
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    85
function vcard(node, host, stanza)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    86
	mkdirs(host);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    87
	local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza));
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    88
	print("["..(err or "success").."] vCard: "..node.."@"..host);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    89
end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    90
function password(node, host, password)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    91
	mkdirs(host);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    92
	local ret, err = dm.store(node, host, "accounts", {password = password});
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    93
	print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    94
end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    95
function roster(node, host, jid, item)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    96
	mkdirs(host);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    97
	local roster = dm.load(node, host, "roster") or {};
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    98
	roster[jid] = item;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
    99
	local ret, err = dm.store(node, host, "roster", roster);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   100
	print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   101
end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   102
function private_storage(node, host, xmlns, stanza)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   103
	mkdirs(host);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   104
	local private = dm.load(node, host, "private") or {};
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   105
	private[xmlns] = st.preserialize(stanza);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   106
	local ret, err = dm.store(node, host, "private", private);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   107
	print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   108
end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   109
function offline_msg(node, host, t, stanza)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   110
	mkdirs(host);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   111
	stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   112
	stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   113
	local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza));
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   114
	print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t));
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   115
end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   116
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   117
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   118
local filters = {
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   119
	passwd = function(tuple)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   120
		password(tuple[2][1], tuple[2][2], tuple[3]);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   121
	end;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   122
	vcard = function(tuple)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   123
		vcard(tuple[2][1], tuple[2][2], build_stanza(tuple[3]));
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   124
	end;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   125
	roster = function(tuple)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   126
		local node = tuple[3][1]; local host = tuple[3][2];
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   127
		local contact = tuple[4][1].."@"..tuple[4][2];
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   128
		local name = tuple[5]; local subscription = tuple[6];
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   129
		local ask = tuple[7]; local groups = tuple[8];
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   130
		if type(name) ~= type("") then name = nil; end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   131
		if ask == "none" then ask = nil; elseif ask == "out" then ask = "subscribe" else error(ask) end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   132
		if subscription ~= "both" and subscription ~= "from" and subscription ~= "to" and subscription ~= "none" then error(subscription) end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   133
		local item = {name = name, ask = ask, subscription = subscription, groups = {}};
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   134
		for _, g in ipairs(groups) do item.groups[g] = true; end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   135
		roster(node, host, contact, item);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   136
	end;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   137
	private_storage = function(tuple)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   138
		private_storage(tuple[2][1], tuple[2][2], tuple[2][3], build_stanza(tuple[3]));
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   139
	end;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   140
	offline_msg = function(tuple)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   141
		offline_msg(tuple[2][1], tuple[2][2], build_time(tuple[3]), build_stanza(tuple[7]));
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   142
	end;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   143
	config = function(tuple)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   144
		if tuple[2] == "hosts" then
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   145
			local output = io.output(); io.output("prosody.cfg.lua");
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   146
			io.write("-- Configuration imported from ejabberd --\n");
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   147
			io.write([[Host "*"
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   148
	modules_enabled = {
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   149
		"saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   150
		"legacyauth"; -- Legacy authentication. Only used by some old clients and bots.
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   151
		"roster"; -- Allow users to have a roster. Recommended ;)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   152
		"register"; -- Allow users to register on this server using a client
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   153
		"tls"; -- Add support for secure TLS on c2s/s2s connections
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   154
		"vcard"; -- Allow users to set vCards
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   155
		"private"; -- Private XML storage (for room bookmarks, etc.)
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   156
		"version"; -- Replies to server version requests
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   157
		"dialback"; -- s2s dialback support
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   158
		"uptime";
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   159
		"disco";
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   160
		"time";
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   161
		"ping";
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   162
		--"selftests";
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   163
	};
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   164
]]);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   165
			for _, h in ipairs(tuple[3]) do
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   166
				io.write("Host \"" .. h .. "\"\n");
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   167
			end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   168
			io.output(output);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   169
			print("prosody.cfg.lua created");
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   170
		end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   171
	end;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   172
};
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   173
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   174
local arg = ...;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   175
local help = "/? -? ? /h -h /help -help --help";
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   176
if not arg or help:find(arg, 1, true) then
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   177
	print([[ejabberd db dump importer for Prosody
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   178
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   179
  Usage: ejabberd2prosody.lua filename.txt
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   180
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   181
The file can be generated from ejabberd using:
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   182
  sudo ./bin/ejabberdctl dump filename.txt
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   183
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   184
Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   185
	os.exit(1);
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   186
end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   187
local count = 0;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   188
local t = {};
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   189
for item in erlparse.parseFile(arg) do
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   190
	count = count + 1;
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   191
	local name = item[1];
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   192
	t[name] = (t[name] or 0) + 1;
547
265c4b8f0a8a Changed the ejabberd import script to use util.serialization
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
   193
	--print(count, serialize(item));
489
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   194
	if filters[name] then filters[name](item); end
237cddb1a785 Fixed the ejabberd importer to work with the pipe sign "|" as a separator in erlang lists
Waqas Hussain <waqas20@gmail.com>
parents: 485
diff changeset
   195
end
547
265c4b8f0a8a Changed the ejabberd import script to use util.serialization
Waqas Hussain <waqas20@gmail.com>
parents: 519
diff changeset
   196
--print(serialize(t));