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 config.get(module.host, "core", "allow_registration") == false then
98 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
99 elseif stanza.tags[1].name == "query" then
100 local query = stanza.tags[1];
101 if stanza.attr.type == "get" then
102 local reply = st.reply(stanza);
103 reply:tag("query", {xmlns = "jabber:iq:register"})
104 :tag("instructions"):text("Choose a username and password for use with this service."):up()
105 :tag("username"):up()
106 :tag("password"):up();
108 elseif stanza.attr.type == "set" then
109 if query.tags[1] and query.tags[1].name == "remove" then
110 session.send(st.error_reply(stanza, "auth", "registration-required"));
112 local username = query:child_with_name("username");
113 local password = query:child_with_name("password");
114 if username and password then
115 -- FIXME shouldn't use table.concat
116 username = table.concat(username);
117 password = table.concat(password);
118 if usermanager_user_exists(username, session.host) then
119 session.send(st.error_reply(stanza, "cancel", "conflict"));
121 if usermanager_create_user(username, password, session.host) then
122 session.send(st.reply(stanza)); -- user created!
124 -- TODO unable to write file, file may be locked, etc, what's the correct error?
125 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
129 session.send(st.error_reply(stanza, "modify", "not-acceptable"));
134 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));