util.pluginloader: Remove unnecessary return value suppressing the real load error
[prosody.git] / util / datamanager.lua
index 6a811879706facc934cad31834d2fa44d3e28146..54cf195932eba962e254d9016da76ea51311116f 100644 (file)
@@ -1,20 +1,9 @@
--- Prosody IM v0.2
--- Copyright (C) 2008 Matthew Wild
--- Copyright (C) 2008 Waqas Hussain
+-- Prosody IM v0.4
+-- Copyright (C) 2008-2009 Matthew Wild
+-- Copyright (C) 2008-2009 Waqas Hussain
 -- 
--- This program is free software; you can redistribute it and/or
--- modify it under the terms of the GNU General Public License
--- as published by the Free Software Foundation; either version 2
--- of the License, or (at your option) any later version.
--- 
--- This program is distributed in the hope that it will be useful,
--- but WITHOUT ANY WARRANTY; without even the implied warranty of
--- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--- GNU General Public License for more details.
--- 
--- You should have received a copy of the GNU General Public License
--- along with this program; if not, write to the Free Software
--- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
 --
 
 
@@ -61,14 +50,42 @@ local function mkdir(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 not username 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);
@@ -88,7 +105,7 @@ end
 function load(username, host, datastore)
        local data, ret = loadfile(getpath(username, host, datastore));
        if not data then
-               log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
+               log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
                return nil;
        end
        setfenv(data, {});
@@ -104,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 not username 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
@@ -113,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, host);
                os_remove(getpath(username, host, datastore));
        end
        -- we write data even when we are deleting because lua doesn't have a
@@ -123,6 +147,7 @@ end
 
 function list_append(username, host, datastore, data)
        if not data then return; end
+       if callback and callback(username, host, datastore) then return true; end
        -- save the datastore
        local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+");
        if not f then
@@ -140,6 +165,7 @@ function list_store(username, host, datastore, data)
        if not data then
                data = {};
        end
+       if callback and callback(username, host, datastore) then return true; end
        -- save the datastore
        local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+");
        if not f then
@@ -152,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, host);
                os_remove(getpath(username, host, datastore, "list"));
        end
        -- we write data even when we are deleting because lua doesn't have a
@@ -163,7 +190,7 @@ end
 function list_load(username, host, datastore)
        local data, ret = loadfile(getpath(username, host, datastore, "list"));
        if not data then
-               log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
+               log("debug", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
                return nil;
        end
        local items = {};