Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local addr = "http://ccforum.comyr.com"
- local reg = "register.php"
- local getk = "getuserkey.php"
- local forum = "forum.php"
- local userKey
- local loginScreen
- os.loadAPI("/ser");
- local cancelRead
- local ins = {}
- local w, h = term.getSize()
- local function register(user, pass, name)
- local res = http.post(
- addr .. "/" .. reg,
- "user=" .. user .. "&" ..
- "pass=" .. pass .. "&" ..
- "name=" .. name
- )
- local ret = res.readAll()
- res.close()
- if string.sub(ret, 1, 5) ~= "Error" then
- return true
- else
- return false, string.sub(ret, 8)
- end
- end
- local function listForums()
- local res = http.post(
- addr .. "/" .. forum,
- "type=listforums"
- )
- local ret = ser.unserialize(res.readAll())
- res.close()
- return ret
- end
- local function getThreads(group, forump)
- local res = http.post(
- addr .. "/" .. forum,
- "type=listthreads&" ..
- "group=" .. group .. "&" ..
- "forum=" .. forump
- )
- local ret = ser.unserialize(res.readAll())
- res.close()
- return ret
- end
- local function getKey(user, pass)
- local res = http.post(
- addr .. "/" .. getk,
- "user=" .. user .. "&" ..
- "pass=" .. pass
- )
- local ret = res.readAll()
- res.close()
- if string.sub(ret, 1, 5) ~= "Error" then
- return true
- else
- return false, string.sub(ret, 8)
- end
- end
- local function centerPrint(text, size, pos, y)
- term.setCursorPos((size - #text) / 2 + pos - 1, y)
- term.write(text)
- end
- local function drawBox(sizex, sizey, posx, posy)
- for i = 1, sizey do
- term.setCursorPos(posx, posy + i - 1)
- term.write(string.rep(" ", sizex))
- end
- end
- local function addN(name, sizex, sizey, posx, posy)
- table.insert(ins, {name, sizex, sizey, posx, posy})
- end
- local function getN(x, y)
- for i,v in ipairs(ins) do
- -- term.setCursorPos(1, 1)
- -- print(x .. " >= " .. v[4] .. " and " .. y .. " >= " .. v[5] .. " and " .. x .. " <= " .. v[4] .. " + " .. v[2] .. " - 1 and " .. y .. " <= " .. v[5] .. " + " .. v[3] .. " - 1")
- if x >= v[4] and y >= v[5] and x <= v[4] + v[2] - 1 and y <= v[5] + v[3] - 1 then
- return v[1]
- end
- end
- end
- local function getText(sizex, sizey, posx, posy, bColor, tColor, wrap, output, sChar)
- if sChar then sChar = string.sub(sChar, 1, 1) end
- if wrap == nil then wrap = false end
- term.setBackgroundColor(bColor)
- term.setTextColor(tColor)
- drawBox(sizex, sizey, posx, posy)
- term.setCursorPos(posx, posy)
- term.setCursorBlink(true)
- local tCan = nil
- cancelRead = function()
- term.setCursorBlink(false)
- tCan = true
- os.queueEvent("CancelRead")
- end
- local text = ""
- local wText = text
- local function calcText()
- if #text > sizex - 1 then
- wText = string.sub(text, #text - sizex + 2, #text)
- else
- wText = text
- end
- if sChar then
- wText = string.rep(sChar, #wText)
- end
- end
- local function updateText()
- drawBox(sizex, sizey, posx, posy)
- term.setCursorPos(posx, posy)
- term.write(wText)
- end
- while true do
- if tCan == true then
- return text
- end
- local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
- if output then
- output(event, p1, p2, p3, p4, p5)
- end
- if event == "char" then
- text = text .. p1
- calcText()
- updateText()
- elseif event == "key" then
- if p1 == 14 then
- text = string.sub(text, 1, #text - 1)
- calcText()
- updateText()
- elseif p1 == 28 then
- cancelRead()
- end
- end
- end
- end
- local function openThread(group, forum, info)
- ins = {}
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.gray)
- drawBox(w, 1, 1, 1)
- term.setTextColor(colors.white)
- term.setCursorPos(2, 1)
- term.write("[ Back ] [ Account ] [ Reply ]")
- local i = info.info
- local lInd = 1
- term.setBackgroundColor(colors.lightGray)
- drawBox(w - 2, h - 3, 2, 3)
- term.setBackgroundColor(colors.gray)
- drawBox(2, 3, w - 2, 3)
- local lines = {}
- local function addLine(backc, textc, text)
- table.insert(lines, {backc, textc, text})
- end
- local lf = i
- local pOn = 0
- local cur = ""
- for s = 1, #lf do
- pOn = pOn + 1
- cur = cur .. string.sub(lf, s, s)
- if #cur == w - 4 then
- if string.find(cur, " ") then
- for t = #cur, 1, -1 do
- if string.sub(cur, t, t) == " " then
- local raw = string.sub(cur, t + 1)
- cur = string.sub(cur, 1, t - 1)
- addLine(colors.lightGray, colors.black, cur)
- cur = raw
- break
- end
- end
- else
- addLine(colors.lightGray, colors.black, cur)
- cur = ""
- end
- end
- end
- addLine(colors.lightGray, colors.black, cur)
- --[[
- term.setTextColor(colors.black)
- for i,v in pairs(lines) do
- term.setCursorPos(2, i + 2)
- term.write(v)
- end
- ]]
- local function drawLines()
- local pOn = 2
- for i = lInd, lInd + h - 3 do
- local v = lines[i]
- if v then
- pOn = pOn + 1
- term.setBackgroundColor(v[1])
- drawBox(w - 4, 1, 2, pOn)
- term.setCursorPos(2, pOn)
- term.setTextColor(v[2])
- term.write(v[3])
- end
- end
- end
- drawLines()
- local function output(event, p1, p2, p3, p4, p5)
- if event == "mouse_click" then
- local n = getN(p2, p3)
- if n then
- queue = n
- if cancelRead then
- cancelRead()
- cancelRead = nil
- end
- end
- end
- end
- local function checkQueue()
- if queue then
- if string.sub(queue, 1, 7) == "thread:" then
- local tbl = textutils.unserialize(string.sub(queue, 8))
- openThread(group, forum, tbl)
- return true
- end
- queue = nil
- end
- end
- while true do
- local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
- output(event, p1, p2, p3, p4, p5)
- local b = checkQueue()
- if b == true then
- break
- end
- end
- end
- local function subScreen(group, forum, threads)
- ins = {}
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.gray)
- drawBox(w, 1, 1, 1)
- term.setTextColor(colors.white)
- term.setCursorPos(2, 1)
- term.write("[ Back ] [ Account ] [ New Thread ]")
- local pOn = 2
- term.setBackgroundColor(colors.blue)
- drawBox(w - 4, 1, 3, 3)
- term.setTextColor(colors.black)
- term.setCursorPos(4, 3)
- term.write(forum)
- for k,v in pairs(threads) do
- pOn = pOn + 2
- term.setBackgroundColor(colors.lightBlue)
- drawBox(w - 4, 2, 3, pOn)
- term.setTextColor(colors.black)
- term.setCursorPos(4, pOn)
- term.write(v.name)
- term.setTextColor(colors.gray)
- term.setCursorPos(4, pOn + 1)
- term.write("Started by: " .. v.user .. " | Created: " .. v.date)
- addN("thread:" .. textutils.serialize(v), w - 4, 2, 3, pOn)
- end
- local function output(event, p1, p2, p3, p4, p5)
- if event == "mouse_click" then
- local n = getN(p2, p3)
- if n then
- queue = n
- if cancelRead then
- cancelRead()
- cancelRead = nil
- end
- end
- end
- end
- local function checkQueue()
- if queue then
- if string.sub(queue, 1, 7) == "thread:" then
- local tbl = textutils.unserialize(string.sub(queue, 8))
- openThread(group, forum, tbl)
- return true
- end
- queue = nil
- end
- end
- while true do
- local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
- output(event, p1, p2, p3, p4, p5)
- local b = checkQueue()
- if b == true then
- break
- end
- end
- end
- local function forumScreen()
- ins = {}
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.gray)
- drawBox(w, 1, 1, 1)
- term.setTextColor(colors.white)
- term.setCursorPos(2, 1)
- term.write("[ Account ]")
- local forums = listForums()
- local pOn = 0
- for k,v in pairs(forums) do
- pOn = pOn + 1
- term.setBackgroundColor(colors.blue)
- drawBox(w - 4, 1, 3, pOn + 2)
- term.setTextColor(colors.black)
- term.setCursorPos(4, pOn + 2)
- term.write(k)
- local tOn = pOn
- for k2, v2 in pairs(v) do
- tOn = tOn + 3
- term.setBackgroundColor(colors.lightBlue)
- drawBox(w - 4, 2, 3, tOn)
- term.setCursorPos(4, tOn)
- term.setTextColor(colors.black)
- term.write(k2)
- term.setTextColor(colors.gray)
- term.setCursorPos(4, tOn + 1)
- term.write(v2["desc"])
- term.setBackgroundColor(colors.cyan)
- drawBox(w - 4, 1, 3, tOn + 2)
- addN("forum:" .. textutils.serialize({k2, k}), w - 4, 2, 3, tOn)
- end
- end
- local function output(event, p1, p2, p3, p4, p5)
- if event == "mouse_click" then
- local n = getN(p2, p3)
- if n then
- queue = n
- if cancelRead then
- cancelRead()
- cancelRead = nil
- end
- end
- end
- end
- local function checkQueue()
- if queue then
- if string.sub(queue, 1, 6) == "forum:" then
- local tbl = textutils.unserialize(string.sub(queue, 7))
- local thr = getThreads(tbl[2], tbl[1])
- subScreen(tbl[2], tbl[1], thr)
- return true
- end
- queue = nil
- end
- end
- while true do
- local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
- output(event, p1, p2, p3, p4, p5)
- local b = checkQueue()
- if b == true then
- break
- end
- end
- end
- local function registerScreen()
- ins = {}
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.gray)
- drawBox(w, 1, 1, 1)
- term.setTextColor(colors.white)
- centerPrint("Register for ccForum", w, 1, 1)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(13, 5)
- term.setTextColor(colors.black)
- term.write("Username: ")
- term.setBackgroundColor(colors.white)
- term.setCursorPos(13, 7)
- term.setTextColor(colors.black)
- term.write("Password: ")
- term.setBackgroundColor(colors.white)
- term.setCursorPos(9, 9)
- term.setTextColor(colors.black)
- term.write("Confirm Password: ")
- term.setBackgroundColor(colors.gray)
- drawBox(7, 1, 17, 11)
- addN("login", 7, 1, 15, 11)
- term.setBackgroundColor(colors.gray)
- drawBox(8, 1, 25, 11)
- addN("create", 8, 1, 23, 11)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(18, 11)
- term.setTextColor(colors.white)
- term.write("Login")
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(26, 11)
- term.setTextColor(colors.white)
- term.write("Create")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.red)
- centerPrint("Notice: Passwords are NOT secure!", w, 1, 13)
- term.setBackgroundColor(colors.lightGray)
- drawBox(15, 1, 24, 5)
- addN("UserInput", 15, 1, 24, 5)
- term.setBackgroundColor(colors.lightGray)
- drawBox(15, 1, 24, 7)
- addN("PassInput", 15, 1, 24, 7)
- term.setBackgroundColor(colors.lightGray)
- drawBox(15, 1, 28, 9)
- addN("PassConfirmInput", 15, 1, 28, 9)
- local queue
- local pass
- local passc
- local user
- local function output(event, p1, p2, p3, p4, p5)
- if event == "mouse_click" then
- local n = getN(p2, p3)
- if n then
- queue = n
- if cancelRead then
- cancelRead()
- cancelRead = nil
- end
- end
- end
- end
- local function checkQueue()
- if queue then
- if queue == "create" then
- if pass ~= passc then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.red)
- term.setCursorPos(1, 13)
- term.clearLine()
- centerPrint("Passwords do not match", w, 1, 13)
- else
- local res, err = register(user, pass, "Unnamed")
- if res == true then
- loginScreen()
- return true
- else
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.red)
- term.setCursorPos(1, 13)
- term.clearLine()
- centerPrint(err, w, 1, 13)
- end
- end
- elseif queue == "login" then
- loginScreen()
- return true
- elseif queue == "UserInput" then
- user = getText(15, 1, 24, 5, colors.lightGray, colors.black, nil, output)
- elseif queue == "PassInput" then
- pass = getText(15, 1, 24, 7, colors.lightGray, colors.black, nil, output, "*")
- elseif queue == "PassConfirmInput" then
- passc = getText(15, 1, 28, 9, colors.lightGray, colors.black, nil, output, "*")
- end
- queue = nil
- end
- end
- user = getText(15, 1, 24, 5, colors.lightGray, colors.black, nil, output)
- local b = checkQueue()
- if b == true then
- return
- end
- while true do
- local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
- output(event, p1, p2, p3, p4, p5)
- local b = checkQueue()
- if b == true then
- break
- end
- end
- end
- function loginScreen()
- ins = {}
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.gray)
- drawBox(w, 1, 1, 1)
- term.setTextColor(colors.white)
- centerPrint("Login to ccForum", w, 1, 1)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(13, 5)
- term.setTextColor(colors.black)
- term.write("Username: ")
- term.setBackgroundColor(colors.white)
- term.setCursorPos(13, 7)
- term.setTextColor(colors.black)
- term.write("Password: ")
- term.setBackgroundColor(colors.gray)
- drawBox(7, 1, 16, 9)
- addN("login", 7, 1, 16, 9)
- term.setBackgroundColor(colors.gray)
- drawBox(10, 1, 24, 9)
- addN("register", 10, 1, 24, 9)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(17, 9)
- term.setTextColor(colors.white)
- term.write("Login")
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(25, 9)
- term.setTextColor(colors.white)
- term.write("Register")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.red)
- centerPrint("Notice: Passwords are NOT secure!", w, 1, 11)
- term.setBackgroundColor(colors.lightGray)
- drawBox(15, 1, 24, 5)
- addN("UserInput", 15, 1, 24, 5)
- term.setBackgroundColor(colors.lightGray)
- drawBox(15, 1, 24, 7)
- addN("PassInput", 15, 1, 24, 7)
- local queue
- local pass
- local user
- local function output(event, p1, p2, p3, p4, p5)
- if event == "mouse_click" then
- local n = getN(p2, p3)
- if n then
- if cancelRead then
- cancelRead()
- cancelRead = nil
- end
- queue = n
- end
- end
- end
- local function checkQueue()
- if queue then
- if queue == "register" then
- registerScreen()
- return true
- elseif queue == "login" then
- if user ~= "" and pass ~= "" then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.red)
- term.setCursorPos(1, 11)
- term.clearLine()
- centerPrint("Logging in...", w, 1, 11)
- local key, err = getKey(user, pass)
- if key == false then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.red)
- term.setCursorPos(1, 11)
- term.clearLine()
- centerPrint(err, w, 1, 11)
- else
- userKey = key
- forumScreen()
- return true
- end
- end
- elseif queue == "UserInput" then
- user = getText(15, 1, 24, 5, colors.lightGray, colors.black, nil, output)
- elseif queue == "PassInput" then
- pass = getText(15, 1, 24, 7, colors.lightGray, colors.black, nil, output, "*")
- end
- queue = nil
- end
- end
- user = getText(15, 1, 24, 5, colors.lightGray, colors.black, nil, output)
- local b = checkQueue()
- if b == true then
- return
- end
- while true do
- local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
- output(event, p1, p2, p3, p4, p5)
- local b = checkQueue()
- if b == true then
- break
- end
- end
- end
- loginScreen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement