X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Fdatamanager.lua;h=30cd082bdf31eb5232542c19034caf44eb37f97f;hb=322fdb77fe58eafac7204955fb5b3146ee8700ea;hp=f67c886478456db206a2b4857870e520a1568fba;hpb=e5890bab7fd36dfb553935413b97c3a87ac1ddad;p=prosody.git diff --git a/util/datamanager.lua b/util/datamanager.lua index f67c8864..30cd082b 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -1,4 +1,4 @@ --- Prosody IM v0.4 +-- 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 @@ -103,7 +137,7 @@ function store(username, host, datastore, data) append(f, data); f:close(); if next(data) == nil then -- try to delete empty datastore - log("debug", "Removing empty %s datastore for user %s@%s", datastore, username, host); + 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 @@ -113,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 @@ -130,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 @@ -143,7 +179,7 @@ function list_store(username, host, datastore, data) end f:close(); if next(data) == nil then -- try to delete empty datastore - log("debug", "Removing empty %s datastore for user %s@%s", datastore, username, host); + 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