rostermanager: Remove unused imports
[prosody.git] / prosodyctl
index 0e733d07b8b47cf3a2842e7a121de0b4a1829385..0d1194f44131d92e4f8a81d145fd2c6ac20c9207 100755 (executable)
@@ -50,6 +50,7 @@ local prosody = {
        platform = "posix";
        lock_globals = function () end;
        unlock_globals = function () end;
+       installed = CFG_SOURCEDIR ~= nil;
 };
 _G.prosody = prosody;
 
@@ -60,16 +61,17 @@ end
 
 config = require "core.configmanager"
 
+local ENV_CONFIG;
 do
        local filenames = {};
        
        local filename;
        if arg[1] == "--config" and arg[2] then
                table.insert(filenames, arg[2]);
-               table.remove(arg, 1); table.remove(arg, 1);
                if CFG_CONFIGDIR then
                        table.insert(filenames, CFG_CONFIGDIR.."/"..arg[2]);
                end
+               table.remove(arg, 1); table.remove(arg, 1);
        else
                for _, format in ipairs(config.parsers()) do
                        table.insert(filenames, (CFG_CONFIGDIR or ".").."/prosody.cfg."..format);
@@ -80,6 +82,7 @@ do
                local file = io.open(filename);
                if file then
                        file:close();
+                       ENV_CONFIG = filename;
                        CFG_CONFIGDIR = filename:match("^(.*)[\\/][^\\/]*$");
                        break;
                end
@@ -119,6 +122,11 @@ end
 prosody.paths = { source = CFG_SOURCEDIR, config = CFG_CONFIGDIR, 
                  plugins = CFG_PLUGINDIR or "plugins", data = data_path };
 
+if prosody.installed then
+       -- Change working directory to data path.
+       require "lfs".chdir(data_path);
+end
+
 require "core.loggingmanager"
 
 dependencies.log_warnings();
@@ -155,10 +163,13 @@ if ok and pposix then
        
        -- Set our umask to protect data files
        pposix.umask(config.get("*", "core", "umask") or "027");
+       pposix.setenv("HOME", data_path);
+       pposix.setenv("PROSODY_CONFIG", ENV_CONFIG);
 else
        print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
        print("For more help send the below error to us through http://prosody.im/discuss");
        print(tostring(pposix))
+       os.exit(1);
 end
 
 local function test_writeable(filename)
@@ -219,6 +230,7 @@ local function make_host(hostname)
        return {
                type = "local",
                events = prosody.events,
+               modules = {},
                users = require "core.usermanager".new_null_provider(hostname)
        };
 end
@@ -227,9 +239,9 @@ for hostname, config in pairs(config.getconfig()) do
        hosts[hostname] = make_host(hostname);
 end
        
-require "core.modulemanager"
+local modulemanager = require "core.modulemanager"
 
-require "util.prosodyctl"
+local prosodyctl = require "util.prosodyctl"
 require "socket"
 -----------------------
 
@@ -366,7 +378,7 @@ function commands.deluser(arg)
                return 1;
        end
        
-       local ok, msg = prosodyctl.passwd { user = user, host = host };
+       local ok, msg = prosodyctl.deluser { user = user, host = host };
        
        if ok then return 0; end
        
@@ -509,7 +521,7 @@ function commands.about(arg)
                return 1;
        end
        
-       require "util.array";
+       local array = require "util.array";
        local keys = require "util.iterators".keys;
        
        print("Prosody "..(prosody.version or "(unknown version)"));
@@ -630,8 +642,8 @@ function commands.unregister(arg)
        return 1;
 end
 
-local openssl = require "util.openssl";
-local lfs = require "lfs";
+local openssl;
+local lfs;
 
 local cert_commands = {};
 
@@ -678,11 +690,13 @@ function cert_commands.key(arg)
                if ask_overwrite(key_filename) then
                        return nil, key_filename;
                end
-               os.remove(key_filename); -- We chmod this file to not have write permissions
+               os.remove(key_filename); -- This file, if it exists is unlikely to have write permissions
                local key_size = tonumber(arg[2] or show_prompt("Choose key size (2048):") or 2048);
+               local old_umask = pposix.umask("0377");
                if openssl.genrsa{out=key_filename, key_size} then
                        os.execute(("chmod 400 '%s'"):format(key_filename));
                        show_message("Key written to ".. key_filename);
+                       pposix.umask(old_umask);
                        return nil, key_filename;
                end
                show_message("There was a problem, see OpenSSL output");
@@ -712,9 +726,9 @@ end
 
 function cert_commands.generate(arg)
        if #arg >= 1 and arg[1] ~= "--help" then
-               local cert_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".cert";
+               local cert_filename = (CFG_DATADIR or ".") .. "/" .. arg[1] .. ".crt";
                if ask_overwrite(cert_filename) then
-                       return nil, conf_filename;
+                       return nil, cert_filename;
                end
                local _, key_filename = cert_commands.key({arg[1]});
                local _, conf_filename = cert_commands.config(arg);
@@ -733,6 +747,8 @@ end
 
 function commands.cert(arg)
        if #arg >= 1 and arg[1] ~= "--help" then
+               openssl = require "util.openssl";
+               lfs = require "lfs";
                local subcmd = table.remove(arg, 1);
                if type(cert_commands[subcmd]) == "function" then
                        if not arg[1] then