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