Changeset

2294:90e4941ea8b6

Merge with Tobias
author Matthew Wild <mwild1@gmail.com>
date Wed, 02 Dec 2009 20:33:09 +0000
parents 2278:8c10f13c0c20 (diff) 2293:abd28ed5d8b0 (current diff)
children 2297:a67d06338db0
files
diffstat 6 files changed, 70 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/core/modulemanager.lua	Tue Dec 01 21:17:13 2009 +0100
+++ b/core/modulemanager.lua	Wed Dec 02 20:33:09 2009 +0000
@@ -158,6 +158,7 @@
 		log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil");
 	end
 	if success then
+		hosts[host].events.fire_event("module-loaded", { module = module_name, host = host });
 		return true;
 	else -- load failed, unloading
 		unload(api_instance.host, module_name);
@@ -174,7 +175,7 @@
 end
 
 function unload(host, name, ...)
-	local mod = get_module(host, name); 
+	local mod = get_module(host, name);
 	if not mod then return nil, "module-not-loaded"; end
 	
 	if module_has_method(mod, "unload") then
@@ -200,6 +201,7 @@
 	end
 	hooks:remove(host, name);
 	modulemap[host][name] = nil;
+	hosts[host].events.fire_event("module-unloaded", { module = name, host = host });
 	return true;
 end
 
@@ -280,7 +282,7 @@
 end
 
 function call_module_method(module, method, ...)
-	if module_has_method(module, method) then	
+	if module_has_method(module, method) then
 		local f = module.module[method];
 		return pcall(f, ...);
 	else
@@ -289,7 +291,7 @@
 end
 
 ----- API functions exposed to modules -----------
--- Must all be in api.* 
+-- Must all be in api.*
 
 -- Returns the name of the current module
 function api:get_name()
--- a/net/dns.lua	Tue Dec 01 21:17:13 2009 +0100
+++ b/net/dns.lua	Wed Dec 02 20:33:09 2009 +0000
@@ -726,7 +726,7 @@
 			local packet = sock:receive();
 			if packet then
 				response = self:decode(packet);
-				if response and self.active[response.header.id] 
+				if response and self.active[response.header.id]
 					and self.active[response.header.id][response.question.raw] then
 					--print('received response');
 					--self.print(response);
--- a/net/httpserver.lua	Tue Dec 01 21:17:13 2009 +0100
+++ b/net/httpserver.lua	Wed Dec 02 20:33:09 2009 +0000
@@ -23,6 +23,9 @@
 
 local log = require "util.logger".init("httpserver");
 
+-- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?)
+local mime_map = { html = "text/html", txt = "plain/text; charset=utf-8", js = "text/javascript" };
+
 local http_servers = {};
 
 module "httpserver"
@@ -65,6 +68,9 @@
 		
 		resp = { "HTTP/1.0 200 OK\r\n" };
 		t_insert(resp, "Connection: close\r\n");
+		t_insert(resp, "Content-Type: ");
+		t_insert(resp, mime_map[request.url.path:match("%.(%w+)")] or "application/octet-stream");
+		t_insert(resp, "\r\n");
 		t_insert(resp, "Content-Length: ");
 		t_insert(resp, #response);
 		t_insert(resp, "\r\n\r\n");
@@ -210,7 +216,7 @@
 function new_request(handler)
 	return { handler = handler, conn = handler.socket, 
 			write = function (...) return handler:write(...); end, state = "request", 
-			server = http_servers[handler.serverport()],
+			server = http_servers[handler:serverport()],
 			send = send_response,
 			destroy = destroy_request,
 			id = tostring{}:match("%x+$")
--- a/plugins/mod_proxy65.lua	Tue Dec 01 21:17:13 2009 +0100
+++ b/plugins/mod_proxy65.lua	Wed Dec 02 20:33:09 2009 +0000
@@ -67,7 +67,7 @@
 			data:sub(4):byte() == 0x03 and -- ATYP must be 3
 			data:sub(5):byte() == 40 and -- SHA1 HASH length must be 40 (0x28)
 			data:sub(-2):byte() == 0x00 and -- PORT must be 0, size 2 byte
-			data:sub(-1):byte() == 0x00 		
+			data:sub(-1):byte() == 0x00
 		then
 			local sha = data:sub(6, 45); -- second param is not count! it's the ending index (included!)
 			if transfers[sha] == nil then
@@ -83,7 +83,7 @@
 			end
 			conn:write(string.char(5, 0, 0, 3, sha:len()) .. sha .. string.char(0, 0)); -- VER, REP, RSV, ATYP, BND.ADDR (sha), BND.PORT (2 Byte)
 		else
-			log:module("warn", "Neither data transfer nor initial connect of a participator of a transfer.")
+			module:log("warn", "Neither data transfer nor initial connect of a participator of a transfer.")
 			conn.close();
 		end
 	else
--- a/prosodyctl	Tue Dec 01 21:17:13 2009 +0100
+++ b/prosodyctl	Wed Dec 02 20:33:09 2009 +0000
@@ -462,6 +462,28 @@
 	return 1;
 end
 
+function commands.addplugin(arg)
+	local url = arg[1];
+	if url:match("^http://") then
+		local http = require "socket.http";
+		show_message("Fetching...");
+		local code, err = http.request(url);
+		if not code then
+			show_message("Failed: "..err);
+			return 1;
+		end
+		if url:match("%.lua$") then
+			local ok, err = datamanager.store(url:match("/mod_([^/]+)$"), "*", "plugins", {code});
+			if not ok then
+				show_message("Failed to save to data store: "..err);
+				return 1;
+			end
+		end
+		show_message("Saved. Don't forget to load the module using the config file or admin console!");
+	else
+		show_message("Sorry, I don't understand how to fetch plugins from there.");
+	end
+end
 
 ---------------------
 
--- a/util/pluginloader.lua	Tue Dec 01 21:17:13 2009 +0100
+++ b/util/pluginloader.lua	Wed Dec 02 20:33:09 2009 +0000
@@ -9,11 +9,19 @@
 
 local plugin_dir = CFG_PLUGINDIR or "./plugins/";
 
-local io_open = io.open;
-local loadstring = loadstring;
+local io_open, os_time = io.open, os.time;
+local loadstring, pairs = loadstring, pairs;
+
+local datamanager = require "util.datamanager";
 
 module "pluginloader"
 
+local function load_from_datastore(name)
+	local content = datamanager.load(name, "*", "plugins");
+	if not content or not content[1] then return nil, "Resource not found"; end
+	return content[1], name;
+end
+
 local function load_file(name)
 	local file, err = io_open(plugin_dir..name);
 	if not file then return file, err; end
@@ -22,16 +30,36 @@
 	return content, name;
 end
 
-function load_resource(plugin, resource)
+function load_resource(plugin, resource, loader)
 	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
+	loader = loader or load_file;
+
+	local content, err = loader(plugin.."/"..resource);
+	if not content then content, err = loader(resource); end
 	-- TODO add support for packed plugins
+	
+	if not content and loader == load_file then
+		return load_resource(plugin, resource, load_from_datastore);
+	end
+	
 	return content, err;
 end
 
+function store_resource(plugin, resource, content, metadata)
+	if not resource then
+		resource = "mod_"..plugin..".lua";
+	end
+	local store = { content };
+	if metadata then
+		for k,v in pairs(metadata) do
+			store[k] = v;
+		end
+	end
+	datamanager.store(plugin.."/"..resource, "*", "plugins", store);
+end
+
 function load_code(plugin, resource)
 	local content, err = load_resource(plugin, resource);
 	if not content then return content, err; end