Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Ice-mail. Mail client
- By: Aron (Gulligagardinen)
- Terms Of Use:
- This file is provided as it is, you may not delete this terms of use protocol from the source code.
- You may change what you want in this code. I even encourage you to do changes after your own preferences! =D
- Provided that you credit the original maker: Gulligagardinen.
- You may use this program in any kind of UI or OS, provided that you credit the original maker: Gulligagardinen.
- If you experience any bugs or glitches, please contact me.
- --]]
- --Mail Inbox
- local mail = {}
- --Main variables that contains general information
- if not os.getComputerLabel() then os.setComputerLabel("User") end
- local w, h = term.getSize()
- local slc = 0
- local slc2 = 1
- local modem;
- local sSide = ""
- local newE = 0
- running = true
- quit = function() --Simple function that exit the program
- term.setTextColor(1) term.setBackgroundColor(32768)
- term.clear() term.setCursorPos(w/2-20/2, 1) term.write("Thanks for using! =D") term.setCursorPos(1, 2)
- running = false
- end
- --Getting all the available side
- local validSides = {}
- for k, v in pairs(rs.getSides()) do
- validSides[v] = true
- end
- --Useful variables and commands
- local function setColor(tC, bC)
- if not tC then tC = 1 end
- if not bC then bC = 32768 end
- term.setTextColor(tC) term.setBackgroundColor(bC)
- end
- local function printCenter(str, y, tC, bC)
- if str then
- setColor(tC, bC)
- if not y then y = 1 end
- term.setCursorPos((w/2)-(#str/2), y)
- term.write(str)
- end
- end
- local function printRight(str, y, tC, bC)
- if str then
- setColor(tC, bC)
- if not y then y = 1 end
- term.setCursorPos(w-#str, y)
- term.write(str)
- end
- end
- local function printLeft(str, y, tC, bC)
- if str then
- setColor(tC, bC)
- if not y then y = 1 end
- term.setCursorPos(1, y)
- term.write(str)
- end
- end
- local function printHere(str, x, y, tC, bC)
- if str then
- setColor(tC, bC)
- if not x then x = 1 end
- if not y then y = 1 end
- term.setCursorPos(x, y)
- term.write(str)
- end
- end
- local function printLoad()
- term.setCursorPos(w/2-7, h/2)
- term.setTextColor(32768) term.setBackgroundColor(256)
- term.write("Loading")
- textutils.slowWrite(".....", 1.5)
- end
- --Modem functions: open, close and isOpen
- local function open()
- modem = peripheral.wrap(sSide)
- modem.open(os.getComputerID())
- end
- local function close()
- if sSide ~= "" then
- modem.closeAll()
- end
- end
- local function isOpen()
- if modem.isOpen(os.getComputerID()) then
- return true;
- end
- return false;
- end
- --Send and receive functions
- local function send(channel, str, rec)
- if isOpen() then
- local rCh;
- if not rec then rCh = os.getComputerID() else rCh = rec end
- if sSide ~= "" then
- modem.transmit(channel, rCh, str)
- else
- error("No chosen modem side")
- end
- else
- error("Please make sure that the modem is opened")
- end
- end
- local function receive(n)
- if isOpen() then
- os.startTimer(n)
- local event, side, sCh, rCh, msg, sd = os.pullEvent()
- if event == "modem_message" then
- return sCh, rCh, msg; --Sender Channel, Reply Channel, Message
- end
- return false;
- else
- error("Please make sure that the modem is opened")
- end
- end
- --Server talking
- local function getAmount()
- send(65535, "pleaseIndex")
- local sCh, rCh, msg = receive(1)
- if not msg then newE = 0
- else newE = tonumber(msg) end
- end
- local function getMessage()
- repeat
- for i = 1, newE do
- send(65535, "indexTextMsg "..tostring(i))
- local sCh, rCh, msg = receive(1)
- if msg then
- mail[#mail+1] = {sender = rCh, message = msg}
- sleep(1)
- end
- end
- if #mail < newE or #mail > newE then
- mail = {}
- end
- until #mail == newE
- end
- local function removeMesssage(n)
- if mail[n] then
- newE = newE-1
- table.remove(mail, n)
- send(65535, "removeIndex "..tostring(n))
- end
- end
- local function sendMessage(n, str)
- if str ~= "" then
- send(65535, tostring(n).."]"..str)
- end
- end
- --Graphical select modem side
- function checkSide()
- if sSide == "" then
- repeat
- printCenter(" Modem side ", h/2-1, 1, 128)
- printCenter(" ", h/2, 1, 128)
- printCenter(" ", h/2, 1, 256)
- printCenter(" ", h/2+1, 1, 128)
- term.setCursorPos(w/2-12, h/2)
- setColor(1, 256)
- sSide = io.read()
- until sSide ~= "" and sSide ~= nil and validSides[sSide]
- end
- end
- --Graphical User Interface
- local function printTime()
- printRight(tostring(textutils.formatTime(os.time(), false)), h, 1, 128)
- end
- local function drawTitle()
- setColor(1, 128)
- term.setCursorPos(1, 1) term.clearLine()
- term.setCursorPos(1, h) term.clearLine()
- printLeft("X", 1, 16384, 128)
- printCenter("Mail Client", 1, 1, 128)
- printLeft("User: "..os.getComputerLabel()..'['..os.getComputerID()..']', h, 1, 128)
- printTime()
- printCenter("New: "..tostring(newE), h, 1, 128)
- end
- local function drawDesktop()
- setColor(32768, 256) term.clear()
- drawTitle()
- end
- --Buttons
- local aMopt = { --This table contains additional buttons for the inbox menu
- [4] = {str = " Back ", y = 10, action = function() slc=slc-1 slc2=1 end};
- [5] = {str = " New ", y = 12, action = function() slc=4 slc2=1 end};
- }
- local mopt = { --Standard buttons for both menus
- [1] = {str = " Inbox ", y = 4, action = function() slc2=3 slc=1 parallel.waitForAny(getMessage, printLoad) end};
- [2] = {str = "Load mail", y = 6, action = function() slc2=2 getAmount() end};
- [3] = {str = " Quit ", y = 8, action = function() quit() end};
- }
- setmetatable(mopt, {__index = aMopt}) --We use aMopt as a meta table to mopt. Just to make it a lot easier
- --Menus
- local function drawMenu()
- for k, v in pairs(mopt) do
- if v == mopt[slc2] then
- printCenter('['..v.str.."] ", v.y, 32768, 1)
- else
- printCenter(v.str, v.y, 1, 256)
- end
- end
- end
- local function drawMenu1()
- for i = 3, #mopt+2 do
- if mopt[i] == mopt[slc2] and slc == 1 then
- printRight('['..mopt[i].str..']', mopt[i].y, 32768, 1)
- else
- printRight(mopt[i].str, mopt[i].y, 1, 256)
- end
- end
- printLeft(" You got "..#mail.." new messages:", 2, 1, 256)
- if #mail > 0 then
- local k = 4
- for i = 1, #mail do
- printLeft("Computer "..tostring(mail[i].sender).." has sent a message for you!", k, 32768, 256)
- k = k+1
- end
- end
- end
- local function drawMenu2()
- drawMenu1()
- printHere("<", 37+string.len(tostring(mail[slc2].sender)), 3+slc2, 32, 256)
- end
- local function drawMenu3()
- printLeft("Computer "..mail[slc2].sender.." has a message for you:", 3, 32768, 256)
- printLeft(string.rep("_", w-5), 4, 32768, 256)
- local text = {[1] = tostring(mail[slc2].message)}
- local i = 1
- while true do
- if #text[i] > 46 then
- text[i+1] = text[i]:sub(47, #text[i])
- text[i] = text[i]:sub(1, 46)
- i = i+1
- else
- break;
- end
- end
- for i = 1, #text do
- printLeft(text[i], 5+i, 32768, 256)
- end
- printRight("Hit R to remove", h-2, 1, 256)
- printRight("Hit Q to exit", h-1, 1, 256)
- end
- local function drawMenu4()
- local comp, msg, status = "", "", false
- while true do
- printLeft("New:", 3, 32768, 256)
- printLeft(string.rep("_", w-5), 4, 32768, 256)
- printLeft("Computer: ", 5, 32768, 256)
- term.setCursorPos(11, 5)
- comp = io.read()
- if comp == "exit" then
- break;
- elseif comp ~= "" then
- status = true
- break;
- end
- end
- if status then
- printLeft("Message:", 7, 32768, 256)
- for i = 1, h-8 do
- term.setCursorPos(1, 7+i)
- msg = msg.." "..io.read()
- end
- sendMessage(comp, msg)
- end
- slc=1 slc2=3
- end
- --Final Drawing
- local function drawButtons()
- drawDesktop()
- if slc == 0 then
- drawMenu()
- elseif slc == 1 then
- drawMenu1()
- elseif slc == 2 then
- drawMenu2()
- elseif slc == 3 then
- drawMenu3()
- elseif slc == 4 then
- drawMenu4()
- else --Just a check, if something went wrong.
- slc=0 slc2=1
- end
- end
- local function updateKeys(event)
- local a
- local active
- if slc == 0 then
- a = #mopt
- active = true
- elseif slc == 1 then
- a = #mopt+2
- active = true
- elseif slc == 2 then
- a = #mail
- active = true
- elseif slc == 3 then
- a = 0
- active = false
- elseif slc == 4 then
- a = #mopt+2
- active = true
- end
- if active then
- if event[2] == keys.down then
- if slc2 < a then
- slc2=slc2+1
- end
- elseif event[2] == keys.up then
- if slc2 > 1 then
- if slc == 1 or slc == 4 then
- if slc2 > 3 then slc2=slc2-1 end
- else
- slc2=slc2-1
- end
- end
- elseif event[2] == 28 then
- if slc == 2 then
- slc=3
- else
- mopt[slc2].action()
- end
- elseif event[2] == keys.left then
- if slc == 1 and #mail > 0 then
- slc=2 slc2=1
- end
- elseif event[2] == keys.right then
- if slc == 2 then
- slc=1 slc2=3
- end
- end
- else
- if event[2] == keys.r then
- if slc == 3 or slc == 4 then
- removeMesssage(slc2)
- slc=1 slc2=3
- end
- elseif event[2] == keys.q then
- if slc == 3 or slc == 4 then
- slc=1 slc2=3
- end
- end
- end
- end
- --Main loop
- checkSide()
- open()
- getAmount()
- while running do
- drawButtons()
- os.startTimer(1)
- local event = {os.pullEventRaw()}
- if event[1] == "mouse_click" or event[1] == "monitor_touch" then
- if event[3] == 1 and event[4] == 1 then quit() end
- elseif event[1] == "key" then
- updateKeys(event)
- elseif event[1] == "timer" then
- printTime()
- elseif event[1] == "terminate" then quit()
- else
- slc = 0
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement