3 -- Copyright (C) 2008-2010 Matthew Wild
4 -- Copyright (C) 2008-2010 Waqas Hussain
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
12 package
.path
= package
.path
..";../?.lua";
14 local my_name
= arg
[0];
15 if my_name
:match("[/\\]") then
16 package
.path
= package
.path
..";"..my_name
:gsub("[^/\\]+$", "../?.lua");
17 package
.path
= package
.path
..";"..my_name
:gsub("[^/\\]+$", "?.lua");
18 package
.cpath
= package
.cpath
..";"..my_name
:gsub("[^/\\]+$", "../?.so");
21 local erlparse
= require
"erlparse";
25 package
.loaded
["util.logger"] = {init
= function() return function() end; end}
26 local serialize
= require
"util.serialization".serialize
;
27 local st
= require
"util.stanza";
28 local dm
= require
"util.datamanager"
29 dm
.set_data_path("data");
31 function build_stanza(tuple
, stanza
)
32 assert(type(tuple
) == "table", "XML node is of unexpected type: "..type(tuple
));
33 if tuple
[1] == "xmlelement" or tuple
[1] == "xmlel" then
34 assert(type(tuple
[2]) == "string", "element name has type: "..type(tuple
[2]));
35 assert(type(tuple
[3]) == "table", "element attribute array has type: "..type(tuple
[3]));
36 assert(type(tuple
[4]) == "table", "element children array has type: "..type(tuple
[4]));
37 local name
= tuple
[2];
39 for _
, a
in ipairs(tuple
[3]) do
40 if type(a
[1]) == "string" and type(a
[2]) == "string" then attr
[a
[1]]
= a
[2]; end
43 if stanza
then stanza
:tag(name
, attr
); up
= true; else stanza
= st
.stanza(name
, attr
); end
44 for _
, a
in ipairs(tuple
[4]) do build_stanza(a
, stanza
); end
45 if up
then stanza
:up(); else return stanza
end
46 elseif tuple
[1] == "xmlcdata" then
47 if type(tuple
[2]) ~= "table" then
48 assert(type(tuple
[2]) == "string", "XML CDATA has unexpected type: "..type(tuple
[2]));
49 stanza
:text(tuple
[2]);
50 end -- else it's [], i.e., the null value, used for the empty string
52 error("unknown element type: "..serialize(tuple
));
55 function build_time(tuple
)
56 local Megaseconds
,Seconds
,Microseconds
= unpack(tuple
);
57 return Megaseconds
* 1000000 + Seconds
;
59 function build_jid(tuple
, full
)
60 local node
, jid
, resource
= tuple
[1], tuple
[2], tuple
[3]
61 if type(node
) == "string" and node
~= "" then
62 jid
= tuple
[1] .. "@" .. jid
;
64 if full
and type(resource
) == "string" and resource
~= "" then
65 jid
= jid
.. "/" .. resource
;
70 function vcard(node
, host
, stanza
)
71 local ret
, err
= dm
.store(node
, host
, "vcard", st
.preserialize(stanza
));
72 print("["..(err
or "success").."] vCard: "..node
.."@"..host
);
74 function password(node
, host
, password
)
76 if type(password
) == "string" then
77 data
.password
= password
;
78 elseif type(password
) == "table" and password
[1] == "scram" then
79 local unb64
= require
"mime".unb64
;
81 return s
:gsub(".", function (c
)
82 return ("%02x"):format(c
:byte());
85 data
.stored_key
= hex(unb64(password
[2]));
86 data
.server_key
= hex(unb64(password
[3]));
87 data
.salt
= unb64(password
[4]);
88 data
.iteration_count
= password
[5];
90 local ret
, err
= dm
.store(node
, host
, "accounts", data
);
91 print("["..(err
or "success").."] accounts: "..node
.."@"..host
);
93 function roster(node
, host
, jid
, item
)
94 local roster
= dm
.load(node
, host
, "roster") or {};
96 local ret
, err
= dm
.store(node
, host
, "roster", roster
);
97 print("["..(err
or "success").."] roster: " ..node
.."@"..host
.." - "..jid
);
99 function roster_pending(node
, host
, jid
)
100 local roster
= dm
.load(node
, host
, "roster") or {};
101 roster
.pending
= roster
.pending
or {};
102 roster
.pending
[jid
] = true;
103 local ret
, err
= dm
.store(node
, host
, "roster", roster
);
104 print("["..(err
or "success").."] roster: " ..node
.."@"..host
.." - "..jid
);
106 function private_storage(node
, host
, xmlns
, stanza
)
107 local private
= dm
.load(node
, host
, "private") or {};
108 private
[stanza
.name
..":"..xmlns
] = st
.preserialize(stanza
);
109 local ret
, err
= dm
.store(node
, host
, "private", private
);
110 print("["..(err
or "success").."] private: " ..node
.."@"..host
.." - "..xmlns
);
112 function offline_msg(node
, host
, t
, stanza
)
113 stanza
.attr
.stamp
= os
.date("!%Y-%m-%dT%H:%M:%SZ", t
);
114 stanza
.attr
.stamp_legacy
= os
.date("!%Y%m%dT%H:%M:%S", t
);
115 local ret
, err
= dm
.list_append(node
, host
, "offline", st
.preserialize(stanza
));
116 print("["..(err
or "success").."] offline: " ..node
.."@"..host
.." - "..os
.date("!%Y-%m-%dT%H:%M:%SZ", t
));
118 function privacy(node
, host
, default
, lists
)
119 local privacy
= { lists
= {} };
121 if default
then privacy
.default
= default
; end
122 for _
, inlist
in ipairs(lists
) do
123 local name
, items
= inlist
[1], inlist
[2];
124 local list
= { name
= name
; items
= {}; };
126 for _
, item
in pairs(items
) do
128 if item
[1] ~= "listitem" then print("[error] privacy: unhandled item: "..tostring(item
[1])); break; end
129 local _type
, value
= item
[2], item
[3];
130 if _type
== "jid" then
131 if type(value
) ~= "table" then print("[error] privacy: jid value is not valid: "..tostring(value
)); break; end
132 local _node
, _host
, _resource
= value
[1], value
[2], value
[3];
133 value
= build_jid(value
, true)
134 elseif _type
== "none" then
137 elseif _type
== "group" then
138 if type(value
) ~= "string" then print("[error] privacy: group value is not string: "..tostring(value
)); break; end
139 elseif _type
== "subscription" then
140 if value
~="both" and value
~="from" and value
~="to" and value
~="none" then
141 print("[error] privacy: subscription value is invalid: "..tostring(value
)); break;
143 else print("[error] privacy: invalid item type: "..tostring(_type
)); break; end
144 local action
= item
[4];
145 if action
~= "allow" and action
~= "deny" then print("[error] privacy: unhandled action: "..tostring(action
)); break; end
146 local order
= item
[5];
147 if type(order
) ~= "number" or order
<0 then print("[error] privacy: order is not numeric: "..tostring(order
)); break; end
148 if orders
[order
] then print("[error] privacy: duplicate order value: "..tostring(order
)); break; end
149 orders
[order
] = true;
150 local match_all
= item
[6];
151 local match_iq
= item
[7];
152 local match_message
= item
[8];
153 local match_presence_in
= item
[9];
154 local match_presence_out
= item
[10];
155 list
.items
[#list
.items
+1] = {
160 message
= match_message
== "true";
161 iq
= match_iq
== "true";
162 ["presence-in"] = match_presence_in
== "true";
163 ["presence-out"] = match_presence_out
== "true";
167 table.sort(list
.items
, function(a
, b
) return a
.order
< b
.order
; end);
168 if privacy
.lists
[list
.name
] then print("[warn] duplicate privacy list: "..tostring(list
.name
)); end
169 privacy
.lists
[list
.name
] = list
;
172 if default
and not privacy
.lists
[default
] then
173 if default
== "none" then privacy
.default
= nil;
174 else print("[warn] default privacy list doesn't exist: "..tostring(default
)); end
176 local ret
, err
= dm
.store(node
, host
, "privacy", privacy
);
177 print("["..(err
or "success").."] privacy: " ..node
.."@"..host
.." - "..count
.." list(s)");
179 function muc_room(node
, host
, properties
)
180 local store
= { jid
= node
.."@"..host
, _data
= {}, _affiliations
= {} };
181 for _
,aff
in ipairs(properties
.affiliations
) do
182 store
._affiliations
[build_jid(aff
[1])] = aff
[2][1] or aff
[2];
184 store
._data
.subject
= properties
.subject
;
185 if properties
.subject_author
then
186 store
._data
.subject_from
= store
.jid
.. "/" .. properties
.subject_author
;
188 store
._data
.name
= properties
.title
;
189 store
._data
.description
= properties
.description
;
190 store
._data
.password
= properties
.password
;
191 store
._data
.moderated
= (properties
.moderated
== "true") or nil;
192 store
._data
.members_only
= (properties
.members_only
== "true") or nil;
193 store
._data
.persistent
= (properties
.persistent
== "true") or nil;
194 store
._data
.changesubject
= (properties
.allow_change_subj
== "true") or nil;
195 store
._data
.whois
= properties
.anonymous
== "true" and "moderators" or "anyone";
196 store
._data
.hidden
= (properties
.public_list
== "false") or nil;
198 if not store
._data
.persistent
then
199 return print("[error] muc_room: skipping non-persistent room: "..node
.."@"..host
);
202 local ret
, err
= dm
.store(node
, host
, "config", store
);
204 ret
, err
= dm
.load(nil, host
, "persistent");
205 if ret
or not err
then
207 ret
[store
.jid
] = true;
208 ret
, err
= dm
.store(nil, host
, "persistent", ret
);
211 print("["..(err
or "success").."] muc_room: " ..node
.."@"..host
);
216 passwd
= function(tuple
)
217 password(tuple
[2][1], tuple
[2][2], tuple
[3]);
219 vcard
= function(tuple
)
220 vcard(tuple
[2][1], tuple
[2][2], build_stanza(tuple
[3]));
222 roster
= function(tuple
)
223 local node
= tuple
[3][1]; local host
= tuple
[3][2];
224 local contact
= build_jid(tuple
[4]);
225 local name
= tuple
[5]; local subscription
= tuple
[6];
226 local ask
= tuple
[7]; local groups
= tuple
[8];
227 if type(name
) ~= type("") then name
= nil; end
228 if ask
== "none" then
230 elseif ask
== "out" then
232 elseif ask
== "in" then
233 roster_pending(node
, host
, contact
);
235 elseif ask
== "both" then
236 roster_pending(node
, host
, contact
);
238 else error("Unknown ask type: "..ask
); end
239 if subscription
~= "both" and subscription
~= "from" and subscription
~= "to" and subscription
~= "none" then error(subscription
) end
240 local item
= {name
= name
, ask
= ask
, subscription
= subscription
, groups
= {}};
241 for _
, g
in ipairs(groups
) do
242 if type(g
) == "string" then
243 item
.groups
[g
] = true;
246 roster(node
, host
, contact
, item
);
248 private_storage
= function(tuple
)
249 private_storage(tuple
[2][1], tuple
[2][2], tuple
[2][3], build_stanza(tuple
[3]));
251 offline_msg
= function(tuple
)
252 offline_msg(tuple
[2][1], tuple
[2][2], build_time(tuple
[3]), build_stanza(tuple
[7]));
254 privacy
= function(tuple
)
255 privacy(tuple
[2][1], tuple
[2][2], tuple
[3], tuple
[4]);
257 muc_room
= function(tuple
)
258 local properties
= {};
259 for _
,pair
in ipairs(tuple
[3]) do
260 if not(type(pair
[2]) == "table" and #pair
[2] == 0) then -- skip nil values
261 properties
[pair
[1]]
= pair
[2];
264 muc_room(tuple
[2][1], tuple
[2][2], properties
);
266 --[=[config = function(tuple)
267 if tuple
[2] == "hosts" then
268 local output
= io
.output(); io
.output("prosody.cfg.lua");
269 io
.write("-- Configuration imported from ejabberd --\n");
272 "saslauth"; -- Authentication for clients and servers. Recommended if you want to log in.
273 "legacyauth"; -- Legacy authentication. Only used by some old clients and bots.
274 "roster"; -- Allow users to have a roster. Recommended ;)
275 "register"; -- Allow users to register on this server using a client
276 "tls"; -- Add support for secure TLS on c2s/s2s connections
277 "vcard"; -- Allow users to set vCards
278 "private"; -- Private XML storage (for room bookmarks, etc.)
279 "version"; -- Replies to server version requests
280 "dialback"; -- s2s dialback support
288 for _
, h
in ipairs(tuple
[3]) do
289 io
.write("Host \"" .. h
.. "\"\n");
292 print("prosody.cfg.lua created");
298 local help
= "/? -? ? /h -h /help -help --help";
299 if not arg
or help
:find(arg
, 1, true) then
300 print([[ejabberd db dump importer for Prosody
302 Usage: ]]..my_name
..[[ filename.txt
304 The file can be generated from ejabberd using:
305 sudo ejabberdctl dump filename.txt
307 Note: The path of ejabberdctl depends on your ejabberd installation, and ejabberd needs to be running for ejabberdctl to work.]]);
312 for item
in erlparse
.parseFile(arg
) do
314 local name
= item
[1];
315 t
[name
] = (t
[name
] or 0) + 1;
316 --print(count, serialize(item));
317 if filters
[name
] then filters
[name
](item
); end
319 --print(serialize(t));