Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local configFile = ".emailClientConfig"
- local config = {
- modem = "top",
- server = 0
- }
- local function waitForSpace()
- local key = false
- while not key do
- local event, _key = os.pullEvent("key")
- key = (_key == 57)
- end
- end
- local function saveConfig()
- local handle = fs.open(configFile, "w")
- handle.write(textutils.serialize(config))
- handle.close()
- end
- local function loadConfig()
- local handle = fs.open(configFile, "r")
- if not handle then
- saveConfig() -- saves default config
- handle = fs.open(configFile, "r")
- end
- config = textutils.unserialize(handle.readAll())
- handle.close()
- end
- local function isValidSide(_side)
- local rsSides = rs.getSides()
- local valid = false
- for key, value in pairs(rsSides) do valid = (value == _side) or valid end
- return valid
- end
- local function configureModem()
- term.clear()
- term.setCursorPos(1, 1)
- print("Error: No modem on side '" .. config.modem .. "'")
- local side = false
- while not side do
- print("Enter modem side:")
- local _side = read()
- if isValidSide(_side) then
- if peripheral.getType(_side) == "modem" then
- print("Configuration updated")
- side = _side
- print("Press Space to continue...")
- waitForSpace()
- else
- print("There is no modem on side '" .. _side .. "'")
- end
- else
- print("Invalid side")
- end
- end
- config.modem = side
- saveConfig()
- end
- local function configureServer()
- term.clear()
- term.setCursorPos(1, 1)
- print("Current server ID: " .. config.server)
- local server = false
- while not server do
- print("Enter server ID:")
- local _server = read()
- if tostring(tonumber(_server)) == _server then
- print("Configuration updated")
- server = _server
- print("Press Space to continue...")
- waitForSpace()
- else
- print("'" .. _server .. "' is not a valid ID")
- end
- end
- config.server = tonumber(server)
- saveConfig()
- end
- loadConfig()
- local termw, termh = term.getSize()
- local modemAlreadyOpened
- if isValidSide(config.modem) then
- if peripheral.getType(config.modem) == "modem" then
- modemAlreadyOpened = rednet.isOpen(config.modem)
- rednet.open(config.modem)
- else
- configureModem()
- modemAlreadyOpened = rednet.isOpen(config.modem)
- rednet.open(config.modem)
- end
- else
- configureModem()
- modemAlreadyOpened = rednet.isOpen(config.modem)
- rednet.open(config.modem)
- end
- local function receive()
- local email = false
- local from, message
- while not email do
- event, sender, rmessage = os.pullEvent("rednet_message")
- if sender == config.server then
- local fullMessage = textutils.unserialize(rmessage)
- from = fullMessage.from
- message = fullMessage.message
- email = true
- end
- end
- return from, message
- end
- local running = true
- while running do
- term.clear()
- term.setCursorPos(1, 1)
- print("Type 'e' to send an email")
- print("Type 'r' to receive an email")
- print("Type 's' to select server")
- print("Type 'c' to create an account")
- print("Type 'x' to exit")
- local userInput = read()
- if string.find(string.lower(userInput), "r") then
- print("Waiting for incoming email...")
- param1, param2 = receive()
- term.clear()
- term.setCursorPos(1, 1)
- print(" You have just received an email!")
- print(" From: " .. param1)
- print(" Message: " .. param2)
- term.setCursorPos(1, termh)
- term.write("Press Space to go back")
- waitForSpace()
- elseif string.find(string.lower(userInput), "e") then
- term.clear()
- term.setCursorPos(1, 1)
- local fullMessage = {}
- write("To which user would you like this message to be forwarded to?\n> ")
- fullMessage.user = read()
- write("And what is the message you want to forward?\n> ")
- fullMessage.message = read()
- fullMessage.ecommand = "send"
- rednet.send(config.server, textutils.serialize(fullMessage))
- print("Waiting for response...")
- local response = false
- while not response do
- local sender, message = rednet.receive(5)
- if not sender then
- response = true
- print("Failure! (Server didn't respond)")
- print("Press Space to go back")
- waitForSpace()
- elseif sender == config.server then
- local status = textutils.unserialize(message).status
- if status == "ok" then
- print("Sent!")
- print("Press Space to go back")
- waitForSpace()
- else
- print("Failure! (Status: '" .. status .. "')")
- end
- end
- end
- elseif string.find(string.lower(userInput), "c") then
- term.clear()
- term.setCursorPos(1, 1)
- local fullMessage = {}
- write("Type in your username:\n> ")
- fullMessage.username = read()
- fullMessage.ecommand = "create"
- rednet.send(config.server, textutils.serialize(fullMessage))
- print("Waiting for response...")
- local response = false
- while not response do
- local sender, message = rednet.receive(5)
- if not sender then
- response = true
- print("Failure! (Server didn't respond)")
- print("Press Space to go back")
- waitForSpace()
- elseif sender == config.server then
- local status = textutils.unserialize(message).status
- if status == "ok" then
- print("Account created successfully!")
- print("Press Space to go back")
- waitForSpace()
- else
- print("Failure! (Status: '" .. status .. "')")
- end
- end
- end
- elseif string.find(string.lower(userInput), "s") then
- configureServer()
- elseif string.find(string.lower(userInput), "x") then
- running = false
- end
- end
- if not modemAlreadyOpened then rednet.close(config.modem) end
- term.clear()
- term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement