Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- dld
- -- Simple download program for Computercraft
- -- ============================================
- -- == config
- -- ============================================
- URL = "http://zzz.yyy.example.com/ulddld.php"
- PASSWORD = "xxxxxx"
- -- ============================================
- -- == utilities
- -- ============================================
- local function fileOverwrite(fileName, text)
- local hFile = fs.open(fileName, "w")
- hFile.writeLine(text)
- hFile.close()
- end
- local function makePostText(tbl)
- local rslt = ""
- for key, val in pairs(tbl) do
- if ("" ~= rslt) then
- rslt = rslt .. "&"
- end
- rslt = rslt ..
- textutils.urlEncode(tostring(key)) ..
- "=" ..
- textutils.urlEncode(tostring(val))
- end
- return rslt
- end
- local function httpPost(url, data)
- local file = http.post(url, makePostText(data))
- local rslt
- if file ~= nil then
- rslt = file.readAll()
- file.close()
- else
- rslt = ""
- end
- return rslt
- end
- local function getOneLine(src, start)
- local nl = string.find(src, "\n", start)
- if nil == nl then
- return string.sub(src, start), nil
- else
- local next_pos = nl + 1
- if string.len(src) < next_pos then
- next_pos = nil
- end
- return string.sub(src, start, nl - 1), next_pos
- end
- end
- -- ============================================
- -- == main
- -- ============================================
- data = {}
- data.command = "dld"
- data.password = PASSWORD
- response = httpPost(URL, data)
- -- line 1 : OK or NG
- -- line 2 : when OK -> file name
- -- when NG -> error message
- -- line 3 : when OK -> file data
- line1, nxt = getOneLine(response, 1)
- line2, nxt = getOneLine(response, nxt)
- if ("NG" == line1) then
- print(line2)
- return
- elseif ("OK" == line1) then
- fileOverwrite(line2, string.sub(response, nxt))
- print("downloaded " .. line2)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement