Advertisement
Baennon

RedMail

Aug 27th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.10 KB | None | 0 0
  1. userConfig = "RedMailCfg/user.config"
  2. modemConfig = "RedMailCfg/modem.config"
  3. listConfig = "RedMailCfg/list"
  4. user = ""
  5. side = ""
  6. list = {}
  7.  
  8. local unreadM = nil
  9. local menu = nil
  10.  
  11. if not fs.exists("GUI") then
  12. http.request("http://pastebin.com/raw/PmCUHuTr")
  13. local requesting = true
  14.  
  15. while requesting do
  16.   local event, url, sourceText = os.pullEvent()
  17.  
  18.   if event == "http_success" then
  19.     local respondedText = sourceText.readAll()
  20.    
  21.     sourceText.close()
  22.    local f = fs.open("GUI","w")
  23.    f.write(respondedText)
  24.    f.close()
  25.    
  26.     requesting = false
  27.   elseif event == "http_failure" then
  28.     print("Server didn't respond.")
  29.    
  30.     requesting = false
  31.   end
  32. end
  33. end
  34.  
  35. os.loadAPI("GUI")
  36. overlay = GUI.newOverlay()
  37.  
  38. local function InBox(text)
  39.   t = {}
  40.   table.insert(t,text)
  41.   local InBox = GUI.inpBox(t)
  42.   return InBox
  43. end
  44.  
  45. local function msgBox(text)
  46.   local t = {}
  47.   table.insert(t,text)
  48.   local msgBox = GUI.msgBox(t)
  49. end
  50.  
  51. local function readFile(path)
  52.   local f = fs.open(path,"r")
  53.   local data = f.readAll()
  54.   f.close()
  55.   return data
  56. end
  57.  
  58. local function writeFile(path,text)
  59.  local f = fs.open(path,"w")
  60.  f.write(text)
  61.  f.close()
  62. end
  63.  
  64. local function newPage(title)
  65. term.clear()
  66. term.setCursorPos(1,1)
  67. overlay:setHeaderTitle(title)
  68. overlay:draw()
  69. end
  70.  
  71. function split(pString, pPattern)
  72.    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  73.    local fpat = "(.-)" .. pPattern
  74.    local last_end = 1
  75.    local s, e, cap = pString:find(fpat, 1)
  76.    while s do
  77.           if s ~= 1 or cap ~= "" then
  78.          table.insert(Table,cap)
  79.           end
  80.           last_end = e+1
  81.           s, e, cap = pString:find(fpat, last_end)
  82.    end
  83.    if last_end <= #pString then
  84.           cap = pString:sub(last_end)
  85.           table.insert(Table, cap)
  86.    end
  87.    return Table
  88. end
  89.  
  90. local function printScreen(lines)
  91.   local text = lines
  92.   local len = #text
  93.   x,y = term.getCursorPos()
  94.   maxX,maxY = term.getSize()
  95.   diff = maxX - (x+len)
  96.   if diff > maxX then
  97.   while diff > maxX do
  98.     print(string.sub(text,0,maxX-x))
  99.     diff = maxX - (x+#string.sub(text,maxX-x))
  100.     text = string.sub(text,maxX-x)
  101.   end
  102.   else
  103.     print(text)
  104.   end
  105. end
  106.  
  107. local function sendM()
  108.   newPage("Send a mail - RedMail")
  109.   term.setCursorPos(2,5)
  110.   print("To : ")
  111.   term.setCursorPos(7,5)
  112.   to = read()
  113.   print()
  114.   term.setCursorPos(2,7)
  115.   print("Subject : ")
  116.   term.setCursorPos(12,7)
  117.   subject = read()
  118.   print()
  119.   term.setCursorPos(2,9)
  120.   print("Message : ")
  121.   x,y = term.getCursorPos()
  122.   term.setCursorPos(2,y+2)
  123.   corpse = read()
  124.   while true do
  125.     confirm = InBox("Send ? (Y/N)")
  126.     confirm = string.upper(confirm)
  127.     if confirm == "Y" then
  128.       rednet.open(side)
  129.       rednet.send(53,"send|".. to .."|".. user .."|".. subject .."|".. corpse)
  130.       rednet.close()
  131.       break
  132.     elseif confirm == "N" then
  133.       break
  134.     else
  135.  
  136.     end
  137.   end
  138. end
  139.  
  140. local function detailM(j)
  141.   local detail = split(list[j],"|")
  142.   local from = detail[2]
  143.   local subject = detail[3]
  144.   local corpse = detail[4]
  145.   newPage(detail[3] .." - RedMail")
  146.   term.setCursorPos(1,1)
  147.   term.setBackgroundColor(colors.gray)
  148.   term.setTextColor(colors.red)
  149.   print("X")
  150.   term.setCursorPos(2,5)
  151.   term.setBackgroundColor(colors.black)
  152.   term.setTextColor(colors.white)
  153.   print("From : ".. from)
  154.   term.setCursorPos(2,7)
  155.   print("Subject : ".. subject)
  156.   term.setCursorPos(2,9)
  157.   print("Message : ")
  158.   x,y = term.getCursorPos()
  159.   term.setCursorPos(2,y+2)
  160.   printScreen(corpse)
  161.   file = ""
  162.   for i=1, #list do
  163.     if i == j then
  164.     else
  165.       file = file .. list[i] .."ยง"
  166.     end
  167.   end
  168.   writeFile(listConfig,file)
  169.   exited = false
  170.   while not exited do
  171.     local event, button, xPos, yPos = os.pullEvent("mouse_click")
  172.     if xPos == 1 and yPos == 1 then
  173.       unreadM()
  174.       menu()
  175.       exited = true
  176.     end
  177.   end
  178.   error("")
  179. end
  180.  
  181. unreadM = function()
  182.   newPage("Unread mails - RedMail")
  183.   term.setCursorPos(2,5)
  184.   rednet.open(side)
  185.   rednet.send(53,"fetch|"..user)
  186.   _,message = rednet.receive()
  187.   rednet.close()
  188.   if not fs.exists(listConfig) then
  189.     writeFile(listConfig,"")
  190.   end
  191.  
  192.   unread_all = readFile(listConfig)
  193.   l = split(message,"ยง")
  194.   l_s = ""
  195.   for i=1,#l do
  196.     l_s = l_s .. l[i] .."ยง"
  197.   end
  198.   writeFile(listConfig,unread_all .. l_s)
  199.   local unread_mails = readFile(listConfig)
  200.  
  201.   if #unread_mails == 0 then
  202.     print("You don't have unread mails")
  203.     sleep(2)
  204.   else
  205.     unread_mails_t = split(unread_mails,"ยง")
  206.     list = unread_mails_t
  207.     for i=1, #list do
  208.       detail = split(list[i],"|")
  209.       print(i .." - ".. detail[3] .."|"..detail[2])
  210.       term.setCursorPos(2,5+i)
  211.     end
  212.     while true do
  213.       event, button, xPos, yPos = os.pullEvent("mouse_click")
  214.       if xPos == 2 and yPos >= 5 then
  215.         j = yPos - 4
  216.         detailM(j)
  217.       end
  218.     end
  219.   end
  220. end
  221.  
  222. menu = function()
  223.   exited = false
  224.   while not exited do
  225.     newPage("RedMail")
  226.     local x,y = term.getSize()
  227.     local send = "Send Mail"
  228.     local send_y = 5
  229.     local send_x = math.ceil((x-#send)/2)
  230.    
  231.     local unread = "Unread Mails"
  232.     local unread_y = 7
  233.     local unread_x = math.ceil((x-#unread)/2)
  234.    
  235.     local archive = "Archives"
  236.     local archive_y = 9
  237.     local archive_x = math.ceil((x-#archive)/2)
  238.    
  239.     local quit = "Quit"
  240.     local quit_y = 11
  241.     local quit_x = math.ceil((x-#quit)/2)
  242.    
  243.     term.setCursorPos(send_x,send_y)
  244.     print(send)
  245.    
  246.     term.setCursorPos(unread_x,unread_y)
  247.     print(unread)
  248.    
  249.     term.setCursorPos(archive_x,archive_y)
  250.     print(archive)
  251.    
  252.     term.setCursorPos(quit_x,quit_y)
  253.     print(quit)
  254.      
  255.     local event, button, xPos, yPos = os.pullEvent("mouse_click")
  256.       if (yPos == 5) and (xPos >= send_x and xPos <= (send_x + #send)) then
  257.          sendM()        
  258.       elseif (yPos == 11) and (xPos >= quit_x and xPos <= (quit_x + #quit)) then
  259.         term.clear()
  260.         term.setCursorPos(1,0)
  261.          exited = true
  262.       elseif (yPos == 7) and (xPos >= unread_x and xPos <= (unread_x + #unread)) then
  263.         unreadM()
  264.       end
  265.    end
  266. end
  267.  
  268. overlay:setHeaderTitle("RedMail")
  269. overlay:setFooterText("")
  270. overlay:draw()
  271.  
  272. if not fs.exists(userConfig) then
  273.   while true do
  274.     local userInBox = InBox("Enter your username")
  275.     if #userInBox > 0 then
  276.       user = userInBox
  277.       writeFile(userConfig,userInBox)
  278.       break
  279.     end
  280.   end
  281.  
  282. else
  283.   user = readFile(userConfig)
  284. end
  285.  
  286. if not fs.exists(modemConfig) then  
  287.   while true do
  288.     local modemInBox = InBox("Which side is the modem ?")
  289.     if #modemInBox > 0 then
  290.       if peripheral.isPresent(modemInBox) and peripheral.getType(modemInBox) == "modem" then
  291.         writeFile(modemConfig,modemInBox)
  292.         side = modemInBox
  293.         break
  294.       else
  295.         msgBox("Can't find modem this side")
  296.         term.clear()
  297.         overlay:setHeaderTitle("RedMail")
  298.         overlay:setFooterText("")
  299.         overlay:draw()
  300.       end
  301.     end
  302.   end
  303. else
  304.   side = readFile(modemConfig)
  305. end
  306.  
  307. menu()
  308.  
  309. rednet.open(side)
  310. rednet.send(53,"fetch|Deathspreader")
  311. id, message = rednet.receive()
  312. print(message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement