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