Changeset

814:f12b1ddd458d

Merge from waqas
author Matthew Wild <mwild1@gmail.com>
date Mon, 16 Feb 2009 15:44:54 +0000
parents 806:655293df3d7e (current diff) 813:ebfb904640d8 (diff)
children 815:cf8392613f46 817:e3e3919b6c7e 859:43f7e342135d
files
diffstat 3 files changed, 175 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_muc.lua	Mon Feb 16 15:44:23 2009 +0000
+++ b/plugins/mod_muc.lua	Mon Feb 16 15:44:54 2009 +0000
@@ -41,6 +41,29 @@
 
 local component;
 
+function filter_xmlns_from_array(array, filters)
+	local count = 0;
+	for i=#array,1,-1 do
+		local attr = array[i].attr;
+		if filters[attr and attr.xmlns] then
+			t_remove(array, i);
+			count = count + 1;
+		end
+	end
+	return count;
+end
+function filter_xmlns_from_stanza(stanza, filters)
+	if filters then
+		if filter_xmlns_from_array(stanza.tags, filters) ~= 0 then
+			return stanza, filter_xmlns_from_array(stanza, filters);
+		end
+	end
+	return stanza, 0;
+end
+local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true};
+function get_filtered_presence(stanza)
+	return filter_xmlns_from_stanza(st.deserialize(st.preserialize(stanza)), presence_filters);
+end
 function getUsingPath(stanza, path, getText)
 	local tag = stanza;
 	for _, name in ipairs(path) do
@@ -93,7 +116,10 @@
 	if subject == "" then subject = nil; end
 	rooms_info:set(room, 'subject', subject);
 	save_room();
-	broadcast_message(current_nick, room, subject or "", nil);
+	local msg = st.message({type='groupchat', from=from})
+		:tag('subject'):text(subject):up();
+	broadcast_message_stanza(room, msg, false);
+	--broadcast_message(current_nick, room, subject or "", nil);
 	return true;
 end
 
@@ -144,6 +170,56 @@
 		end
 	end
 end
+function broadcast_message_stanza(room, stanza, historic)
+	local r = rooms:get(room);
+	if r then
+		for occupant, o_data in pairs(r) do
+			for jid in pairs(o_data.sessions) do
+				stanza.attr.to = jid;
+				core_route_stanza(component, stanza);
+			end
+		end
+		if historic then -- add to history
+			local history = rooms_info:get(room, 'history');
+			if not history then history = {}; rooms_info:set(room, 'history', history); end
+			-- stanza = st.deserialize(st.preserialize(stanza));
+			stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203
+			stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
+			t_insert(history, st.preserialize(stanza));
+			while #history > history_length do t_remove(history, 1) end
+		end
+	end
+end
+function broadcast_presence_stanza(room, stanza, code, nick)
+	stanza = get_filtered_presence(stanza);
+	local data = rooms:get(room, stanza.attr.from);
+	stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
+		:tag("item", {affiliation=data.affiliation, role=data.role, nick=nick}):up();
+	if code then
+		stanza:tag("status", {code=code}):up();
+	end
+	local me;
+	local r = rooms:get(room);
+	if r then
+		for occupant, o_data in pairs(r) do
+			if occupant ~= stanza.attr.from then
+				for jid in pairs(o_data.sessions) do
+					stanza.attr.to = jid;
+					core_route_stanza(component, stanza);
+				end
+			else
+				me = o_data;
+			end
+		end
+	end
+	if me then
+		stanza:tag("status", {code='110'});
+		for jid in pairs(me.sessions) do
+			stanza.attr.to = jid;
+			core_route_stanza(component, stanza);
+		end
+	end
+end
 
 function handle_to_occupant(origin, stanza) -- PM, vCards, etc
 	local from, to = stanza.attr.from, stanza.attr.to;
@@ -151,24 +227,34 @@
 	local current_nick = jid_nick:get(from, room);
 	local type = stanza.attr.type;
 	if stanza.name == "presence" then
+		local pr = get_filtered_presence(stanza);
+		pr.attr.from = to;
 		if type == "error" then -- error, kick em out!
-			local data = rooms:get(room, to);
-			data.role = 'none';
-			broadcast_presence('unavailable', to, room); -- TODO also add <status>This participant is kicked from the room because he sent an error presence: badformed error stanza</status>
-			rooms:remove(room, to);
-			jid_nick:remove(from, room);
-		elseif type == "unavailable" then -- unavailable
-			if current_nick == to then
+			if current_nick then
 				local data = rooms:get(room, to);
 				data.role = 'none';
-				broadcast_presence('unavailable', to, room);
+				local pr = st.presence({type='unavailable', from=current_nick}):tag('status'):text('This participant is kicked from the room because he sent an error presence'):up()
+					:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
+					:tag("item", {affiliation=data.affiliation, role=data.role}):up();
+				broadcast_presence_stanza(room, pr);
+				--broadcast_presence('unavailable', to, room); -- TODO also add <status>This participant is kicked from the room because he sent an error presence: badformed error stanza</status>
 				rooms:remove(room, to);
 				jid_nick:remove(from, room);
-			end -- TODO else do nothing?
+			end
+		elseif type == "unavailable" then -- unavailable
+			if current_nick then
+				local data = rooms:get(room, to);
+				data.role = 'none';
+				broadcast_presence_stanza(room, pr);
+				--broadcast_presence('unavailable', to, room);
+				rooms:remove(room, to);
+				jid_nick:remove(from, room);
+			end
 		elseif not type then -- available
 			if current_nick then
 				if current_nick == to then -- simple presence
-					-- TODO broadcast
+					broadcast_presence_stanza(room, pr);
+					-- FIXME check if something was filtered. if it was, then user may be rejoining
 				else -- change nick
 					if rooms:get(room, to) then
 						origin.send(st.error_reply(stanza, "cancel", "conflict"));
@@ -176,26 +262,36 @@
 						local data = rooms:get(room, current_nick);
 						local to_nick = select(3, jid_split(to));
 						if to_nick then
-							broadcast_presence('unavailable', current_nick, room, '303', to_nick);
+							local p = st.presence({type='unavailable', from=current_nick});
+								--[[:tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
+									:tag('item', {affiliation=data.affiliation, role=data.role, nick=to_nick}):up()
+									:tag('status', {code='303'});]]
+							broadcast_presence_stanza(room, p, '303', to_nick);
+							--broadcast_presence('unavailable', current_nick, room, '303', to_nick);
 							rooms:remove(room, current_nick);
 							rooms:set(room, to, data);
 							jid_nick:set(from, room, to);
-							broadcast_presence(nil, to, room, nil);
+							broadcast_presence_stanza(room, pr);
+							--broadcast_presence(nil, to, room, nil);
 						else
-							--TODO: malformed-jid
+							--TODO malformed-jid
 						end
 					end
 				end
 			else -- enter room
+				local new_nick = to;
 				if rooms:get(room, to) then
+					new_nick = nil;
+				end
+				if not new_nick then
 					origin.send(st.error_reply(stanza, "cancel", "conflict"));
 				else
 					local data;
 					if not rooms:get(room) and not rooms_info:get(room) then -- new room
-						data = {affiliation='owner', role='moderator', jid=from};
+						data = {affiliation='owner', role='moderator', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
 					end
 					if not data then -- new occupant
-						data = {affiliation='none', role='participant', jid=from};
+						data = {affiliation='none', role='participant', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
 					end
 					rooms:set(room, to, data);
 					jid_nick:set(from, room, to);
@@ -203,14 +299,18 @@
 					if r then
 						for occupant, o_data in pairs(r) do
 							if occupant ~= from then
-								local pres = st.presence({to=from, from=occupant})
+								local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
+								pres.attr.to, pres.attr.from = from, occupant;
+								pres
+								--local pres = st.presence({to=from, from=occupant})
 									:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
 									:tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
 								core_route_stanza(component, pres);
 							end
 						end
 					end
-					broadcast_presence(nil, to, room);
+					broadcast_presence_stanza(room, pr);
+					--broadcast_presence(nil, to, room);
 					local history = rooms_info:get(room, 'history'); -- send discussion history
 					if history then
 						for _, msg in ipairs(history) do
@@ -227,11 +327,31 @@
 		elseif type ~= 'result' then -- bad type
 			origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
 		end
-	elseif stanza.name == "message" and type == "groupchat" then
-		-- groupchat messages not allowed in PM
+	elseif not current_nick then -- not in room
+		origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
+	elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM
 		origin.send(st.error_reply(stanza, "modify", "bad-request"));
-	else
-		origin.send(st.error_reply(stanza, "cancel", "not-implemented", "Private stanzas not implemented")); -- TODO route private stanza
+	elseif stanza.name == "message" and type == "error" then
+		if current_nick then
+			local data = rooms:get(room, to);
+			data.role = 'none';
+			local pr = st.presence({type='unavailable', from=current_nick}):tag('status'):text('This participant is kicked from the room because he sent an error message to another occupant'):up()
+				:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
+				:tag("item", {affiliation=data.affiliation, role=data.role}):up();
+			broadcast_presence_stanza(room, pr);
+			rooms:remove(room, to);
+			jid_nick:remove(from, room);
+		end
+	else -- private stanza
+		local o_data = rooms:get(room, to);
+		if o_data then
+			local jid = o_data.jid;
+			if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end
+			stanza.attr.to, stanza.attr.from = jid, current_nick;
+			core_route_stanza(component, stanza);
+		else -- recipient not in room
+			origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
+		end
 	end
 end
 
@@ -253,14 +373,26 @@
 		if not current_nick then -- not in room
 			origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
 		else
+			local from = stanza.attr.from;
+			stanza.attr.from = current_nick;
 			local subject = getText(stanza, {"subject"});
 			if subject then
-				set_subject(current_nick, room, subject);
+				set_subject(current_nick, room, subject); -- TODO use broadcast_message_stanza
 			else
-				broadcast_message(current_nick, room, nil, getText(stanza, {"body"}));
-				-- TODO add to discussion history
+				--broadcast_message(current_nick, room, nil, getText(stanza, {"body"}));
+				broadcast_message_stanza(room, stanza, true);
 			end
 		end
+	elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick
+		local to = stanza.attr.to;
+		local current_nick = jid_nick:get(stanza.attr.from, to);
+		if current_nick then
+			stanza.attr.to = current_nick;
+			handle_to_occupant(origin, stanza);
+			stanza.attr.to = to;
+		else
+			origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
+		end
 	else
 		if type == "error" or type == "result" then return; end
 		origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
@@ -286,10 +418,7 @@
 
 register_component(muc_domain, function(origin, stanza)
 	local to_node, to_host, to_resource = jid_split(stanza.attr.to);
-	if stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
-		if type == "error" or type == "result" then return; end
-		origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME what's appropriate?
-	elseif to_resource and not to_node then
+	if to_resource and not to_node then
 		if type == "error" or type == "result" then return; end
 		origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource
 	elseif to_resource then
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/run_tests.bat	Mon Feb 16 15:44:54 2009 +0000
@@ -0,0 +1,10 @@
+@echo off
+
+set oldpath=%path%
+set path=%path%;..;..\lualibs
+
+del reports\*.report
+lua test.lua %*
+
+set path=%oldpath%
+set oldpath=
\ No newline at end of file
--- a/tests/test.lua	Mon Feb 16 15:44:23 2009 +0000
+++ b/tests/test.lua	Mon Feb 16 15:44:54 2009 +0000
@@ -21,8 +21,13 @@
 
 local verbosity = tonumber(arg[1]) or 2;
 
-package.path = package.path..";../?.lua";
-package.cpath = package.cpath..";../?.so";
+if os.getenv("WINDIR") then
+	package.path = package.path..";..\\?.lua";
+	package.cpath = package.cpath..";..\\?.dll";
+else
+	package.path = package.path..";../?.lua";
+	package.cpath = package.cpath..";../?.so";
+end
 
 require "util.import"