Changeset

1362:75c39ac6efa0

Merge with trunk
author Waqas Hussain <waqas20@gmail.com>
date Sat, 20 Jun 2009 01:05:06 +0500
parents 1361:4e56889c33e1 (diff) 1347:4eccafa3609f (current diff)
children 1363:daa51f48736d 1374:e85726d084d6
files
diffstat 7 files changed, 51 insertions(+), 273 deletions(-) [+]
line wrap: on
line diff
--- a/core/modulemanager.lua	Fri Jun 19 17:32:01 2009 +0100
+++ b/core/modulemanager.lua	Sat Jun 20 01:05:06 2009 +0500
@@ -18,6 +18,7 @@
 local multitable_new = require "util.multitable".new;
 local register_actions = require "core.actions".register;
 local st = require "util.stanza";
+local pluginloader = require "util.pluginloader";
 
 local hosts = hosts;
 local prosody = prosody;
@@ -113,7 +114,7 @@
 	end
 	
 
-	local mod, err = loadfile(get_module_filename(module_name));
+	local mod, err = pluginloader.load_code(module_name);
 	if not mod then
 		log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
 		return nil, err;
@@ -187,7 +188,7 @@
 	local mod = get_module(host, name);
 	if not mod then return nil, "module-not-loaded"; end
 
-	local _mod, err = loadfile(get_module_filename(name)); -- checking for syntax errors
+	local _mod, err = pluginloader.load_code(name); -- checking for syntax errors
 	if not _mod then
 		log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
 		return nil, err;
@@ -267,12 +268,6 @@
 	end
 end
 
-local _modulepath = { plugin_dir, "mod_", nil, ".lua"};
-function get_module_filename(name)
-	_modulepath[3] = name;
-	return t_concat(_modulepath);
-end
-
 ----- API functions exposed to modules -----------
 -- Must all be in api.* 
 
--- a/core/presencemanager.lua	Fri Jun 19 17:32:01 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,233 +0,0 @@
--- Prosody IM v0.4
--- Copyright (C) 2008-2009 Matthew Wild
--- Copyright (C) 2008-2009 Waqas Hussain
--- 
--- This project is MIT/X11 licensed. Please see the
--- COPYING file in the source package for more information.
---
-
-
-
-local log = require "util.logger".init("presencemanager")
-
-local require = require;
-local pairs, ipairs = pairs, ipairs;
-local t_concat = table.concat;
-local s_find = string.find;
-local tonumber = tonumber;
-
-local st = require "util.stanza";
-local jid_split = require "util.jid".split;
-local hosts = hosts;
-
-local rostermanager = require "core.rostermanager";
-local sessionmanager = require "core.sessionmanager";
-local offlinemanager = require "core.offlinemanager";
-
-module "presencemanager"
-
-function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound)
-	local type = stanza.attr.type;
-	if type and type ~= "unavailable" and type ~= "error" then
-		if inbound then
-			handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
-		else
-			handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
-		end
-	elseif not inbound and not stanza.attr.to then
-		handle_normal_presence(origin, stanza, core_route_stanza);
-	else
-		core_route_stanza(origin, stanza);
-	end
-end
-
-function handle_normal_presence(origin, stanza, core_route_stanza)
-	if origin.roster then
-		for jid in pairs(origin.roster) do -- broadcast to all interested contacts
-			local subscription = origin.roster[jid].subscription;
-			if subscription == "both" or subscription == "from" then
-				stanza.attr.to = jid;
-				core_route_stanza(origin, stanza);
-			end
-		end
-		local node, host = jid_split(stanza.attr.from);
-		for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
-			if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence
-				stanza.attr.to = res.full_jid;
-				core_route_stanza(origin, stanza);
-			end
-		end
-		if stanza.attr.type == nil and not origin.presence then -- initial presence
-			local probe = st.presence({from = origin.full_jid, type = "probe"});
-			for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
-				local subscription = origin.roster[jid].subscription;
-				if subscription == "both" or subscription == "to" then
-					probe.attr.to = jid;
-					core_route_stanza(origin, probe);
-				end
-			end
-			for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
-				if res ~= origin and res.presence then
-					res.presence.attr.to = origin.full_jid;
-					core_route_stanza(res, res.presence);
-					res.presence.attr.to = nil;
-				end
-			end
-			if origin.roster.pending then -- resend incoming subscription requests
-				for jid in pairs(origin.roster.pending) do
-					origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
-				end
-			end
-			local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
-			for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
-				if item.ask then
-					request.attr.to = jid;
-					core_route_stanza(origin, request);
-				end
-			end
-			local offline = offlinemanager.load(node, host);
-			if offline then
-				for _, msg in ipairs(offline) do
-					origin.send(msg); -- FIXME do we need to modify to/from in any way?
-				end
-				offlinemanager.deleteAll(node, host);
-			end
-		end
-		origin.priority = 0;
-		if stanza.attr.type == "unavailable" then
-			origin.presence = nil;
-			if origin.directed then
-				local old_from = stanza.attr.from;
-				stanza.attr.from = origin.full_jid;
-				for jid in pairs(origin.directed) do
-					stanza.attr.to = jid;
-					core_route_stanza(origin, stanza);
-				end
-				stanza.attr.from = old_from;
-				origin.directed = nil;
-			end
-		else
-			origin.presence = stanza;
-			local priority = stanza:child_with_name("priority");
-			if priority and #priority > 0 then
-				priority = t_concat(priority);
-				if s_find(priority, "^[+-]?[0-9]+$") then
-					priority = tonumber(priority);
-					if priority < -128 then priority = -128 end
-					if priority > 127 then priority = 127 end
-					origin.priority = priority;
-				end
-			end
-		end
-		stanza.attr.to = nil; -- reset it
-	else
-		log("error", "presence recieved from client with no roster");
-	end
-end
-
-function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)
-	local h = hosts[host];
-	local count = 0;
-	if h and h.type == "local" then
-		local u = h.sessions[user];
-		if u then
-			for k, session in pairs(u.sessions) do
-				local pres = session.presence;
-				if pres then
-					pres.attr.to = jid;
-					pres.attr.from = session.full_jid;
-					core_route_stanza(session, pres);
-					pres.attr.to = nil;
-					pres.attr.from = nil;
-					count = count + 1;
-				end
-			end
-		end
-	end
-	log("debug", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);
-	return count;
-end
-
-function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
-	local node, host = jid_split(from_bare);
-	local st_from, st_to = stanza.attr.from, stanza.attr.to;
-	stanza.attr.from, stanza.attr.to = from_bare, to_bare;
-	log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
-	if stanza.attr.type == "subscribe" then
-		-- 1. route stanza
-		-- 2. roster push (subscription = none, ask = subscribe)
-		if rostermanager.set_contact_pending_out(node, host, to_bare) then
-			rostermanager.roster_push(node, host, to_bare);
-		end -- else file error
-		core_route_stanza(origin, stanza);
-	elseif stanza.attr.type == "unsubscribe" then
-		-- 1. route stanza
-		-- 2. roster push (subscription = none or from)
-		if rostermanager.unsubscribe(node, host, to_bare) then
-			rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
-		end -- else file error
-		core_route_stanza(origin, stanza);
-	elseif stanza.attr.type == "subscribed" then
-		-- 1. route stanza
-		-- 2. roster_push ()
-		-- 3. send_presence_of_available_resources
-		if rostermanager.subscribed(node, host, to_bare) then
-			rostermanager.roster_push(node, host, to_bare);
-		end
-		core_route_stanza(origin, stanza);
-		send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);
-	elseif stanza.attr.type == "unsubscribed" then
-		-- 1. route stanza
-		-- 2. roster push (subscription = none or to)
-		if rostermanager.unsubscribed(node, host, to_bare) then
-			rostermanager.roster_push(node, host, to_bare);
-		end
-		core_route_stanza(origin, stanza);
-	end
-	stanza.attr.from, stanza.attr.to = st_from, st_to;
-end
-
-function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)
-	local node, host = jid_split(to_bare);
-	local st_from, st_to = stanza.attr.from, stanza.attr.to;
-	stanza.attr.from, stanza.attr.to = from_bare, to_bare;
-	log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);
-	if stanza.attr.type == "probe" then
-		if rostermanager.is_contact_subscribed(node, host, from_bare) then
-			if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
-				-- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
-			end
-		else
-			core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
-		end
-	elseif stanza.attr.type == "subscribe" then
-		if rostermanager.is_contact_subscribed(node, host, from_bare) then
-			core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
-			-- Sending presence is not clearly stated in the RFC, but it seems appropriate
-			if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then
-				-- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
-			end
-		else
-			if not rostermanager.is_contact_pending_in(node, host, from_bare) then
-				if rostermanager.set_contact_pending_in(node, host, from_bare) then
-					sessionmanager.send_to_available_resources(node, host, stanza);
-				end -- TODO else return error, unable to save
-			end
-		end
-	elseif stanza.attr.type == "unsubscribe" then
-		if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
-			rostermanager.roster_push(node, host, from_bare);
-		end
-	elseif stanza.attr.type == "subscribed" then
-		if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
-			rostermanager.roster_push(node, host, from_bare);
-		end
-	elseif stanza.attr.type == "unsubscribed" then
-		if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then
-			rostermanager.roster_push(node, host, from_bare);
-		end
-	end -- discard any other type
-	stanza.attr.from, stanza.attr.to = st_from, st_to;
-end
-
-return _M;
--- a/core/stanza_router.lua	Fri Jun 19 17:32:01 2009 +0100
+++ b/core/stanza_router.lua	Sat Jun 20 01:05:06 2009 +0500
@@ -17,28 +17,18 @@
 local user_exists = require "core.usermanager".user_exists;
 
 local rostermanager = require "core.rostermanager";
-local sessionmanager = require "core.sessionmanager";
 local offlinemanager = require "core.offlinemanager";
 
 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
 local component_handle_stanza = require "core.componentmanager".handle_stanza;
 
-local handle_outbound_presence_subscriptions_and_probes = function()end;--require "core.presencemanager".handle_outbound_presence_subscriptions_and_probes;
-local handle_inbound_presence_subscriptions_and_probes = function()end;--require "core.presencemanager".handle_inbound_presence_subscriptions_and_probes;
-local handle_normal_presence = function()end;--require "core.presencemanager".handle_normal_presence;
-
-local format = string.format;
 local tostring = tostring;
-local t_concat = table.concat;
 local t_insert = table.insert;
-local tonumber = tonumber;
-local s_find = string.find;
 local pairs = pairs;
 local ipairs = ipairs;
 
 local jid_split = require "util.jid".split;
 local jid_prepped_split = require "util.jid".prepped_split;
-local print = print;
 local fire_event = prosody.events.fire_event;
 
 local select_best_resources;
@@ -228,7 +218,7 @@
 				-- if we get here, resource was not specified or was unavailable
 				if stanza.name == "presence" then
 					if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
-						handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
+						-- inbound presence subscriptions and probes, already handled, so should never get here
 					elseif not resource then -- sender is available or unavailable or error
 						for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
 							if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
@@ -268,7 +258,7 @@
 			if user_exists(node, host) then
 				if stanza.name == "presence" then
 					if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
-						handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
+						-- inbound presence subscriptions and probes, already handled, so should never get here
 					else
 						-- TODO send unavailable presence or unsubscribed
 					end
@@ -310,8 +300,8 @@
 		stanza.attr.xmlns = xmlns; -- reset
 	elseif origin.type == "component" or origin.type == "local" then
 		-- Route via s2s for components and modules
-		log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
-		send_s2s(origin.host, host, stanza);
+		log("debug", "Routing outgoing stanza for %s to %s", from_host, host);
+		send_s2s(from_host, host, stanza);
 	else
 		log("warn", "received stanza from unhandled connection type: %s", origin.type);
 	end
--- a/plugins/mod_muc.lua	Fri Jun 19 17:32:01 2009 +0100
+++ b/plugins/mod_muc.lua	Sat Jun 20 01:05:06 2009 +0500
@@ -321,7 +321,7 @@
 		elseif type ~= 'result' then -- bad type
 			origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
 		end
-	elseif not current_nick and type ~= "error" then -- not in room
+	elseif not current_nick and type ~= "error" and type ~= "result" 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"));
@@ -333,7 +333,7 @@
 		if o_data then
 			log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
 			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
+			-- TODO 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);
 		elseif type ~= "error" and type ~= "result" then -- recipient not in room
--- a/plugins/mod_roster.lua	Fri Jun 19 17:32:01 2009 +0100
+++ b/plugins/mod_roster.lua	Sat Jun 20 01:05:06 2009 +0500
@@ -15,11 +15,10 @@
 local t_concat = table.concat;
 local tostring = tostring;
 
-local handle_presence = require "core.presencemanager".handle_presence;
 local rm_remove_from_roster = require "core.rostermanager".remove_from_roster;
 local rm_add_to_roster = require "core.rostermanager".add_to_roster;
 local rm_roster_push = require "core.rostermanager".roster_push;
-local core_route_stanza = core_route_stanza;
+local core_post_stanza = core_post_stanza;
 
 module:add_feature("jabber:iq:roster");
 
@@ -83,11 +82,9 @@
 											rm_roster_push(from_node, from_host, jid);
 											local to_bare = node and (node.."@"..host) or host; -- bare JID
 											if r_item.subscription == "both" or r_item.subscription == "from" then
-												handle_presence(session, st.presence({type="unsubscribed"}), from_bare, to_bare,
-													core_route_stanza, false);
+												core_post_stanza(session, st.presence({type="unsubscribed", from=session.full_jid, to=to_bare}));
 											elseif r_item.subscription == "both" or r_item.subscription == "to" then
-												handle_presence(session, st.presence({type="unsubscribe"}), from_bare, to_bare,
-													core_route_stanza, false);
+												core_post_stanza(session, st.presence({type="unsubscribe", from=session.full_jid, to=to_bare}));
 											end
 										else
 											session.send(st.error_reply(stanza, err_type, err_cond, err_msg));
--- a/util/muc.lua	Fri Jun 19 17:32:01 2009 +0100
+++ b/util/muc.lua	Sat Jun 20 01:05:06 2009 +0500
@@ -195,7 +195,7 @@
 				log("debug", "%s leaving %s", current_nick, room);
 				local data = self._participants[current_nick];
 				data.role = 'none';
-				room_broadcast_presence(room, pr);
+				room_broadcast_presence(self, pr);
 				self._participants[current_nick] = nil;
 				self._jid_nick[from] = nil;
 			end
@@ -262,7 +262,7 @@
 		elseif type ~= 'result' then -- bad type
 			origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
 		end
-	elseif not current_nick and type ~= "error" then -- not in room
+	elseif not current_nick and type ~= "error" and type ~= "result" 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"));
@@ -274,7 +274,7 @@
 		if o_data then
 			log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
 			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
+			-- TODO 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;
 			self:route_stanza(stanza);
 		elseif type ~= "error" and type ~= "result" then -- recipient not in room
@@ -338,14 +338,10 @@
 
 local function room_handle_stanza(self, origin, stanza)
 	local to_node, to_host, to_resource = jid_split(stanza.attr.to);
-	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
+	if to_resource then
 		room_handle_to_occupant(self, origin, stanza);
-	elseif to_node then
-		room_handle_to_room(self, origin, stanza)
-	else -- to the main muc domain
+	else
+		room_handle_to_room(self, origin, stanza);
 	end
 end
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/util/pluginloader.lua	Sat Jun 20 01:05:06 2009 +0500
@@ -0,0 +1,33 @@
+
+local plugin_dir = CFG_PLUGINDIR or "./plugins/";
+
+local io_open = io.open;
+local loadstring = loadstring;
+
+module "pluginloader"
+
+local function load_file(name)
+	local file, err = io_open(plugin_dir..name);
+	if not file then return file, err; end
+	local content = file:read("*a");
+	file:close();
+	return content, name;
+end
+
+function load_resource(plugin, resource)
+	if not resource then
+		resource = "mod_"..plugin..".lua";
+	end
+	local content, err = load_file(plugin.."/"..resource);
+	if not content then content, err = load_file(resource); end
+	-- TODO add support for packed plugins
+	return content, err;
+end
+
+function load_code(plugin, resource)
+	local content, err = load_resource(plugin, resource);
+	if not content then return content, err; end
+	return loadstring(content, err), err;
+end
+
+return _M;