2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local st
= require
"util.stanza";
11 local usermanager_set_password
= require
"core.usermanager".set_password
;
12 local usermanager_delete_user
= require
"core.usermanager".delete_user
;
13 local nodeprep
= require
"util.encodings".stringprep
.nodeprep
;
14 local jid_bare
= require
"util.jid".bare
;
16 local compat
= module
:get_option_boolean("registration_compat", true);
18 module
:add_feature("jabber:iq:register");
20 -- Password change and account deletion handler
21 local function handle_registration_stanza(event
)
22 local session
, stanza
= event
.origin
, event
.stanza
;
23 local log = session
.log or module
._log
;
25 local query
= stanza
.tags
[1];
26 if stanza
.attr
.type == "get" then
27 local reply
= st
.reply(stanza
);
28 reply
:tag("query", {xmlns
= "jabber:iq:register"})
29 :tag("registered"):up()
30 :tag("username"):text(session
.username
):up()
31 :tag("password"):up();
33 else -- stanza.attr.type == "set"
34 if query
.tags
[1] and query
.tags
[1].name
== "remove" then
35 local username
, host
= session
.username
, session
.host
;
37 -- This one weird trick sends a reply to this stanza before the user is deleted
38 local old_session_close
= session
.close
;
39 session
.close
= function(self
, ...)
40 self
.send(st
.reply(stanza
));
41 return old_session_close(self
, ...);
44 local ok
, err
= usermanager_delete_user(username
, host
);
47 log("debug", "Removing user account %s@%s failed: %s", username
, host
, err
);
48 session
.close
= old_session_close
;
49 session
.send(st
.error_reply(stanza
, "cancel", "service-unavailable", err
));
53 log("info", "User removed their account: %s@%s", username
, host
);
54 module
:fire_event("user-deregistered", { username
= username
, host
= host
, source
= "mod_register", session
= session
});
56 local username
= nodeprep(query
:get_child_text("username"));
57 local password
= query
:get_child_text("password");
58 if username
and password
then
59 if username
== session
.username
then
60 if usermanager_set_password(username
, password
, session
.host
, session
.resource
) then
61 session
.send(st
.reply(stanza
));
63 -- TODO unable to write file, file may be locked, etc, what's the correct error?
64 session
.send(st
.error_reply(stanza
, "wait", "internal-server-error"));
67 session
.send(st
.error_reply(stanza
, "modify", "bad-request"));
70 session
.send(st
.error_reply(stanza
, "modify", "bad-request"));
77 module
:hook("iq/self/jabber:iq:register:query", handle_registration_stanza
);
79 module
:hook("iq/host/jabber:iq:register:query", function (event
)
80 local session
, stanza
= event
.origin
, event
.stanza
;
81 if session
.type == "c2s" and jid_bare(stanza
.attr
.to
) == session
.host
then
82 return handle_registration_stanza(event
);