mod_posix: Some (perhaps temporary) changes to re-lock the pidfile after truncating...
[prosody.git] / plugins / mod_console.lua
index 8f574704c2fdd82dd88d815dc2b6acb3d0e07056..e87ef536158cf39afb295406fcd929cdcc6dfa5a 100644 (file)
@@ -53,76 +53,78 @@ end
 
 local sessions = {};
 
+function console_listener.onconnect(conn)
+       -- Handle new connection
+       local session = console:new_session(conn);
+       sessions[conn] = session;
+       printbanner(session);
+       session.send(string.char(0));
+end
+
 function console_listener.onincoming(conn, data)
        local session = sessions[conn];
-       
-       if not session then
-               -- Handle new connection
-               session = console:new_session(conn);
-               sessions[conn] = session;
-               printbanner(session);
-       end
-       if data then
-               -- Handle data
-               (function(session, data)
-                       local useglobalenv;
-                       
-                       if data:match("^>") then
-                               data = data:gsub("^>", "");
-                               useglobalenv = true;
-                       elseif data == "\004" then
-                               commands["bye"](session, data);
+
+       -- Handle data
+       (function(session, data)
+               local useglobalenv;
+               
+               if data:match("^>") then
+                       data = data:gsub("^>", "");
+                       useglobalenv = true;
+               elseif data == "\004" then
+                       commands["bye"](session, data);
+                       return;
+               else
+                       local command = data:lower();
+                       command = data:match("^%w+") or data:match("%p");
+                       if commands[command] then
+                               commands[command](session, data);
                                return;
-                       else
-                               local command = data:lower();
-                               command = data:match("^%w+") or data:match("%p");
-                               if commands[command] then
-                                       commands[command](session, data);
-                                       return;
-                               end
                        end
+               end
 
-                       session.env._ = data;
-                       
-                       local chunk, err = loadstring("return "..data);
+               session.env._ = data;
+               
+               local chunkname = "=console";
+               local chunk, err = loadstring("return "..data, chunkname);
+               if not chunk then
+                       chunk, err = loadstring(data, chunkname);
                        if not chunk then
-                               chunk, err = loadstring(data);
-                               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
-                       
-                       setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil);
-                       
-                       local ranok, taskok, message = pcall(chunk);
-                       
-                       if not (ranok or message or useglobalenv) and commands[data:lower()] then
-                               commands[data:lower()](session, data);
-                               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));
+                               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
-                       
-                       session.print("OK: "..tostring(message));
-               end)(session, data);
-       end
+               end
+               
+               setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil);
+               
+               local ranok, taskok, message = pcall(chunk);
+               
+               if not (ranok or message or useglobalenv) and commands[data:lower()] then
+                       commands[data:lower()](session, data);
+                       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)(session, data);
+       
        session.send(string.char(0));
 end