Advertisement
Guest User

Startup for Server

a guest
Sep 2nd, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  1. os.loadAPI(".server/api")
  2. os.loadAPI(".server/encrypt")
  3. api.open("right")
  4.  
  5.  
  6. function main()
  7.     while true do
  8.         local event, id, msg, dist = api.evt("rednet_message")
  9.             if string.find(msg, "update") then
  10.                 msg = string.sub(msg, string.len("update")+2)
  11.                 ids = textutils.unserialize(msg)
  12.                 if api.isID(id) then
  13.                     mesg = textutils.unserialize(msg)
  14.                 end
  15.             elseif string.find(msg, "apply") then  
  16.                 if api.isID(id) then
  17.                     msg = string.sub(msg, string.len("apply")+2)
  18.                     local _, _, user, pass = string.find(msg, "(%w+)&pwd=(%w+)")
  19.                     term.scroll(1)
  20.                     term.setTextColor(colors.yellow)
  21.                     print("Authentificating...")
  22.                     if api.isPermitted(user, pass) then
  23.                         term.scroll(1)
  24.                         term.setTextColor(colors.lime)
  25.                         print("Authentificated!")
  26.                         api.send(id, encrypt.hash("OK"))
  27.                     else
  28.                         term.scroll(1)
  29.                         term.setTextColor(colors.red)
  30.                         print("Not authentificated!")
  31.                         api.send(id, encrypt.hash("FAIL"))
  32.                     end
  33.  
  34.                     sleep(2)
  35.                 end
  36.             elseif string.find(msg, "applicate") then
  37.                 if api.isID(id) then
  38.                     msg = string.sub(msg, string.len("applicate")+2)
  39.                     local _, _, user, pass = string.find(msg, "(%w+)&pwd=(%w+)")
  40.                     if not api.isPermitted(user, pass) then
  41.                         api.setPermitted(user, pass)
  42.                         api.send(id, encrypt.hash("DONE"))
  43.                     else
  44.                         api.send(id, encrypt.hash("EXISTS"))
  45.                     end
  46.                 end
  47.             elseif string.find(msg, "balance") then
  48.                 if api.isID(id) then
  49.                     msg = string.sub(msg, string.len("balance")+2)
  50.                     local _, _, user, pass = string.find(msg, "(%w+)&pwd=(%w+)")
  51.                     term.scroll(1)
  52.                     term.setTextColor(colors.orange)
  53.                     print("The Cash database has been read!")
  54.                     local balance = api.getBalance(user, pass)
  55.                     if not balance then
  56.                         error("BALANCE cant be read!", 2)
  57.                     end
  58.                     api.send(id, balance)
  59.                 end
  60.             elseif string.find(msg, "error") then
  61.                 term.setTextColor(colors.red)
  62.                 print(msg) 
  63.             elseif msg == "ping" then
  64.                 term.scroll(1)
  65.                 term.setTextColor(colors.yellow)
  66.                 print("Got \"ping\" from "..id)
  67.                 if not api.isID(id) then
  68.                     api.saveID(id)
  69.                     term.scroll(1)
  70.                     term.setTextColor(colors.lime)
  71.                     print("Saved ID of client")
  72.                 end
  73.                 api.send(id, "pong")
  74.                 term.scroll(1)
  75.                 term.setTextColor(colors.lime)
  76.                 print("Successfully sended \"pong\"")  
  77.             end
  78.     end
  79. end
  80.  
  81. --while true do
  82.     main()
  83.     --parallel.waitForAny(main, pong)
  84.  
  85.     --sleep(1)
  86. --end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement