Merge 0.9->trunk
[prosody.git] / plugins / mod_admin_telnet.lua
index 2f7bb57e1a592cc89d9c8d3a174e5c263a0da458..0af571eb0aef00300b245c1cd64b6b9dc3ce185d 100644 (file)
@@ -72,6 +72,64 @@ function console:new_session(conn)
        return session;
 end
 
+function console:process_line(session, line)
+       local useglobalenv;
+       
+       if line:match("^>") then
+               line = line:gsub("^>", "");
+               useglobalenv = true;
+       elseif line == "\004" then
+               commands["bye"](session, line);
+               return;
+       else
+               local command = line:match("^%w+") or line:match("%p");
+               if commands[command] then
+                       commands[command](session, line);
+                       return;
+               end
+       end
+       
+       session.env._ = line;
+       
+       local chunkname = "=console";
+       local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil
+       local chunk, err = envload("return "..line, chunkname, env);
+       if not chunk then
+               chunk, err = envload(line, chunkname, env);
+               if not chunk then
+                       err = err:gsub("^%[string .-%]:%d+: ", "");
+                       err = err:gsub("^:%d+: ", "");
+                       err = err:gsub("'<eof>'", "the end of the line");
+                       session.print("Sorry, I couldn't understand that... "..err);
+                       return;
+               end
+       end
+       
+       local ranok, taskok, message = pcall(chunk);
+       
+       if not (ranok or message or useglobalenv) and commands[line:lower()] then
+               commands[line:lower()](session, line);
+               return;
+       end
+       
+       if not ranok then
+               session.print("Fatal error while running command, it did not complete");
+               session.print("Error: "..taskok);
+               return;
+       end
+       
+       if not message then
+               session.print("Result: "..tostring(taskok));
+               return;
+       elseif (not taskok) and message then
+               session.print("Command completed with a problem");
+               session.print("Message: "..tostring(message));
+               return;
+       end
+       
+       session.print("OK: "..tostring(message));
+end
+
 local sessions = {};
 
 function console_listener.onconnect(conn)
@@ -91,65 +149,7 @@ function console_listener.onincoming(conn, data)
        end
 
        for line in data:gmatch("[^\n]*[\n\004]") do
-               -- Handle data (loop allows us to break to add \0 after response)
-               repeat
-                       local useglobalenv;
-
-                       if line:match("^>") then
-                               line = line:gsub("^>", "");
-                               useglobalenv = true;
-                       elseif line == "\004" then
-                               commands["bye"](session, line);
-                               break;
-                       else
-                               local command = line:match("^%w+") or line:match("%p");
-                               if commands[command] then
-                                       commands[command](session, line);
-                                       break;
-                               end
-                       end
-
-                       session.env._ = line;
-                       
-                       local chunkname = "=console";
-                       local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil
-                       local chunk, err = envload("return "..line, chunkname, env);
-                       if not chunk then
-                               chunk, err = envload(line, chunkname, env);
-                               if not chunk then
-                                       err = err:gsub("^%[string .-%]:%d+: ", "");
-                                       err = err:gsub("^:%d+: ", "");
-                                       err = err:gsub("'<eof>'", "the end of the line");
-                                       session.print("Sorry, I couldn't understand that... "..err);
-                                       break;
-                               end
-                       end
-               
-                       local ranok, taskok, message = pcall(chunk);
-                       
-                       if not (ranok or message or useglobalenv) and commands[line:lower()] then
-                               commands[line:lower()](session, line);
-                               break;
-                       end
-                       
-                       if not ranok then
-                               session.print("Fatal error while running command, it did not complete");
-                               session.print("Error: "..taskok);
-                               break;
-                       end
-                       
-                       if not message then
-                               session.print("Result: "..tostring(taskok));
-                               break;
-                       elseif (not taskok) and message then
-                               session.print("Command completed with a problem");
-                               session.print("Message: "..tostring(message));
-                               break;
-                       end
-                       
-                       session.print("OK: "..tostring(message));
-               until true
-               
+               console:process_line(session, line);
                session.send(string.char(0));
        end
        session.partial_data = data:match("[^\n]+$");
@@ -227,7 +227,8 @@ 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]]
+               print [[user:list(hostname, pattern) - List users on the specified host, optionally filtering with a pattern]]
        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 +916,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 +929,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,16 +940,36 @@ 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
 end
 
+function def_env.user:list(host, pat)
+       if not host then
+               return nil, "No host given";
+       elseif not hosts[host] then
+               return nil, "No such host";
+       end
+       local print = self.session.print;
+       local count = 0;
+       for user in um.users(host) do
+               if not pat or user:match(pat) then
+                       print(user.."@"..host);
+               end
+               count = count + 1;
+       end
+       return true, count .. " users total";
+end
+
 def_env.xmpp = {};
 
 local st = require "util.stanza";
@@ -986,7 +1013,7 @@ function printbanner(session)
        end
 end
 
-module:add_item("net-provider", {
+module:provides("net", {
        name = "console";
        listener = console_listener;
        default_port = 5582;