Guest User

CozyOS - Email(CozyMail) Client

a guest
Jul 3rd, 2015
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.73 KB | None | 0 0
  1. local mailpath = "cozy/programs/email/"
  2. local mailList = {}
  3. local bgcolor = colors.black
  4. local inEmail = false
  5. local mType = "inbox"
  6. local opMail = ""
  7. local mailSender= ""
  8. local id = os.getComputerID()
  9. local username = ""
  10. foundMail = false
  11. local inInbox=false
  12. local w,h = term.getSize()
  13.  
  14.  
  15.  
  16. os.loadAPI("ea")
  17. os.loadAPI("cozy_string")
  18.  
  19. write("Enter Rednet Modem Side: ")
  20. rednet.open(read())
  21.  
  22.  
  23. function clearLineLength(x,y,l)
  24.  
  25.     ea.scp(x,y)
  26.     ea.sbc(colors.white)
  27.     print("                                ")
  28.  
  29. end
  30.  
  31. function getUser()
  32.  
  33.  
  34.     ea.stc(colors.black)
  35.     ea.sbc(colors.blue)
  36.     term.clear()
  37.     ea.scp(20,5)
  38.     print("Sign Into User")
  39.     ea.scp(1,10)
  40.     print("Username: ")
  41.     print("Password: ")
  42.     ea.scp(10,10)
  43.     us = read()
  44.     ea.scp(10,11)
  45.     up = read("*")
  46.  
  47.     rednet.broadcast("COZY_GETUSER")
  48.     local id, msg = rednet.receive(2)
  49.     if(msg == nil)then error("Connection to server failed!") end
  50.     rednet.send(id,us)
  51.     local id, valid = rednet.receive()
  52.  
  53.     if(valid)then
  54.         local id, cpass = rednet.receive()
  55.         if(up == cpass)then corpass=true else corpass = false end
  56.     end
  57.    
  58.  
  59.     if(valid and corpass)then inEmail = true; username = us; updateEmailFiles(); clear(); CMail() end
  60.     if(not valid)then ea.sp("Username Invalid") getUser() end
  61.     if(not corpass)then ea.sp("Password Invalid") getUser() end
  62.  
  63. end
  64.  
  65.  
  66. function createUser()
  67.  
  68.     ea.sbc(colors.blue)
  69.     term.clear()
  70.     ea.scp(20,5)
  71.     print("Create User")
  72.     ea.scp(1,10)
  73.     print("Username: ")
  74.     print("Password: ")
  75.     ea.scp(10,10)
  76.     us = read()
  77.     ea.scp(10,11)
  78.     up = read()
  79.    
  80.     rednet.broadcast("COZY_CREATEUSER")
  81.     local id, got = rednet.receive(2)
  82.  
  83.     if(got ~= nil)then
  84.         rednet.send(tonumber(id),us)
  85.         local id, valid = rednet.receive()
  86.  
  87.             if(valid)then
  88.                 rednet.send(tonumber(id),up)
  89.                 ea.sp("User Created!")
  90.                
  91.                 f = fs.open("cozy/programs/email/login/.user","w")
  92.                 f.writeLine("--]] Has Login [[--")
  93.                 f.writeLine("true")
  94.                 f.close()
  95.                
  96.                 getUser()
  97.             else
  98.                  ea.sp("Username Unavailable!")
  99.             end
  100.  
  101.     else
  102.         error("Server Connection Failed!")
  103.     end
  104.  
  105.  
  106.  
  107.  
  108. end
  109.  
  110.  
  111.  
  112. function clear()
  113.  
  114.     ea.sbc(bgcolor);
  115.     term.clear();
  116.     ea.scp(1,1)
  117.     ea.sbc(colors.blue)
  118.     term.clearLine()
  119.     ea.scp(1,1)
  120.     ea.stc(colors.white)
  121.     print("COZYMAIL")
  122.     ea.scp(w-((#username)+2), 1)
  123.     print("["..username.."]")
  124.  
  125.     ea.sbc(colors.gray)
  126.     for i = 3, 15 do
  127.  
  128.         ea.scp(1,i)
  129.         print(string.rep(" ",12))
  130.  
  131.     end
  132.  
  133.     ea.sbc(colors.white)
  134.     for i = 3, 17 do
  135.         ea.scp(15,i)
  136.         print(string.rep(" ",36))
  137.  
  138.     end
  139.     ea.scp(15,3)
  140.     ea.stc(colors.black)
  141.     print("["..string.rep("=",33).."]")
  142.  
  143.  
  144.     ea.scp(1,3)
  145.     ea.stc(colors.green)
  146.     ea.sbc(colors.gray)
  147.     print("|---------|")
  148.     print("| [INBOX] |")
  149.     print("| [USER ] |")
  150.     print("| [SEND ] |")
  151.     print("|NO[Block]|")
  152.     print("|NO[Chat] |")
  153.     print("|---------|")
  154.  
  155. end
  156.  
  157.  
  158. function printEmail(type)
  159.  
  160.     for k,v in pairs(mailList) do      
  161.  
  162.         if(v.type == type)then
  163.  
  164.             ea.sbc(colors.white)
  165.             ea.scp(15,v.offset+3)
  166.  
  167.             if(v.sender == nil or v.disc == nil)then ea.stc(colors.red); print("CORRUPED EMAIL") else ea.stc(colors.black); print(v.sender.." - "..v.disc) end
  168.         end
  169.  
  170.     end
  171.  
  172. end
  173.  
  174. function openMail(mail)
  175.  
  176.     i = 0
  177.         clear()
  178.         ea.scp(15,4)
  179.         ea.scp(15,15)
  180.         print("[REPLY]")
  181.         ea.stc(colors.red)
  182.         ea.scp(43,15)
  183.         print("[DELETE]")
  184.         ea.stc(colors.black)
  185.         ea.sbc(colors.white)
  186.         f = io.open(mail,"r")
  187.  
  188.         for line in f:lines() do
  189.             ea.scp(15,i+4)
  190.             print(line)
  191.             i = i + 1
  192.         end
  193. end
  194.  
  195. function updateEmail()
  196.  
  197.  
  198.     m = {os.pullEvent()}
  199.  
  200.     if(m[1] == "mouse_click" and m[2] == 1)then
  201.  
  202.        
  203.  
  204.         if(m[3] >= 3 and m[3] <= 8 and m[4] == 4 and not inInbox)then
  205.            
  206.             --mType = "inbox"
  207.             clear()
  208.             ea.scp(15,4)
  209.             updateEmailFiles()
  210.             if(readingMail)then readingMail = false end
  211.             if(sending)then sending = false end
  212.             --inInbox=true
  213.         end
  214.  
  215.         if(m[3] >= 3 and m[3] <= 8 and m[4] == 5)then
  216.            
  217.             --mType = "spam"
  218.             if(readingMail)then readingMail = false end
  219.             if(sending)then sending = false end
  220.             clear()
  221.             inEmail = false
  222.             getUser()
  223.  
  224.         end
  225.  
  226.         if(m[3] >= 3 and m[3] <= 8 and m[4] == 6)then
  227.            
  228.             clear()
  229.             rcr=nil
  230.             rcsub=nil
  231.             bp1=nil
  232.             bp2=nil
  233.             bp3=nil
  234.             bp5=nil
  235.             bp6=nil
  236.             bp7=nil
  237.             sendEmail()
  238.             t = 0;
  239.             sending=true
  240.         end
  241.  
  242.         if(not readingMail and not sending)then
  243.                 for k,v in pairs(mailList) do
  244.  
  245.                            
  246.                         if(m[3] >= 15 and m[3] <= #v.sender+15 and m[4] == v.offset+3)then
  247.                             ea.sbc(colors.white)
  248.                             readingMail = true
  249.                             print(v.path.." "..v.offset)
  250.  
  251.                                     openMail(v.path)
  252.                                     opMail = v.path
  253.                                     mailSender = v.sender
  254.                                     opName = k
  255.                                     mType = v.type
  256.  
  257.                         end
  258.  
  259.                 end
  260.  
  261.         else
  262.  
  263.             if(not sending)then
  264.  
  265.                 if(m[3] >= 16 and m[3] <= 21 and m[4] == 15)then
  266.                         clear()
  267.                         rcr="Bob"
  268.                         sendEmail()
  269.                         sending=true
  270.                         readingMail=false
  271.                 end
  272.  
  273.            
  274.                 if(m[3] >= 43 and m[3] <= 50 and m[4] == 15)then
  275.                    
  276.                     rednet.broadcast("COZY_REMOVE")
  277.                     local id, m =rednet.receive(2)
  278.  
  279.                     if(m == nil)then error("Server Connect Failed!") end
  280.  
  281.                     rednet.send(tonumber(id),opName)
  282.                     rednet.send(tonumber(id),username)
  283.  
  284.                     if(mType == "inbox")then
  285.  
  286.                         clear()
  287.                         ea.scp(15,4)
  288.                         updateEmailFiles()
  289.                         readingMail = false
  290.                     end
  291.  
  292.                     if(mType == "spam")then
  293.  
  294.                         clear()
  295.                         ea.scp(15,4)
  296.                         readingMail = false
  297.                     end
  298.  
  299.                 end
  300.             end
  301.  
  302.             if(sending and not readingMail)then
  303.  
  304.                     if(m[3] >= 16 and m[4] == 4)then
  305.                                
  306.                                 clearLineLength(19,4,15)
  307.                                 ea.scp(19,4)   
  308.                                 rcr=read()
  309.                     end
  310.  
  311.                     if(m[3] >= 16 and m[4] == 5)then
  312.                             clearLineLength(24,5,15)
  313.                             ea.scp(24,5)
  314.                             rcsub=read()
  315.                     end
  316.  
  317.                     if(m[3] >= 16 and m[4] == 8)then
  318.  
  319.  
  320.                         ea.scp(15,8)
  321.                         clearLineLength(15,8,15)
  322.                         ea.scp(15,9)
  323.                         clearLineLength(15,9,15)
  324.                         ea.scp(15,10)
  325.                         clearLineLength(15,10,15)
  326.                         ea.scp(15,11)
  327.                         clearLineLength(15,11,15)
  328.                         ea.scp(15,12)
  329.                         clearLineLength(15,12,15)
  330.                         ea.scp(15,13)
  331.                         clearLineLength(15,13,15)
  332.                         ea.scp(15,14)
  333.                         clearLineLength(15,14,15)
  334.  
  335.                         ea.scp(15,8)
  336.                         bp1=read()
  337.                         ea.scp(15,9)
  338.                         bp2=read()
  339.                         ea.scp(15,10)
  340.                         bp3=read()
  341.                         ea.scp(15,11)
  342.                         bp4=read()
  343.                         ea.scp(15,12)
  344.                         bp5=read()
  345.                         ea.scp(15,13)
  346.                         bp6=read()
  347.                         ea.scp(15,14)
  348.                         bp7=read()
  349.  
  350.                     end
  351.  
  352.                     if(m[3] >= 43 and m[3] <= 49 and m[4] == 17)then
  353.  
  354.                         if(rcr ~= nil and rcsub ~= nil)then
  355.                             sendCozyMail(rcr,rcsub,bp1,bp2,bp3,bp4,bp5,bp6,bp7)
  356.                             rcr=nil
  357.                             rcsub=nil
  358.                             bp1=nil
  359.                             bp2=nil
  360.                             bp3=nil
  361.                             bp5=nil
  362.                             bp6=nil
  363.                             bp7=nil
  364.                         end
  365.  
  366.                     end
  367.  
  368.             end
  369.  
  370.         end
  371.     end
  372.  
  373.  
  374. end
  375.  
  376.  
  377. function sendCozyMail(to,subject,bl1,bl2,bl3,bl4,bl5,bl6,bl7)
  378.    
  379.             f = fs.open(".tempcozymail","w")
  380.             f.writeLine(username)
  381.             f.writeLine(subject)
  382.             f.writeLine(" ")
  383.             f.writeLine(bl1)
  384.             f.writeLine(bl2)
  385.             f.writeLine(bl3)
  386.             f.writeLine(bl4)
  387.             f.writeLine(bl5)
  388.             f.writeLine(bl6)
  389.             f.writeLine(bl7)
  390.             f.close()
  391.  
  392.  
  393.             f = fs.open(".tempcozymail","r")
  394.             sendm = f.readAll()
  395.             f.close()
  396.  
  397.             fs.delete(".tempcozymail")
  398.  
  399.             rednet.broadcast("COZY_EMAIL")
  400.             id, m1 = rednet.receive(1)
  401.             if(m1 ~= nil)then
  402.                 rednet.send(id,to);
  403.                 local id, usr = rednet.receive()
  404.             if(usr)then
  405.                 rednet.send(id,sendm)
  406.                 local id, isOK = rednet.receive(2)
  407.                 if(isOK ~= nil)then
  408.                     ea.scp(15,17)
  409.                     ea.stc(colors.green)
  410.                     ea.sp("Mail Sent!")
  411.                 else
  412.                     error("[SEVER] Server Crashed! Please Contact Server Admin RIGHT NOW!!")
  413.                 end
  414.             else
  415.                 ea.scp(15,17)
  416.                 ea.sp("No such user!")
  417.             end
  418.         end
  419.  
  420.             if(m1 == nil)then ea.scp(15,17); ea.sp("Connection With Server Failed :(") end
  421.  
  422. end
  423.  
  424.  
  425. function sendEmail()
  426.  
  427.     clear()
  428.  
  429.  
  430.     ea.stc(colors.black)
  431.     ea.sbc(colors.white)
  432.  
  433.     ea.scp(15,4)
  434.     print("|To:                               |")
  435.     ea.scp(15,5)
  436.     print("|Subject:                                           |")
  437.     ea.scp(15,6)
  438.     print("|==================================|")
  439.     ea.scp(15,7)
  440.     print("[Body]")
  441.  
  442.     ea.scp(45,17)
  443.     ea.sbc(colors.green)
  444.     print("[SEND]")
  445.  
  446.     ea.sbc(colors.white)
  447.     ea.stc(colors.black)
  448.     if(rcr ~= nil)then ea.scp(19,4); print(rcr) end
  449.  
  450. end
  451.  
  452.  
  453. function receiveEmailedFile()
  454.    
  455.         amm = math.random(1,9999999)
  456.  
  457.         rednet.send(senID,"FILE")
  458.         id, dehfile = rednet.receive(1)
  459.  
  460.         f = fs.open("cozy/programs/email/inbox/cozymailID_"..amm,"w")
  461.         f.write(dehfile)
  462.         f.close()
  463. end
  464.  
  465.  
  466. function updateEmailFiles()
  467.  
  468.     for k, v in pairs(fs.list("cozy/programs/email/inbox")) do
  469.  
  470.         fs.delete("cozy/programs/email/inbox/"..v)
  471.  
  472.     end
  473.  
  474.  
  475.      rednet.broadcast("COZY_EMAIL_UPDATE")
  476.     local id, m = rednet.receive(2)
  477.     rednet.send(tonumber(id),username)
  478.  
  479.  
  480.     if(m == nil)then ea.scp(1,1) ea.sbc(colors.red) term.clearLine() ea.stc(colors.white); print("Connection With Server Failed!") end
  481.  
  482.     if(m ~= nil)then
  483.  
  484.         local id, amount = rednet.receive()
  485.        
  486.  
  487.         for i = 0, amount do
  488.            
  489.             if(i > 0 )then
  490.                 ti=(i-1)
  491.                 id, file = rednet.receive();
  492.                 id, fileN = rednet.receive();
  493.                 f = fs.open("cozy/programs/email/inbox/"..fileN,"w")
  494.                 f.write(file)
  495.                 f.close()
  496.             end
  497.  
  498.         end
  499.  
  500.             mailList = {}
  501.            
  502.  
  503.             for k,v in pairs(fs.list(mailpath.."inbox")) do
  504.  
  505.                 if(mailList[v] == nil and not fs.isDir(mailpath.."inbox"..v))then
  506.  
  507.                     f = fs.open(mailpath.."inbox".."/"..v,"r")
  508.                     conl = f.readLine()
  509.                     cDisc = f.readLine()
  510.                     f.close()
  511.  
  512.                     mailList[v] = {type="inbox",sender=conl,disc = cDisc,offset = k,path=mailpath.."inbox".."/"..v}
  513.  
  514.                 end
  515.  
  516.             end
  517.  
  518.         clear()
  519.         printEmail("inbox")
  520.        
  521.     end
  522.  
  523. end
  524.  
  525.  
  526. function para()
  527.     para()
  528. end
  529.  
  530. function CMail()
  531.  
  532.     while inEmail do
  533.         updateEmail()
  534.         --para()
  535.     end
  536.  
  537. end
  538.  
  539.  
  540.  
  541.  
  542. if(not fs.exists("cozy/programs/email/login/.user"))then
  543.     createUser()
  544. else
  545.     getUser()
  546. end
Advertisement
Add Comment
Please, Sign In to add comment