mod_console: Update for new net.server API
[prosody.git] / util / datamanager.lua
index 41d09f060302acdd9f6625d450916f8391617c07..4d07d6ccfdcdbd9d5246f0beb0402f32467554ed 100644 (file)
@@ -1,4 +1,4 @@
--- Prosody IM v0.4
+-- Prosody IM
 -- Copyright (C) 2008-2009 Matthew Wild
 -- Copyright (C) 2008-2009 Waqas Hussain
 -- 
@@ -50,7 +50,7 @@ local function mkdir(path)
 end
 
 local data_path = "data";
-local callback;
+local callbacks = {};
 
 ------- API -------------
 
@@ -58,8 +58,32 @@ function set_data_path(path)
        log("debug", "Setting data path to: %s", path);
        data_path = path;
 end
-function set_callback(func)
-       callback = func;
+
+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)
@@ -97,7 +121,12 @@ function store(username, host, datastore, data)
        if not data then
                data = {};
        end
-       if callback and callback(username, host, datastore) then return true; 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
@@ -108,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
@@ -118,7 +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
+       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
@@ -136,7 +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
+       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
@@ -150,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