Advertisement
tiin57

rprints

Dec 18th, 2014
1,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.57 KB | None | 0 0
  1. -- RPrint Server by Tiin57
  2. local requiredConfigValues = {"modemSide", "printerSide", "authUsername", "authPassword"}
  3. function string.split(inputstr, sep) -- Taken from http://stackoverflow.com/questions/1426954/split-string-in-lua
  4.     if sep == nil then
  5.         sep = "%s"
  6.     end
  7.     local t = {}
  8.     local i = 1
  9.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  10.         t[i] = str
  11.         i = i + 1
  12.     end
  13.     return t
  14. end
  15. local function createConfig()
  16.     local defaultValues = {
  17.         modemSide = "top",
  18.         printerSide = "right",
  19.         authUsername = "default",
  20.         authPassword = ""
  21.     }
  22.     local text = ""
  23.     for key, value in pairs(defaultValues) do
  24.         text = text .. key .. "=" .. tostring(value) .. "\n"
  25.     end
  26.     local file = fs.open("rprint/server.cfg", "w")
  27.     file.write(text)
  28.     file.close()
  29. end
  30. local function parseConfig()
  31.     local file = fs.open("rprint/server.cfg", "r")
  32.     local lines = string.split(file.readAll(), "\n")
  33.     file.close()
  34.     local config = {}
  35.     for k, line in pairs(lines) do
  36.         local tokens = string.split(line, "=")
  37.         config[tokens[1]] = tokens[2]
  38.     end
  39.     return config
  40. end
  41. local function validateConfig(config)
  42.     for _, key in pairs(requiredConfigValues) do
  43.         if config[key] == nil then
  44.             return key
  45.         end
  46.     end
  47.     return false
  48. end
  49. local function splitByInterval(str, interval)
  50.     local tbl = {}
  51.     local iterator = 1
  52.     for i=1, string.len(str) do
  53.         if (i % interval) == 1 then
  54.             iterator = iterator + 1
  55.             tbl[iterator] = ""
  56.         end
  57.         tbl[iterator] = tbl[iterator] .. string.sub(str, i, i)
  58.     end
  59.     return tbl
  60. end
  61. local function beginPrint(config, id, title, body)
  62.     sleep(0.5)
  63.     if peripheral.getType(config.printerSide) ~= "printer" then
  64.         rednet.send(id, "error:noprinter", "rprint")
  65.         return
  66.     end
  67.     local printer = peripheral.wrap(config.printerSide)
  68.     if printer.getInkLevel() == 0 or printer.getPaperLevel() == 0 then
  69.         rednet.send(id, "error:noresources", "rprint")
  70.         return
  71.     end
  72.     local key = tostring(math.random())
  73.     local height = 1
  74.     local pageCount = 1
  75.     local lines = string.split(body, "\n")
  76.     printer.newPage()
  77.     local pageWidth = ({printer.getPageSize()})[1]
  78.     local pageHeight = ({printer.getPageSize()})[2]
  79.     local totalPageCount = math.ceil((#lines) / pageHeight)
  80.     if printer.getPaperLevel() < totalPageCount then
  81.         rednet.send(id, "error:morepaper", "rprint")
  82.         printer.endPage()
  83.         return
  84.     end
  85.     printer.setPageTitle(title .. " " .. pageCount .. "/" .. totalPageCount)
  86.     for key, line in pairs(lines) do
  87.         if height >= pageHeight then
  88.             printer.endPage()
  89.             printer.newPage()
  90.             printer.setPageTitle(title .. " " .. pageCount .. "/" .. totalPageCount)
  91.             height = 1
  92.             pageCount = pageCount + 1
  93.         end
  94.         line = string.gsub(line, "\t", "")
  95.         printer.write(line)
  96.         local currentPos = {printer.getCursorPos()}
  97.         printer.setCursorPos(1, currentPos[2] + 1)
  98.         height = height + 1
  99.     end
  100.     printer.endPage()
  101.     rednet.send(id, "success:" .. key, "rprint")
  102. end
  103. local function startServer(config)
  104.     if peripheral.getType(config.modemSide) ~= "modem" then
  105.         print("A modem is not connected on the " .. config.modemSide .. " side!")
  106.         return
  107.     end
  108.     rednet.open(config.modemSide)
  109.     local authedIDs = {}
  110.     while true do
  111.         local id, message = rednet.receive("rprint")
  112.         sleep(0.5)
  113.         local tokens = string.split(message, ":")
  114.         local header = tokens[1]
  115.         table.remove(tokens, 1)
  116.         local body = table.concat(tokens, ":")
  117.         if header == "auth" then
  118.             if authedIDs[id] then
  119.                 rednet.send(id, "auth:true", "rprint")
  120.             else
  121.                 body = string.split(body, ",")
  122.                 if #body >= 2 then
  123.                     local username = body[1]
  124.                     local password = body[2]
  125.                     if username == config.authUsername and password == config.authPassword then
  126.                         authedIDs[id] = true
  127.                         rednet.send(id, "auth:true", "rprint")
  128.                     else
  129.                         rednet.send(id, "auth:false", "rprint")
  130.                     end
  131.                 else
  132.                     rednet.send(id, "auth:false", "rprint")
  133.                 end
  134.             end
  135.         elseif header == "print" then
  136.             if not authedIDs[id] then
  137.                 rednet.send(id, "auth:false", "rprint")
  138.             else
  139.                 tokens = string.split(body, "@")
  140.                 local title = tokens[1]
  141.                 table.remove(tokens, 1)
  142.                 body = table.concat(tokens, "@")
  143.                 beginPrint(config, id, title, body)
  144.             end
  145.         end
  146.     end
  147. end
  148. local function main()
  149.     if fs.exists("rprint") and not fs.isDir("rprint") then
  150.         fs.delete("rprint")
  151.     end
  152.     if not fs.exists("rprint") then
  153.         fs.makeDir("rprint")
  154.     end
  155.     if not fs.exists("rprint/server.cfg") then
  156.         createConfig()
  157.     end
  158.     local config = parseConfig()
  159.     local missingKey = validateConfig(config)
  160.     if missingKey then
  161.         print("rprint/server.cfg is missing required key " .. missingKey)
  162.         return
  163.     end
  164.     startServer(config)
  165. end
  166. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement