Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- RPrint Client by Tiin57
- local requiredConfigValues = {"authUsername", "authPassword", "serverID"}
- local args = {...}
- function string.split(inputstr, sep) -- Taken from http://stackoverflow.com/questions/1426954/split-string-in-lua
- if sep == nil then
- sep = "%s"
- end
- local t = {}
- local i = 1
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- t[i] = str
- i = i + 1
- end
- return t
- end
- local function createConfig()
- local defaultValues = {
- authUsername = "default",
- authPassword = "",
- serverID = 0
- }
- write("Username (enter nothing for default): ")
- local username = read()
- if username == "" then
- username = defaultValues.authUsername
- end
- term.write("Password: ")
- local password = read("*")
- term.write("Server ID: ")
- local id = read()
- defaultValues = {
- authUsername = username,
- authPassword = password,
- serverID = tonumber(id)
- }
- local text = ""
- for key, value in pairs(defaultValues) do
- text = text .. key .. "=" .. value .. "\n"
- end
- local file = fs.open("rprint/client.cfg", "w")
- file.write(text)
- file.close()
- end
- local function parseConfig()
- local file = fs.open("rprint/client.cfg", "r")
- local lines = string.split(file.readAll(), "\n")
- file.close()
- local config = {}
- for k, line in pairs(lines) do
- local tokens = string.split(line, "=")
- if tonumber(tokens[2]) ~= nil then
- tokens[2] = tonumber(tokens[2])
- end
- config[tokens[1]] = tokens[2]
- end
- return config
- end
- local function validateConfig(config)
- for _, key in pairs(requiredConfigValues) do
- if not config[key] then
- return key
- end
- end
- return false
- end
- local function listen(config)
- id, message = rednet.receive("rprint")
- return message
- end
- local function sendPrint(config, title, file)
- local f = fs.open(file, "r")
- local body = f.readAll()
- f.close()
- rednet.open("back")
- rednet.send(config.serverID, "auth:" .. config.authUsername .. "," .. config.authPassword, "rprint")
- local message = listen(config)
- if message == "auth:true" then
- print("Authenticated with the server, starting print job...")
- else
- print("Authentication failed! Please modify rprint/client.cfg appropriately.")
- return
- end
- rednet.send(config.serverID, "print:" .. title .. "@" .. body, "rprint")
- message = listen(config)
- if message == "error:noprinter" then
- print("There is no printer attached to the server!")
- elseif message == "error:noresources" then
- print("The remote printer is out of ink or paper!")
- elseif message == "error:morepaper" then
- print("There is not enough paper to finish the print job.")
- else
- print("Printed " .. file .. " successfully.")
- end
- end
- local function main()
- if #args == 0 then
- print("Usage: rprint <file>")
- return
- end
- if fs.exists("rprint") and not fs.isDir("rprint") then
- fs.delete("rprint")
- end
- if not fs.exists("rprint") then
- fs.makeDir("rprint")
- end
- if not fs.exists("rprint/client.cfg") then
- createConfig()
- end
- local config = parseConfig()
- local missingKey = validateConfig(config)
- if missingKey then
- print("rprint/client.cfg is missing required key " .. missingKey)
- return
- end
- local title = args[1]
- local file = args[1]
- if #args > 1 then
- table.remove(args, 1)
- title = table.concat(args, " ")
- end
- if (not fs.exists(file)) or fs.isDir(file) then
- print(args[1] .. " does not exist or is a directory.")
- return
- end
- sendPrint(config, title, file)
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement