Advertisement
Gulligagardinen

Ice-Mail-client

Jul 21st, 2013
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.41 KB | None | 0 0
  1. --[[
  2.     Ice-mail. Mail client
  3.     By: Aron (Gulligagardinen)
  4.    
  5.     Terms Of Use:
  6.         This file is provided as it is, you may not delete this terms of use protocol from the source code.
  7.        
  8.         You may change what you want in this code. I even encourage you to do changes after your own preferences! =D
  9.         Provided that you credit the original maker: Gulligagardinen.
  10.        
  11.         You may use this program in any kind of UI or OS, provided that you credit the original maker: Gulligagardinen.
  12.        
  13.        
  14.     If you experience any bugs or glitches, please contact me.
  15. --]]
  16.  
  17. --Mail Inbox
  18. local mail = {}
  19.  
  20. --Main variables that contains general information
  21. if not os.getComputerLabel() then os.setComputerLabel("User") end
  22. local w, h = term.getSize()
  23. local slc = 0
  24. local slc2 = 1
  25. local modem;
  26. local sSide = ""
  27. local newE = 0
  28.  
  29. running = true
  30.  
  31. quit = function() --Simple function that exit the program
  32.     term.setTextColor(1) term.setBackgroundColor(32768)
  33.     term.clear() term.setCursorPos(w/2-20/2, 1) term.write("Thanks for using! =D") term.setCursorPos(1, 2)
  34.     running = false
  35. end
  36.  
  37. --Getting all the available side
  38. local validSides = {}
  39. for k, v in pairs(rs.getSides()) do
  40.     validSides[v] = true
  41. end
  42.  
  43. --Useful variables and commands
  44. local function setColor(tC, bC)
  45.     if not tC then tC = 1 end
  46.     if not bC then bC = 32768 end
  47.     term.setTextColor(tC) term.setBackgroundColor(bC)
  48. end
  49.  
  50. local function printCenter(str, y, tC, bC)
  51.     if str then
  52.         setColor(tC, bC)
  53.         if not y then y = 1 end
  54.         term.setCursorPos((w/2)-(#str/2), y)
  55.         term.write(str)
  56.     end
  57. end
  58.  
  59. local function printRight(str, y, tC, bC)
  60.     if str then
  61.         setColor(tC, bC)
  62.         if not y then y = 1 end
  63.         term.setCursorPos(w-#str, y)
  64.         term.write(str)
  65.     end
  66. end
  67.  
  68. local function printLeft(str, y, tC, bC)
  69.     if str then
  70.         setColor(tC, bC)
  71.         if not y then y = 1 end
  72.         term.setCursorPos(1, y)
  73.         term.write(str)
  74.     end
  75. end
  76.  
  77. local function printHere(str, x, y, tC, bC)
  78.     if str then
  79.         setColor(tC, bC)
  80.         if not x then x = 1 end
  81.         if not y then y = 1 end
  82.         term.setCursorPos(x, y)
  83.         term.write(str)
  84.     end
  85. end
  86.  
  87. local function printLoad()
  88.     term.setCursorPos(w/2-7, h/2)
  89.     term.setTextColor(32768) term.setBackgroundColor(256)
  90.     term.write("Loading")
  91.     textutils.slowWrite(".....", 1.5)
  92. end
  93.  
  94. --Modem functions: open, close and isOpen
  95. local function open()
  96.     modem = peripheral.wrap(sSide)
  97.     modem.open(os.getComputerID())
  98. end
  99.  
  100. local function close()
  101.     if sSide ~= "" then
  102.         modem.closeAll()
  103.     end
  104. end
  105.  
  106. local function isOpen()
  107.     if modem.isOpen(os.getComputerID()) then
  108.         return true;
  109.     end
  110.     return false;
  111. end
  112.  
  113. --Send and receive functions
  114. local function send(channel, str, rec)
  115.     if isOpen() then
  116.         local rCh;
  117.         if not rec then rCh = os.getComputerID() else rCh = rec end
  118.         if sSide ~= "" then
  119.             modem.transmit(channel, rCh, str)
  120.         else
  121.             error("No chosen modem side")
  122.         end
  123.     else
  124.         error("Please make sure that the modem is opened")
  125.     end
  126. end
  127.  
  128. local function receive(n)
  129.     if isOpen() then
  130.         os.startTimer(n)
  131.         local event, side, sCh, rCh, msg, sd = os.pullEvent()
  132.         if event == "modem_message" then
  133.             return sCh, rCh, msg; --Sender Channel, Reply Channel, Message
  134.         end
  135.         return false;
  136.     else
  137.         error("Please make sure that the modem is opened")
  138.     end
  139. end
  140.  
  141. --Server talking
  142. local function getAmount()
  143.     send(65535, "pleaseIndex")
  144.     local sCh, rCh, msg = receive(1)
  145.     if not msg then newE = 0
  146.     else newE = tonumber(msg) end
  147. end
  148.  
  149. local function getMessage()
  150.     repeat
  151.         for i = 1, newE do
  152.             send(65535, "indexTextMsg "..tostring(i))
  153.             local sCh, rCh, msg = receive(1)
  154.             if msg then
  155.                 mail[#mail+1] = {sender = rCh, message = msg}
  156.                 sleep(1)
  157.             end
  158.         end
  159.         if #mail < newE or #mail > newE then
  160.             mail = {}
  161.         end
  162.     until #mail == newE
  163. end
  164.  
  165. local function removeMesssage(n)
  166.     if mail[n] then
  167.         newE = newE-1
  168.         table.remove(mail, n)
  169.         send(65535, "removeIndex "..tostring(n))
  170.     end
  171. end
  172.  
  173. local function sendMessage(n, str)
  174.     if str ~= "" then
  175.         send(65535, tostring(n).."]"..str)
  176.     end
  177. end
  178.  
  179. --Graphical select modem side
  180. function checkSide()
  181.     if sSide == "" then
  182.         repeat
  183.             printCenter("        Modem side        ", h/2-1, 1, 128)
  184.             printCenter("                          ", h/2, 1, 128)
  185.             printCenter("                        ", h/2, 1, 256)
  186.             printCenter("                          ", h/2+1, 1, 128)
  187.             term.setCursorPos(w/2-12, h/2)
  188.             setColor(1, 256)
  189.             sSide = io.read()
  190.         until sSide ~= "" and sSide ~= nil and validSides[sSide]
  191.     end
  192. end
  193.  
  194. --Graphical User Interface
  195.  
  196. local function printTime()
  197.     printRight(tostring(textutils.formatTime(os.time(), false)), h, 1, 128)
  198. end
  199.  
  200. local function drawTitle()
  201.     setColor(1, 128)
  202.     term.setCursorPos(1, 1) term.clearLine()
  203.     term.setCursorPos(1, h) term.clearLine()
  204.     printLeft("X", 1, 16384, 128)
  205.     printCenter("Mail Client", 1, 1, 128)
  206.     printLeft("User: "..os.getComputerLabel()..'['..os.getComputerID()..']', h, 1, 128)
  207.     printTime()
  208.     printCenter("New: "..tostring(newE), h, 1, 128)
  209. end
  210.  
  211. local function drawDesktop()
  212.     setColor(32768, 256) term.clear()
  213.     drawTitle()
  214. end
  215.  
  216. --Buttons
  217. local aMopt = { --This table contains additional buttons for the inbox menu
  218.     [4] = {str = "  Back   ", y = 10, action = function() slc=slc-1 slc2=1 end};
  219.     [5] = {str = "   New   ", y = 12, action = function() slc=4 slc2=1 end};
  220. }
  221.  
  222. local mopt = { --Standard buttons for both menus
  223.     [1] = {str = "  Inbox  ",  y = 4, action = function() slc2=3 slc=1 parallel.waitForAny(getMessage, printLoad) end};
  224.     [2] = {str = "Load mail", y = 6, action = function() slc2=2 getAmount() end};
  225.     [3] = {str = "  Quit   ", y = 8, action = function() quit() end};
  226. }
  227.  
  228. setmetatable(mopt, {__index = aMopt}) --We use aMopt as a meta table to mopt. Just to make it a lot easier
  229.  
  230. --Menus
  231. local function drawMenu()
  232.     for k, v in pairs(mopt) do
  233.         if v == mopt[slc2] then
  234.             printCenter('['..v.str.."] ", v.y, 32768, 1)
  235.         else
  236.             printCenter(v.str, v.y, 1, 256)
  237.         end
  238.     end
  239. end
  240.  
  241. local function drawMenu1()
  242.     for i = 3, #mopt+2 do
  243.         if mopt[i] == mopt[slc2] and slc == 1 then
  244.             printRight('['..mopt[i].str..']', mopt[i].y, 32768, 1)
  245.         else
  246.             printRight(mopt[i].str, mopt[i].y, 1, 256)
  247.         end
  248.     end
  249.     printLeft("  You got "..#mail.." new messages:", 2, 1, 256)
  250.     if #mail > 0 then
  251.         local k = 4
  252.         for i = 1, #mail do
  253.             printLeft("Computer "..tostring(mail[i].sender).." has sent a message for you!", k, 32768, 256)
  254.             k = k+1
  255.         end
  256.     end
  257. end
  258.  
  259. local function drawMenu2()
  260.     drawMenu1()
  261.     printHere("<", 37+string.len(tostring(mail[slc2].sender)), 3+slc2, 32, 256)
  262. end
  263.  
  264. local function drawMenu3()
  265.     printLeft("Computer "..mail[slc2].sender.." has a message for you:", 3, 32768, 256)
  266.     printLeft(string.rep("_", w-5), 4, 32768, 256)
  267.    
  268.     local text = {[1] = tostring(mail[slc2].message)}
  269.     local i = 1
  270.     while true do
  271.         if #text[i] > 46 then
  272.             text[i+1] = text[i]:sub(47, #text[i])
  273.             text[i] = text[i]:sub(1, 46)
  274.             i = i+1
  275.         else
  276.             break;
  277.         end
  278.     end
  279.     for i = 1, #text do
  280.         printLeft(text[i], 5+i, 32768, 256)
  281.     end
  282.    
  283.     printRight("Hit R to remove", h-2, 1, 256)
  284.     printRight("Hit Q to exit", h-1, 1, 256)
  285. end
  286.  
  287. local function drawMenu4()
  288.     local comp, msg, status = "", "", false
  289.     while true do
  290.         printLeft("New:", 3, 32768, 256)
  291.         printLeft(string.rep("_", w-5), 4, 32768, 256)
  292.         printLeft("Computer:                    ", 5, 32768, 256)
  293.         term.setCursorPos(11, 5)
  294.         comp = io.read()
  295.         if comp == "exit" then
  296.             break;
  297.         elseif comp ~= "" then
  298.             status = true
  299.             break;
  300.         end
  301.     end
  302.     if status then
  303.         printLeft("Message:", 7, 32768, 256)
  304.         for i = 1, h-8 do
  305.             term.setCursorPos(1, 7+i)
  306.             msg = msg.." "..io.read()
  307.         end
  308.         sendMessage(comp, msg)
  309.     end
  310.     slc=1 slc2=3
  311. end
  312.  
  313. --Final Drawing
  314. local function drawButtons()
  315.     drawDesktop()
  316.     if slc == 0 then
  317.         drawMenu()
  318.     elseif slc == 1 then
  319.         drawMenu1()
  320.     elseif slc == 2 then
  321.         drawMenu2()
  322.     elseif slc == 3 then
  323.         drawMenu3()
  324.     elseif slc == 4 then
  325.         drawMenu4()
  326.     else    --Just a check, if something went wrong.
  327.         slc=0 slc2=1
  328.     end
  329. end
  330.  
  331. local function updateKeys(event)
  332.     local a
  333.     local active
  334.     if slc == 0 then
  335.         a = #mopt
  336.         active = true
  337.     elseif slc == 1 then
  338.         a = #mopt+2
  339.         active = true
  340.     elseif slc == 2 then
  341.         a = #mail
  342.         active = true
  343.     elseif slc == 3 then
  344.         a = 0
  345.         active = false
  346.     elseif slc == 4 then
  347.         a = #mopt+2
  348.         active = true
  349.     end
  350.    
  351.     if active then
  352.         if event[2] == keys.down then
  353.             if slc2 < a then
  354.                 slc2=slc2+1
  355.             end
  356.         elseif event[2] == keys.up then
  357.             if slc2 > 1 then
  358.                 if slc == 1 or slc == 4 then
  359.                     if slc2 > 3 then slc2=slc2-1 end
  360.                 else
  361.                     slc2=slc2-1
  362.                 end
  363.             end
  364.         elseif event[2] == 28 then
  365.             if slc == 2 then
  366.                 slc=3
  367.             else
  368.                 mopt[slc2].action()
  369.             end
  370.         elseif event[2] == keys.left then
  371.             if slc == 1 and #mail > 0 then
  372.                 slc=2 slc2=1
  373.             end
  374.         elseif event[2] == keys.right then
  375.             if slc == 2 then
  376.                 slc=1 slc2=3
  377.             end
  378.         end
  379.        
  380.     else
  381.         if event[2] == keys.r then
  382.             if slc == 3 or slc == 4 then
  383.                 removeMesssage(slc2)
  384.                 slc=1 slc2=3
  385.             end
  386.         elseif event[2] == keys.q then
  387.             if slc == 3 or slc == 4 then
  388.                 slc=1 slc2=3
  389.             end
  390.         end
  391.     end
  392. end
  393.  
  394. --Main loop
  395. checkSide()
  396. open()
  397. getAmount()
  398. while running do
  399.     drawButtons()
  400.     os.startTimer(1)
  401.     local event = {os.pullEventRaw()}
  402.    
  403.     if event[1] == "mouse_click" or event[1] == "monitor_touch" then
  404.         if event[3] == 1 and event[4] == 1 then quit() end
  405.        
  406.     elseif event[1] == "key" then
  407.         updateKeys(event)
  408.        
  409.     elseif event[1] == "timer" then
  410.         printTime()
  411.    
  412.     elseif event[1] == "terminate" then quit()
  413.     else
  414.         slc = 0
  415.     end
  416. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement