Advertisement
Guest User

craftmail

a guest
Jun 29th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.00 KB | None | 0 0
  1. -- ORIGINAL PROGRAM BY MIDNIGHTAS --
  2.  
  3. os.loadAPI("disk/json")
  4.  
  5. w, h = term.getSize()
  6.  
  7. -- limitRead: Source by remiX on the ComputerCraft forums.
  8. function limitRead(nLimit, specialChars, replaceChar)
  9.   term.setCursorBlink(true)
  10.   local cX, cY = term.getCursorPos()
  11.   local rString = ""
  12.   if replaceChar == "" then replaceChar = nil end
  13.   repeat
  14.     local event, p1 = os.pullEvent()
  15.     if event == "char" then
  16.       -- Character event
  17.       if #rString + 1 <= nLimit then
  18.         if string.match(p1, "%W") and specialChars == false then
  19.           p1 = ""
  20.         end
  21.         rString = rString .. p1
  22.         write(replaceChar or p1)
  23.       end
  24.     elseif event == "key" and p1 == keys.backspace and #rString >= 1 then
  25.       -- Backspace
  26.       rString = string.sub(rString, 1, #rString-1)
  27.       xPos, yPos = term.getCursorPos()
  28.       if xPos > 1 then
  29.         term.setCursorPos(xPos - 1, yPos)
  30.         write(" ")
  31.         term.setCursorPos(xPos - 1, yPos)
  32.       else
  33.         if #rString ~= 0 then
  34.           term.setCursorPos(w, yPos - 1)
  35.           write(" ")
  36.           term.setCursorPos(w, yPos - 1)
  37.         end
  38.       end
  39.     end
  40.   until event == "key" and p1 == keys.enter and #rString > 0
  41.   term.setCursorBlink(false)
  42.   print() -- Skip to the next line after clicking enter.
  43.   return rString
  44. end
  45.  
  46. function printCenter(text, y, offset)
  47.   offset = offset or 0
  48.   local x = math.floor(w - string.len(text)) / 2 + offset
  49.   term.setCursorPos(x, y)
  50.   print(text)
  51.   return x
  52. end
  53.  
  54. function drawLogo()
  55.   printCenter("    _____            __ _                    _ _ ", 3)
  56.   printCenter("   / ____|          / _| |                  (_| |", 4)
  57.   printCenter("  | |     _ __ __ _| |_| |_    __ ___   __ _ _| |", 5)
  58.   printCenter("  | |    | '__/ _` |  _| __|  /_ ` _ \\ / _` | | |", 6)
  59.   printCenter("  | |____| | | (_| | | | |_   | | | | | (_| | | |", 7)
  60.   printCenter("    \\_____|_|  \\__,_|_| \\_ _|  |_| |_|  \\__,_|_|_|", 8)
  61. end
  62.  
  63. function drawBackground()
  64.   term.setBackgroundColor(colors.orange)
  65.   term.setTextColor(colors.black)
  66.   term.clear()
  67. end
  68.  
  69. username = nil
  70. password = nil
  71.  
  72. -- States: -1 = register; 0 = menu; 1 = login; 2 = send; 3 = read page; 4 = read message;
  73. state = 0
  74. menuOption = 0
  75.  
  76. readPage = 1
  77. mails = nil
  78. selectedMail = nil
  79.  
  80. function drawMenu()
  81.   drawLogo()
  82.   term.setBackgroundColor(colors.orange)
  83.   term.setTextColor(colors.black)
  84.   if password ~= nil then
  85.   if menuOption == 0 then
  86.     term.setBackgroundColor(colors.lightGray)
  87.   end
  88.   btnSendX = printCenter("[ SEND ]", h / 2 + 3)
  89.   term.setBackgroundColor(colors.orange)
  90.   if menuOption == 1 then
  91.     term.setBackgroundColor(colors.lightGray)
  92.   end
  93.   btnReadX = printCenter("[ READ ]", h / 2 + 4)
  94.   term.setBackgroundColor(colors.orange)
  95.   if menuOption == 2 then
  96.     term.setBackgroundColor(colors.lightGray)
  97.   end
  98.   btnExitX = printCenter("[ EXIT ]", h / 2 + 5)
  99.   term.setBackgroundColor(colors.orange)
  100.   else
  101.     if menuOption == 0 then
  102.       term.setBackgroundColor(colors.lightGray)
  103.     end
  104.     btnSendX = printCenter("[ SIGN IN ]", h / 2 + 3)
  105.     term.setBackgroundColor(colors.orange)
  106.     if menuOption == 1 then
  107.       term.setBackgroundColor(colors.lightGray)
  108.     end
  109.     btnReadX = printCenter("[ SIGN UP ]", h / 2 + 4)
  110.     term.setBackgroundColor(colors.orange)
  111.     if menuOption == 2 then
  112.       term.setBackgroundColor(colors.lightGray)
  113.     end
  114.     btnReadX = printCenter("[  EXIT   ]", h / 2 + 5)
  115.     term.setBackgroundColor(colors.orange)
  116.   end
  117. end
  118.  
  119. function drawLabel()
  120.   term.setBackgroundColor(colors.lightGray)
  121.   term.setCursorPos(1, 1)
  122.   term.clearLine()
  123.   printCenter("CRAFTMAIL CLIENT", 1)
  124.   term.setBackgroundColor(colors.red)
  125.   term.setTextColor(colors.black)
  126.   term.setCursorPos(w,1)
  127.   write("X")
  128. end
  129.  
  130. function drawState(id)
  131.   if id == 2 then
  132.     term.setBackgroundColor(colors.orange)
  133.     term.setTextColor(colors.black)
  134.     term.setCursorPos(w / 2 - 5, 6)
  135.     write("RECIPIENT:")
  136.     paintutils.drawFilledBox(w / 2 - 5, 7, w / 2 + 5, 7, colors.lightGray)
  137.     term.setCursorPos(w / 2 - 5, 8)
  138.     term.setBackgroundColor(colors.orange)
  139.     write("SUBJECT:")
  140.     paintutils.drawFilledBox(w / 2 - 10, 9, w / 2 + 10, 9, colors.lightGray)
  141.     term.setCursorPos(1, 10)
  142.     term.setBackgroundColor(colors.orange)
  143.     write("MESSAGE (MULTILINE NOT SUPPORTED):")
  144.     term.setCursorPos(1, 11)
  145.   elseif id == 1 then
  146.     drawLogo()
  147.     paintutils.drawFilledBox(w / 2 - 5, h - 6, w / 2 + 5, h - 6, colors.lightGray)
  148.   elseif id == -1 then
  149.     drawLogo()
  150.   elseif id == 3 then
  151.     line = 3
  152.     term.setBackgroundColor(colors.orange)
  153.     term.setTextColor(colors.black)
  154.     term.setCursorPos(1, 1)
  155.     term.clearLine()
  156.     write("BACK - BACKSPACE | SCROLL = LEFT & RIGHT ARROWS")
  157.     term.setBackgroundColor(colors.lightGray)
  158.     for i = readPage * 8 - 8, readPage * 8, 1 do
  159.       local v = mails[i + 1]
  160.       if v ~= nil then
  161.         term.setCursorPos(1, line)
  162.         term.clearLine()
  163.         print(v[2] .. " - " .. string.sub(v[5], 0, w / 2))
  164.         line = line + 1
  165.       end
  166.     end
  167.     term.setBackgroundColor(colors.orange)
  168.     term.setCursorPos(1, h)
  169.     write(readPage .. "/" .. math.floor(table.getn(mails) / 8) + 1)
  170.   elseif id == 4 then
  171.     local v = mails[selectedMail + 1]
  172.     term.setCursorPos(1, 1)
  173.     term.setBackgroundColor(colors.orange)
  174.     term.setTextColor(colors.black)
  175.     print("Press BACKSPACE to leave.");
  176.     print("Mail ID: " .. v[1])
  177.     print("From: " .. v[2])
  178.     print()
  179.     print(v[4])
  180.     print(string.sub("----------------------------------------------------------------------------", 0, w))
  181.     print(v[5])
  182.   end
  183. end
  184.  
  185. function draw()
  186.   drawBackground()
  187.   if state == 0 then
  188.     drawMenu()
  189.   else
  190.     drawState(state)
  191.   end
  192. end
  193.  
  194. drawBackground()
  195. drawMenu()
  196.  
  197. while true do
  198.   local event, param1, param2, param3 = os.pullEvent()
  199.   if event == "key" then
  200.     if state == 0 then
  201.       if param1 == keys.down then
  202.         if menuOption == 0 then
  203.           menuOption = 1
  204.         elseif menuOption == 1 then
  205.           menuOption = 2
  206.         end
  207.         drawMenu()
  208.       elseif param1 == keys.up then
  209.         if menuOption == 2 then
  210.           menuOption = 1
  211.         elseif menuOption == 1 then
  212.           menuOption = 0
  213.         end
  214.         drawMenu()
  215.       elseif param1 == keys.enter then
  216.         if menuOption == 2 then
  217.           break
  218.         end
  219.         if password ~= nil then
  220.           if menuOption == 0 then
  221.             state = 2
  222.             draw()
  223.             term.setCursorPos(w / 2 - 5, 7)
  224.             term.setBackgroundColor(colors.lightGray)
  225.             local recipient = limitRead(10, false)
  226.             term.setCursorPos(w / 2 - 10, 9)
  227.             local subject = limitRead(20, true)
  228.             term.setCursorPos(1, 11)
  229.             term.setBackgroundColor(colors.orange)
  230.             local message = limitRead((h - 11) * w, true)
  231.             term.setBackgroundColor(colors.orange)
  232.             term.clear()
  233.             printCenter("Accessing remote database", h / 2)
  234.             local page = http.get("http://midnightasapi.vapr.cc/?request=mail&username=" .. username .. "&password=" .. textutils.urlEncode(password) .. "&recipient=" .. recipient .. "&subject=" .. textutils.urlEncode(subject) .. "&message=" .. textutils.urlEncode(message))
  235.             local pageContent = page.readAll()
  236.             if string.find(pageContent, "1") then
  237.                 printCenter("Mail sent", h / 2 + 1)
  238.             elseif string.find(pageContent, "0") then
  239.                 printCenter("Error found", h / 2 + 1)
  240.             end
  241.             printCenter("Press any key to continue", h / 2 + 2)
  242.             os.pullEvent("key")
  243.             state = 0
  244.             draw()
  245.           elseif menuOption == 1 then
  246.             term.setBackgroundColor(colors.orange)
  247.             term.clear()
  248.             printCenter("Accessing remote database", h / 2)
  249.             local page = http.get("http://midnightasapi.vapr.cc/?request=read&username=" .. username .. "&password=" .. textutils.urlEncode(password))
  250.             mails = {}
  251.             local obj = json.decode(page.readAll())
  252.             for k in pairs(obj) do
  253.               local v = obj[k]
  254.               table.insert(mails, obj[k])
  255.             end
  256.             printCenter("Request successful", h / 2 + 1)
  257.             printCenter("Press any key to continue", h / 2 + 2)
  258.             os.pullEvent("key")
  259.             state = 3
  260.             draw()
  261.           end
  262.         else
  263.           if menuOption == 0 then
  264.             state = 1
  265.             draw()
  266.             term.setCursorPos(w / 2 - 5, h - 7)
  267.             term.setBackgroundColor(colors.orange)
  268.             term.setTextColor(colors.black)
  269.             write("USERNAME:")
  270.             term.setBackgroundColor(colors.lightGray)
  271.             term.setCursorPos(w / 2 - 5, h - 6)
  272.             local newUsername = limitRead(10, false)
  273.             term.setBackgroundColor(colors.orange)
  274.             term.setCursorPos(w / 2 - 5, h - 5)
  275.             write("PASSWORD:")
  276.             paintutils.drawFilledBox(w / 2 - 5, h - 4, w / 2 + 5, h - 4, colors.lightGray)
  277.             term.setBackgroundColor(colors.lightGray)
  278.             term.setCursorPos(w / 2 - 5, h - 4)
  279.             newPassword = limitRead(10, true, "*")
  280.             term.setBackgroundColor(colors.orange)
  281.             term.clear()
  282.             printCenter("Accessing remote database", h / 2)
  283.             local page = http.get("http://midnightasapi.vapr.cc/?request=login&username=" .. newUsername .. "&password=" .. textutils.urlEncode(newPassword))
  284.             local pageContent = page.readAll()
  285.             if string.find(pageContent, "1") then
  286.                 printCenter("Request successful", h / 2 + 1)
  287.                 username = newUsername
  288.                 password = newPassword
  289.             elseif string.find(pageContent, "0") then
  290.                 printCenter("Invalid credentials", h / 2 + 1)
  291.             end
  292.             printCenter("Press any key to continue", h / 2 + 2)
  293.             os.pullEvent("key")
  294.             state = 0
  295.             draw()
  296.           elseif menuOption == 1 then
  297.             state = -1
  298.             draw()
  299.             paintutils.drawFilledBox(w / 2 - 5, h - 6, w / 2 + 5, h - 6, colors.lightGray)
  300.             term.setBackgroundColor(colors.orange)
  301.             term.setTextColor(colors.black)
  302.             term.setCursorPos(w / 2 - 5, h - 7)
  303.             write("USERNAME:")
  304.             term.setBackgroundColor(colors.lightGray)
  305.             term.setCursorPos(w / 2 - 5, h - 6)
  306.             local newUsername = limitRead(10, false)
  307.             term.setBackgroundColor(colors.orange)
  308.             term.setCursorPos(w / 2 - 5, h - 5)
  309.             write("PASSWORD:")
  310.             paintutils.drawFilledBox(w / 2 - 5, h - 4, w / 2 + 5, h - 4, colors.lightGray)
  311.             term.setBackgroundColor(colors.lightGray)
  312.             term.setCursorPos(w / 2 - 5, h - 4)
  313.             local newPassword = limitRead(10, true, "*")
  314.             term.setBackgroundColor(colors.orange)
  315.             term.clear()
  316.             printCenter("Accessing remote database", h / 2)
  317.             local page = http.get("http://midnightasapi.vapr.cc/?request=register&username=" .. newUsername .. "&password=" .. textutils.urlEncode(newPassword))
  318.             local pageContent = page.readAll()
  319.             if string.find(pageContent, "1") then
  320.                 printCenter("Registration successful", h / 2 + 1)
  321.             elseif string.find(pageContent, "0") then
  322.                 printCenter("Username already is registered", h / 2 + 1)
  323.             elseif string.find(pageContent, "2") then
  324.                 printCenter("Using hacked client, cancelling registration.", h / 2 + 1)
  325.             end
  326.             printCenter("Press any key to continue", h / 2 + 2)
  327.             os.pullEvent("key")
  328.             state = 0
  329.             draw()
  330.           end
  331.         end
  332.     end
  333.     elseif state == 3 then
  334.       if param1 == keys.backspace then
  335.         state = 0
  336.         draw()
  337.       elseif param1 == keys.left then
  338.         if readPage > 1 then
  339.           readPage = readPage - 1
  340.         end
  341.       elseif param1 == keys.right then
  342.         if readPage < math.floor(table.getn(mails) / 8) + 1 then
  343.           readPage = readPage + 1
  344.         end
  345.       end
  346.     elseif state == 4 then
  347.       if param1 == keys.backspace then
  348.         state = 3
  349.         draw()
  350.       elseif param1 == keys.delete then
  351.         term.setBackgroundColor(colors.orange)
  352.         term.clear()
  353.         printCenter("Accessing remote database", h / 2)
  354.         local page = http.get("http://midnightasapi.vapr.cc/?request=deletemail&username=" .. username .. "&password=" .. textutils.urlEncode(password) .. "&id=" .. mails[selectedMail + 1][1])
  355.         local pageContent = page.readAll()
  356.         if string.find(pageContent, "1") then
  357.           printCenter("Mail deleted", h / 2 + 1)
  358.         elseif string.find(pageContent, "0") then
  359.           printCenter("Failed to delete mail.", h / 2 + 1)
  360.         end
  361.         printCenter("Press any key to continue", h / 2 + 2)
  362.         os.pullEvent("key")
  363.         state = 0
  364.         draw()
  365.       end
  366.     end
  367.   elseif event == "mouse_click" then
  368.     if state == 3 then
  369.       if param3 >= 3 and param3 <= 11 then
  370.         selectedMail = (8 * readPage) - 8 + param3 - 3
  371.         if mails[selectedMail + 1] ~= nil then
  372.           state = 4
  373.           draw()
  374.         end
  375.       end
  376.     end
  377.   end
  378. end
  379. term.setBackgroundColor(colors.black)
  380. term.setTextColor(colors.yellow)
  381. term.clear()
  382. term.setCursorPos(1, 1)
  383. print("Thank you for using CraftMail service.")
  384. print("Project by Midnightas")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement