mod_presence: return true from the presence handler
[prosody.git] / plugins / mod_register.lua
1 -- Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local hosts = _G.hosts;
11 local st = require "util.stanza";
12 local config = require "core.configmanager";
13 local datamanager = require "util.datamanager";
14 local usermanager_user_exists = require "core.usermanager".user_exists;
15 local usermanager_create_user = require "core.usermanager".create_user;
16 local datamanager_store = require "util.datamanager".store;
17 local os_time = os.time;
18 local nodeprep = require "util.encodings".stringprep.nodeprep;
19
20 module:add_feature("jabber:iq:register");
21
22 module:add_iq_handler("c2s", "jabber:iq:register", function (session, stanza)
23         if stanza.tags[1].name == "query" then
24                 local query = stanza.tags[1];
25                 if stanza.attr.type == "get" then
26                         local reply = st.reply(stanza);
27                         reply:tag("query", {xmlns = "jabber:iq:register"})
28                                 :tag("registered"):up()
29                                 :tag("username"):text(session.username):up()
30                                 :tag("password"):up();
31                         session.send(reply);
32                 elseif stanza.attr.type == "set" then
33                         if query.tags[1] and query.tags[1].name == "remove" then
34                                 -- TODO delete user auth data, send iq response, kick all user resources with a <not-authorized/>, delete all user data
35                                 local username, host = session.username, session.host;
36                                 --session.send(st.error_reply(stanza, "cancel", "not-allowed"));
37                                 --return;
38                                 usermanager_create_user(username, nil, host); -- Disable account
39                                 -- FIXME the disabling currently allows a different user to recreate the account
40                                 -- we should add an in-memory account block mode when we have threading
41                                 session.send(st.reply(stanza));
42                                 local roster = session.roster;
43                                 for _, session in pairs(hosts[host].sessions[username].sessions) do -- disconnect all resources
44                                         session:close({condition = "not-authorized", text = "Account deleted"});
45                                 end
46                                 -- TODO datamanager should be able to delete all user data itself
47                                 datamanager.store(username, host, "roster", nil);
48                                 datamanager.store(username, host, "vcard", nil);
49                                 datamanager.store(username, host, "private", nil);
50                                 datamanager.store(username, host, "offline", nil);
51                                 --local bare = username.."@"..host;
52                                 for jid, item in pairs(roster) do
53                                         if jid ~= "pending" then
54                                                 if item.subscription == "both" or item.subscription == "to" then
55                                                         -- TODO unsubscribe
56                                                 end
57                                                 if item.subscription == "both" or item.subscription == "from" then
58                                                         -- TODO unsubscribe
59                                                 end
60                                         end
61                                 end
62                                 datamanager.store(username, host, "accounts", nil); -- delete accounts datastore at the end
63                         else
64                                 local username = query:child_with_name("username");
65                                 local password = query:child_with_name("password");
66                                 if username and password then
67                                         -- FIXME shouldn't use table.concat
68                                         username = nodeprep(table.concat(username));
69                                         password = table.concat(password);
70                                         if username == session.username then
71                                                 if usermanager_create_user(username, password, session.host) then -- password change -- TODO is this the right way?
72                                                         session.send(st.reply(stanza));
73                                                 else
74                                                         -- TODO unable to write file, file may be locked, etc, what's the correct error?
75                                                         session.send(st.error_reply(stanza, "wait", "internal-server-error"));
76                                                 end
77                                         else
78                                                 session.send(st.error_reply(stanza, "modify", "bad-request"));
79                                         end
80                                 else
81                                         session.send(st.error_reply(stanza, "modify", "bad-request"));
82                                 end
83                         end
84                 end
85         else
86                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
87         end;
88 end);
89
90 local recent_ips = {};
91 local min_seconds_between_registrations = config.get(module.host, "core", "min_seconds_between_registrations");
92 local whitelist_only = config.get(module.host, "core", "whitelist_registration_only");
93 local whitelisted_ips = config.get(module.host, "core", "registration_whitelist") or { "127.0.0.1" };
94 local blacklisted_ips = config.get(module.host, "core", "registration_blacklist") or {};
95
96 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end
97 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end
98
99 module:add_iq_handler("c2s_unauthed", "jabber:iq:register", function (session, stanza)
100         if config.get(module.host, "core", "allow_registration") == false then
101                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
102         elseif stanza.tags[1].name == "query" then
103                 local query = stanza.tags[1];
104                 if stanza.attr.type == "get" then
105                         local reply = st.reply(stanza);
106                         reply:tag("query", {xmlns = "jabber:iq:register"})
107                                 :tag("instructions"):text("Choose a username and password for use with this service."):up()
108                                 :tag("username"):up()
109                                 :tag("password"):up();
110                         session.send(reply);
111                 elseif stanza.attr.type == "set" then
112                         if query.tags[1] and query.tags[1].name == "remove" then
113                                 session.send(st.error_reply(stanza, "auth", "registration-required"));
114                         else
115                                 local username = query:child_with_name("username");
116                                 local password = query:child_with_name("password");
117                                 if username and password then
118                                         -- Check that the user is not blacklisted or registering too often
119                                         if blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then
120                                                         session.send(st.error_reply(stanza, "cancel", "not-acceptable"));
121                                                         return;
122                                         elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
123                                                 if not recent_ips[session.ip] then
124                                                         recent_ips[session.ip] = { time = os_time(), count = 1 };
125                                                 else
126                                                 
127                                                         local ip = recent_ips[session.ip];
128                                                         ip.count = ip.count + 1;
129                                                         
130                                                         if os_time() - ip.time < min_seconds_between_registrations then
131                                                                 ip.time = os_time();
132                                                                 session.send(st.error_reply(stanza, "cancel", "not-acceptable"));
133                                                                 return;
134                                                         end
135                                                         ip.time = os_time();
136                                                 end
137                                         end
138                                         -- FIXME shouldn't use table.concat
139                                         username = nodeprep(table.concat(username));
140                                         password = table.concat(password);
141                                         if usermanager_user_exists(username, session.host) then
142                                                 session.send(st.error_reply(stanza, "cancel", "conflict"));
143                                         else
144                                                 if usermanager_create_user(username, password, session.host) then
145                                                         session.send(st.reply(stanza)); -- user created!
146                                                 else
147                                                         -- TODO unable to write file, file may be locked, etc, what's the correct error?
148                                                         session.send(st.error_reply(stanza, "wait", "internal-server-error"));
149                                                 end
150                                         end
151                                 else
152                                         session.send(st.error_reply(stanza, "modify", "not-acceptable"));
153                                 end
154                         end
155                 end
156         else
157                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
158         end;
159 end);
160