Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- GLOBALS
- local serverID = -1
- local side = "top"
- local myID = os.getComputerID()
- local template = "$SERVER$MAIL$EMAIL!ED!"
- local w, h = term.getSize()
- local states = {
- ["home"] = {
- id = 1,
- options = 6
- },
- ["composeMail"] = {
- id = 2,
- options = 4
- },
- ["changeName"] = {
- id = 3,
- options = 3
- },
- ["hasMailBeenSent"] = {
- id = 4,
- options = 0
- },
- ["mails"] = {
- id = 5,
- options = 3
- },
- ["archive"] = {
- id = 6,
- options = 4
- }
- }
- local state = 1
- local select = 1
- local username = "New User"
- local newMail = {
- recipient = "",
- body = ""
- }
- local tempUN = ""
- local newMessages = 0
- local accepted_chars = {
- 'zero','one','two','three','four','five','six','seven','eight','nine','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','space','comma'
- }
- local mails = {}
- local archivedMails = {}
- if fs.exists("gravimail/client/archivedMails") then
- local file = fs.open("gravimail/client/archivedMails", "r")
- file = file.readAll()
- archivedMails = textutils.unserialise(file)
- end
- local currentMail = 1
- -- Functions & Stuff
- function writeToFile(data, filename)
- local file = fs.open(filename, "w")
- file.write(textutils.serialise(data))
- file.close()
- end
- function readFromFile(filename)
- if not fs.exists(filename) then
- return
- end
- local file = fs.open(filename, "r")
- local data = file.readAll()
- file.close()
- return data
- end
- function split(str, pat)
- local t = { }
- local fpat = "(.-)"..pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- function includes(table, string)
- if #table < 1 then return false, -1 end
- for i = 1, #table, 1 do
- if table[i] == string then return true, i end
- end
- return false, -1
- end
- function printCentered(text, y)
- term.setCursorPos(w/2 - #text/2, y)
- term.write(text)
- end
- function sendMail(mail, username)
- local time = os.date("*t")
- local msg = template..mail.recipient.."!ED!"..username.."!ED!"..time.day.."!ED!"..time.month.."!ED!"..time.year.."!ED!"..time.hour.."!ED!"..time.min.."!ED!"..mail.body.."!ED!"
- rednet.open(side)
- rednet.send(serverID, msg)
- rednet.close()
- end
- function refreshMails()
- currentMail = 1
- local msg = "$SERVER$MAIL$REQUEST!ED!"..myID.."!ED!EMAILS!ED!"
- rednet.open(side)
- rednet.send(serverID, msg)
- term.clear()
- printCentered("Requesting mails...", h/2)
- local id, message = rednet.receive(1)
- if message and string.find(message, "$RESPONSE") then
- message = string.gsub(message, "$RESPONSE", "")
- local newMails = split(message, "!NM!")
- for i, mail in pairs(newMails) do
- mailData = split(mail, "!ED!")
- table.insert(mails, mailData)
- term.clear()
- end
- end
- newMessages = #mails
- rednet.close()
- end
- function decodeKey(key)
- key = keys.getName(key)
- succes, index = includes(accepted_chars,key)
- if succes then
- if index <= 10 then
- return tostring(index - 1)
- elseif key == "space" then
- return " "
- elseif key == "comma" then
- return ","
- else
- return key
- end
- end
- return ""
- end
- ---- UI
- function printHeader(headerText)
- term.setCursorPos(1,1)
- print(string.rep("-", w))
- term.setCursorPos(w/2 - #headerText/2, 2)
- print(headerText)
- term.setCursorPos(1, 3)
- print(string.rep("-", w))
- end
- function printHome()
- if select == 1 then
- printCentered("[ COMPOSE ]", 5)
- printCentered("CHANGE NAME", 6)
- printCentered(" MAILS ", 7)
- printCentered(" ARCHIVE ", 8)
- printCentered(" REFRESH ", 9)
- printCentered("LEAVE", 10)
- elseif select == 2 then
- printCentered("COMPOSE", 5)
- printCentered("[ CHANGE NAME ]", 6)
- printCentered("MAILS", 7)
- printCentered("ARCHIVE", 8)
- printCentered("REFRESH", 9)
- printCentered("LEAVE", 10)
- elseif select == 3 then
- printCentered("COMPOSE", 5)
- printCentered("CHANGE NAME", 6)
- printCentered("[ MAILS ]", 7)
- printCentered("ARCHIVE", 8)
- printCentered("REFRESH", 9)
- printCentered("LEAVE", 10)
- elseif select == 4 then
- printCentered("COMPOSE", 5)
- printCentered("CHANGE NAME", 6)
- printCentered("MAILS", 7)
- printCentered("[ ARCHIVE ]", 8)
- printCentered("REFRESH", 9)
- printCentered("LEAVE", 10)
- elseif select == 5 then
- printCentered("COMPOSE", 5)
- printCentered("CHANGE NAME", 6)
- printCentered("MAILS", 7)
- printCentered("ARCHIVE", 8)
- printCentered("[ REFRESH ]", 9)
- printCentered("LEAVE", 10)
- elseif select == 6 then
- printCentered("COMPOSE", 5)
- printCentered("CHANGE NAME", 6)
- printCentered("MAILS", 7)
- printCentered("ARCHIVE", 8)
- printCentered("REFRESH", 9)
- printCentered("[ LEAVE ]", 10)
- end
- term.setCursorPos(1, h-1)
- term.write("Hi, "..username.."! Your ID: "..myID)
- term.setCursorPos(1, h)
- term.write("You have "..newMessages.." new messages!")
- end
- function printCompose()
- term.setCursorPos(1, 5)
- term.write("TO: ")
- term.write(newMail.recipient)
- term.setCursorPos(1, 6)
- print("BODY: ")
- term.setCursorPos(1, 7)
- print(newMail.body)
- if select == 1 then
- term.setCursorPos(10, h)
- term.write(" SEND ")
- term.setCursorPos(w - 10 - #" CANCEL ", h)
- term.write(" CANCEL ")
- term.setCursorPos(#"TO: " + #newMail.recipient + 1, 5)
- term.setCursorBlink(true)
- elseif select == 2 then
- term.setCursorPos(10, h)
- term.write(" SEND ")
- term.setCursorPos(w - 10 - #" CANCEL ", h)
- term.write(" CANCEL ")
- term.setCursorPos(#newMail.body + 1, 7)
- term.setCursorBlink(true)
- elseif select == 3 then
- term.setCursorPos(10, h)
- term.write("[ SEND ]")
- term.setCursorPos(w - 10 - #" CANCEL ", h)
- term.write(" CANCEL ")
- term.setCursorBlink(false)
- elseif select == 4 then
- term.setCursorPos(10, h)
- term.write(" SEND ")
- term.setCursorPos(w - 10 - #"[ CANCEL ]", h)
- term.write("[ CANCEL ]")
- term.setCursorBlink(false)
- end
- end
- function printChangeName()
- term.setCursorPos(1, 5)
- term.write("NEW NAME:")
- term.setCursorPos(1, 8)
- term.write("(press enter to start typing)")
- if select == 1 then
- term.setCursorPos(10, h)
- term.write(" CONFIRM ")
- term.setCursorPos(w - 10 - #" CANCEL ", h)
- term.write(" CANCEL ")
- term.setCursorBlink(true)
- elseif select == 2 then
- term.setCursorPos(10, h)
- term.write("[ CONFIRM ]")
- term.setCursorPos(w - 10 - #" CANCEL ", h)
- term.write(" CANCEL ")
- term.setCursorBlink(false)
- elseif select == 3 then
- term.setCursorPos(10, h)
- term.write(" CONFIRM ")
- term.setCursorPos(w - 10 - #"[ CANCEL ]", h)
- term.write("[ CANCEL ]")
- term.setCursorBlink(false)
- end
- term.setCursorPos(1, 6)
- print(tempUN)
- term.setCursorPos(#tempUN + 1, 6)
- end
- function printHasMailBeenSent()
- printCentered("Mail has been sent!", h/2)
- end
- function printMails()
- if #mails > 0 then
- local mail = mails[currentMail]
- term.setCursorPos(1,1)
- print("From: "..mail[2])
- term.setCursorPos(1, 2)
- print("Sent at: "..mail[3]..", "..mail[4])
- term.setCursorPos(1, 3)
- print(string.rep("-", 15))
- term.setCursorPos(1, 4)
- print(mail[5])
- else
- printCentered("No emails found.", h/2)
- end
- if select == 1 then
- term.setCursorPos(w - #"[ CANCEL ]", 1)
- print("[ CANCEL ]")
- term.setCursorPos(3, h)
- term.write(" ARCHIVE ")
- term.setCursorPos(w - 3 - #" DELETE ", h)
- term.write(" DELETE ")
- elseif select == 2 then
- term.setCursorPos(w - #" CANCEL ", 1)
- print(" CANCEL ")
- term.setCursorPos(3, h)
- term.write(" ARCHIVE ")
- term.setCursorPos(w - 3 - #"[ DELETE ]", h)
- term.write("[ DELETE ]")
- elseif select == 3 then
- term.setCursorPos(w - #" CANCEL ", 1)
- print(" CANCEL ")
- term.setCursorPos(3, h)
- term.write("[ ARCHIVE ]")
- term.setCursorPos(w - 3 - #" DELETE ", h)
- term.write(" DELETE ")
- end
- end
- function printArchive()
- if #archivedMails > 0 then
- local mail = archivedMails[currentMail]
- term.setCursorPos(1,1)
- print("From: "..mail[2])
- term.setCursorPos(1, 2)
- print("Sent at: "..mail[3]..", "..mail[4])
- term.setCursorPos(1, 3)
- print(string.rep("-", 15))
- term.setCursorPos(1, 4)
- print(mail[5])
- else
- printCentered("No emails found.", h/2)
- end
- if select == 1 then
- term.setCursorPos(w - #"[ CANCEL ]", 1)
- print("[ CANCEL ]")
- term.setCursorPos(w - 3 - #" NEXT ", h)
- term.write(" NEXT ")
- printCentered("DELETE", h)
- term.setCursorPos(3, h)
- term.write(" BACK ")
- elseif select == 2 then
- term.setCursorPos(w - #" CANCEL ", 1)
- print(" CANCEL ")
- term.setCursorPos(w - 3 - #"[ NEXT ]", h)
- term.write("[ NEXT ]")
- printCentered("DELETE", h)
- term.setCursorPos(3, h)
- term.write(" BACK ")
- elseif select == 3 then
- term.setCursorPos(w - #" CANCEL ", 1)
- print(" CANCEL ")
- term.setCursorPos(w - 3 - #" NEXT ", h)
- term.write(" NEXT ")
- printCentered("[ DELETE ]", h)
- term.setCursorPos(3, h)
- term.write(" BACK ")
- elseif select == 4 then
- term.setCursorPos(w - #" CANCEL ", 1)
- print(" CANCEL ")
- term.setCursorPos(w - 3 - #" NEXT ", h)
- term.write(" NEXT ")
- printCentered("DELETE", h)
- term.setCursorPos(3, h)
- term.write("[ BACK ]")
- end
- end
- function runUi()
- term.clear()
- while true do
- if state == states["home"].id then
- term.clear()
- printHeader("GRAVIMAIL CLIENT")
- printHome()
- local e, key = os.pullEvent("key")
- if key == keys.up and select > 1 then select = select - 1
- elseif key== keys.down and select < states["home"].options then select = select + 1
- elseif key== keys.enter then
- if select == 1 then
- state = 2
- select = 1
- elseif select == 2 then
- state = 3
- select = 1
- refreshMails()
- elseif select == 3 then
- state = 5
- select = 1
- elseif select == 4 then
- state = 6
- select = 1
- currentMail = 1
- elseif select == 5 then
- refreshMails()
- elseif select == 6 then
- term.clear()
- printCentered("Bye!", h/2)
- os.sleep(0.5)
- term.clear()
- term.setCursorPos(1, 1)
- return
- end
- end
- elseif state == states["composeMail"].id then
- term.clear()
- printHeader("COMPOSE NEW MAIL")
- printCompose()
- local e, key = os.pullEvent("key")
- if key == keys.up and select > 1 then select = select - 1
- elseif key == keys.down and select < states["composeMail"].options then select = select + 1
- elseif key == keys.enter then
- if select == 3 then
- if newMail.recipient:gsub('%W','') ~= "" and newMail.body:gsub('%W','') ~= "" then
- sendMail(newMail,username)
- newMail.recipient = ""
- newMail.body = ""
- select = 1
- state = 4
- end
- elseif select == 4 then
- state = 1
- select = 1
- end
- elseif select == 1 then
- local decodedKey = decodeKey(key)
- if tonumber(decodedKey) then
- newMail.recipient = newMail.recipient..decodeKey(key)
- end
- if key == keys.backspace then
- newMail.recipient = string.sub(newMail.recipient, 1, #newMail.recipient - 1)
- end
- elseif select == 2 then
- newMail.body = newMail.body..decodeKey(key)
- if key == keys.backspace then
- newMail.body = string.sub(newMail.body, 1, #newMail.body - 1)
- end
- end
- elseif state == states["changeName"].id then
- term.clear()
- printHeader("CHANGE NAME")
- printChangeName()
- local e, key = os.pullEvent("key")
- if key == keys.up and select > 1 then select = select - 1
- elseif key == keys.down and select < states["changeName"].options then select = select + 1
- elseif key == keys.enter then
- if select == 1 then
- select = select + 1
- elseif select == 2 then
- if tempUN:gsub('%W','') ~= "" then
- username = tempUN
- tempUN = ""
- state = 1
- select = 1
- end
- elseif select == 3 then
- tempUN = ""
- state = 1
- select = 1
- end
- elseif select == 1 then
- tempUN = tempUN..decodeKey(key)
- if key == keys.backspace then
- tempUN = string.sub(tempUN, 1, #tempUN - 1)
- end
- end
- elseif state == states["hasMailBeenSent"].id then
- term.clear()
- printHasMailBeenSent()
- os.sleep(0.5)
- state = 1
- elseif state == states["mails"].id then
- term.clear()
- printMails()
- local e, key = os.pullEvent("key")
- if key == keys.up and select > 1 then select = select - 1
- elseif key== keys.down and select < states["mails"].options then select = select + 1
- elseif key== keys.enter then
- if select == 1 then
- state = 1
- select = 1
- elseif select == 2 then
- table.remove(mails, currentMail)
- newMessages = #mails
- if currentMail < #mails then
- currentMail = currentMail + 1
- elseif currentMail > 1 then
- currentMail = currentMail - 1
- end
- elseif select == 3 then
- table.insert(archivedMails, mails[currentMail])
- writeToFile(archivedMails, "gravimail/client/archivedMails")
- table.remove(mails, currentMail)
- newMessages = #mails
- if currentMail < #mails then
- currentMail = currentMail + 1
- elseif currentMail > 1 then
- currentMail = currentMail - 1
- end
- end
- end
- elseif state == states["archive"].id then
- term.clear()
- printArchive()
- local e, key = os.pullEvent("key")
- if key == keys.up and select > 1 then select = select - 1
- elseif key== keys.down and select < states["archive"].options then select = select + 1
- elseif key== keys.enter then
- if select == 1 then
- state = 1
- select = 1
- elseif select == 2 then
- if currentMail < #archivedMails then
- currentMail = currentMail + 1
- end
- elseif select == 3 then
- table.remove(archivedMails, currentMail)
- writeToFile(archivedMails, "gravimail/client/archivedMails")
- if currentMail < #archivedMails then
- currentMail = currentMail + 1
- elseif currentMail > 1 then
- currentMail = currentMail - 1
- end
- elseif select == 4 then
- if currentMail > 1 then
- currentMail = currentMail - 1
- end
- end
- end
- end
- os.sleep(0.1)
- end
- end
- --Rednet Logic
- if not fs.exists("gravinet/client/pref") then
- error("No preferences setup. Make sure to run the connect script before running GraviNet apps!")
- return
- end
- local prefs = fs.open("gravinet/client/pref", "r")
- prefs = prefs.readAll()
- prefs = textutils.unserialise(prefs)
- serverID = prefs.routerId
- side = prefs.side
- runUi()
Add Comment
Please, Sign In to add comment