377bf15311e2601872a216ea13bf3fe17e2d8eab
[prosody.git] / plugins / mod_register.lua
1 -- Prosody IM v0.2
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
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.
9 -- 
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.
14 -- 
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.
18 --
19
20
21
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;
26
27 module:add_feature("jabber:iq:register");
28
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();
38                         session.send(reply);
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"));
43                                 --return;
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"});
51                                 end
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
61                                                         -- TODO unsubscribe
62                                                 end
63                                                 if item.subscription == "both" or item.subscription == "from" then
64                                                         -- TODO unsubscribe
65                                                 end
66                                         end
67                                 end
68                                 datamanager.store(session.username, session.host, "accounts", nil); -- delete accounts datastore at the end
69                         else
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));
79                                                 else
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"));
82                                                 end
83                                         else
84                                                 session.send(st.error_reply(stanza, "modify", "bad-request"));
85                                         end
86                                 else
87                                         session.send(st.error_reply(stanza, "modify", "bad-request"));
88                                 end
89                         end
90                 end
91         else
92                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
93         end;
94 end);
95
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();
107                         session.send(reply);
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"));
111                         else
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"));
120                                         else
121                                                 if usermanager_create_user(username, password, session.host) then
122                                                         session.send(st.reply(stanza)); -- user created!
123                                                 else
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"));
126                                                 end
127                                         end
128                                 else
129                                         session.send(st.error_reply(stanza, "modify", "not-acceptable"));
130                                 end
131                         end
132                 end
133         else
134                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
135         end;
136 end);
137