Advertisement
Guest User

startup

a guest
Feb 20th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.91 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1, 1)
  3.  
  4.  
  5. function receive()
  6.     rednet.open("left")
  7.     while true do
  8.         local senderId, message = rednet.receive()
  9.         print("Received message from ID: " .. senderId .. ", length: " .. #message)
  10.        
  11.         local cmd = message:match("%S+")
  12.         message = message:gsub(cmd.." ", "")
  13.         if cmd == "LOGIN" then
  14.             local data = message:match("%S+:%S+")
  15.             local f, t = message:find(":")
  16.             if f ~= nil and t ~= nil then
  17.                 local username = data:sub(0, t-1)
  18.                 local password = data:sub(t+1)
  19.                
  20.                 if fs.exists("users/" .. username) then
  21.                     local file = fs.open("users/"..username, "r")
  22.                     local pw = file.readLine()
  23.                     if password == pw then
  24.                         rednet.send(senderId, "RIGHT_PASSWORD")
  25.                         print("User "..username.." successfully logged in!")
  26.                     else
  27.                         rednet.send(senderId, "WRONG_PASSWORD")
  28.                         print("User "..username.." failed to log in!")
  29.                     end
  30.                     file.close()
  31.                 else
  32.                     rednet.send(senderId, "USER_NOT_EXISTS")
  33.                     print("User "..username.." doesn't exists!")
  34.                 end
  35.             else
  36.                 rednet.send(senderId, "WRONG_FORMAT")
  37.                 print("WRONG_FORMAT")
  38.             end
  39.         elseif cmd == "SEND" then
  40.             local data = message:match("%S+:%S+")
  41.             local f, t = message:find(":")
  42.             if f ~= nil and t ~= nil then
  43.                 local username = data:sub(0, t-1)
  44.                 local password = data:sub(t+1)
  45.                 message = message:gsub(data.." ", "")
  46.                
  47.                 if fs.exists("users/" .. username) then
  48.                     local file = fs.open("users/"..username, "r")
  49.                     local pw = file.readLine()
  50.                     file.close()
  51.                     if password == pw then
  52.                         local to = message:match("%S+")
  53.                         message = message:gsub(to.." ", "")
  54.                        
  55.                         if fs.exists("users/" .. to) then
  56.                             local count = #fs.list("mails/" .. to)
  57.                             if not fs.exists("mails/"..to) then
  58.                                 fs.makeDir("mails/"..to)
  59.                             end
  60.                             local file = fs.open("mails/" .. to .. "/"  .. count+1, "w")
  61.                             file.writeLine(username)
  62.                             file.writeLine(message)
  63.                             file.close()
  64.                        
  65.                             rednet.send(senderId, "SUCCESSFUL_SENT")
  66.                             print("E-Mail sent from "..username.." to "..to..", length: "..#message)
  67.                         else
  68.                             rednet.send(senderId, "TARGET_NOT_EXISTS")
  69.                         end
  70.                     else
  71.                         rednet.send(senderId, "FAILED_SENT")
  72.                     end
  73.                 else
  74.                     rednet.send(senderId, "FAILED_SENT")
  75.                 end
  76.             end
  77.         elseif cmd == "CHECK" then
  78.             local data = message:match("%S+:%S+")
  79.             local f, t = message:find(":")
  80.             if f ~= nil and t ~= nil then
  81.                 local username = data:sub(0, t-1)
  82.                 local password = data:sub(t+1)
  83.                
  84.                 if fs.exists("users/" .. username) then
  85.                     local file = fs.open("users/"..username, "r")
  86.                     local pw = file.readLine()
  87.                     file.close()
  88.                     if password == pw then
  89.                         for i, file in ipairs(fs.list("mails/"..username)) do
  90.                            
  91.                         end
  92.                    
  93.                    
  94.                         rednet.send(senderId, "lag")
  95.                     end
  96.                 end
  97.             end
  98.         else
  99.             print("Unknown command: "..cmd)
  100.         end
  101.     end
  102. end
  103.  
  104. receive()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement