Advertisement
Alakazard12

ccForum

May 11th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.40 KB | None | 0 0
  1. local addr = "http://ccforum.comyr.com"
  2. local reg = "register.php"
  3. local getk = "getuserkey.php"
  4. local forum = "forum.php"
  5. local userKey
  6. local loginScreen
  7.  
  8. os.loadAPI("/ser");
  9.  
  10. local cancelRead
  11. local ins = {}
  12.  
  13. local w, h = term.getSize()
  14.  
  15. local function register(user, pass, name)
  16.     local res = http.post(
  17.         addr .. "/" .. reg,
  18.         "user=" .. user .. "&" ..
  19.         "pass=" .. pass .. "&" ..
  20.         "name=" .. name
  21.     )
  22.    
  23.     local ret = res.readAll()
  24.     res.close()
  25.     if string.sub(ret, 1, 5) ~= "Error" then
  26.         return true
  27.     else
  28.         return false, string.sub(ret, 8)
  29.     end
  30. end
  31.  
  32. local function listForums()
  33.     local res = http.post(
  34.         addr .. "/" .. forum,
  35.         "type=listforums"
  36.     )
  37.  
  38.     local ret = ser.unserialize(res.readAll())
  39.     res.close()
  40.  
  41.     return ret
  42. end
  43.  
  44. local function getThreads(group, forump)
  45.     local res = http.post(
  46.         addr .. "/" .. forum,
  47.         "type=listthreads&" ..
  48.         "group=" .. group .. "&" ..
  49.         "forum=" .. forump
  50.     )
  51.  
  52.     local ret = ser.unserialize(res.readAll())
  53.     res.close()
  54.  
  55.     return ret
  56. end
  57.  
  58. local function getKey(user, pass)
  59.     local res = http.post(
  60.         addr .. "/" .. getk,
  61.         "user=" .. user .. "&" ..
  62.         "pass=" .. pass
  63.     )
  64.  
  65.     local ret = res.readAll()
  66.     res.close()
  67.  
  68.     if string.sub(ret, 1, 5) ~= "Error" then
  69.         return true
  70.     else
  71.         return false, string.sub(ret, 8)
  72.     end
  73. end
  74.  
  75. local function centerPrint(text, size, pos, y)
  76.     term.setCursorPos((size - #text) / 2 + pos - 1, y)
  77.     term.write(text)
  78. end
  79.  
  80. local function drawBox(sizex, sizey, posx, posy)
  81.     for i = 1, sizey do
  82.         term.setCursorPos(posx, posy + i - 1)
  83.         term.write(string.rep(" ", sizex))
  84.     end
  85. end
  86.  
  87. local function addN(name, sizex, sizey, posx, posy)
  88.     table.insert(ins, {name, sizex, sizey, posx, posy})
  89. end
  90.  
  91. local function getN(x, y)
  92.     for i,v in ipairs(ins) do
  93.         -- term.setCursorPos(1, 1)
  94.         -- print(x .. " >= " ..  v[4] .. " and " .. y .. " >= " .. v[5] .. " and " .. x .. " <= " .. v[4] .. " + " .. v[2] .. " - 1 and " .. y .. " <= " .. v[5] .. " + " .. v[3] .. " - 1")
  95.         if x >= v[4] and y >= v[5] and x <= v[4] + v[2] - 1 and y <= v[5] + v[3] - 1 then
  96.             return v[1]
  97.         end
  98.     end
  99. end
  100.  
  101. local function getText(sizex, sizey, posx, posy, bColor, tColor, wrap, output, sChar)
  102.     if sChar then sChar = string.sub(sChar, 1, 1) end
  103.     if wrap == nil then wrap = false end
  104.     term.setBackgroundColor(bColor)
  105.     term.setTextColor(tColor)
  106.     drawBox(sizex, sizey, posx, posy)
  107.     term.setCursorPos(posx, posy)
  108.     term.setCursorBlink(true)
  109.  
  110.     local tCan = nil
  111.  
  112.     cancelRead = function()
  113.         term.setCursorBlink(false)
  114.         tCan = true
  115.         os.queueEvent("CancelRead")
  116.     end
  117.  
  118.     local text = ""
  119.     local wText = text
  120.  
  121.     local function calcText()
  122.         if #text > sizex - 1 then
  123.             wText = string.sub(text, #text - sizex + 2, #text)
  124.         else
  125.             wText = text
  126.         end
  127.         if sChar then
  128.             wText = string.rep(sChar, #wText)
  129.         end
  130.     end
  131.  
  132.     local function updateText()
  133.         drawBox(sizex, sizey, posx, posy)
  134.         term.setCursorPos(posx, posy)
  135.         term.write(wText)
  136.     end
  137.  
  138.     while true do
  139.         if tCan == true then
  140.             return text
  141.         end
  142.         local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
  143.         if output then
  144.             output(event, p1, p2, p3, p4, p5)
  145.         end
  146.         if event == "char" then
  147.             text = text .. p1
  148.             calcText()
  149.             updateText()
  150.         elseif event == "key" then
  151.             if p1 == 14 then
  152.                 text = string.sub(text, 1, #text - 1)
  153.                 calcText()
  154.                 updateText()
  155.             elseif p1 == 28 then
  156.                 cancelRead()
  157.             end
  158.         end
  159.     end
  160. end
  161.  
  162. local function openThread(group, forum, info)
  163.     ins = {}
  164.     term.setBackgroundColor(colors.white)
  165.     term.clear()
  166.     term.setCursorPos(1, 1)
  167.     term.setBackgroundColor(colors.gray)
  168.     drawBox(w, 1, 1, 1)
  169.     term.setTextColor(colors.white)
  170.     term.setCursorPos(2, 1)
  171.     term.write("[ Back ] [ Account ] [ Reply ]")
  172.  
  173.     local i = info.info
  174.     local lInd = 1
  175.     term.setBackgroundColor(colors.lightGray)
  176.     drawBox(w - 2, h - 3, 2, 3)
  177.  
  178.     term.setBackgroundColor(colors.gray)
  179.     drawBox(2, 3, w - 2, 3)
  180.  
  181.     local lines = {}
  182.  
  183.     local function addLine(backc, textc, text)
  184.         table.insert(lines, {backc, textc, text})
  185.     end
  186.  
  187.     local lf = i
  188.     local pOn = 0
  189.     local cur = ""
  190.     for s = 1, #lf do
  191.         pOn = pOn + 1
  192.         cur = cur .. string.sub(lf, s, s)
  193.         if #cur == w - 4 then
  194.             if string.find(cur, " ") then
  195.                 for t = #cur, 1, -1 do
  196.                     if string.sub(cur, t, t) == " " then
  197.                         local raw = string.sub(cur, t + 1)
  198.                         cur = string.sub(cur, 1, t - 1)
  199.                         addLine(colors.lightGray, colors.black, cur)
  200.                         cur = raw
  201.                         break
  202.                     end
  203.                 end
  204.             else
  205.                 addLine(colors.lightGray, colors.black, cur)
  206.                 cur = ""
  207.             end
  208.         end
  209.     end
  210.     addLine(colors.lightGray, colors.black, cur)
  211.  
  212.     --[[
  213.     term.setTextColor(colors.black)
  214.     for i,v in pairs(lines) do
  215.         term.setCursorPos(2, i + 2)
  216.         term.write(v)
  217.     end
  218.     ]]
  219.  
  220.     local function drawLines()
  221.         local pOn = 2
  222.         for i = lInd, lInd + h - 3 do
  223.             local v = lines[i]
  224.             if v then
  225.                 pOn = pOn + 1
  226.                 term.setBackgroundColor(v[1])
  227.                 drawBox(w - 4, 1, 2, pOn)
  228.                 term.setCursorPos(2, pOn)
  229.                 term.setTextColor(v[2])
  230.                 term.write(v[3])
  231.             end
  232.         end
  233.     end
  234.  
  235.     drawLines()
  236.  
  237.     local function output(event, p1, p2, p3, p4, p5)
  238.         if event == "mouse_click" then
  239.             local n = getN(p2, p3)
  240.             if n then
  241.                 queue = n
  242.                 if cancelRead then
  243.                     cancelRead()
  244.                     cancelRead = nil
  245.                 end
  246.             end
  247.         end
  248.     end
  249.  
  250.     local function checkQueue()
  251.         if queue then
  252.             if string.sub(queue, 1, 7) == "thread:" then
  253.                 local tbl = textutils.unserialize(string.sub(queue, 8))
  254.                 openThread(group, forum, tbl)
  255.                 return true
  256.             end
  257.             queue = nil
  258.         end
  259.     end
  260.  
  261.     while true do
  262.         local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
  263.         output(event, p1, p2, p3, p4, p5)
  264.         local b = checkQueue()
  265.         if b == true then
  266.             break
  267.         end
  268.     end
  269. end
  270.  
  271.  
  272. local function subScreen(group, forum, threads)
  273.     ins = {}
  274.     term.setBackgroundColor(colors.white)
  275.     term.clear()
  276.     term.setCursorPos(1, 1)
  277.     term.setBackgroundColor(colors.gray)
  278.     drawBox(w, 1, 1, 1)
  279.     term.setTextColor(colors.white)
  280.     term.setCursorPos(2, 1)
  281.     term.write("[ Back ] [ Account ] [ New Thread ]")
  282.  
  283.     local pOn = 2
  284.  
  285.     term.setBackgroundColor(colors.blue)
  286.     drawBox(w - 4, 1, 3, 3)
  287.     term.setTextColor(colors.black)
  288.     term.setCursorPos(4, 3)
  289.     term.write(forum)
  290.  
  291.     for k,v in pairs(threads) do
  292.         pOn = pOn + 2
  293.         term.setBackgroundColor(colors.lightBlue)
  294.         drawBox(w - 4, 2, 3, pOn)
  295.         term.setTextColor(colors.black)
  296.         term.setCursorPos(4, pOn)
  297.         term.write(v.name)
  298.         term.setTextColor(colors.gray)
  299.         term.setCursorPos(4, pOn + 1)
  300.         term.write("Started by: " .. v.user .. " | Created: " .. v.date)
  301.         addN("thread:" .. textutils.serialize(v), w - 4, 2, 3, pOn)
  302.     end
  303.  
  304.     local function output(event, p1, p2, p3, p4, p5)
  305.         if event == "mouse_click" then
  306.             local n = getN(p2, p3)
  307.             if n then
  308.                 queue = n
  309.                 if cancelRead then
  310.                     cancelRead()
  311.                     cancelRead = nil
  312.                 end
  313.             end
  314.         end
  315.     end
  316.  
  317.     local function checkQueue()
  318.         if queue then
  319.             if string.sub(queue, 1, 7) == "thread:" then
  320.                 local tbl = textutils.unserialize(string.sub(queue, 8))
  321.                 openThread(group, forum, tbl)
  322.                 return true
  323.             end
  324.             queue = nil
  325.         end
  326.     end
  327.  
  328.     while true do
  329.         local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
  330.         output(event, p1, p2, p3, p4, p5)
  331.         local b = checkQueue()
  332.         if b == true then
  333.             break
  334.         end
  335.     end
  336. end
  337.  
  338.  
  339. local function forumScreen()
  340.     ins = {}
  341.     term.setBackgroundColor(colors.white)
  342.     term.clear()
  343.     term.setCursorPos(1, 1)
  344.     term.setBackgroundColor(colors.gray)
  345.     drawBox(w, 1, 1, 1)
  346.     term.setTextColor(colors.white)
  347.     term.setCursorPos(2, 1)
  348.     term.write("[ Account ]")
  349.     local forums = listForums()
  350.  
  351.     local pOn = 0
  352.     for k,v in pairs(forums) do
  353.         pOn = pOn + 1
  354.         term.setBackgroundColor(colors.blue)
  355.         drawBox(w - 4, 1, 3, pOn + 2)
  356.         term.setTextColor(colors.black)
  357.         term.setCursorPos(4, pOn + 2)
  358.         term.write(k)
  359.         local tOn = pOn
  360.         for k2, v2 in pairs(v) do
  361.             tOn = tOn + 3
  362.             term.setBackgroundColor(colors.lightBlue)
  363.             drawBox(w - 4, 2, 3, tOn)
  364.             term.setCursorPos(4, tOn)
  365.             term.setTextColor(colors.black)
  366.             term.write(k2)
  367.             term.setTextColor(colors.gray)
  368.             term.setCursorPos(4, tOn + 1)
  369.             term.write(v2["desc"])
  370.             term.setBackgroundColor(colors.cyan)
  371.             drawBox(w - 4, 1, 3, tOn + 2)
  372.             addN("forum:" .. textutils.serialize({k2, k}), w - 4, 2, 3, tOn)
  373.         end
  374.     end
  375.  
  376.     local function output(event, p1, p2, p3, p4, p5)
  377.         if event == "mouse_click" then
  378.             local n = getN(p2, p3)
  379.             if n then
  380.                 queue = n
  381.                 if cancelRead then
  382.                     cancelRead()
  383.                     cancelRead = nil
  384.                 end
  385.             end
  386.         end
  387.     end
  388.  
  389.     local function checkQueue()
  390.         if queue then
  391.             if string.sub(queue, 1, 6) == "forum:" then
  392.                 local tbl = textutils.unserialize(string.sub(queue, 7))
  393.                 local thr = getThreads(tbl[2], tbl[1])
  394.                 subScreen(tbl[2], tbl[1], thr)
  395.                 return true
  396.             end
  397.             queue = nil
  398.         end
  399.     end
  400.  
  401.     while true do
  402.         local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
  403.         output(event, p1, p2, p3, p4, p5)
  404.         local b = checkQueue()
  405.         if b == true then
  406.             break
  407.         end
  408.     end
  409. end
  410.  
  411. local function registerScreen()
  412.     ins = {}
  413.     term.setBackgroundColor(colors.white)
  414.     term.clear()
  415.     term.setCursorPos(1, 1)
  416.     term.setBackgroundColor(colors.gray)
  417.     drawBox(w, 1, 1, 1)
  418.     term.setTextColor(colors.white)
  419.     centerPrint("Register for ccForum", w, 1, 1)
  420.  
  421.     term.setBackgroundColor(colors.white)
  422.     term.setCursorPos(13, 5)
  423.     term.setTextColor(colors.black)
  424.     term.write("Username: ")
  425.  
  426.     term.setBackgroundColor(colors.white)
  427.     term.setCursorPos(13, 7)
  428.     term.setTextColor(colors.black)
  429.     term.write("Password: ")
  430.  
  431.     term.setBackgroundColor(colors.white)
  432.     term.setCursorPos(9, 9)
  433.     term.setTextColor(colors.black)
  434.     term.write("Confirm Password: ")
  435.  
  436.     term.setBackgroundColor(colors.gray)
  437.     drawBox(7, 1, 17, 11)
  438.     addN("login", 7, 1, 15, 11)
  439.  
  440.     term.setBackgroundColor(colors.gray)
  441.     drawBox(8, 1, 25, 11)
  442.     addN("create", 8, 1, 23, 11)
  443.  
  444.     term.setBackgroundColor(colors.gray)
  445.     term.setCursorPos(18, 11)
  446.     term.setTextColor(colors.white)
  447.     term.write("Login")
  448.  
  449.     term.setBackgroundColor(colors.gray)
  450.     term.setCursorPos(26, 11)
  451.     term.setTextColor(colors.white)
  452.     term.write("Create")
  453.  
  454.     term.setBackgroundColor(colors.white)
  455.     term.setTextColor(colors.red)
  456.     centerPrint("Notice: Passwords are NOT secure!", w, 1, 13)
  457.  
  458.     term.setBackgroundColor(colors.lightGray)
  459.     drawBox(15, 1, 24, 5)
  460.     addN("UserInput", 15, 1, 24, 5)
  461.  
  462.     term.setBackgroundColor(colors.lightGray)
  463.     drawBox(15, 1, 24, 7)
  464.     addN("PassInput", 15, 1, 24, 7)
  465.  
  466.     term.setBackgroundColor(colors.lightGray)
  467.     drawBox(15, 1, 28, 9)
  468.     addN("PassConfirmInput", 15, 1, 28, 9)
  469.  
  470.     local queue
  471.     local pass
  472.     local passc
  473.     local user
  474.  
  475.     local function output(event, p1, p2, p3, p4, p5)
  476.         if event == "mouse_click" then
  477.             local n = getN(p2, p3)
  478.             if n then
  479.                 queue = n
  480.                 if cancelRead then
  481.                     cancelRead()
  482.                     cancelRead = nil
  483.                 end
  484.             end
  485.         end
  486.     end
  487.  
  488.     local function checkQueue()
  489.         if queue then
  490.             if queue == "create" then
  491.                 if pass ~= passc then
  492.                     term.setBackgroundColor(colors.white)
  493.                     term.setTextColor(colors.red)
  494.                     term.setCursorPos(1, 13)
  495.                     term.clearLine()
  496.                     centerPrint("Passwords do not match", w, 1, 13)
  497.                 else
  498.                     local res, err = register(user, pass, "Unnamed")
  499.                     if res == true then
  500.                         loginScreen()
  501.                         return true
  502.                     else
  503.                         term.setBackgroundColor(colors.white)
  504.                         term.setTextColor(colors.red)
  505.                         term.setCursorPos(1, 13)
  506.                         term.clearLine()
  507.                         centerPrint(err, w, 1, 13)
  508.                     end
  509.                 end
  510.             elseif queue == "login" then
  511.                 loginScreen()
  512.                 return true
  513.             elseif queue == "UserInput" then
  514.                 user = getText(15, 1, 24, 5, colors.lightGray, colors.black, nil, output)
  515.             elseif queue == "PassInput" then
  516.                 pass = getText(15, 1, 24, 7, colors.lightGray, colors.black, nil, output, "*")
  517.             elseif queue == "PassConfirmInput" then
  518.                 passc = getText(15, 1, 28, 9, colors.lightGray, colors.black, nil, output, "*")
  519.             end
  520.             queue = nil
  521.         end
  522.     end
  523.  
  524.     user = getText(15, 1, 24, 5, colors.lightGray, colors.black, nil, output)
  525.     local b = checkQueue()
  526.     if b == true then
  527.         return
  528.     end
  529.  
  530.     while true do
  531.         local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
  532.         output(event, p1, p2, p3, p4, p5)
  533.         local b = checkQueue()
  534.         if b == true then
  535.             break
  536.         end
  537.     end
  538. end
  539.  
  540.  
  541. function loginScreen()
  542.     ins = {}
  543.     term.setBackgroundColor(colors.white)
  544.     term.clear()
  545.     term.setCursorPos(1, 1)
  546.     term.setBackgroundColor(colors.gray)
  547.     drawBox(w, 1, 1, 1)
  548.     term.setTextColor(colors.white)
  549.     centerPrint("Login to ccForum", w, 1, 1)
  550.  
  551.     term.setBackgroundColor(colors.white)
  552.     term.setCursorPos(13, 5)
  553.     term.setTextColor(colors.black)
  554.     term.write("Username: ")
  555.  
  556.     term.setBackgroundColor(colors.white)
  557.     term.setCursorPos(13, 7)
  558.     term.setTextColor(colors.black)
  559.     term.write("Password: ")
  560.  
  561.     term.setBackgroundColor(colors.gray)
  562.     drawBox(7, 1, 16, 9)
  563.     addN("login", 7, 1, 16, 9)
  564.  
  565.     term.setBackgroundColor(colors.gray)
  566.     drawBox(10, 1, 24, 9)
  567.     addN("register", 10, 1, 24, 9)
  568.  
  569.     term.setBackgroundColor(colors.gray)
  570.     term.setCursorPos(17, 9)
  571.     term.setTextColor(colors.white)
  572.     term.write("Login")
  573.  
  574.     term.setBackgroundColor(colors.gray)
  575.     term.setCursorPos(25, 9)
  576.     term.setTextColor(colors.white)
  577.     term.write("Register")
  578.  
  579.     term.setBackgroundColor(colors.white)
  580.     term.setTextColor(colors.red)
  581.     centerPrint("Notice: Passwords are NOT secure!", w, 1, 11)
  582.  
  583.     term.setBackgroundColor(colors.lightGray)
  584.     drawBox(15, 1, 24, 5)
  585.     addN("UserInput", 15, 1, 24, 5)
  586.  
  587.     term.setBackgroundColor(colors.lightGray)
  588.     drawBox(15, 1, 24, 7)
  589.     addN("PassInput", 15, 1, 24, 7)
  590.  
  591.     local queue
  592.     local pass
  593.     local user
  594.  
  595.     local function output(event, p1, p2, p3, p4, p5)
  596.         if event == "mouse_click" then
  597.             local n = getN(p2, p3)
  598.             if n then
  599.                 if cancelRead then
  600.                     cancelRead()
  601.                     cancelRead = nil
  602.                 end
  603.                 queue = n
  604.             end
  605.         end
  606.     end
  607.  
  608.     local function checkQueue()
  609.         if queue then
  610.             if queue == "register" then
  611.                 registerScreen()
  612.                 return true
  613.             elseif queue == "login" then
  614.                 if user ~= "" and pass ~= "" then
  615.                     term.setBackgroundColor(colors.white)
  616.                     term.setTextColor(colors.red)
  617.                     term.setCursorPos(1, 11)
  618.                     term.clearLine()
  619.                     centerPrint("Logging in...", w, 1, 11)
  620.                     local key, err = getKey(user, pass)
  621.                     if key == false then
  622.                         term.setBackgroundColor(colors.white)
  623.                         term.setTextColor(colors.red)
  624.                         term.setCursorPos(1, 11)
  625.                         term.clearLine()
  626.                         centerPrint(err, w, 1, 11)
  627.                     else
  628.                         userKey = key
  629.                         forumScreen()
  630.                         return true
  631.                     end
  632.                 end
  633.             elseif queue == "UserInput" then
  634.                 user = getText(15, 1, 24, 5, colors.lightGray, colors.black, nil, output)
  635.             elseif queue == "PassInput" then
  636.                 pass = getText(15, 1, 24, 7, colors.lightGray, colors.black, nil, output, "*")
  637.             end
  638.             queue = nil
  639.         end
  640.     end
  641.  
  642.     user = getText(15, 1, 24, 5, colors.lightGray, colors.black, nil, output)
  643.     local b = checkQueue()
  644.     if b == true then
  645.         return
  646.     end
  647.  
  648.     while true do
  649.         local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
  650.         output(event, p1, p2, p3, p4, p5)
  651.         local b = checkQueue()
  652.         if b == true then
  653.             break
  654.         end
  655.     end
  656. end
  657.  
  658. loginScreen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement