]> git.sesse.net Git - vlc/blobdiff - share/lua/intf/telnet.lua
luatelnet: end remotely closed connections
[vlc] / share / lua / intf / telnet.lua
index da74c6183a2eb26f5d0fc4d0f2448e69315ff25e..c964bd28d2407b0411e0b7ee545812174800edcb 100644 (file)
@@ -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
 
@@ -183,68 +181,46 @@ while not vlc.misc.should_die() do
 
     -- Handle reads
     for _, client in pairs(r) do
-        local str = string.gsub(client:recv(1000),"\r","\n")
-        local done = false
-
-        -- the telnet client program has leave
-        if not str then
-            client.buffer = "quit"
-            done = true
-
-        -- Caught a ^D
-        elseif client.buffer == ""
-           and ((client.type == host.client_type.stdio and str == "")
-           or  (client.type == host.client_type.net and str == "\004")) then
-            client.buffer = "quit"
-            done = true
-
-        -- '\n' found: a command was sent
-        elseif string.match(str,"\n") then
-            client.buffer = client.buffer .. str
-            done = true
-
-        -- The command is not finished yet
-        else
-            client.buffer = client.buffer .. str
+        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.cmds = "quit\n"
         end
 
-        -- Some cleaning for telnet
-        if client.type == host.client_type.net then
-            telnet_commands( client )
-        end
+        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
 
-        -- If a command must be parsed
-        if done then
-            -- loop on all commands (might have more than one commands seperated by '\n'
-            local returned_values = ""
-            while not (client.buffer == "") do
-                -- pick the first command
-                local commands = ""
-                if string.find(client.buffer, "\n") then
-                    commands = string.sub(client.buffer, string.find(client.buffer, "\n") + 1)
-                    client.buffer = string.sub(client.buffer, 0, string.find(client.buffer, "\n") - 1)
-                end
-                local cmd = client.buffer
-
-                if client.status == host.status.password then
-                    if client.buffer == password then
-                        client:send( IAC..WONT..ECHO.."\r\nWelcome, Master\r\n" )
-                        client.buffer = ""
-                        client:switch_status( host.status.write )
-                    else
-                        client:send( "\r\nWrong password\r\nPassword: " )
-                        client.buffer = ""
-                    end
-                elseif client_command( client ) then
+            -- 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" )
+                    client.buffer = ""
                     client:switch_status( host.status.write )
-                    -- special case to exit the loop
-                    if cmd == "quit" or cmd == "shutdown" then break end
+                else
+                    client:send( "\r\nWrong password\r\nPassword: " )
+                    client.buffer = ""
                 end
-                returned_values = returned_values .. client.buffer
-                client.buffer = commands
+            elseif client_command( client ) then
+                client:switch_status( host.status.write )
             end
-            vlc.msg.err("end of loop")
-            client.buffer = returned_values
+            client.buffer = saved_buffer .. client.buffer
         end
     end
 end