Added: mod_register now replies with an error stanza when file write fails
[prosody.git] / plugins / mod_register.lua
1
2 local st = require "util.stanza";
3 local send = require "core.sessionmanager".send_to_session;
4 local usermanager_user_exists = require "core.usermanager".user_exists;
5 local usermanager_create_user = require "core.usermanager".create_user;
6
7 add_iq_handler("c2s", "jabber:iq:register", function (session, stanza)
8         if stanza.tags[1].name == "query" then
9                 local query = stanza.tags[1];
10                 if stanza.attr.type == "get" then
11                         local reply = st.reply(stanza);
12                         reply:tag("query", {xmlns = "jabber:iq:register"})
13                                 :tag("registered"):up()
14                                 :tag("username"):text(session.username):up()
15                                 :tag("password"):up();
16                         send(session, reply);
17                 elseif stanza.attr.type == "set" then
18                         if query.tags[1] and query.tags[1].name == "remove" then
19                                 -- TODO delete user auth data, send iq response, kick all user resources with a <not-authorized/>, delete all user data
20                                 send(session, st.error_reply(stanza, "cancel", "not-allowed"));
21                         else
22                                 local username = query:child_with_name("username");
23                                 local password = query:child_with_name("password");
24                                 if username and password then
25                                         -- FIXME shouldn't use table.concat
26                                         username = table.concat(username);
27                                         password = table.concat(password);
28                                         if username == session.username then
29                                                 if usermanager_create_user(username, password, session.host) then -- password change -- TODO is this the right way?
30                                                         send(session, st.reply(stanza));
31                                                 else
32                                                         -- TODO unable to write file, file may be locked, etc, what's the correct error?
33                                                         send(session, st.error_reply(stanza, "wait", "internal-server-error"));
34                                                 end
35                                         else
36                                                 send(session, st.error_reply(stanza, "modify", "bad-request"));
37                                         end
38                                 else
39                                         send(session, st.error_reply(stanza, "modify", "bad-request"));
40                                 end
41                         end
42                 end
43         else
44                 send(session, st.error_reply(stanza, "cancel", "service-unavailable"));
45         end;
46 end);
47
48 add_iq_handler("c2s_unauthed", "jabber:iq:register", function (session, stanza)
49         if stanza.tags[1].name == "query" then
50                 local query = stanza.tags[1];
51                 if stanza.attr.type == "get" then
52                         local reply = st.reply(stanza);
53                         reply:tag("query", {xmlns = "jabber:iq:register"})
54                                 :tag("instructions"):text("Choose a username and password for use with this service."):up()
55                                 :tag("username"):up()
56                                 :tag("password"):up();
57                         send(session, reply);
58                 elseif stanza.attr.type == "set" then
59                         if query.tags[1] and query.tags[1].name == "remove" then
60                                 send(session, st.error_reply(stanza, "auth", "registration-required"));
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 = table.concat(username);
67                                         password = table.concat(password);
68                                         if usermanager_user_exists(username, session.host) then
69                                                 send(session, st.error_reply(stanza, "cancel", "conflict"));
70                                         else
71                                                 if usermanager_create_user(username, password, session.host) then
72                                                         send(session, st.reply(stanza)); -- user created!
73                                                 else
74                                                         -- TODO unable to write file, file may be locked, etc, what's the correct error?
75                                                         send(session, st.error_reply(stanza, "wait", "internal-server-error"));
76                                                 end
77                                         end
78                                 else
79                                         send(session, st.error_reply(stanza, "modify", "not-acceptable"));
80                                 end
81                         end
82                 end
83         else
84                 send(session, st.error_reply(stanza, "cancel", "service-unavailable"));
85         end;
86 end);