]> git.sesse.net Git - vlc/blobdiff - share/lua/intf/telnet.lua
luatelnet: end remotely closed connections
[vlc] / share / lua / intf / telnet.lua
index 1f40af01e7154bdf97839002b437844dfe0274ee..c964bd28d2407b0411e0b7ee545812174800edcb 100644 (file)
@@ -58,7 +58,7 @@ function on_password( client )
     if client.type == host.client_type.net then
         client:send( "Password: " ..IAC..WILL..ECHO )
     else
-        -- no authentification needed on stdin
+        -- no authentication needed on stdin
         client:switch_status( host.status.read )
     end
 end
@@ -78,15 +78,13 @@ function vlm_message_to_string(client,message,prefix)
     local prefix = prefix or ""
     if message.value then
         client:append(prefix .. message.name .. " : " .. message.value)
-        return
     else
         client:append(prefix .. message.name)
-        if message.children then
-            for i,c in ipairs(message.children) do
-                vlm_message_to_string(client,c,prefix.."    ")
-            end
+    end
+    if message.children then
+        for i,c in ipairs(message.children) do
+            vlm_message_to_string(client,c,prefix.."    ")
         end
-        return
     end
 end
 
@@ -101,13 +99,13 @@ h:listen( config.hosts or config.host or "localhost:4212" )
 password = config.password or "admin"
 
 --[[ Launch vlm ]]
-vlm = vlc.vlm.new()
+vlm = vlc.vlm()
 
 --[[ Commands ]]
 function shutdown(client)
     h:broadcast("Shutting down.\r\n")
     vlc.msg.err("shutdown requested")
-    vlc.quit()
+    vlc.misc.quit()
     return true
 end
 function logout(client)
@@ -146,7 +144,7 @@ commands = {
     ["logout"]      = { func = logout, help = "logout" },
     ["lock"]        = { func = lock, help = "lock the telnet prompt" },
     ["description"] = { func = print_text(description), help = "describe this module" },
-    ["license"]     = { func = print_text(vlc.license()), help = "print VLC's license message" },
+    ["license"]     = { func = print_text(vlc.misc.license()), help = "print VLC's license message" },
     ["help"]        = { func = help, help = "show this help", dovlm = true },
     }
 
@@ -155,7 +153,7 @@ function client_command( client )
     client.buffer = ""
     if not commands[cmd] or not commands[cmd].func or commands[cmd].dovlm then
         -- if it's not an interface specific command, it has to be a VLM command
-        local message, vlc_err = vlc.vlm.execute_command( vlm, cmd )
+        local message, vlc_err = vlm:execute_command( cmd )
         vlm_message_to_string( client, message )
         if not commands[cmd] or not commands[cmd].func and not commands[cmd].dovlm then
             if vlc_err ~= 0 then client:append( "Type `help' for help." ) end
@@ -171,9 +169,8 @@ function client_command( client )
 end
 
 --[[ The main loop ]]
-while not vlc.should_die() do
-    h:accept()
-    local w, r = h:select( 0.1 )
+while not vlc.misc.should_die() do
+    local w, r = h:accept_and_select()
 
     -- Handle writes
     for _, client in pairs(w) do
@@ -184,24 +181,33 @@ while not vlc.should_die() do
 
     -- Handle reads
     for _, client in pairs(r) do
-        local str = client:recv(1000)
-        local done = false
-        if string.match(str,"\n$") then
-            client.buffer = string.gsub(client.buffer..str,"\r?\n$","")
-            done = true
-        elseif client.buffer == ""
-           and ((client.type == host.client_type.stdio and str == "")
-           or  (client.type == host.client_type.net and str == "\004")) then
+        local str = client.cmds .. client:recv(1000)
+
+        if string.match(str,"\n") then
+            client.cmds = str
+        elseif not str or str == "" -- the telnet client program has left
+           or  (client.type == host.client_type.net and str == "\004") then
             -- Caught a ^D
-            client.buffer = "quit"
-            done = true
-        else
-            client.buffer = client.buffer .. str
+            client.cmds = "quit\n"
         end
-        if client.type == host.client_type.net then
-            telnet_commands( client )
-        end
-        if done then
+
+        client.buffer = ""
+        -- split the command at the first '\n'
+        while string.find(client.cmds, "\n") do
+            -- save the buffer to send to the client
+            local saved_buffer = client.buffer
+
+            -- get the next command
+            local index = string.find(client.cmds, "\n")
+            client.buffer = string.gsub(string.sub(client.cmds, 0, index - 1), "^%s*(.-)%s*$", "%1")
+            client.cmds = string.sub(client.cmds, index + 1)
+
+            -- Remove telnet commands from the command line
+            if client.type == host.client_type.net then
+                telnet_commands( client )
+            end
+
+            -- Run the command
             if client.status == host.status.password then
                 if client.buffer == password then
                     client:send( IAC..WONT..ECHO.."\r\nWelcome, Master\r\n" )
@@ -214,9 +220,10 @@ while not vlc.should_die() do
             elseif client_command( client ) then
                 client:switch_status( host.status.write )
             end
+            client.buffer = saved_buffer .. client.buffer
         end
     end
 end
 
 --[[ Clean up ]]
-vlc.vlm.delete( vlm )
+vlm = nil