2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 local st = require "util.stanza";
23 local usermanager_user_exists = require "core.usermanager".user_exists;
24 local usermanager_create_user = require "core.usermanager".create_user;
25 local datamanager_store = require "util.datamanager".store;
27 module:add_feature("jabber:iq:register");
29 module:add_iq_handler("c2s", "jabber:iq:register", function (session, stanza)
30 if stanza.tags[1].name == "query" then
31 local query = stanza.tags[1];
32 if stanza.attr.type == "get" then
33 local reply = st.reply(stanza);
34 reply:tag("query", {xmlns = "jabber:iq:register"})
35 :tag("registered"):up()
36 :tag("username"):text(session.username):up()
37 :tag("password"):up();
39 elseif stanza.attr.type == "set" then
40 if query.tags[1] and query.tags[1].name == "remove" then
41 -- TODO delete user auth data, send iq response, kick all user resources with a <not-authorized/>, delete all user data
42 --session.send(st.error_reply(stanza, "cancel", "not-allowed"));
44 usermanager_create_user(session.username, nil, session.host); -- Disable account
45 -- FIXME the disabling currently allows a different user to recreate the account
46 -- we should add an in-memory account block mode when we have threading
47 session.send(st.reply(stanza));
48 local roster = session.roster;
49 for _, session in pairs(hosts[session.host].sessions[session.username].sessions) do -- disconnect all resources
50 session:disconnect({condition = "not-authorized", text = "Account deleted"});
52 -- TODO datamanager should be able to delete all user data itself
53 datamanager.store(session.username, session.host, "roster", nil);
54 datamanager.store(session.username, session.host, "vCard", nil);
55 datamanager.store(session.username, session.host, "private", nil);
56 datamanager.store(session.username, session.host, "offline", nil);
57 local bare = session.username.."@"..session.host;
58 for jid, item in pairs(roster) do
59 if jid ~= "pending" then
60 if item.subscription == "both" or item.subscription == "to" then
63 if item.subscription == "both" or item.subscription == "from" then
68 datamanager.store(session.username, session.host, "accounts", nil); -- delete accounts datastore at the end
70 local username = query:child_with_name("username");
71 local password = query:child_with_name("password");
72 if username and password then
73 -- FIXME shouldn't use table.concat
74 username = table.concat(username);
75 password = table.concat(password);
76 if username == session.username then
77 if usermanager_create_user(username, password, session.host) then -- password change -- TODO is this the right way?
78 session.send(st.reply(stanza));
80 -- TODO unable to write file, file may be locked, etc, what's the correct error?
81 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
84 session.send(st.error_reply(stanza, "modify", "bad-request"));
87 session.send(st.error_reply(stanza, "modify", "bad-request"));
92 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
96 module:add_iq_handler("c2s_unauthed", "jabber:iq:register", function (session, stanza)
97 if stanza.tags[1].name == "query" then
98 local query = stanza.tags[1];
99 if stanza.attr.type == "get" then
100 local reply = st.reply(stanza);
101 reply:tag("query", {xmlns = "jabber:iq:register"})
102 :tag("instructions"):text("Choose a username and password for use with this service."):up()
103 :tag("username"):up()
104 :tag("password"):up();
106 elseif stanza.attr.type == "set" then
107 if query.tags[1] and query.tags[1].name == "remove" then
108 session.send(st.error_reply(stanza, "auth", "registration-required"));
110 local username = query:child_with_name("username");
111 local password = query:child_with_name("password");
112 if username and password then
113 -- FIXME shouldn't use table.concat
114 username = table.concat(username);
115 password = table.concat(password);
116 if usermanager_user_exists(username, session.host) then
117 session.send(st.error_reply(stanza, "cancel", "conflict"));
119 if usermanager_create_user(username, password, session.host) then
120 session.send(st.reply(stanza)); -- user created!
122 -- TODO unable to write file, file may be locked, etc, what's the correct error?
123 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
127 session.send(st.error_reply(stanza, "modify", "not-acceptable"));
132 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));