Advertisement
Guest User

CCEmail Real Email

a guest
Nov 13th, 2012
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.47 KB | None | 0 0
  1. --[[ TODO LIST!
  2.  
  3. GET INBOX
  4.  
  5. --]]
  6.  
  7.  
  8.  
  9.  
  10.  
  11. os.loadAPI("apis/StrUtils")
  12. print("CCEmail client Alpha 1.0 by Jasonfran")
  13.  
  14. function clear()
  15.     term.clear()
  16.     term.setCursorPos(1,1)
  17. end
  18.  
  19. function main()
  20.  
  21.     login = false
  22.     Register = false
  23.     loggedin = false
  24.     clear()
  25.     print("What would you like to do, register, login or exit")
  26.     write("Command> ")
  27.     command = string.gsub(string.lower(read()), "%s+", "")
  28.     if command == "register" then
  29.         loggedin = false
  30.         login = false
  31.         Register = true
  32.         drawRegister()
  33.     elseif command == "login" then
  34.         Register = false
  35.         loggedin = false
  36.         login = true
  37.         drawLogin()
  38.     elseif command == "exit" then
  39.         return
  40.     else
  41.         clear()
  42.         print("Command not recognised")
  43.         sleep(2)
  44.         main()
  45.     end
  46. end
  47.  
  48.  
  49. function sendMail(username, message)
  50.     http.request("http://computercraftemail.tk/Send.php", "message=" .. message .. "&username=" .. string.lower(username) .."&" .."password=" .. password2 .. "&loginname=" .. loginname)
  51.  
  52.     local requesting = true
  53.  
  54.     while requesting do
  55.  
  56.         local event, url, sourceText = os.pullEvent()
  57.  
  58.         if event == "http_success" then
  59.  
  60.             local respondedText = sourceText.readLine(1)
  61.             local respondedText2 = sourceText.readLine(2)
  62.             if respondedText == "Username not found." then
  63.                 term.setTextColor(colors.red)
  64.                 print("User not found. Maybe the user hasn't registered yet")
  65.                 term.setTextColor(colors.white)
  66.             elseif respondedText == "Could not connect." then
  67.                 term.setTextColor(colors.red)
  68.                 print("Something went wrong with the Mysql database. This could just be a one off error but if it continues to happen contact Jasonfran on the computercraft forums.")
  69.                 term.setTextColor(colors.white)
  70.             else
  71.                 term.setTextColor(colors.lime)
  72.                 print("Message sent!\n" .. respondedText .. "\n\n" .. respondedText2)
  73.                 term.setTextColor(colors.white)
  74.             end
  75.             requesting = false
  76.  
  77.         elseif event == "http_failure" then
  78.  
  79.             print("Server didn't respond.")
  80.  
  81.             requesting = false
  82.  
  83.         end
  84.     end
  85. end
  86.  
  87. function checkLogin(username, password)
  88.  
  89. http.request("http://computercraftemail.tk/Checklogin.php", "username=" .. username .. "&password=" .. password )
  90.  
  91. local requesting2 = true
  92.  
  93.     while requesting2 do
  94.  
  95.         local event, url, sourceText = os.pullEvent()
  96.  
  97.         if event == "http_success" then
  98.  
  99.             local respondedText = sourceText.readLine(1)
  100.             local respondedText2 = sourceText.readLine(2)
  101.             if respondedText == "Login failed" then
  102.                 term.setTextColor(colors.red)
  103.                 print("Username or password incorrect")
  104.                 term.setTextColor(colors.white)
  105.                 sleep(2)
  106.                 loggedin = false
  107.                 login = false
  108.                 Register = false
  109.                 loggedin = false
  110.                 main()
  111.             elseif respondedText == "Login successful" then
  112.                 term.setTextColor(colors.lime)
  113.                 print("Login successful!")
  114.                 term.setTextColor(colors.white)
  115.                 sleep(2)
  116.                 clear()
  117.                 loginMain()
  118.             else
  119.                 term.setTextColor(colors.red)
  120.                 print("An error has occured...")
  121.                 sleep(2)
  122.                 term.setTextColor(colors.white)
  123.             end
  124.             requesting2 = false
  125.  
  126.         elseif event == "http_failure" then
  127.  
  128.             print("Server didn't respond.")
  129.  
  130.             requesting2 = false
  131.  
  132.         end
  133.     end
  134. end
  135. function sendMessageForm()
  136.    
  137.     login = false
  138.     Register = false
  139.     loggedin = true
  140.     clear()
  141.     print("You are now logged in. You can send messages to different users.")
  142.     write("Username: ")
  143.     term.setTextColor(colors.red)
  144.     username = string.lower(read())
  145.     term.setTextColor(colors.white)
  146.     write("Message: ")
  147.     term.setTextColor(colors.red)
  148.     message = read()
  149.     term.setTextColor(colors.white)
  150.     clear()
  151.     if username == "" and message == "" then
  152.         term.setTextColor(colors.red)
  153.         print("ERROR: No post data!")
  154.         term.setTextColor(colors.white)
  155.         sleep(3)
  156.         loginMain()
  157.     else
  158.         textutils.slowPrint("Sending...")
  159.         sendMail(username,message)
  160.         sleep(3)
  161.         loginMain()
  162.     end
  163. end
  164.  
  165.  
  166. function register(username, password)
  167.  
  168. http.request("http://computercraftemail.tk/Register.php", "username=" .. username .. "&" .."password=" .. password)
  169.  
  170. local requesting3 = true
  171.  
  172.     while requesting3 do
  173.  
  174.         local event, url, sourceText = os.pullEvent()
  175.  
  176.         if event == "http_success" then
  177.  
  178.             local respondedText = sourceText.readLine(1)
  179.             local respondedText2 = sourceText.readLine(2)
  180.             if respondedText == "Registration failed" then
  181.                 term.setTextColor(colors.red)
  182.                 print("Username exists. Or a rare error occured")
  183.                 sleep(2)
  184.                 term.setTextColor(colors.white)
  185.                 loggedin = false
  186.                 Register = false
  187.                 login = false
  188.  
  189.                 main()
  190.             elseif repsondedText == "Contains space" then
  191.                 term.setTextColor(colors.red)
  192.                 print("Username cannot contain a space")
  193.                 sleep(2)
  194.                 term.setTextColor(colors.white)
  195.            
  196.             elseif respondedText == "Registration success" then
  197.                 term.setTextColor(colors.lime)
  198.                 print("Registration successful! You may now log in")
  199.                 term.setTextColor(colors.white)
  200.                 sleep(2)
  201.                 clear()
  202.                 Register = false
  203.                 loggedin = false
  204.                 drawLogin()
  205.             else
  206.                 term.setTextColor(colors.red)
  207.                 print("An error has occured... Or your username contains a space")
  208.                 sleep(2)
  209.                 term.setTextColor(colors.white)
  210.                 main()
  211.             end
  212.             requesting3 = false
  213.  
  214.         elseif event == "http_failure" then
  215.  
  216.             print("Server didn't respond.")
  217.  
  218.             requesting3 = false
  219.  
  220.         end
  221.     end
  222. end
  223.  
  224.  
  225. function printCentered(sText)
  226.     local w, h = term.getSize()
  227.     local x, y = term.getCursorPos()
  228.     x = math.max(math.floor((w / 2) - (#sText / 2)), 0)
  229.     term.setCursorPos(x, y)
  230.     print(sText)
  231. end
  232. function printCenteredY(sText, Y)
  233.     local w, h = term.getSize()
  234.     local x, y = term.getCursorPos()
  235.     x = math.max(math.floor((w / 2) - (#sText / 2)), 0)
  236.     term.setCursorPos(x, Y)
  237.     print(sText)
  238. end
  239. function writeCenteredY(sText, Y)
  240.     local w, h = term.getSize()
  241.     local x, y = term.getCursorPos()
  242.     x = math.max(math.floor((w / 2) - (#sText / 2)), 0)
  243.     term.setCursorPos(x, Y)
  244.     write(sText)
  245. end
  246.  
  247. function printBottomRight(text)
  248.   local w,h = term.getSize()
  249.   term.setCursorPos(w - #text, h)
  250.   write(text)
  251. end
  252.  
  253.  
  254. ------------LOG IN FORM----------
  255.  
  256. function drawLogin()
  257.     login = true
  258.     Register = false
  259.     loggedin = false
  260.     local w, h = term.getSize()
  261.     clear()
  262.     term.setCursorPos(1,1)
  263.     printCentered("Please log in to continue...")
  264.     term.setCursorPos(1,4)
  265.     write("Username: ")
  266.     username = read()
  267.     term.setCursorPos(1,6)
  268.     write("Password: ")
  269.     password = StrUtils.SHA1(read("*"))
  270.     clear()
  271.     textutils.slowPrint("Logging in...")
  272.     loginname = username
  273.     password2 = password
  274.     checkLogin(username,password)
  275. end
  276. function pressSpace()
  277. print("Press space to continue")
  278.     while true do
  279.         local sEvent, param = os.pullEvent("key")
  280.         if sEvent == "key" then
  281.             if param == 57 then
  282.                 break
  283.             end
  284.         end
  285.     end
  286. end
  287. function getInbox(username, password)
  288.     http.request("http://computercraftemail.tk/Inbox.php", "username=" .. string.lower(loginname) .. "&password=" .. password2)
  289.  
  290.     local requesting = true
  291.  
  292.     while requesting do
  293.  
  294.         local event, url, sourceText = os.pullEvent()
  295.  
  296.         if event == "http_success" then
  297.             local respondedText = sourceText.readLine(1)
  298.            
  299.             if respondedText == "Login failed" then
  300.                 term.setTextColor(colors.red)
  301.                 print("Something went wrong with the login")
  302.                 term.setTextColor(colors.white)
  303.                 loginMain()
  304.             elseif respondedText == "No messages" then
  305.                 term.setTextColor(colors.red)
  306.                 print("Your inbox is empty!")
  307.                 term.setTextColor(colors.white)
  308.                 sleep(1)
  309.                 pressSpace()
  310.                 loginMain()
  311.                
  312.             else
  313.                 term.setTextColor(colors.lime)
  314.                 amount = respondedText
  315.                 if repsondedText == "No messages" then
  316.                     term.setTextColor(colors.red)
  317.                     print("Your inbox is empty!")
  318.                     term.setTextColor(colors.white)
  319.                     sleep(1)
  320.                     pressSpace()
  321.                     loginMain()
  322.                 else
  323.                     amount2 = tonumber(respondedText)
  324.                     --SORT THIS OUT
  325.                         for i = 1, amount2 do
  326.                             print(sourceText.readLine(i))
  327.                         end
  328.                     term.setTextColor(colors.white)
  329.                     pressSpace()
  330.                     manageInbox()
  331.                 end
  332.             end
  333.             requesting = false
  334.  
  335.         elseif event == "http_failure" then
  336.  
  337.             print("Server didn't respond.")
  338.  
  339.             requesting = false
  340.  
  341.         end
  342.     end
  343. end
  344.  
  345. function deleteQuery(username, password)
  346.     http.request("http://computercraftemail.tk/Delete.php", "username=" .. string.lower(loginname) .. "&password=" .. password2)
  347.  
  348.     local requesting = true
  349.  
  350.     while requesting do
  351.  
  352.         local event, url, sourceText = os.pullEvent()
  353.  
  354.         if event == "http_success" then
  355.             local respondedText = sourceText.readLine(1)
  356.            
  357.             if respondedText == "Success" then
  358.                 term.setTextColor(colors.red)
  359.                 print("Inbox cleared")
  360.                 term.setTextColor(colors.white)
  361.                 sleep(2)
  362.                 loginMain()
  363.             elseif respondedText == "No messages to clear" then
  364.            
  365.             term.setTextColor(colors.red)
  366.                 print("There was no messages to clear. How did you even get to this menu?")
  367.                 term.setTextColor(colors.white)
  368.                 sleep(2)
  369.                 loginMain()
  370.                
  371.             else
  372.                 term.setTextColor(colors.red)
  373.                 print("Something went wrong with the query")
  374.                 term.setTextColor(colors.white)
  375.                 sleep(2)
  376.                 manageInbox()
  377.             end
  378.             requesting = false
  379.  
  380.         elseif event == "http_failure" then
  381.  
  382.             print("Server didn't respond.")
  383.  
  384.             requesting = false
  385.  
  386.         end
  387.     end
  388. end
  389.  
  390. function drawRegister()
  391.     login = false
  392.     Register = true
  393.     loggedin = false
  394.     local w, h = term.getSize()
  395.     clear()
  396.     term.setCursorPos(1,1)
  397.     printCentered("Registration form")
  398.     print("Usernames containing a space will have them removed")
  399.     term.setCursorPos(1,5)
  400.     write("Username: ")
  401.     username = string.gsub(read(), "%s+", "")
  402.     term.setCursorPos(1,7)
  403.     write("Password: ")
  404.     password = StrUtils.SHA1(read("*"))
  405.     term.setCursorPos(1,9)
  406.     write("Retype password: ")
  407.     retype = StrUtils.SHA1(read("*"))
  408.     clear()
  409.     if retype == password then
  410.     textutils.slowPrint("Registering...")
  411.     register(username,password)
  412.     else
  413.         clear()
  414.         print("The 2 passwords do not match!")
  415.         sleep(2)
  416.         loggedin = false
  417.         login = false
  418.         register = false
  419.         main()
  420.     end
  421.    
  422. end
  423.  
  424. function loginMain()
  425.  
  426.     login = false
  427.     Register = false
  428.     loggedin = false
  429.     clear()
  430.     print("What would you like to do, send, inbox or logout")
  431.     write("Command> ")
  432.     command = string.gsub(string.lower(read()), "%s+", "")
  433.     if command == "send" then
  434.         sendMessageForm()
  435.     elseif command == "inbox" then
  436.         getInbox(loginname, password2)
  437.     elseif command == "logout" then
  438.    
  439.         main()
  440.     else
  441.         clear()
  442.         print("Command not recognised")
  443.         sleep(2)
  444.         loginMain()
  445.     end
  446. end
  447.  
  448. function manageInbox()
  449.     print("What would you like to do, clear or exit")
  450.     write("Command> ")
  451.     command = string.gsub(string.lower(read()), "%s+", "")
  452.     if command == "clear" then
  453.         deleteQuery(loginname, password2)
  454.     elseif command == "exit" then
  455.         loginMain()
  456.     else
  457.         clear()
  458.         print("Command not recognised")
  459.         sleep(2)
  460.         manageInbox()
  461.     end
  462. end
  463.  
  464. ------------------------------END OF FUNCTIONS-----------------------------------
  465. ------------------------------END OF FUNCTIONS-----------------------------------
  466. ------------------------------END OF FUNCTIONS-----------------------------------
  467. ------------------------------END OF FUNCTIONS-----------------------------------
  468.  
  469. loggedin = false
  470. Register = false
  471. login = false
  472.  
  473. if loggedin == true and login == false and Register == false then
  474.     sendMessageForm()
  475. end
  476.  
  477. main()
  478.  
  479. --Log in form
  480. if loggedin == false and Register == false and login == true then
  481.     drawLogin()
  482. end
  483.  
  484. if loggedin == false and login == false and Register == true then
  485.     drawRegister()
  486. end
  487.  
  488. --Create message form
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement