Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- RPrint Server by Tiin57
- local requiredConfigValues = {"modemSide", "printerSide", "authUsername", "authPassword"}
- 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 = {
- modemSide = "top",
- printerSide = "right",
- authUsername = "default",
- authPassword = ""
- }
- local text = ""
- for key, value in pairs(defaultValues) do
- text = text .. key .. "=" .. tostring(value) .. "\n"
- end
- local file = fs.open("rprint/server.cfg", "w")
- file.write(text)
- file.close()
- end
- local function parseConfig()
- local file = fs.open("rprint/server.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, "=")
- config[tokens[1]] = tokens[2]
- end
- return config
- end
- local function validateConfig(config)
- for _, key in pairs(requiredConfigValues) do
- if config[key] == nil then
- return key
- end
- end
- return false
- end
- local function splitByInterval(str, interval)
- local tbl = {}
- local iterator = 1
- for i=1, string.len(str) do
- if (i % interval) == 1 then
- iterator = iterator + 1
- tbl[iterator] = ""
- end
- tbl[iterator] = tbl[iterator] .. string.sub(str, i, i)
- end
- return tbl
- end
- local function beginPrint(config, id, title, body)
- sleep(0.5)
- if peripheral.getType(config.printerSide) ~= "printer" then
- rednet.send(id, "error:noprinter", "rprint")
- return
- end
- local printer = peripheral.wrap(config.printerSide)
- if printer.getInkLevel() == 0 or printer.getPaperLevel() == 0 then
- rednet.send(id, "error:noresources", "rprint")
- return
- end
- local key = tostring(math.random())
- local height = 1
- local pageCount = 1
- local lines = string.split(body, "\n")
- printer.newPage()
- local pageWidth = ({printer.getPageSize()})[1]
- local pageHeight = ({printer.getPageSize()})[2]
- local totalPageCount = math.ceil((#lines) / pageHeight)
- if printer.getPaperLevel() < totalPageCount then
- rednet.send(id, "error:morepaper", "rprint")
- printer.endPage()
- return
- end
- printer.setPageTitle(title .. " " .. pageCount .. "/" .. totalPageCount)
- for key, line in pairs(lines) do
- if height >= pageHeight then
- printer.endPage()
- printer.newPage()
- printer.setPageTitle(title .. " " .. pageCount .. "/" .. totalPageCount)
- height = 1
- pageCount = pageCount + 1
- end
- line = string.gsub(line, "\t", "")
- printer.write(line)
- local currentPos = {printer.getCursorPos()}
- printer.setCursorPos(1, currentPos[2] + 1)
- height = height + 1
- end
- printer.endPage()
- rednet.send(id, "success:" .. key, "rprint")
- end
- local function startServer(config)
- if peripheral.getType(config.modemSide) ~= "modem" then
- print("A modem is not connected on the " .. config.modemSide .. " side!")
- return
- end
- rednet.open(config.modemSide)
- local authedIDs = {}
- while true do
- local id, message = rednet.receive("rprint")
- sleep(0.5)
- local tokens = string.split(message, ":")
- local header = tokens[1]
- table.remove(tokens, 1)
- local body = table.concat(tokens, ":")
- if header == "auth" then
- if authedIDs[id] then
- rednet.send(id, "auth:true", "rprint")
- else
- body = string.split(body, ",")
- if #body >= 2 then
- local username = body[1]
- local password = body[2]
- if username == config.authUsername and password == config.authPassword then
- authedIDs[id] = true
- rednet.send(id, "auth:true", "rprint")
- else
- rednet.send(id, "auth:false", "rprint")
- end
- else
- rednet.send(id, "auth:false", "rprint")
- end
- end
- elseif header == "print" then
- if not authedIDs[id] then
- rednet.send(id, "auth:false", "rprint")
- else
- tokens = string.split(body, "@")
- local title = tokens[1]
- table.remove(tokens, 1)
- body = table.concat(tokens, "@")
- beginPrint(config, id, title, body)
- end
- end
- end
- end
- local function main()
- 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/server.cfg") then
- createConfig()
- end
- local config = parseConfig()
- local missingKey = validateConfig(config)
- if missingKey then
- print("rprint/server.cfg is missing required key " .. missingKey)
- return
- end
- startServer(config)
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement