2 local st = require "util.stanza";
3 local send = require "core.sessionmanager".send_to_session;
4 local usermanager_user_exists = require "core.usermanager".user_exists;
5 local usermanager_create_user = require "core.usermanager".create_user;
7 add_iq_handler("c2s", "jabber:iq:register", function (session, stanza)
8 if stanza.tags[1].name == "query" then
9 local query = stanza.tags[1];
10 if stanza.attr.type == "get" then
11 local reply = st.reply(stanza);
12 reply:tag("query", {xmlns = "jabber:iq:register"})
13 :tag("registered"):up()
14 :tag("username"):text(session.username):up()
15 :tag("password"):up();
17 elseif stanza.attr.type == "set" then
18 if query.tags[1] and query.tags[1].name == "remove" then
19 -- TODO delete user auth data, send iq response, kick all user resources with a <not-authorized/>, delete all user data
20 send(session, st.error_reply(stanza, "cancel", "not-allowed"));
22 local username = query:child_with_name("username");
23 local password = query:child_with_name("password");
24 if username and password then
25 -- FIXME shouldn't use table.concat
26 username = table.concat(username);
27 password = table.concat(password);
28 if username == session.username then
29 if usermanager_create_user(username, password, session.host) then -- password change -- TODO is this the right way?
30 send(session, st.reply(stanza));
32 -- TODO unable to write file, file may be locked, etc, what's the correct error?
33 send(session, st.error_reply(stanza, "wait", "internal-server-error"));
36 send(session, st.error_reply(stanza, "modify", "bad-request"));
39 send(session, st.error_reply(stanza, "modify", "bad-request"));
44 send(session, st.error_reply(stanza, "cancel", "service-unavailable"));
48 add_iq_handler("c2s_unauthed", "jabber:iq:register", function (session, stanza)
49 if stanza.tags[1].name == "query" then
50 local query = stanza.tags[1];
51 if stanza.attr.type == "get" then
52 local reply = st.reply(stanza);
53 reply:tag("query", {xmlns = "jabber:iq:register"})
54 :tag("instructions"):text("Choose a username and password for use with this service."):up()
56 :tag("password"):up();
58 elseif stanza.attr.type == "set" then
59 if query.tags[1] and query.tags[1].name == "remove" then
60 send(session, st.error_reply(stanza, "auth", "registration-required"));
62 local username = query:child_with_name("username");
63 local password = query:child_with_name("password");
64 if username and password then
65 -- FIXME shouldn't use table.concat
66 username = table.concat(username);
67 password = table.concat(password);
68 if usermanager_user_exists(username, session.host) then
69 send(session, st.error_reply(stanza, "cancel", "conflict"));
71 if usermanager_create_user(username, password, session.host) then
72 send(session, st.reply(stanza)); -- user created!
74 -- TODO unable to write file, file may be locked, etc, what's the correct error?
75 send(session, st.error_reply(stanza, "wait", "internal-server-error"));
79 send(session, st.error_reply(stanza, "modify", "not-acceptable"));
84 send(session, st.error_reply(stanza, "cancel", "service-unavailable"));