X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Fdatamanager.lua;h=30cd082bdf31eb5232542c19034caf44eb37f97f;hb=1acc8f231b80f3806f99ba20a7bd981cf5c85f63;hp=82f840efac0a7afe06e1b230b32632e8dee4b659;hpb=a4d1701bd5e08cce7b221200394776a3171ddcac;p=prosody.git diff --git a/util/datamanager.lua b/util/datamanager.lua index 82f840ef..30cd082b 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -1,4 +1,4 @@ --- Prosody IM v0.3 +-- Prosody IM -- Copyright (C) 2008-2009 Matthew Wild -- Copyright (C) 2008-2009 Waqas Hussain -- @@ -15,13 +15,13 @@ local loadfile, setfenv, pcall = loadfile, setfenv, pcall; local log = require "util.logger".init("datamanager"); local io_open = io.open; local os_remove = os.remove; -local io_popen = io.popen; local tostring, tonumber = tostring, tonumber; local error = error; local next = next; local t_insert = table.insert; local append = require "util.serialization".append; local path_separator = "/"; if os.getenv("WINDIR") then path_separator = "\\" end +local lfs_mkdir = require "lfs".mkdir; module "datamanager" @@ -43,24 +43,52 @@ local _mkdir = {}; local function mkdir(path) path = path:gsub("/", path_separator); -- TODO as an optimization, do this during path creation rather than here if not _mkdir[path] then - local x = io_popen("mkdir \""..path.."\" 2>&1"):read("*a"); + lfs_mkdir(path); _mkdir[path] = true; end return path; end local data_path = "data"; +local callbacks = {}; ------- API ------------- function set_data_path(path) - log("info", "Setting data path to: %s", path); + log("debug", "Setting data path to: %s", path); data_path = path; end +local function callback(username, host, datastore, data) + for _, f in ipairs(callbacks) do + username, host, datastore, data = f(username, host, datastore, data); + if username == false then break; end + end + + return username, host, datastore, data; +end +function add_callback(func) + if not callbacks[func] then -- Would you really want to set the same callback more than once? + callbacks[func] = true; + callbacks[#callbacks+1] = func; + return true; + end +end +function remove_callback(func) + if callbacks[func] then + for i, f in ipairs(callbacks) do + if f == func then + callbacks[i] = nil; + callbacks[f] = nil; + return true; + end + end + end +end + function getpath(username, host, datastore, ext, create) ext = ext or "dat"; - host = host and encode(host); + host = (host and encode(host)) or "_global"; username = username and encode(username); if username then if create then mkdir(mkdir(mkdir(data_path).."/"..host).."/"..datastore); end @@ -93,6 +121,12 @@ function store(username, host, datastore, data) if not data then data = {}; end + + username, host, datastore, data = callback(username, host, datastore, data); + if username == false then + return true; -- Don't save this data at all + end + -- save the datastore local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+"); if not f then @@ -102,7 +136,8 @@ function store(username, host, datastore, data) f:write("return "); append(f, data); f:close(); - if not next(data) then -- try to delete empty datastore + if next(data) == nil then -- try to delete empty datastore + log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil"); os_remove(getpath(username, host, datastore)); end -- we write data even when we are deleting because lua doesn't have a @@ -112,6 +147,7 @@ end function list_append(username, host, datastore, data) if not data then return; end + if callback(username, host, datastore) == false then return true; end -- save the datastore local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+"); if not f then @@ -129,6 +165,7 @@ function list_store(username, host, datastore, data) if not data then data = {}; end + if callback(username, host, datastore) == false then return true; end -- save the datastore local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+"); if not f then @@ -141,7 +178,8 @@ function list_store(username, host, datastore, data) f:write(");\n"); end f:close(); - if not next(data) then -- try to delete empty datastore + if next(data) == nil then -- try to delete empty datastore + log("debug", "Removing empty %s datastore for user %s@%s", datastore, username or "nil", host or "nil"); os_remove(getpath(username, host, datastore, "list")); end -- we write data even when we are deleting because lua doesn't have a