Advertisement
gpgautier

ccChat 0.2

Oct 28th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.26 KB | None | 0 0
  1. local AUTHENTICATED = false
  2. local RUNNING = false
  3. local MODEM = "right"
  4.  
  5. local FILE_CONFIG = "config"
  6. local USERNAME = nil
  7. local PASSWORD = nil
  8.  
  9. local LINES = {}
  10.  
  11. local ID = os.computerID()
  12.  
  13. local ONLINE = {}
  14.  
  15. --
  16. function install()
  17.   term.clear()
  18.   term.setCursorPos(1, 1)
  19.  
  20.   while USERNAME == nil or USERNAME == "" do
  21.     USERNAME = inputUsername()
  22.   end
  23.  
  24.   term.setCursorPos(1, 2)
  25.   while PASSWORD == nil or PASSWORD == "" do
  26.     PASSWORD = inputPassword()
  27.   end
  28.  
  29.   local file = io.open(FILE_CONFIG, "w")  
  30.   file:write(textutils.serialize({username = USERNAME, password = PASSWORD}))
  31.   file:close()
  32.  
  33.   term.setCursorPos(1, 3)
  34.   print("Installation successfull. Restarting application ...")
  35.  
  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.   local i, v
  57.  
  58.   term.setCursorPos(1, 1)
  59.   term.clearLine()
  60.  
  61.   term.write("Online: ")
  62.  
  63.   for i, v in ipairs(ONLINE) do
  64.     if i > 1 then
  65.       term.write(", ")
  66.     end
  67.     term.write(ONLINE[i])
  68.   end
  69.  
  70.   term.setCursorPos(8, 3)
  71. end
  72.  
  73. function writeLineBuffer()
  74.   local i
  75.  
  76.   for i = 0, 13 do
  77.     term.setCursorPos(1, i + 5)
  78.     term.clearLine()
  79.     term.write(LINES[i])
  80.   end
  81.  
  82.   term.setCursorPos(8, 3)
  83. end
  84.  
  85. function updateBuffer()
  86.   --local senderId, message, distance
  87.  
  88.   while RUNNING do
  89.     --senderId, message, distance = rednet.receive(1)
  90.  
  91.     --if message ~= "" and message ~= nil then
  92.     --  addMessage(message)
  93.     --end
  94.  
  95.     recieve()
  96.   end
  97.  
  98.   return false
  99. end
  100.  
  101. function handleInput()
  102.   local message
  103.  
  104.   while RUNNING do
  105.     term.setCursorPos(1, 3)
  106.     term.clearLine()
  107.     term.write("Send > ")
  108.    
  109.     message = read()
  110.  
  111.     if message ~= "" and message ~= nil then
  112.       if message:sub(0, 1) == "/" then
  113.         executeCommand(message:sub(2))
  114.       else
  115.         message = USERNAME .. ": " .. message
  116.         addMessage(message)
  117.         --rednet.broadcast(message)
  118.         sendMessage(message)
  119.       end
  120.     end
  121.   end
  122.  
  123.   return false
  124. end
  125.  
  126. function executeCommand(command)
  127.   command = command:lower()
  128.  
  129.   if command == "quit" then
  130.     addMessage("[CONSOLE] Quiting application")
  131.     sleep(2)
  132.    
  133.     endSession()
  134.  
  135.     term.clear()
  136.     term.setCursorPos(1, 1)
  137.     RUNNING = false
  138.   end
  139. end
  140.  
  141. function addMessage(message)
  142.   local i, length
  143.  
  144.   for i = 0, 12 do
  145.     LINES[i] = LINES[i + 1]
  146.   end
  147.  
  148.   length = message:len()
  149.  
  150.   if length > 50 then
  151.     LINES[13] = message:sub(1, 50)
  152.     addMessage(message:sub(51))
  153.   else
  154.     LINES[13] = message
  155.   end
  156.  
  157.   writeLineBuffer()
  158. end
  159.  
  160. function reset()
  161.   AUTHENTICATED = false
  162. end
  163.  
  164. -- [ --------------------------------------------------------------------------
  165.  
  166. function send(type, body)
  167.   local request = {protocol = "rnc", username = USERNAME, type = type, body = body}
  168.   rednet.broadcast(textutils.serialize(request))
  169. end
  170.  
  171. function sendMessage(message)
  172.   send("message", message)
  173. end
  174.  
  175. function sendCommand(command)
  176.   send("command", command)
  177. end
  178.  
  179. function recieve()
  180.   local sender, request, distance = rednet.receive(1)
  181.  
  182.   if request == "" or request == nil then
  183.     return
  184.   end
  185.  
  186.   request = textutils.unserialize(request)
  187.  
  188.   if request.protocol ~= "rnc" then
  189.     return
  190.   end
  191.  
  192.   if request.type == "command" then
  193.     if request.body == "online" then
  194.       addMessage("[CONSOLE] " .. request.username .. " is now online")
  195.       table.insert(ONLINE, request.username)
  196.       updateOnlineList()
  197.       sendCommand("metoo")
  198.     elseif request.body == "offline" then
  199.       addMessage("[CONSOLE] " .. request.username .. " is now offline")
  200.       for i, v in ipairs(ONLINE) do
  201.         if ONLINE[i] == request.username then
  202.           table.remove(ONLINE, i)
  203.         end
  204.       end
  205.       updateOnlineList()
  206.     elseif request.body == "metoo" then
  207.       table.insert(ONLINE, request.username)
  208.       updateOnlineList()
  209.     end
  210.   elseif request.type == "message" then
  211.     addMessage(request.body)
  212.   end
  213. end
  214.  
  215. function startSession()
  216.   table.insert(ONLINE, USERNAME)
  217.   updateOnlineList()
  218.   sendCommand("online")
  219. end
  220.  
  221. function endSession()
  222.   sendCommand("offline")
  223. end
  224.  
  225. -- ] --------------------------------------------------------------------------
  226.  
  227. if fs.exists(FILE_CONFIG) then
  228.   local file = fs.open(FILE_CONFIG, "r")
  229.   local config = textutils.unserialize(file:readLine())
  230.   file:close()
  231.  
  232.   USERNAME = config.username
  233.   PASSWORD = config.password
  234.  
  235.   if USERNAME == nil or username == "" or PASSWORD == nil or PASSWORD == "" then
  236.     install()
  237.   end
  238. else
  239.   install()
  240. end
  241.  
  242. term.clear()
  243. term.setCursorPos(1, 1)
  244. print("Welcome " .. USERNAME)
  245.  
  246. while AUTHENTICATED == false do
  247.   term.setCursorPos(1, 3)
  248.   local password = inputAuthenticate()
  249.   if password == PASSWORD then
  250.     AUTHENTICATED = true
  251.     RUNNING = true
  252.   end
  253. end
  254.  
  255. term.clear()
  256.  
  257. term.setCursorPos(1, 2)
  258. term.write("-------------------------")
  259. term.setCursorPos(1, 4)
  260. term.write("-------------------------")
  261.  
  262. rednet.open("right")
  263.  
  264. startSession()
  265.  
  266. parallel.waitForAny(handleInput, updateBuffer)
  267.  
  268. rednet.close("right")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement