Advertisement
Redxone

Kosy Mail Client Updated!

Jul 4th, 2015
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.36 KB | None | 0 0
  1. --]] Created By: Redxone
  2. --]] MC Ign: trainerred2000
  3.  
  4. local mailpath = "cozy/programs/email/"
  5. local mailList = {}
  6. local bgcolor = colors.white
  7. local inEmail = false
  8. local mType = "inbox"
  9. local opMail = ""
  10. local mailSender= ""
  11. local id = os.getComputerID()
  12. local accMngr = true
  13. local username = ""
  14. foundMail = false
  15. local inInbox=false
  16. local dodyPar = {}
  17. local inbody = false
  18. local bt_str = ""
  19. local w,h = term.getSize()
  20. local parnum = 0
  21.  
  22. function fadeScreen(delay)
  23.  
  24.     local cols = {colors.black,colors.gray,colors.lightGray,colors.white};
  25.  
  26.     for i = 1, #cols do
  27.  
  28.         term.setBackgroundColor(cols[i])
  29.         sleep(delay)
  30.         term.clear()
  31.  
  32.     end
  33.  
  34. end
  35.  
  36.  
  37. os.loadAPI("ea")
  38. os.loadAPI("cozy_string")
  39.  
  40.  
  41. local sides = {"up","down","left","right","bottom","back"}
  42. for i = 1, #sides do if(peripheral.isPresent(sides[i]) and peripheral.getType(sides[i]) == "modem")then rednet.open(sides[i]) end end
  43.  
  44.  
  45. function start()
  46.  
  47.     ea.stc(colors.white)
  48.     ea.sbc(colors.white)
  49.     term.clear()
  50.     ea.scp(1,1)
  51.     ea.sbc(colors.blue)
  52.     term.clearLine()
  53.     ea.scp(1,2)
  54.     term.clearLine()
  55.     ea.scp(1,3)
  56.     term.clearLine()
  57.        
  58.     ea.scp(1,1)
  59.     print("Kosy Mail Checking For Updates...")
  60.  
  61.     ea.scp(2,10)
  62.     ea.sbc(colors.white)
  63.     ea.stc(colors.green)
  64.  
  65.     shell.run("pastebin","get","516EUVX2",".temp")
  66.  
  67.     print("Reading...")
  68.     f = fs.open(".temp","r")
  69.     con = f.readAll()
  70.     f.close()
  71.  
  72.     fs.delete(".temp")
  73.  
  74.     print("Comparing Code...")
  75.     f = fs.open(shell.getRunningProgram(),"r")
  76.     ccon = f.readAll()
  77.     f.close()
  78.  
  79.     if(con == ccon)then print("KosyMail Is Up To Date :)") accountMngr() else print("KosyMail Update Available!")
  80.  
  81.         print("Update? (Y) or (N)")
  82.         s = {os.pullEvent("key")}
  83.         if(s[2] == keys.y)then
  84.             fs.delete(shell.getRunningProgram())
  85.             shell.run("pastebin","get","516EUVX2",shell.getRunningProgram())
  86.             print("File Received, Restarting Kosy Mail..")
  87.             sleep(2)
  88.             shell.run(shell.getRunningProgram())
  89.         else
  90.             print("Update Canceled..")
  91.             sleep(2)
  92.             accountMngr()
  93.         end
  94.     end
  95.  
  96. end
  97.  
  98. function accountMngr()
  99.  
  100.     ea.stc(colors.white)
  101.     ea.sbc(colors.white)
  102.     term.clear()
  103.     ea.scp(1,1)
  104.     ea.sbc(colors.blue)
  105.     term.clearLine()
  106.     ea.scp(1,2)
  107.     term.clearLine()
  108.     ea.scp(1,3)
  109.     term.clearLine()
  110.        
  111.     ea.scp(1,1)
  112.     print("Kosy Mail Login Options")
  113.  
  114.     ea.scp(2,10)
  115.     ea.sbc(colors.white)
  116.     ea.stc(colors.green)
  117.     print("<- Or -> Arrow Keys To Select Enter To Continue")
  118.  
  119.     slop = 1
  120.  
  121.     local accOp = {
  122.  
  123.         ["    Create User     "] = {offset = 1,x = 3, y = 18, bcol = colors.gray, scol = colors.blue, action = function() accMngr = false; createUser() end};
  124.         ["      Sign In       "] = {offset = 2,x = 30, y = 18, bcol = colors.gray, scol = colors.blue,action = function() accMngr = false; getUser() end};
  125.     }
  126.  
  127.  
  128.     function prntAcc()
  129.     ea.stc(colors.white)
  130.     for k , v in pairs(accOp) do
  131.  
  132.         ea.scp(v.x,v.y)
  133.         if(v.offset == slop)then
  134.             ea.sbc(v.scol)
  135.         else
  136.             ea.sbc(v.bcol)
  137.         end
  138.  
  139.         print(k)
  140.     end
  141.     end
  142.  
  143.     function updateAcc()
  144.  
  145.         a = {os.pullEvent("key")}
  146.  
  147.         if(a[2] == keys.left and slop > 1 )then slop = slop - 1 end
  148.         if(a[2] == keys.right and slop <= 1 )then slop = slop + 1 end
  149.         if(a[2] == keys.enter)then for k, v in pairs(accOp) do if(v.offset == slop)then v.action() end end end
  150.     end
  151.  
  152.     while not inEmail and accMngr do
  153.  
  154.  
  155.         prntAcc()
  156.         updateAcc()
  157.  
  158.     end
  159.  
  160. end
  161.  
  162.  
  163. function clearLineLength(x,y,l)
  164.  
  165.     ea.scp(x,y)
  166.     ea.sbc(colors.gray)
  167.     print("                                ")
  168.  
  169. end
  170.  
  171. function getUser()
  172.  
  173.  
  174.     ea.stc(colors.white)
  175.     ea.sbc(colors.white)
  176.     term.clear()
  177.     ea.scp(1,1)
  178.     ea.sbc(colors.blue)
  179.     term.clearLine()
  180.     ea.scp(1,2)
  181.     term.clearLine()
  182.     ea.scp(1,3)
  183.     term.clearLine()
  184.        
  185.     ea.scp(1,1)
  186.     print("Kosy Mail - Login")
  187.  
  188.     ea.scp(1,10)
  189.     ea.stc(colors.black)
  190.     ea.sbc(colors.white)
  191.     print("Username: ")
  192.     print("Password: ")
  193.     ea.scp(10,10)
  194.     us = read()
  195.     ea.scp(10,11)
  196.     up = read("*")
  197.  
  198.     rednet.broadcast("COZY_GETUSER")
  199.     local id, msg = rednet.receive(2)
  200.     if(msg == nil)then error("Connection to server failed!") end
  201.     rednet.send(id,us)
  202.     local id, valid = rednet.receive()
  203.  
  204.     if(valid)then
  205.         local id, cpass = rednet.receive()
  206.         if(up == cpass)then corpass=true else corpass = false end
  207.     end
  208.    
  209.  
  210.     if(valid and corpass)then fadeScreen(0.2) inEmail = true; username = us; updateEmailFiles(); clear(); CMail() end
  211.     if(not valid)then ea.sp("Username Invalid") getUser() end
  212.     if(not corpass)then ea.sp("Password Invalid") getUser() end
  213.  
  214. end
  215.  
  216.  
  217. function createUser()
  218.  
  219.     ea.stc(colors.white)
  220.     ea.sbc(colors.white)
  221.     term.clear()
  222.     ea.scp(1,1)
  223.     ea.sbc(colors.blue)
  224.     term.clearLine()
  225.     ea.scp(1,2)
  226.     term.clearLine()
  227.     ea.scp(1,3)
  228.     term.clearLine()
  229.        
  230.     ea.scp(1,1)
  231.     print("Kosy Mail - Create Login")
  232.  
  233.     ea.scp(1,10)
  234.     ea.stc(colors.black)
  235.     ea.sbc(colors.white)
  236.     print("Username: ")
  237.     print("Password: ")
  238.     print("Repeat Password: ")
  239.     ea.scp(10,10)
  240.     us = read()
  241.     ea.scp(10,11)
  242.     up = read()
  243.     ea.scp(18,12)
  244.     up2=read()
  245.  
  246.     if(up ~= up2)then ea.sp("Password's Do Not Match!") createUser() end
  247.    
  248.     rednet.broadcast("COZY_CREATEUSER")
  249.     local id, got = rednet.receive(2)
  250.  
  251.     if(got ~= nil)then
  252.         rednet.send(tonumber(id),us)
  253.         local id, valid = rednet.receive()
  254.  
  255.             if(valid)then
  256.                 rednet.send(tonumber(id),up)
  257.                 ea.sp("User Created!")
  258.                
  259.                 f = fs.open("cozy/programs/email/login/.user","w")
  260.                 f.writeLine("--]] Has Login [[--")
  261.                 f.writeLine("true")
  262.                 f.close()
  263.                
  264.                 accMngr = true
  265.                 accountMngr()
  266.             else
  267.                  ea.sp("Username Unavailable!")
  268.             end
  269.  
  270.     else
  271.         error("Server Connection Failed!")
  272.     end
  273.  
  274.  
  275.  
  276.  
  277. end
  278.  
  279.  
  280.  
  281. function clear()
  282.  
  283.     ea.sbc(bgcolor);
  284.     term.clear();
  285.     ea.scp(1,1)
  286.     ea.sbc(colors.blue)
  287.     term.clearLine()
  288.     ea.scp(1,1)
  289.     ea.stc(colors.white)
  290.     print("COZYMAIL")
  291.     ea.scp(w-((#username)+2), 1)
  292.     print("["..username.."]")
  293.  
  294.     ea.sbc(colors.blue)
  295.     for i = 3, 15 do
  296.  
  297.         ea.scp(1,i)
  298.         print(string.rep(" ",12))
  299.  
  300.     end
  301.  
  302.     ea.sbc(colors.gray)
  303.     for i = 3, 17 do
  304.         ea.scp(15,i)
  305.         print(string.rep(" ",36))
  306.  
  307.     end
  308.     ea.scp(15,3)
  309.     ea.stc(colors.white)
  310.     print("INBOX")
  311.     ea.scp(15,4)
  312.  
  313.  
  314.     ea.scp(1,3)
  315.     ea.stc(colors.white)
  316.     ea.sbc(colors.blue)
  317.     print("          ")
  318.     print(" Inbox    ")
  319.     print(" Swap User")
  320.     print(" Compose  ")
  321.     --print("|NO[Block]|")
  322.     --print("|NO[Chat] |")
  323.     --print("|---------|")
  324.  
  325. end
  326.  
  327.  
  328. function printEmail(type)
  329.  
  330.     for k,v in pairs(mailList) do      
  331.  
  332.         if(v.type == type)then
  333.  
  334.             ea.sbc(colors.gray)
  335.             ea.scp(15,v.offset+4)
  336.  
  337.             if(v.sender == nil or v.disc == nil)then ea.stc(colors.red); print("CORRUPED EMAIL") else ea.stc(colors.white); print(v.sender.." - "..v.disc) end
  338.         end
  339.  
  340.     end
  341.  
  342. end
  343.  
  344. function openMail(mail)
  345.  
  346.     i = 0
  347.     xod = 0
  348.         clear()
  349.         ea.scp(15,4)
  350.         ea.scp(15,17)
  351.         ea.sbc(colors.gray)
  352.         print(" REPLY ")
  353.         ea.stc(colors.red)
  354.         ea.scp(43,17)
  355.         print(" DELETE ")
  356.         ea.stc(colors.white)
  357.         ea.sbc(colors.gray)
  358.         f = io.open(mail,"r")
  359.  
  360.         for line in f:lines() do
  361.             ea.scp(15+xod,i+5)
  362.             if(i == 0)then print("Sender: "..line) end
  363.             if(i == 1)then print("Subject: "..line) end
  364.             if(i ~= 0 and i ~= 1)then print(line) end
  365.             if(i >= 2)then xod = 1 else xod = 0 end
  366.             i = i + 1
  367.         end
  368.  
  369.         ea.scp(15,7)
  370.         print("Body: ")
  371. end
  372.  
  373.  
  374. function updateEmail()
  375.  
  376.     if(inbody)then
  377.  
  378.             term.setCursorPos(15,parnum+8)
  379.    write(string.rep(" ",33))
  380.    term.setCursorPos(15,parnum+8)
  381.    --term.clearLine()
  382.             write(bt_str)
  383.    term.setCursorBlink(true)
  384.    
  385. end
  386.  
  387.  
  388.   m = {os.pullEvent()}
  389.     --]] Read Body Mngr [[ --
  390.  
  391. if(inbody)then
  392.             if(m[1] == "char")then
  393.                
  394.     if(#bt_str <= 32)then
  395.                     term.setCursorPos(15,#bodyPar+8)
  396.                     bt_str = bt_str .. m[2]
  397.                 else
  398.                     if(#bodyPar < 8)then table.insert(bodyPar,bt_str) term.setCursorPos(15,#bodyPar+8) bt_str = "" parnum = parnum + 1 end
  399.                 end
  400.        
  401.   end
  402.  
  403.             if(m[1] == "key")then
  404.                 if(m[2] == keys.backspace)then
  405.       if(parnum > 0 and #bt_str == 0)then
  406.         bt_str = bodyPar[parnum]
  407.         parnum = parnum - 1
  408.       end
  409.       bt_str = bt_str:sub(1,-2)
  410.     end -- returns string beginging until last letter before end [[--
  411.            
  412.     if(m[2] == keys.enter)then if(#bodyPar < 8)then table.insert(bodyPar,bt_str) bt_str = "" parnum = parnum + 1 end end
  413.             end
  414. end
  415.  
  416.  
  417.     if(m[1] == "mouse_click" and m[2] == 1)then
  418.  
  419.        
  420.  
  421.         if(m[3] >= 3 and m[3] <= 8 and m[4] == 4 and not inInbox)then
  422.            
  423.             --mType = "inbox"
  424.             ea.scp(1,4)
  425.             ea.sbc(colors.gray)
  426.             print(" Inbox    ")
  427.             inbody=false       
  428.             sleep(0.2)
  429.             clear()
  430.             ea.scp(15,4)
  431.             updateEmailFiles()
  432.             if(readingMail)then readingMail = false end
  433.             if(sending)then sending = false end
  434.             --inInbox=true
  435.         end
  436.  
  437.         if(m[3] >= 3 and m[3] <= 8 and m[4] == 5)then
  438.            
  439.             ea.scp(1,5)
  440.             ea.sbc(colors.gray)
  441.             print(" Swap User")
  442.             inbody=false           
  443.             sleep(0.2)
  444.             if(readingMail)then readingMail = false end
  445.             if(sending)then sending = false end
  446.             clear()
  447.             inEmail = false
  448.             accMngr = true
  449.             accountMngr()
  450.  
  451.         end
  452.  
  453.         if(m[3] >= 3 and m[3] <= 8 and m[4] == 6)then
  454.  
  455.             ea.scp(1,6)
  456.             ea.sbc(colors.gray)
  457.             print(" Compose  ")    
  458.             sleep(0.2)         
  459.             clear()
  460.             rcr=nil
  461.             rcsub=nil
  462.    inbody = false
  463.             bodyPar = {}
  464.             sendEmail()
  465.             t = 0;
  466.             sending=true
  467.    parnum = 0
  468.         end
  469.  
  470.         if(not readingMail and not sending)then
  471.                 for k,v in pairs(mailList) do
  472.  
  473.                            
  474.                         if(m[3] >= 15 and m[3] <= #v.sender+15 and m[4] == v.offset+4)then
  475.                             ea.sbc(colors.gray)
  476.                             readingMail = true
  477.                             print(v.path.." "..v.offset)
  478.  
  479.                                     openMail(v.path)
  480.                                     opMail = v.path
  481.                                     mailSender = v.sender
  482.                                     opName = k
  483.                                     mType = v.type
  484.  
  485.                         end
  486.  
  487.                 end
  488.  
  489.         else
  490.  
  491.             if(not sending)then
  492.  
  493.                 if(m[3] >= 16 and m[3] <= 21 and m[4] == 17)then
  494.                         clear()
  495.  
  496.                         rcr=mailSender
  497.                         sendEmail()
  498.                         sending=true
  499.                         readingMail=false
  500.                 end
  501.  
  502.            
  503.                 if(m[3] >= 43 and m[3] <= 50 and m[4] == 17)then
  504.                    
  505.                     rednet.broadcast("COZY_REMOVE")
  506.                     local id, m =rednet.receive(2)
  507.  
  508.                     if(m == nil)then error("Server Connect Failed!") end
  509.  
  510.                     rednet.send(tonumber(id),opName)
  511.                     rednet.send(tonumber(id),username)
  512.  
  513.                     if(mType == "inbox")then
  514.  
  515.                         clear()
  516.                         ea.scp(15,4)
  517.                         updateEmailFiles()
  518.       mybody = false
  519.       parnum = 0
  520.                         readingMail = false
  521.                     end
  522.  
  523.                     if(mType == "spam")then
  524.  
  525.                         clear()
  526.                         ea.scp(15,4)
  527.                         readingMail = false
  528.                     end
  529.  
  530.                 end
  531.             end
  532.  
  533.             if(sending and not readingMail)then
  534.  
  535.                     if(m[3] >= 16 and m[4] == 4)then
  536.                                
  537.                                 clearLineLength(19,4,15)
  538.                                 ea.scp(19,4)   
  539.                                 rcr=read()
  540.                     end
  541.  
  542.                     if(m[3] >= 16 and m[4] == 5)then
  543.                             clearLineLength(24,5,10)
  544.                             ea.scp(24,5)
  545.                             rcsub=read()
  546.                     end
  547.  
  548.                     if(m[3] >= 16 and m[4] >= 8 and m[4] <= 16)then
  549.  
  550.                         --]] -=- Body Code Par -=- [[--
  551.                         inbody=true
  552.       parnum=0
  553.                     end
  554.  
  555.                     if(m[3] >= 43 and m[3] <= 49 and m[4] == 17)then
  556.  
  557.                         if(rcr ~= nil and rcsub ~= nil)then
  558.                             sendCozyMail(rcr,rcsub,bodyPar)
  559.                             rcr=nil
  560.                             rcsub=nil
  561.                             bodyPar={}
  562.                         end
  563.  
  564.                     end
  565.  
  566.             end
  567.  
  568.         end
  569.     end
  570.  
  571.  
  572. end
  573.  
  574.  
  575. function sendCozyMail(to,subject,bodytable)
  576.            
  577.             term.setCursorBlink(false)
  578.             inbody=false
  579.     parnum = 0
  580.             f = fs.open(".tempcozymail","w")
  581.             f.writeLine(username)
  582.             f.writeLine(subject)
  583.             f.writeLine(" ")
  584.             for i = 1, #bodyPar do f.writeLine(bodyPar[i]) end
  585.             f.close()
  586.  
  587.  
  588.             f = fs.open(".tempcozymail","r")
  589.             sendm = f.readAll()
  590.             f.close()
  591.  
  592.             fs.delete(".tempcozymail")
  593.  
  594.             rednet.broadcast("COZY_EMAIL")
  595.             id, m1 = rednet.receive(1)
  596.             if(m1 ~= nil)then
  597.                 rednet.send(id,to);
  598.                 local id, usr = rednet.receive()
  599.             if(usr)then
  600.                 rednet.send(id,sendm)
  601.                 local id, isOK = rednet.receive(2)
  602.                 if(isOK ~= nil)then
  603.                     ea.scp(15,17)
  604.                     ea.stc(colors.green)
  605.                     ea.sp("Mail Sent!")
  606.                 else
  607.                     error("[SEVER] Server Crashed! Please Contact Server Admin RIGHT NOW!!")
  608.                 end
  609.             else
  610.                 ea.scp(15,17)
  611.                 ea.sp("No such user!")
  612.             end
  613.         end
  614.  
  615.             if(m1 == nil)then ea.scp(15,17); ea.sp("Connection With Server Failed :(") end
  616.  
  617. end
  618.  
  619.  
  620. function sendEmail()
  621.  
  622.     clear()
  623.  
  624.  
  625.     ea.stc(colors.white)
  626.     ea.sbc(colors.gray)
  627.  
  628.     ea.scp(15,3)
  629.     print("|==================================|")
  630.     ea.scp(15,4)
  631.     print(" To:                               ")
  632.     ea.scp(15,5)
  633.     print(" Subject:                                           ")
  634.     ea.scp(15,6)
  635.     print("|==================================|")
  636.     ea.scp(15,7)
  637.     print("[Body]")
  638.  
  639.     ea.scp(45,17)
  640.     ea.sbc(colors.green)
  641.     print(" SEND ")
  642.  
  643.     ea.sbc(colors.gray)
  644.     ea.stc(colors.white)
  645.     if(rcr ~= nil)then ea.scp(19,4); print(rcr) end
  646.  
  647. end
  648.  
  649.  
  650. function receiveEmailedFile()
  651.    
  652.         amm = math.random(1,9999999)
  653.  
  654.         rednet.send(senID,"FILE")
  655.         id, dehfile = rednet.receive(1)
  656.  
  657.         f = fs.open("cozy/programs/email/inbox/cozymailID_"..amm,"w")
  658.         f.write(dehfile)
  659.         f.close()
  660. end
  661.  
  662.  
  663. function updateEmailFiles()
  664.  
  665.     for k, v in pairs(fs.list("cozy/programs/email/inbox")) do
  666.  
  667.         fs.delete("cozy/programs/email/inbox/"..v)
  668.  
  669.     end
  670.  
  671.  
  672.      rednet.broadcast("COZY_EMAIL_UPDATE")
  673.     local id, m = rednet.receive(2)
  674.     rednet.send(tonumber(id),username)
  675.  
  676.  
  677.     if(m == nil)then ea.scp(1,1) ea.sbc(colors.red) term.clearLine() ea.stc(colors.white); print("Connection With Server Failed!") end
  678.  
  679.     if(m ~= nil)then
  680.  
  681.         local id, amount = rednet.receive()
  682.        
  683.  
  684.         for i = 0, amount do
  685.            
  686.             if(i > 0 )then
  687.                 ti=(i-1)
  688.                 id, file = rednet.receive();
  689.                 id, fileN = rednet.receive();
  690.                 f = fs.open("cozy/programs/email/inbox/"..fileN,"w")
  691.                 f.write(file)
  692.                 f.close()
  693.             end
  694.  
  695.         end
  696.  
  697.             mailList = {}
  698.            
  699.  
  700.             for k,v in pairs(fs.list(mailpath.."inbox")) do
  701.  
  702.                 if(mailList[v] == nil and not fs.isDir(mailpath.."inbox"..v))then
  703.  
  704.                     f = fs.open(mailpath.."inbox".."/"..v,"r")
  705.                     conl = f.readLine()
  706.                     cDisc = f.readLine()
  707.                     f.close()
  708.  
  709.                     mailList[v] = {type="inbox",sender=conl,disc = cDisc,offset = k,path=mailpath.."inbox".."/"..v}
  710.  
  711.                 end
  712.  
  713.             end
  714.  
  715.         clear()
  716.         printEmail("inbox")
  717.        
  718.     end
  719.  
  720. end
  721.  
  722.  
  723. function sendMailEnter()
  724.  
  725.     a = {os.pullEvent("key")}
  726.  
  727.     if(a[2] == keys.enter and sending)then
  728.         b = {os.pullEvent("key")}
  729.         if(b[2] == keys.enter)then
  730.            
  731.             term.setCursorBlink(false)
  732.             sendCozyMail(rcr,rcsub,bp1,bp2,bp3,bp4,bp5,bp6,bp7)
  733.             rcr=nil
  734.             rcsub=nil
  735.             bodyPar={}
  736.             inbody=false
  737.             clear()
  738.  
  739.             else
  740.                 sendMailEnter()
  741.             end
  742.  
  743.             else
  744.                 sendMailEnter()
  745.             end
  746.  
  747. end
  748.  
  749. function para()
  750.     --parallel.waitForAny(updateEmail, sendMailEnter)
  751.     para()
  752. end
  753.  
  754. function CMail()
  755.  
  756.     while inEmail do
  757.         updateEmail()
  758.         --para()
  759.     end
  760.  
  761. end
  762.  
  763.  
  764. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement