diff options
-rw-r--r-- | core/storagemanager.lua | 12 | ||||
-rw-r--r-- | core/usermanager.lua | 12 | ||||
-rw-r--r-- | plugins/mod_storage_internal.lua | 8 | ||||
-rw-r--r-- | plugins/mod_storage_sql.lua | 33 | ||||
-rw-r--r-- | util/datamanager.lua | 41 |
5 files changed, 102 insertions, 4 deletions
diff --git a/core/storagemanager.lua b/core/storagemanager.lua index 71e79271..3379cc0c 100644 --- a/core/storagemanager.lua +++ b/core/storagemanager.lua @@ -58,7 +58,7 @@ function load_driver(host, driver_name) return stores_available:get(host, driver_name); end -function open(host, store, typ) +function get_driver(host, store) local storage = config.get(host, "core", "storage"); local driver_name; local option_type = type(storage); @@ -77,7 +77,11 @@ function open(host, store, typ) driver_name = "null"; driver = null_storage_driver; end + return driver, driver_name; + end +function open(host, store, typ) + local driver, driver_name = get_driver(host, store); local ret, err = driver:open(store, typ); if not ret then if err == "unsupported-store" then @@ -96,5 +100,11 @@ end function datamanager.store(username, host, datastore, data) return open(host, datastore):set(username, data); end +function datamanager.list_stores(username, host) + return get_driver(host):list_stores(username, host); +end +function datamanager.purge(username, host) + return get_driver(host):purge(username, host); +end return _M; diff --git a/core/usermanager.lua b/core/usermanager.lua index 3aba5786..efc15b7c 100644 --- a/core/usermanager.lua +++ b/core/usermanager.lua @@ -10,11 +10,13 @@ local modulemanager = require "core.modulemanager"; local log = require "util.logger".init("usermanager"); local type = type; local ipairs = ipairs; +local pairs = pairs; local jid_bare = require "util.jid".bare; local jid_prep = require "util.jid".prep; local config = require "core.configmanager"; local hosts = hosts; local sasl_new = require "util.sasl".new; +local storagemanager = require "core.storagemanager"; local prosody = _G.prosody; @@ -88,7 +90,15 @@ function create_user(username, password, host) end function delete_user(username, host) - return hosts[host].users.delete_user(username); + local user = hosts[host].sessions[username]; + if user and user.sessions then + for jid, session in pairs(user.sessions) do + session:close{ condition = "not-authorized", text = "Account deleted" }; + end + end + local ok, err = hosts[host].users.delete_user(username); + if not ok then return nil, err; end + return storagemanager.get_driver(host):purge(username); end function get_sasl_handler(host, session) diff --git a/plugins/mod_storage_internal.lua b/plugins/mod_storage_internal.lua index 821d1e1a..92ac3ef5 100644 --- a/plugins/mod_storage_internal.lua +++ b/plugins/mod_storage_internal.lua @@ -16,4 +16,12 @@ function driver:set(user, data) return datamanager.store(user, host, self.store, data); end +function driver:list_stores(username) + return datamanager.list_stores(username, host); +end + +function driver:purge(user) + return datamanager.purge(user, host); +end + module:add_item("data-driver", driver); diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index 6a2d36f1..6d19eee6 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -175,7 +175,7 @@ local function deserialize(t, value) end end -local function getsql(sql, ...) +local function dosql(sql, ...) if params.driver == "PostgreSQL" then sql = sql:gsub("`", "\""); end @@ -184,12 +184,15 @@ local function getsql(sql, ...) if not stmt and not test_connection() then error("connection failed"); end if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end -- run query - local ok, err = stmt:execute(host or "", user or "", store or "", ...); + local ok, err = stmt:execute(...); if not ok and not test_connection() then error("connection failed"); end if not ok then return nil, err; end return stmt; end +local function getsql(sql, ...) + return dosql(sql, host or "", user or "", store or "", ...); +end local function setsql(sql, ...) local stmt, err = getsql(sql, ...); if not stmt then return stmt, err; end @@ -349,4 +352,30 @@ function driver:open(store, typ) return nil, "unsupported-store"; end +function driver:list_stores(username) -- Not to be confused with the list store type + local sql = (username == true + and "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`!=?" + or "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`=?"); + if username == true or not username then + username = ""; + end + local stmt, err = dosql(sql, host, username); + if not stmt then + return nil, err; + end + local stores = {}; + for row in stmt:rows() do + stores[#stores+1] = row[1]; + end + return stores; +end + +function driver:purge(username) + local stmt, err = dosql("DELETE FROM `prosody` WHERE `host`=? AND `user`=?", host, username); + if not stmt then return stmt, err; end + local changed, err = stmt:affected(); + if not changed then return changed, err; end + return true, changed; +end + module:add_item("data-driver", driver); diff --git a/util/datamanager.lua b/util/datamanager.lua index 344d2eb1..23a2d74f 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -226,4 +226,45 @@ function list_load(username, host, datastore) return items; end +function list_stores(username, host) + if not host then + return nil, "bad argument #2 to 'list_stores' (string expected, got nothing)"; + end + local list = {}; + local host_dir = format("%s/%s/", data_path, encode(host)); + for node in lfs.dir(host_dir) do + if not node:match"^%." then -- dots should be encoded, this is probably . or .. + local store = decode(node); + local path = host_dir..node; + if username == true then + if lfs.attributes(path, "mode") == "directory" then + list[#list+1] = store; + end + elseif username then + if lfs.attributes(getpath(username, host, store), "mode") + or lfs.attributes(getpath(username, host, store, "list"), "mode") then + list[#list+1] = store; + end + elseif lfs.attributes(path, "mode") == "file" then + list[#list+1] = store:gsub("%.[dalist]+$",""); + end + end + end + return list; +end + +function purge(username, host) + local host_dir = format("%s/%s/", data_path, encode(host)); + local deleted = 0; + for file in lfs.dir(host_dir) do + if lfs.attributes(host_dir..file, "mode") == "directory" then + local store = decode(file); + deleted = deleted + (os_remove(getpath(username, host, store)) and 1 or 0); + deleted = deleted + (os_remove(getpath(username, host, store, "list")) and 1 or 0); + -- We this will generate loads of "No such file or directory", but do we care? + end + end + return deleted > 0, deleted; +end + return _M; |