Advertisement
LBPHacker

Email client (for dexter9)

Feb 24th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.45 KB | None | 0 0
  1. local configFile = ".emailClientConfig"
  2. local config = {
  3.     modem = "top",
  4.     server = 0
  5. }
  6.  
  7. local function waitForSpace()
  8.     local key = false
  9.     while not key do
  10.         local event, _key = os.pullEvent("key")
  11.         key = (_key == 57)
  12.     end
  13. end
  14.  
  15. local function saveConfig()
  16.     local handle = fs.open(configFile, "w")
  17.     handle.write(textutils.serialize(config))
  18.     handle.close()
  19. end
  20.  
  21. local function loadConfig()
  22.     local handle = fs.open(configFile, "r")
  23.     if not handle then
  24.         saveConfig() -- saves default config
  25.         handle = fs.open(configFile, "r")
  26.     end
  27.     config = textutils.unserialize(handle.readAll())
  28.     handle.close()
  29. end
  30.  
  31. local function isValidSide(_side)
  32.     local rsSides = rs.getSides()
  33.     local valid = false
  34.     for key, value in pairs(rsSides) do valid = (value == _side) or valid end
  35.     return valid
  36. end
  37.  
  38. local function configureModem()
  39.     term.clear()
  40.     term.setCursorPos(1, 1)
  41.     print("Error: No modem on side '" .. config.modem .. "'")
  42.     local side = false
  43.     while not side do
  44.         print("Enter modem side:")
  45.         local _side = read()
  46.         if isValidSide(_side) then
  47.             if peripheral.getType(_side) == "modem" then
  48.                 print("Configuration updated")
  49.                 side = _side
  50.                 print("Press Space to continue...")
  51.                 waitForSpace()
  52.             else
  53.                 print("There is no modem on side '" .. _side .. "'")
  54.             end
  55.         else
  56.             print("Invalid side")
  57.         end
  58.     end
  59.     config.modem = side
  60.     saveConfig()
  61. end
  62.  
  63. local function configureServer()
  64.     term.clear()
  65.     term.setCursorPos(1, 1)
  66.     print("Current server ID: " .. config.server)
  67.     local server = false
  68.     while not server do
  69.         print("Enter server ID:")
  70.         local _server = read()
  71.         if tostring(tonumber(_server)) == _server then
  72.             print("Configuration updated")
  73.             server = _server
  74.             print("Press Space to continue...")
  75.             waitForSpace()
  76.         else
  77.             print("'" .. _server .. "' is not a valid ID")
  78.         end
  79.     end
  80.     config.server = tonumber(server)
  81.     saveConfig()
  82. end
  83.  
  84. loadConfig()
  85. local termw, termh = term.getSize()
  86. local modemAlreadyOpened
  87. if isValidSide(config.modem) then
  88.     if peripheral.getType(config.modem) == "modem" then
  89.         modemAlreadyOpened = rednet.isOpen(config.modem)
  90.         rednet.open(config.modem)
  91.     else
  92.         configureModem()
  93.         modemAlreadyOpened = rednet.isOpen(config.modem)
  94.         rednet.open(config.modem)
  95.     end
  96. else
  97.     configureModem()
  98.     modemAlreadyOpened = rednet.isOpen(config.modem)
  99.     rednet.open(config.modem)
  100. end
  101.  
  102. local function receive()
  103.     local email = false
  104.     local from, message
  105.     while not email do
  106.         event, sender, rmessage = os.pullEvent("rednet_message")
  107.         if sender == config.server then
  108.             local fullMessage = textutils.unserialize(rmessage)
  109.             from = fullMessage.from
  110.             message = fullMessage.message
  111.             email = true
  112.         end
  113.     end
  114.     return from, message
  115. end
  116.  
  117. local running = true
  118. while running do
  119.     term.clear()
  120.     term.setCursorPos(1, 1)
  121.     print("Type 'e' to send an email")
  122.     print("Type 'r' to receive an email")
  123.     print("Type 's' to select server")
  124.     print("Type 'c' to create an account")
  125.     print("Type 'x' to exit")
  126.     local userInput = read()
  127.     if string.find(string.lower(userInput), "r") then
  128.         print("Waiting for incoming email...")
  129.         param1, param2 = receive()
  130.         term.clear()
  131.         term.setCursorPos(1, 1)
  132.         print("    You have just received an email!")
  133.         print("       From: " .. param1)
  134.         print("    Message: " .. param2)
  135.         term.setCursorPos(1, termh)
  136.         term.write("Press Space to go back")
  137.         waitForSpace()
  138.     elseif string.find(string.lower(userInput), "e") then
  139.         term.clear()
  140.         term.setCursorPos(1, 1)
  141.         local fullMessage = {}
  142.         write("To which user would you like this message to be forwarded to?\n> ")
  143.         fullMessage.user = read()
  144.         write("And what is the message you want to forward?\n> ")
  145.         fullMessage.message = read()
  146.         fullMessage.ecommand = "send"
  147.         rednet.send(config.server, textutils.serialize(fullMessage))
  148.         print("Waiting for response...")
  149.         local response = false
  150.         while not response do
  151.             local sender, message = rednet.receive(5)
  152.             if not sender then
  153.                 response = true
  154.                 print("Failure! (Server didn't respond)")
  155.                 print("Press Space to go back")
  156.                 waitForSpace()
  157.             elseif sender == config.server then
  158.                 local status = textutils.unserialize(message).status
  159.                 if status == "ok" then
  160.                     print("Sent!")
  161.                     print("Press Space to go back")
  162.                     waitForSpace()
  163.                 else
  164.                     print("Failure! (Status: '" .. status .. "')")
  165.                 end
  166.             end
  167.         end
  168.     elseif string.find(string.lower(userInput), "c") then
  169.         term.clear()
  170.         term.setCursorPos(1, 1)
  171.         local fullMessage = {}
  172.         write("Type in your username:\n> ")
  173.         fullMessage.username = read()
  174.         fullMessage.ecommand = "create"
  175.         rednet.send(config.server, textutils.serialize(fullMessage))
  176.         print("Waiting for response...")
  177.         local response = false
  178.         while not response do
  179.             local sender, message = rednet.receive(5)
  180.             if not sender then
  181.                 response = true
  182.                 print("Failure! (Server didn't respond)")
  183.                 print("Press Space to go back")
  184.                 waitForSpace()
  185.             elseif sender == config.server then
  186.                 local status = textutils.unserialize(message).status
  187.                 if status == "ok" then
  188.                     print("Account created successfully!")
  189.                     print("Press Space to go back")
  190.                     waitForSpace()
  191.                 else
  192.                     print("Failure! (Status: '" .. status .. "')")
  193.                 end
  194.             end
  195.         end
  196.     elseif string.find(string.lower(userInput), "s") then
  197.         configureServer()
  198.     elseif string.find(string.lower(userInput), "x") then
  199.         running = false
  200.     end
  201. end
  202.  
  203. if not modemAlreadyOpened then rednet.close(config.modem) end
  204. term.clear()
  205. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement