SHARE
TWEET

Untitled

a guest Dec 13th, 2018 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. w,h = term.getSize()
  2. umail = { }
  3. rmail = { }
  4. slc = 1
  5. serverid = 0
  6. side = "top"
  7.  
  8. confirmmsg = ""
  9. newmsg = ""
  10. newto = 0
  11.  
  12. emidx = 1
  13. emlist = { }
  14.  
  15. state = 1
  16. states = {
  17.   ["TOP"] = {
  18.     id = 1,
  19.     options = 6
  20.   },
  21.   ["COMPOSETEXT"] = {
  22.     id = 2
  23.   },
  24.   ["COMPOSEMENU"] = {
  25.     id = 3,
  26.     options = 2
  27.   },
  28.   ["UNREAD"] = {
  29.     id = 4,
  30.     options = 2
  31.   },
  32.   ["ARCHIVE"] = {
  33.     id = 5,
  34.     options = 4
  35.   },
  36.   ["CONFIRM"] = {
  37.     id = 6,
  38.     options = 1
  39.   },
  40.   ["CHANGENAME"] = {
  41.     id = 7
  42.   }
  43. }
  44.  
  45. -- Helper Functions --
  46.  
  47. function split(str, pattern)
  48.   local t = { }
  49.   local fpat = "(.-)" .. pattern
  50.   local last_end = 1
  51.   local s, e, cap = str:find(fpat, 1)
  52.   while s do
  53.     if s ~= 1 or cap ~= "" then
  54.       table.insert(t,cap)
  55.     end
  56.     last_end = e+1
  57.     s, e, cap = str:find(fpat, last_end)
  58.   end
  59.   if last_end <= #str then
  60.     cap = str:sub(last_end)
  61.     table.insert(t, cap)
  62.   end
  63.   return t
  64. end
  65.  
  66. -- Interface Drawing --
  67.  
  68. function printHeader()
  69.   term.setCursorPos(1, 1)
  70.   term.write("Lary's EMAIL CLIENT")
  71.   printRight(1, "VERSION 1.0")
  72.   term.setCursorPos(1, 2)
  73.   term.write("Computer ID: "..os.getComputerID())
  74. end
  75.  
  76. function printConfirm()
  77.   local boxsize = 26
  78.   local ok = "[ OK ]"
  79.   printCentered(8, string.rep("*", boxsize))
  80.   printCentered(9, "*"..string.rep(" ", boxsize-2).."*")
  81.   local ws = (boxsize - #confirmmsg - 2)/2
  82.   printCentered(10, "*"..string.rep(" ", ws)..confirmmsg..string.rep(" ", ws).."*")
  83.   ws = (boxsize - #ok - 2)/2
  84.   printCentered(11, "*"..string.rep(" ", ws)..ok..string.rep(" ", ws).."*")
  85.   printCentered(12, "*"..string.rep(" ", boxsize-2).."*")
  86.   printCentered(13, string.rep("*", boxsize))
  87. end
  88.  
  89. function printChangeName()
  90.   term.setCursorPos(4, 6)
  91.   term.write("Enter new name: ")
  92. end
  93.  
  94. function printStored()
  95.   term.setCursorPos(1,4)
  96.   term.write("From: "..emlist[emidx].from)
  97.   term.setCursorPos(1,5)
  98.   term.write("Sent at: "..textutils.formatTime(tonumber(emlist[emidx].time)))
  99.   term.setCursorPos(1,6)
  100.   term.write("--------------------")
  101.   term.setCursorPos(1,7)
  102.   print(emlist[emidx].body)
  103.   term.setCursorPos(1,h-1)
  104.   term.write("Reading Email "..emidx.." of "..table.getn(emlist))  
  105. end
  106.  
  107. function printArchiveMenu()
  108.   local ws = string.rep(" ", 4)
  109.   local pr, ba, de, ne
  110.   if slc==1 then pr = "[ PREV ]" else pr = "  PREV  " end
  111.   if slc==2 then ba = "[ BACK ]" else ba = "  BACK  " end
  112.   if slc==3 then de = "[ DELETE ]" else de = "  DELETE  " end
  113.   if slc==4 then ne = "[ NEXT ]" else ne = "  NEXT  " end
  114.   term.setCursorPos(1,h)
  115.   printCentered(h, ws..pr..ws..ba..ws..de..ws..ne)
  116. end
  117.  
  118. function printUnreadMenu()
  119.   local full = ""
  120.   if slc==1 then full = "[ ARCHIVE ]       DELETE  "
  121.             else full = "  ARCHIVE       [ DELETE ]"
  122.   end
  123.   printCentered(h, full)
  124. end
  125.  
  126. function printCompose()
  127.   term.setCursorPos(1,4)
  128.   term.write("To:")
  129.   term.setCursorPos(1,5)
  130.   term.write("Body:")
  131.   term.setCursorPos(1,h)
  132.   term.write("Hit Enter twice to end the email.")
  133. end
  134.  
  135. function printComposeMenu()
  136.   term.setCursorPos(1,h)
  137.   term.clearLine()
  138.   local optionstr = ""
  139.   if slc == 1 then
  140.      optionstr = " [ SEND ]        DELETE   "
  141.   else
  142.      optionstr = "   SEND        [ DELETE ] "
  143.   end
  144.   printCentered(h, optionstr)
  145. end
  146.  
  147. function printTopMenu()
  148.   term.setCursorPos(1, h-1)
  149.   term.write("Hi, "..os.getComputerLabel().."!")
  150.   term.setCursorPos(1, h)
  151.   term.write("You have "..table.getn(umail).." new message(s).")
  152.  
  153.   printCentered(4, "Select Option:")
  154.   if slc == 1 then printCentered(7, " [ COMPOSE EMAIL ] ")
  155.   else printCentered(7, "   COMPOSE EMAIL   ") end
  156.   if slc == 2 then printCentered(8, " [ UNREAD EMAILS ] ")
  157.   else printCentered(8, "   UNREAD EMAILS   ") end
  158.   if slc == 3 then printCentered(9, "[ ARCHIVED EMAILS ]")
  159.   else printCentered(9, "  ARCHIVED EMAILS  ") end
  160.   if slc == 4 then printCentered(10, "  [ CHANGE NAME ]  ")
  161.   else printCentered(10, "    CHANGE NAME    ") end
  162.   if slc == 5 then printCentered(11, " [ REFRESH EMAIL ] ")
  163.   else printCentered(11, "   REFRESH EMAIL   ") end
  164.   if slc == 6 then printCentered(12, "     [ QUIT ]      ")
  165.   else printCentered(12, "       QUIT        ") end
  166. end
  167.  
  168. function printRight(height, value)
  169.   local xpos = w - string.len(value)
  170.   term.setCursorPos(xpos, height)
  171.   term.write(value)
  172. end
  173.  
  174. function printCentered(height, value)
  175.   local xpos = w/2 - string.len(value)/2
  176.   term.setCursorPos(xpos, height)  
  177.   term.write(value)
  178. end
  179.  
  180. -- Email Tools --
  181.  
  182. function sendEmail()
  183.   rednet.open(side)
  184.   local newemail = "$EMAIL"..newto
  185.                  .."!SP!"..os.getComputerLabel()
  186.                  .."!SP!"..os.time().."!SP!"
  187.                  ..newmsg
  188.   rednet.send(serverid, newemail)
  189.   if rednet.receive(5)~=nil then
  190.     confirmmsg = "MESSAGE SENT"
  191.   else
  192.     confirmmsg = "ERROR OCCURRED"
  193.   end
  194.   rednet.close(side)
  195. end
  196.  
  197. function readEmails()
  198.   rednet.open(side)
  199.   rednet.send(serverid, "$REQUEST")
  200.   local id, mail = rednet.receive(5)
  201.   if mail==nil then
  202.     confirmmsg = "SERVER ERROR"
  203.     rednet.close("left")
  204.     return
  205.   end
  206.   mail = string.gsub(mail, "$RESPONSE", "")
  207.  
  208.   local t = split(mail, "!SP!")
  209.  
  210.   for i=1,table.getn(t),3 do
  211.     table.insert(umail, {
  212.       from = t[i],
  213.       time = t[i+1],
  214.       body = t[i+2]
  215.     })
  216.   end
  217.   rednet.close(side)
  218. end
  219.  
  220. function readFromArchive()
  221.   if not fs.exists(shell.resolve(".").."/archive") then return end
  222.   local file = io.open(shell.resolve(".").."/archive", "r")
  223.   local fullstr = ""
  224.   local fline = file:read()
  225.   while fline do
  226.     fullstr=fullstr..fline
  227.     fline = file:read()
  228.   end  
  229.   file:close()
  230.   local t = split(fullstr, "!SP!")
  231.   for i=1,#t,3 do
  232.     table.insert(rmail, {
  233.       from = t[i],
  234.       time = t[i+1],
  235.       body = t[i+2]
  236.     })
  237.   end
  238. end
  239.  
  240. function writeToArchive()
  241.   local file = io.open(shell.resolve(".").."/archive", "w")
  242.   local arcstr = ""
  243.   for i=1,#rmail do
  244.     arcstr=arcstr.."!SP!"..rmail[i].from.."!SP!"..
  245.                  rmail[i].time.."!SP!"..rmail[i].body
  246.   end
  247.   file:write(arcstr)
  248.   file:close()
  249. end
  250.  
  251. -- Interface Logic --
  252.  
  253. function runInterface()
  254.   term.clear()
  255.   while true do
  256.     if state==states["TOP"].id then
  257.       term.clear()
  258.       printHeader()
  259.       printTopMenu()
  260.       local e,key = os.pullEvent("key")
  261.       if key==200 and slc>1 then slc = slc-1
  262.       elseif key==208 and slc<6 then slc=slc+1
  263.       elseif key==28 then
  264.         if slc==1 then
  265.           state = states["COMPOSETEXT"].id
  266.           newmsg = ""
  267.         elseif slc==2 then
  268.           if table.getn(umail)==0 then
  269.             confirmmsg="No new mail."
  270.             state=states["CONFIRM"].id
  271.           else
  272.             emidx=1
  273.             emlist = umail
  274.             state=states["UNREAD"].id
  275.           end
  276.         elseif slc==3 then
  277.           if table.getn(rmail)==0 then
  278.             confirmmsg="Archive Empty."
  279.             state=states["CONFIRM"].id
  280.           else
  281.             emidx=1
  282.             emlist = rmail
  283.             state=states["ARCHIVE"].id
  284.           end
  285.         elseif slc==4 then
  286.           state=states["CHANGENAME"].id
  287.         elseif slc==5 then
  288.           local ocount = table.getn(umail)
  289.           readEmails()
  290.           if ocount < table.getn(umail) then
  291.             confirmmsg = "Messages Received!"
  292.           else
  293.             confirmmsg = "No new mail."
  294.           end
  295.           state = states["CONFIRM"].id
  296.         elseif slc==6 then
  297.           term.clear()
  298.           term.setCursorPos(1,1)
  299.           return
  300.         end
  301.       end
  302.     elseif state==states["CHANGENAME"].id then
  303.       term.clear()
  304.       printHeader()
  305.       printChangeName()
  306.       local nname = io.read()
  307.       if #nname>25 then
  308.         nname = string.sub(nname, 1, 25)
  309.       end
  310.       os.setComputerLabel(nname)
  311.       state=states["TOP"].id
  312.     elseif state==states["COMPOSETEXT"].id then
  313.       term.clear()
  314.       printHeader()
  315.       printCompose()
  316.       term.setCursorPos(5, 4)
  317.       newto = tonumber(io.read())
  318.       term.setCursorPos(1, 6)
  319.       local newmail=""
  320.       repeat
  321.         newmail = io.read().."\n"
  322.         newmsg=newmsg..newmail.." "
  323.       until newmail=="\n"
  324.       state = states["COMPOSEMENU"].id
  325.     elseif state==states["COMPOSEMENU"].id then
  326.       printComposeMenu()
  327.       local e, key = os.pullEvent("key")
  328.       if key==203 and slc>1 then
  329.         slc=slc-1
  330.       elseif key==205 and slc<2 then
  331.         slc=slc+1
  332.       elseif key==28 then
  333.         if slc==1 then
  334.           sendEmail()
  335.           state = states["CONFIRM"].id
  336.         else
  337.           state = states["TOP"].id
  338.         end
  339.       end
  340.     elseif state==states["ARCHIVE"].id then
  341.       term.clear()
  342.       printHeader()
  343.       printStored()
  344.       printArchiveMenu()
  345.       local e, key = os.pullEvent("key")
  346.       if key==203 and slc>1 then
  347.         slc=slc-1
  348.       elseif key==205 and slc<4 then
  349.         slc=slc+1
  350.       elseif key==28 then
  351.         if slc==1 and emidx>1 then emidx=emidx-1
  352.         elseif slc==2 then state=states["TOP"].id
  353.         elseif slc==3 then
  354.           table.remove(rmail, emidx)
  355.           writeToArchive()
  356.           if #rmail==0 then
  357.             confirmmsg= "Archive Empty."
  358.             state=states["CONFIRM"].id
  359.           end
  360.           if emidx>#rmail then emidx=#rmail end
  361.         elseif slc==4 and emidx<#rmail then
  362.           emidx=emidx+1
  363.         end
  364.       end
  365.     elseif state==states["UNREAD"].id then
  366.       term.clear()
  367.       printHeader()
  368.       printStored()
  369.       printUnreadMenu()
  370.       local e, key = os.pullEvent("key")
  371.       if key==203 and slc>1 then
  372.         slc=slc-1
  373.       elseif key==205 and slc<2 then
  374.         slc=slc+1
  375.       elseif key==28 then
  376.         if slc==1 then
  377.           table.insert(rmail, {
  378.             from = emlist[emidx].from,
  379.             time = emlist[emidx].time,
  380.             body = emlist[emidx].body
  381.            })
  382.            writeToArchive()
  383.         end
  384.         table.remove(umail, emidx)
  385.         emlist = umail
  386.         if table.getn(umail)==0 then
  387.           confirmmsg="Inbox Empty."
  388.           state=states["CONFIRM"].id
  389.         end
  390.       end
  391.     elseif state==states["CONFIRM"].id then
  392.       printConfirm()
  393.       local e, key = os.pullEvent("key")
  394.       if key==28 then
  395.         state = states["TOP"].id
  396.       end
  397.     end
  398.   end
  399. end
  400.  
  401. -- On Startup --
  402.  
  403. function getPrefs()
  404.   local file = io.open(shell.resolve(".").."/pref")
  405.   if not file then
  406.     print("pref file not found! Make sure to include"
  407.         .." it in your mail directory.")
  408.     return false
  409.   end
  410.   local sidl = file:read()
  411.   sidl = string.gsub(sidl, "serverid=", "")
  412.   print(sidl)
  413.   sidl = tonumber(sidl)
  414.   if sidl==0 then
  415.     print("Client prefs not initialized- check readme for detals.")
  416.     return false
  417.   end
  418.   serverid = sidl
  419.   sidl = file:read()
  420.   sidl = string.gsub(sidl, "side=", "")
  421.   side = sidl
  422.   return true
  423. end
  424.  
  425. if not getPrefs() then return end
  426. readFromArchive()
  427. rednet.broadcast("Computer: "..os.getComputerID().." is now on network.")
  428. if os.getComputerLabel() == nil then
  429.   os.setComputerLabel("New User")
  430. end
  431.  
  432. runInterface()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top