Changeset

1640:1d96629f5dda

Merge
author Matthew Wild <mwild1@gmail.com>
date Tue, 31 Mar 2015 12:46:17 +0100
parents 1639:398c4aaccf6d (diff) 1637:ad6694f7b13c (current diff)
children 1641:1fa25cfb0ad4
files
diffstat 4 files changed, 70 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_log_events/mod_log_events.lua	Tue Mar 31 12:46:17 2015 +0100
@@ -0,0 +1,13 @@
+module:set_global();
+
+local helpers = require "util.helpers";
+
+helpers.log_events(prosody.events, "global", module._log);
+
+function module.add_host(module)
+	helpers.log_events(prosody.hosts[module.host].events, module.host, module._log);
+end
+
+function module.remove_host(module)
+	helpers.revert_log_events(prosody.hosts[module.host].events);
+end
--- a/mod_log_messages_sql/mod_log_messages_sql.lua	Sat Mar 21 10:12:23 2015 +0000
+++ b/mod_log_messages_sql/mod_log_messages_sql.lua	Tue Mar 31 12:46:17 2015 +0100
@@ -98,7 +98,11 @@
 	return ...;
 end
 function sql.commit(...)
-	if not connection:commit() then return nil, "SQL commit failed"; end
+	local ok, err = connection:commit();
+	if not ok then
+		module:log("error", "SQL commit failed: %s", tostring(err));
+		return nil, "SQL commit failed: "..tostring(err);
+	end
 	return ...;
 end
 
--- a/mod_mam_muc/mod_mam_muc.lua	Sat Mar 21 10:12:23 2015 +0000
+++ b/mod_mam_muc/mod_mam_muc.lua	Tue Mar 31 12:46:17 2015 +0100
@@ -138,7 +138,7 @@
 };
 
 -- Serve form
-module:hook("iq-get/self/"..xmlns_mam..":query", function(event)
+module:hook("iq-get/bare/"..xmlns_mam..":query", function(event)
 	local origin, stanza = event.origin, event.stanza;
 	return origin.send(st.reply(stanza):add_child(query_form:form()));
 end);
--- a/mod_storage_gdbm/mod_storage_gdbm.lua	Sat Mar 21 10:12:23 2015 +0000
+++ b/mod_storage_gdbm/mod_storage_gdbm.lua	Tue Mar 31 12:46:17 2015 +0100
@@ -1,5 +1,5 @@
 -- mod_storage_gdbm
--- Copyright (C) 2014 Kim Alvefur
+-- Copyright (C) 2014-2015 Kim Alvefur
 --
 -- This file is MIT/X11 licensed.
 -- 
@@ -9,20 +9,28 @@
 local gdbm = require"gdbm";
 local path = require"util.paths";
 local lfs = require"lfs";
+local st = require"util.stanza";
 local uuid = require"util.uuid".generate;
+
 local serialization = require"util.serialization";
-local st = require"util.stanza";
 local serialize = serialization.serialize;
 local deserialize = serialization.deserialize;
 
+local g_set, g_get, g_del = gdbm.replace, gdbm.fetch, gdbm.delete;
+local g_first, g_next = gdbm.firstkey, gdbm.nextkey;
+
+local t_remove = table.remove;
+
+local empty = {};
+
 local function id(v) return v; end
 
 local function is_stanza(s)
 	return getmetatable(s) == st.stanza_mt;
 end
 
-local function ifelse(cond, iftrue, iffalse)
-	if cond then return iftrue; end return iffalse;
+local function t(c, a, b)
+	if c then return a; end return b;
 end
 
 local base_path = path.resolve_relative_path(prosody.paths.data, module.host);
@@ -34,17 +42,31 @@
 local keyval_mt = { __index = keyval, suffix = ".db" };
 
 function keyval:set(user, value)
-	local ok, err = gdbm.replace(self._db, user or "@", serialize(value));
+	if type(value) == "table" and next(value) == nil then
+		value = nil;
+	end
+	if value ~= nil then
+		value = serialize(value);
+	end
+	local ok, err = (value and g_set or g_del)(self._db, user or "@", value);
 	if not ok then return nil, err; end
 	return true;
 end
 
 function keyval:get(user)
-	local data, err = gdbm.fetch(self._db, user or "@");
+	local data, err = g_get(self._db, user or "@");
 	if not data then return nil, err; end
 	return deserialize(data);
 end
 
+local function g_keys(db, key)
+	return (key == nil and g_first or g_next)(db, key);
+end
+
+function keyval:users()
+	return g_keys, self._db, nil;
+end
+
 local archive = {};
 local archive_mt = { __index = archive, suffix = ".adb" };
 
@@ -64,9 +86,10 @@
 	end
 	meta[i] = { key = key, when = when, with = with, type = type };
 	meta[key] = i;
-	local ok, err = self:set(username, meta);
+	local prefix = (username or "@") .. "#";
+	local ok, err = self:set(prefix..key, value);
 	if not ok then return nil, err; end
-	ok, err = self:set(key, value);
+	ok, err = self:set(username, meta);
 	if not ok then return nil, err; end
 	return key;
 end
@@ -76,17 +99,19 @@
 };
 
 function archive:find(username, query)
-	local meta = self:get(username);
+	query = query or empty;
+	local meta = self:get(username) or empty;
+	local prefix = (username or "@") .. "#";
 	local r = query.reverse;
-	local d = r and -1 or 1;
-	local s = meta[ifelse(r, query.before, query.after)];
+	local d = t(r, -1, 1);
+	local s = meta[t(r, query.before, query.after)];
 	local limit = query.limit;
 	if s then
 		s = s + d;
 	else
-		s = ifelse(r, #meta, 1)
+		s = t(r, #meta, 1)
 	end
-	local e = ifelse(r, 1, #meta);
+	local e = t(r, 1, #meta);
 	local c = 0;
 	return function ()
 		if limit and c >= limit then return end
@@ -97,7 +122,7 @@
 			and (not query.start or item.when >= query.start)
 			and (not query["end"] or item.when <= query["end"]) then
 				s = i + d; c = c + 1;
-				value = self:get(item.key);
+				value = self:get(prefix..item.key);
 				return item.key, (deserialize[item.type] or id)(value), item.when, item.with;
 			end
 		end
@@ -126,6 +151,18 @@
 	return setmetatable({ _db = db; _path = db_path; store = store, typ = type }, driver_mt);
 end
 
+function purge(_, user)
+	for dir in lfs.dir(base_path) do
+		local name, ext = dir:match("^(.-)%.a?db$");
+		if ext == ".db" then
+			open(_, name, "keyval"):set(user, nil);
+		elseif ext == ".adb" then
+			open(_, name, "archive"):delete(user);
+		end
+	end
+	return true;
+end
+
 function module.unload()
 	for path, db in pairs(cache) do
 		gdbm.sync(db);