From 0f8e80bdb1656d3d1058b5cccbbb48dffd648e89 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 15 Sep 2012 23:00:59 +0200 Subject: mod_admin_telnet: Fix user:*, correct names, docs, do validation --- plugins/mod_admin_telnet.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/plugins/mod_admin_telnet.lua b/plugins/mod_admin_telnet.lua index 2f7bb57e..64f47631 100644 --- a/plugins/mod_admin_telnet.lua +++ b/plugins/mod_admin_telnet.lua @@ -227,7 +227,7 @@ function commands.help(session, data) elseif section == "user" then print [[user:create(jid, password) - Create the specified user account]] print [[user:password(jid, password) - Set the password for the specified user account]] - print [[user:delete(jid, password) - Permanently remove the specified user account]] + print [[user:delete(jid) - Permanently remove the specified user account]] elseif section == "server" then print [[server:version() - Show the server's version number]] print [[server:uptime() - Show how long the server has been running]] @@ -915,6 +915,9 @@ local um = require"core.usermanager"; def_env.user = {}; function def_env.user:create(jid, password) local username, host = jid_split(jid); + if um.user_exists(username, host) then + return nil, "User exists"; + end local ok, err = um.create_user(username, password, host); if ok then return true, "User created"; @@ -925,6 +928,9 @@ end function def_env.user:delete(jid) local username, host = jid_split(jid); + if not um.user_exists(username, host) then + return nil, "No such user"; + end local ok, err = um.delete_user(username, host); if ok then return true, "User deleted"; @@ -933,11 +939,14 @@ function def_env.user:delete(jid) end end -function def_env.user:passwd(jid, password) +function def_env.user:password(jid, password) local username, host = jid_split(jid); + if not um.user_exists(username, host) then + return nil, "No such user"; + end local ok, err = um.set_password(username, password, host); if ok then - return true, "User created"; + return true, "User password changed"; else return nil, "Could not change password for user: "..err; end -- cgit v1.2.3 From 136674425b54b191ecef0006d8460f84a4a67f35 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 16 Sep 2012 02:18:07 +0200 Subject: storagemanager: Add purge() for purging user data from all backends in use --- core/storagemanager.lua | 21 ++++++++++++++++++++- core/usermanager.lua | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/core/storagemanager.lua b/core/storagemanager.lua index 1b76a394..a9759b6a 100644 --- a/core/storagemanager.lua +++ b/core/storagemanager.lua @@ -94,6 +94,25 @@ function open(host, store, typ) return ret, err; end +function purge(user, host) + local storage = config.get(host, "core", "storage"); + local driver_name; + if type(storage) == "table" then + -- multiple storage backends in use that we need to purge + local purged = {}; + for store, driver in pairs(storage) do + if not purged[driver] then + purged[driver] = get_driver(host, store):purge(user); + end + end + end + get_driver(host):purge(user); -- and the default driver + + olddm.purge(user, host); -- COMPAT list stores, like offline messages end up in the old datamanager + + return true; +end + function datamanager.load(username, host, datastore) return open(host, datastore):get(username); end @@ -104,7 +123,7 @@ function datamanager.list_stores(username, host) return get_driver(host):list_stores(username); end function datamanager.purge(username, host) - return get_driver(host):purge(username); + return purge(username); end return _M; diff --git a/core/usermanager.lua b/core/usermanager.lua index 2ca0aef1..0ed61f23 100644 --- a/core/usermanager.lua +++ b/core/usermanager.lua @@ -93,7 +93,7 @@ function delete_user(username, host) local ok, err = hosts[host].users.delete_user(username); if not ok then return nil, err; end prosody.events.fire_event("user-deleted", { username = username, host = host }); - return storagemanager.get_driver(host):purge(username); + return storagemanager.purge(username, host); end function get_sasl_handler(host, session) -- cgit v1.2.3 From 07bb8a42ce660684bbe4742aeefd5ea92092a37e Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 17 Sep 2012 05:42:10 +0200 Subject: storagemanager, datamanager, mod_storage_{internal,sql}: Replace list_stores() with an iterator version --- core/storagemanager.lua | 4 ++-- plugins/mod_storage_internal.lua | 4 ++-- plugins/mod_storage_sql.lua | 17 +++++++------ util/datamanager.lua | 52 ++++++++++++++++++++++++---------------- 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/core/storagemanager.lua b/core/storagemanager.lua index a9759b6a..29cad2ff 100644 --- a/core/storagemanager.lua +++ b/core/storagemanager.lua @@ -119,8 +119,8 @@ 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); +function datamanager.stores(username, host, typ) + return get_driver(host):stores(username, typ); end function datamanager.purge(username, host) return purge(username); diff --git a/plugins/mod_storage_internal.lua b/plugins/mod_storage_internal.lua index 92ac3ef5..d8fdfc6f 100644 --- a/plugins/mod_storage_internal.lua +++ b/plugins/mod_storage_internal.lua @@ -16,8 +16,8 @@ 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); +function driver:stores(username) + return datamanager.stores(username, host); end function driver:purge(user) diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index ea25c90b..5a08598a 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -374,10 +374,9 @@ 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`=?"); +function driver:stores(username) -- Not to be confused with the list store type + local sql = "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`" .. + (username == true and "!=?" or "=?"); if username == true or not username then username = ""; end @@ -385,11 +384,11 @@ function driver:list_stores(username) -- Not to be confused with the list store if not stmt then return rollback(nil, err); end - local stores = {}; - for row in stmt:rows() do - stores[#stores+1] = row[1]; - end - return commit(stores); + local next = stmt:rows(); + return commit(function() + local row = next(); + return row and row[1]; + end); end function driver:purge(username) diff --git a/util/datamanager.lua b/util/datamanager.lua index e6ad86db..9207f555 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -277,31 +277,41 @@ 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)"; +local type_map = { + keyval = "dat"; + list = "list"; +} + +function stores(username, host, typ) + typ = type_map[typ or "keyval"]; + local store_dir = format("%s/%s/", data_path, encode(host)); + + local mode, err = lfs.attributes(store_dir, "mode"); + if not mode then + return function() log("debug", err or (store_dir .. " does not exist")) end 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; + local next, state = lfs.dir(store_dir); + return function(state) + for node in next, state do + if not node:match"^%." then + if username == true then + if lfs.attributes(store_dir..node, "mode") == "directory" then + return decode(node); + end + elseif username then + local store = decode(node) + if lfs.attributes(getpath(username, host, store, typ), "mode") then + return store; + end + elseif lfs.attributes(node, "mode") == "file" then + local file, ext = node:match("^(.*)%.([dalist]+)$"); + if ext == typ then + return decode(file) + end end - elseif lfs.attributes(path, "mode") == "file" then - list[#list+1] = store:gsub("%.[dalist]+$",""); end end - end - return list; + end, state; end local function do_remove(path) -- cgit v1.2.3 From 6d61a3834d5dd59a2643c386236d5c33cd97fa32 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 19 Sep 2012 12:08:33 +0100 Subject: storagemanager: Remove usage of 'core' when calling configmanager.get() --- core/storagemanager.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/storagemanager.lua b/core/storagemanager.lua index 29cad2ff..10d1a4bc 100644 --- a/core/storagemanager.lua +++ b/core/storagemanager.lua @@ -59,7 +59,7 @@ function load_driver(host, driver_name) end function get_driver(host, store) - local storage = config.get(host, "core", "storage"); + local storage = config.get(host, "storage"); local driver_name; local option_type = type(storage); if option_type == "string" then @@ -68,7 +68,7 @@ function get_driver(host, store) driver_name = storage[store]; end if not driver_name then - driver_name = config.get(host, "core", "default_storage") or "internal"; + driver_name = config.get(host, "default_storage") or "internal"; end local driver = load_driver(host, driver_name); @@ -95,7 +95,7 @@ function open(host, store, typ) end function purge(user, host) - local storage = config.get(host, "core", "storage"); + local storage = config.get(host, "storage"); local driver_name; if type(storage) == "table" then -- multiple storage backends in use that we need to purge -- cgit v1.2.3 From 3b96fac7491ce85877b7ee2038ae90b2d501ca6c Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 19 Sep 2012 12:09:27 +0100 Subject: mod_storage_sql: Remove obsolete comment --- plugins/mod_storage_sql.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index 5a08598a..e05a475d 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -374,7 +374,7 @@ function driver:open(store, typ) return nil, "unsupported-store"; end -function driver:stores(username) -- Not to be confused with the list store type +function driver:stores(username) local sql = "SELECT DISTINCT `store` FROM `prosody` WHERE `host`=? AND `user`" .. (username == true and "!=?" or "=?"); if username == true or not username then -- cgit v1.2.3