Changeset

246:6c60d19e2180

Merge with Zash
author Matthew Wild <mwild1@gmail.com>
date Mon, 28 Nov 2011 15:00:48 +0000
parents 242:ab4773b0ef5e (diff) 245:19356e2150f3 (current diff)
children 247:7c58c16efa3e 253:aa55793c523e
files
diffstat 4 files changed, 84 insertions(+), 98 deletions(-) [+]
line wrap: on
line diff
--- a/init.lua	Thu Nov 24 07:10:26 2011 +0100
+++ b/init.lua	Mon Nov 28 15:00:48 2011 +0000
@@ -7,6 +7,7 @@
 
 local server = require "net.server";
 local events = require "util.events";
+local logger = require "util.logger";
 
 module("verse", package.seeall);
 local verse = _M;
@@ -18,10 +19,13 @@
 
 verse.plugins = {};
 
+local max_id = 0;
+
 function verse.new(logger, base)
 	local t = setmetatable(base or {}, stream);
-	t.id = tostring(t):match("%x*$");
-	t:set_logger(logger, true);
+	max_id = max_id + 1;
+	t.id = tostring(max_id);
+	t.logger = logger or verse.new_logger("stream"..t.id);
 	t.events = events.new();
 	t.plugins = {};
 	return t;
@@ -29,26 +33,33 @@
 
 verse.add_task = require "util.timer".add_task;
 
-verse.logger = logger.init;
+verse.logger = logger.init; -- COMPAT: Deprecated
+verse.new_logger = logger.init;
 verse.log = verse.logger("verse");
 
-function verse.set_logger(logger)
-	verse.log = logger;
-	server.setlogger(logger);
+local function format(format, ...)
+	local n, arg, maxn = 0, { ... }, select('#', ...);
+	return (format:gsub("%%(.)", function (c) if n <= maxn then n = n + 1; return tostring(arg[n]); end end));
 end
 
-function verse.filter_log(levels, logger)
-	local level_set = {};
-	for _, level in ipairs(levels) do
-		level_set[level] = true;
+function verse.set_log_handler(log_handler, levels)
+	levels = levels or { "debug", "info", "warn", "error" };
+	logger.reset();
+	local function _log_handler(name, level, message, ...)
+		return log_handler(name, level, format(message, ...));
 	end
-	return function (level, name, ...)
-		if level_set[level] then
-			return logger(level, name, ...);
+	if log_handler then
+		for i, level in ipairs(levels) do
+			logger.add_level_sink(level, _log_handler);
 		end
-	end;
+	end
 end
 
+function _default_log_handler(name, level, message)
+	return io.stderr:write(name, "\t", level, "\t", message, "\n");
+end
+verse.set_log_handler(_default_log_handler, { "error" });
+
 local function error_handler(err)
 	verse.log("error", "Error: %s", err);
 	verse.log("error", "Traceback: %s", debug.traceback());
@@ -112,42 +123,15 @@
 
 -- Logging functions
 function stream:debug(...)
-	if self.logger and self.log.debug then
-		return self.logger("debug", ...);
-	end
+	return self.logger("debug", ...);
 end
 
 function stream:warn(...)
-	if self.logger and self.log.warn then
-		return self.logger("warn", ...);
-	end
+	return self.logger("warn", ...);
 end
 
 function stream:error(...)
-	if self.logger and self.log.error then
-		return self.logger("error", ...);
-	end
-end
-
-function stream:set_logger(logger, levels)
-	local old_logger = self.logger;
-	if logger then
-		self.logger = logger;
-	end
-	if levels then
-		if levels == true then
-			levels = { "debug", "info", "warn", "error" };
-		end
-		self.log = {};
-		for _, level in ipairs(levels) do
-			self.log[level] = true;
-		end
-	end
-	return old_logger;
-end
-
-function stream_mt:set_log_levels(levels)
-	self:set_logger(nil, levels);
+	return self.logger("error", ...);
 end
 
 -- Event handling
--- a/libs/logger.lua	Thu Nov 24 07:10:26 2011 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,22 +0,0 @@
-local select, tostring = select, tostring;
-local io_write = io.write;
-module "logger"
-
-local function format(format, ...)
-	local n, maxn = 0, #arg;
-	return (format:gsub("%%(.)", function (c) if c ~= "%" and n <= maxn then n = n + 1; return tostring(arg[n]); end end));
-end
-
-local function format(format, ...)
-	local n, maxn = 0, select('#', ...);
-	local arg = { ... };
-	return (format:gsub("%%(.)", function (c) if n <= maxn then n = n + 1; return tostring(arg[n]); end end));
-end
-
-function init(name)
-	return function (level, message, ...)
-		io_write(level, "\t", format(message, ...), "\n");
-	end
-end
-
-return _M;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/register.lua	Mon Nov 28 15:00:48 2011 +0000
@@ -0,0 +1,27 @@
+local xmlns_register = "jabber:iq:register";
+
+function verse.plugins.register(stream)
+	local function handle_features(features_stanza)
+		if features_stanza:get_child("register", "http://jabber.org/features/iq-register") then
+			stream:send_iq(verse.iq({ to = stream.host_, type = "set" })
+				:tag("query", { xmlns = xmlns_register })
+					:tag("username"):text(stream.username):up()
+					:tag("password"):text(stream.password):up()
+			, function (result)
+				if result.attr.type == "result" then
+					stream:event("registration-success");
+				else
+					local type, condition, text = result:get_error();
+					stream:debug("Registration failed: %s", condition);
+					stream:event("registration-failure", { type = type, condition = condition, text = text });
+				end
+			end);
+		else
+			stream:debug("In-band registration not offered by server");
+			stream:event("registration-failed", { condition = "service-unavailable" });
+		end
+		stream:unhook("stream-features", handle_features);
+		return true;
+	end
+	stream:hook("stream-features", handle_features, 310);
+end
--- a/squishy	Thu Nov 24 07:10:26 2011 +0100
+++ b/squishy	Mon Nov 28 15:00:48 2011 +0000
@@ -3,7 +3,6 @@
 -- Verse-specific versions of libraries
 Module "util.encodings"		"libs/encodings.lua"
 Module "util.hashes"		"libs/hashes.lua"
-Module "util.logger"		"libs/logger.lua"
 Module "util.sha1"		"util/sha1.lua"
 Module "lib.adhoc"              "libs/adhoc.lib.lua"
 
@@ -27,40 +26,38 @@
 Module "util.dataforms"		"util/dataforms.lua"
 Module "util.caps"		"util/caps.lua"
 Module "util.vcard"		"util/vcard.lua"
+Module "util.logger"		"util/logger.lua"
 
 -- Verse plugins
-local function Plugin(name)
-	Module("verse.plugins."..name)("plugins/"..name..".lua")
+plugins = {
+	-- Login
+	"tls", "sasl", "bind", "session", "legacy", "compression";
+	-- Reliability
+	"smacks", "keepalive";
+	-- Queries
+	"disco", "version", "ping", "uptime";
+	-- Privacy control
+	"blocking";
+	-- Jingle / file transfer
+	"jingle", "jingle_ft", "jingle_s5b", "proxy65", "jingle_ibb";
+	-- Pubsub
+	"pubsub", "pep";
+	-- Command and control
+	"adhoc";
+	-- Basics
+	"presence", "private", "roster", "register";
+	-- MUC
+	"groupchat";
+	-- vCard
+	"vcard", "vcard_update";
+	-- Carbons
+	"carbons";
+}
+
+for _, plugin in ipairs(plugins) do
+	Module("verse.plugins."..plugin)("plugins/"..plugin..".lua")
 end
-Plugin "tls"
-Plugin "sasl"
-Plugin "bind"
-Plugin "legacy"
-Plugin "pubsub"
-Plugin "version"
-Plugin "ping"
-Plugin "session"
-Plugin "compression"
-Plugin "blocking"
-Plugin "proxy65"
-Plugin "jingle"
-Plugin "jingle_ft"
-Plugin "jingle_s5b"
-Plugin "jingle_ibb"
-Plugin "presence"
-Plugin "disco"
-Plugin "pep"
-Plugin "adhoc"
-Plugin "private"
-Plugin "groupchat"
-Plugin "uptime"
-Plugin "smacks"
-Plugin "keepalive"
-Plugin "roster"
-Plugin "vcard"
-Plugin "vcard_update"
-Plugin "carbons"
-
+	
 Module "net.httpclient_listener" "net/httpclient_listener.lua"
 Module "net.connlisteners" "net/connlisteners.lua"
 Module "util.httpstream" "util/httpstream.lua"