Software /
code /
prosody
Comparison
tools/ejabberd2prosody.lua @ 643:8ff454831f7d
Moved directory auto-creation to datamanager
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Thu, 25 Dec 2008 06:35:05 +0500 |
parent | 628:3712d36b6d25 |
child | 753:3a0f9cf38f99 |
comparison
equal
deleted
inserted
replaced
642:0ae8584ba3e7 | 643:8ff454831f7d |
---|---|
25 package.path = package.path ..";../?.lua"; | 25 package.path = package.path ..";../?.lua"; |
26 local serialize = require "util.serialization".serialize; | 26 local serialize = require "util.serialization".serialize; |
27 local st = require "util.stanza"; | 27 local st = require "util.stanza"; |
28 package.loaded["util.logger"] = {init = function() return function() end; end} | 28 package.loaded["util.logger"] = {init = function() return function() end; end} |
29 local dm = require "util.datamanager" | 29 local dm = require "util.datamanager" |
30 local data_path = "data"; | 30 dm.set_data_path("data"); |
31 dm.set_data_path(data_path); | |
32 | |
33 local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end | |
34 local _mkdir = {} | |
35 function mkdir(path) | |
36 path = path:gsub("/", path_separator); | |
37 --print("mkdir",path); | |
38 local x = io.popen("mkdir "..path.." 2>&1"):read("*a"); | |
39 end | |
40 function encode(s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end | |
41 function getpath(username, host, datastore, ext) | |
42 ext = ext or "dat"; | |
43 if username then | |
44 return format("%s/%s/%s/%s.%s", data_path, encode(host), datastore, encode(username), ext); | |
45 elseif host then | |
46 return format("%s/%s/%s.%s", data_path, encode(host), datastore, ext); | |
47 else | |
48 return format("%s/%s.%s", data_path, datastore, ext); | |
49 end | |
50 end | |
51 function mkdirs(host) | |
52 if not _mkdir[host] then | |
53 local host_dir = string.format("%s/%s", data_path, encode(host)); | |
54 mkdir(host_dir); | |
55 mkdir(host_dir.."/accounts"); | |
56 mkdir(host_dir.."/vcard"); | |
57 mkdir(host_dir.."/roster"); | |
58 mkdir(host_dir.."/private"); | |
59 mkdir(host_dir.."/offline"); | |
60 _mkdir[host] = true; | |
61 end | |
62 end | |
63 mkdir(data_path); | |
64 | 31 |
65 function build_stanza(tuple, stanza) | 32 function build_stanza(tuple, stanza) |
66 if tuple[1] == "xmlelement" then | 33 if tuple[1] == "xmlelement" then |
67 local name = tuple[2]; | 34 local name = tuple[2]; |
68 local attr = {}; | 35 local attr = {}; |
81 local Megaseconds,Seconds,Microseconds = unpack(tuple); | 48 local Megaseconds,Seconds,Microseconds = unpack(tuple); |
82 return Megaseconds * 1000000 + Seconds; | 49 return Megaseconds * 1000000 + Seconds; |
83 end | 50 end |
84 | 51 |
85 function vcard(node, host, stanza) | 52 function vcard(node, host, stanza) |
86 mkdirs(host); | |
87 local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza)); | 53 local ret, err = dm.store(node, host, "vcard", st.preserialize(stanza)); |
88 print("["..(err or "success").."] vCard: "..node.."@"..host); | 54 print("["..(err or "success").."] vCard: "..node.."@"..host); |
89 end | 55 end |
90 function password(node, host, password) | 56 function password(node, host, password) |
91 mkdirs(host); | |
92 local ret, err = dm.store(node, host, "accounts", {password = password}); | 57 local ret, err = dm.store(node, host, "accounts", {password = password}); |
93 print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password); | 58 print("["..(err or "success").."] accounts: "..node.."@"..host.." = "..password); |
94 end | 59 end |
95 function roster(node, host, jid, item) | 60 function roster(node, host, jid, item) |
96 mkdirs(host); | |
97 local roster = dm.load(node, host, "roster") or {}; | 61 local roster = dm.load(node, host, "roster") or {}; |
98 roster[jid] = item; | 62 roster[jid] = item; |
99 local ret, err = dm.store(node, host, "roster", roster); | 63 local ret, err = dm.store(node, host, "roster", roster); |
100 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid); | 64 print("["..(err or "success").."] roster: " ..node.."@"..host.." - "..jid); |
101 end | 65 end |
102 function private_storage(node, host, xmlns, stanza) | 66 function private_storage(node, host, xmlns, stanza) |
103 mkdirs(host); | |
104 local private = dm.load(node, host, "private") or {}; | 67 local private = dm.load(node, host, "private") or {}; |
105 private[xmlns] = st.preserialize(stanza); | 68 private[xmlns] = st.preserialize(stanza); |
106 local ret, err = dm.store(node, host, "private", private); | 69 local ret, err = dm.store(node, host, "private", private); |
107 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns); | 70 print("["..(err or "success").."] private: " ..node.."@"..host.." - "..xmlns); |
108 end | 71 end |
109 function offline_msg(node, host, t, stanza) | 72 function offline_msg(node, host, t, stanza) |
110 mkdirs(host); | |
111 stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t); | 73 stanza.attr.stamp = os.date("!%Y-%m-%dT%H:%M:%SZ", t); |
112 stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t); | 74 stanza.attr.stamp_legacy = os.date("!%Y%m%dT%H:%M:%S", t); |
113 local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza)); | 75 local ret, err = dm.list_append(node, host, "offline", st.preserialize(stanza)); |
114 print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t)); | 76 print("["..(err or "success").."] offline: " ..node.."@"..host.." - "..os.date("!%Y-%m-%dT%H:%M:%SZ", t)); |
115 end | 77 end |