plugins/mod_InBandRegistration.lua

changeset 60
44800be871f5
equal deleted inserted replaced
57:126b25079399 60:44800be871f5
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 internal error, unable to write file, file may be locked, etc
33 end
34 else
35 send(session, st.error_reply(stanza, "modify", "bad-request"));
36 end
37 else
38 send(session, st.error_reply(stanza, "modify", "bad-request"));
39 end
40 end
41 end
42 else
43 send(session, st.error_reply(stanza, "cancel", "service-unavailable"));
44 end;
45 end);
46
47 add_iq_handler("c2s_unauthed", "jabber:iq:register", function (session, stanza)
48 if stanza.tags[1].name == "query" then
49 local query = stanza.tags[1];
50 if stanza.attr.type == "get" then
51 local reply = st.reply(stanza);
52 reply:tag("query", {xmlns = "jabber:iq:register"})
53 :tag("instructions"):text("Choose a username and password for use with this service."):up()
54 :tag("username"):up()
55 :tag("password"):up();
56 send(session, reply);
57 elseif stanza.attr.type == "set" then
58 if query.tags[1] and query.tags[1].name == "remove" then
59 send(session, st.error_reply(stanza, "auth", "registration-required"));
60 else
61 local username = query:child_with_name("username");
62 local password = query:child_with_name("password");
63 if username and password then
64 -- FIXME shouldn't use table.concat
65 username = table.concat(username);
66 password = table.concat(password);
67 if usermanager_user_exists(username, session.host) then
68 send(session, st.error_reply(stanza, "cancel", "conflict"));
69 else
70 if usermanager_create_user(username, password, session.host) then
71 send(session, st.reply(stanza)); -- user created!
72 else
73 -- TODO internal error, unable to write file, file may be locked, etc
74 end
75 end
76 else
77 send(session, st.error_reply(stanza, "modify", "not-acceptable"));
78 end
79 end
80 end
81 else
82 send(session, st.error_reply(stanza, "cancel", "service-unavailable"));
83 end;
84 end);

mercurial