Advertisement
tiin57

rprintc

Dec 18th, 2014
1,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.40 KB | None | 0 0
  1. -- RPrint Client by Tiin57
  2. local requiredConfigValues = {"authUsername", "authPassword", "serverID"}
  3. local args = {...}
  4. function string.split(inputstr, sep) -- Taken from http://stackoverflow.com/questions/1426954/split-string-in-lua
  5.     if sep == nil then
  6.         sep = "%s"
  7.     end
  8.     local t = {}
  9.     local i = 1
  10.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  11.         t[i] = str
  12.         i = i + 1
  13.     end
  14.     return t
  15. end
  16. local function createConfig()
  17.     local defaultValues = {
  18.         authUsername = "default",
  19.         authPassword = "",
  20.         serverID = 0
  21.     }
  22.     write("Username (enter nothing for default): ")
  23.     local username = read()
  24.     if username == "" then
  25.         username = defaultValues.authUsername
  26.     end
  27.     term.write("Password: ")
  28.     local password = read("*")
  29.     term.write("Server ID: ")
  30.     local id = read()
  31.     defaultValues = {
  32.         authUsername = username,
  33.         authPassword = password,
  34.         serverID = tonumber(id)
  35.     }
  36.     local text = ""
  37.     for key, value in pairs(defaultValues) do
  38.         text = text .. key .. "=" .. value .. "\n"
  39.     end
  40.     local file = fs.open("rprint/client.cfg", "w")
  41.     file.write(text)
  42.     file.close()
  43. end
  44. local function parseConfig()
  45.     local file = fs.open("rprint/client.cfg", "r")
  46.     local lines = string.split(file.readAll(), "\n")
  47.     file.close()
  48.     local config = {}
  49.     for k, line in pairs(lines) do
  50.         local tokens = string.split(line, "=")
  51.         if tonumber(tokens[2]) ~= nil then
  52.             tokens[2] = tonumber(tokens[2])
  53.         end
  54.         config[tokens[1]] = tokens[2]
  55.     end
  56.     return config
  57. end
  58. local function validateConfig(config)
  59.     for _, key in pairs(requiredConfigValues) do
  60.         if not config[key] then
  61.             return key
  62.         end
  63.     end
  64.     return false
  65. end
  66. local function listen(config)
  67.     id, message = rednet.receive("rprint")
  68.     return message
  69. end
  70. local function sendPrint(config, title, file)
  71.     local f = fs.open(file, "r")
  72.     local body = f.readAll()
  73.     f.close()
  74.     rednet.open("back")
  75.     rednet.send(config.serverID, "auth:" .. config.authUsername .. "," .. config.authPassword, "rprint")
  76.     local message = listen(config)
  77.     if message == "auth:true" then
  78.         print("Authenticated with the server, starting print job...")
  79.     else
  80.         print("Authentication failed! Please modify rprint/client.cfg appropriately.")
  81.         return
  82.     end
  83.     rednet.send(config.serverID, "print:" .. title .. "@" .. body, "rprint")
  84.     message = listen(config)
  85.     if message == "error:noprinter" then
  86.         print("There is no printer attached to the server!")
  87.     elseif message == "error:noresources" then
  88.         print("The remote printer is out of ink or paper!")
  89.     elseif message == "error:morepaper" then
  90.         print("There is not enough paper to finish the print job.")
  91.     else
  92.         print("Printed " .. file .. " successfully.")
  93.     end
  94. end
  95. local function main()
  96.     if #args == 0 then
  97.         print("Usage: rprint <file>")
  98.         return
  99.     end
  100.     if fs.exists("rprint") and not fs.isDir("rprint") then
  101.         fs.delete("rprint")
  102.     end
  103.     if not fs.exists("rprint") then
  104.         fs.makeDir("rprint")
  105.     end
  106.     if not fs.exists("rprint/client.cfg") then
  107.         createConfig()
  108.     end
  109.     local config = parseConfig()
  110.     local missingKey = validateConfig(config)
  111.     if missingKey then
  112.         print("rprint/client.cfg is missing required key " .. missingKey)
  113.         return
  114.     end
  115.     local title = args[1]
  116.     local file = args[1]
  117.     if #args > 1 then
  118.         table.remove(args, 1)
  119.         title = table.concat(args, " ")
  120.     end
  121.     if (not fs.exists(file)) or fs.isDir(file) then
  122.         print(args[1] .. " does not exist or is a directory.")
  123.         return
  124.     end
  125.     sendPrint(config, title, file)
  126. end
  127. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement