Advertisement
gpgautier

ccChat

Oct 27th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | None | 0 0
  1. local FILE_USERNAME = "username"
  2. local FILE_PASSWORD = "password"
  3. local USERNAME = nil
  4. local PASSWORD = nil
  5. local AUTHENTICATED = false
  6. local RUNNING = false
  7. local MODEM = "right"
  8.  
  9. local LINES = {}
  10.  
  11. --
  12. function install()
  13.   term.clear()
  14.   term.setCursorPos(1, 1)
  15.  
  16.   while USERNAME == nil or USERNAME == "" do
  17.     USERNAME = inputUsername()
  18.   end
  19.  
  20.   local file = io.open(FILE_USERNAME, "w")  
  21.   file:write(USERNAME)
  22.   file:close()
  23.  
  24.   term.setCursorPos(1, 2)
  25.  
  26.   while PASSWORD == nil or PASSWORD == "" do
  27.     PASSWORD = inputPassword()
  28.   end
  29.  
  30.   file = io.open(FILE_PASSWORD, "w")  
  31.   file:write(PASSWORD)
  32.   file:close()
  33.  
  34.   term.setCursorPos(1, 2)
  35.   print("Installation successfull. Starting application.")
  36.   sleep(1)
  37. end
  38.  
  39. function inputUsername()
  40.   term.write("Choose a username: ")
  41.   return read()
  42. end
  43.  
  44. function inputPassword()
  45.   term.write("Choose a password: ")
  46.   return read()
  47. end
  48.  
  49. function inputAuthenticate()
  50.   term.clearLine()
  51.   term.write("Password: ")
  52.   return read("*")
  53. end
  54.  
  55. function updateOnlineList()
  56.   while RUNNING do
  57.     term.setCursorPos(1, 1)
  58.     term.write("Online: ")
  59.     term.setCursorPos(8, 3)
  60.     sleep(1)
  61.   end
  62.  
  63.   return false
  64. end
  65.  
  66. function writeLineBuffer()
  67.   term.setCursorPos(1, 5)
  68.   term.clearLine()
  69.   term.write(LINES[0])
  70.  
  71.   term.setCursorPos(1, 6)
  72.   term.clearLine()
  73.   term.write(LINES[1])
  74.  
  75.   term.setCursorPos(1, 7)
  76.   term.clearLine()
  77.   term.write(LINES[2])
  78.  
  79.   term.setCursorPos(1, 8)
  80.   term.clearLine()
  81.   term.write(LINES[3])
  82.  
  83.   term.setCursorPos(1, 9)
  84.   term.clearLine()
  85.   term.write(LINES[4])
  86.  
  87.   term.setCursorPos(1, 10)
  88.   term.clearLine()
  89.   term.write(LINES[5])
  90.  
  91.   term.setCursorPos(1, 11)
  92.   term.clearLine()
  93.   term.write(LINES[6])
  94.  
  95.   term.setCursorPos(1, 12)
  96.   term.clearLine()
  97.   term.write(LINES[7])
  98.  
  99.   term.setCursorPos(1, 13)
  100.   term.clearLine()
  101.   term.write(LINES[8])
  102.  
  103.   term.setCursorPos(1, 14)
  104.   term.clearLine()
  105.   term.write(LINES[9])
  106.  
  107.   term.setCursorPos(1, 15)
  108.   term.clearLine()
  109.   term.write(LINES[10])
  110.  
  111.   term.setCursorPos(1, 16)
  112.   term.clearLine()
  113.   term.write(LINES[11])
  114.  
  115.   term.setCursorPos(1, 17)
  116.   term.clearLine()
  117.   term.write(LINES[12])
  118.  
  119.   term.setCursorPos(1, 18)
  120.   term.clearLine()
  121.   term.write(LINES[13])
  122.  
  123.   term.setCursorPos(8, 3)
  124. end
  125.  
  126. function updateBuffer()
  127.   local senderId, message, distance
  128.  
  129.   while RUNNING do
  130.     senderId, message, distance = rednet.receive(1)
  131.  
  132.     if message ~= "" and message ~= nil then
  133.       addMessage(message)
  134.     end
  135.   end
  136.  
  137.   return false
  138. end
  139.  
  140. function handleInput()
  141.   local message
  142.  
  143.   while RUNNING do
  144.     term.setCursorPos(1, 3)
  145.     term.clearLine()
  146.     term.write("Send > ")
  147.    
  148.     message = USERNAME .. ": " .. read()
  149.  
  150.     addMessage(message)
  151.     rednet.broadcast(message)
  152.   end
  153.  
  154.   return false
  155. end
  156.  
  157. function addMessage(message)
  158.   LINES[0] = LINES[1]
  159.   LINES[1] = LINES[2]
  160.   LINES[2] = LINES[3]
  161.   LINES[3] = LINES[4]
  162.   LINES[4] = LINES[5]
  163.   LINES[5] = LINES[6]
  164.   LINES[6] = LINES[7]
  165.   LINES[7] = LINES[8]
  166.   LINES[8] = LINES[9]
  167.   LINES[9] = LINES[10]
  168.   LINES[10] = LINES[11]
  169.   LINES[11] = LINES[12]
  170.   LINES[12] = LINES[13]
  171.   LINES[13] = message
  172.  
  173.   writeLineBuffer()
  174. end
  175.  
  176. if fs.exists(FILE_USERNAME) and fs.exists(FILE_PASSWORD) then
  177.   local file = fs.open(FILE_USERNAME, "r")
  178.   USERNAME = file:readLine()
  179.   file:close()
  180.  
  181.   if USERNAME == nil or username == "" then
  182.     install()
  183.   end
  184.  
  185.   file = fs.open(FILE_PASSWORD, "r")
  186.   PASSWORD = file:readLine()
  187.   file:close()
  188.  
  189.   if PASSWORD == nil or PASSWORD == "" then
  190.     install()
  191.   end
  192. else
  193.   install()
  194. end
  195.  
  196. term.clear()
  197. term.setCursorPos(1, 1)
  198. print("Welcome " .. USERNAME)
  199.  
  200. while AUTHENTICATED == false do
  201.   term.setCursorPos(1, 3)
  202.   local password = inputAuthenticate()
  203.   if password == PASSWORD then
  204.     AUTHENTICATED = true
  205.     RUNNING = true
  206.   end
  207. end
  208.  
  209. term.clear()
  210.  
  211. term.setCursorPos(1, 2)
  212. term.write("-------------------------")
  213. term.setCursorPos(1, 4)
  214. term.write("-------------------------")
  215.  
  216. rednet.open("right")
  217. parallel.waitForAny(handleInput, updateBuffer, updateOnlineList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement