Advertisement
Guest User

chat

a guest
Oct 31st, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.65 KB | None | 0 0
  1. local args = {...}
  2. local m = peripheral.find("modem") -- Yay for CC1.6
  3. local hist = {}
  4. local histc = 0
  5. local flines = {}
  6. local nick = "Luca"
  7. local x, y = term.getSize()
  8. local line = 1
  9. local msg = ""
  10.  
  11. local aes = dofile("/libs/cryptLib").aes()
  12. local utils = dofile("/libs/cryptLib").utils()
  13.  
  14. local f = fs.open(args[1],"r")
  15.  
  16. for fline in f.readLine do
  17.     flines[#flines+1] = fline
  18. end
  19. f.close()
  20.  
  21. f = fs.open(args[2], "r")
  22. local key = textutils.unserialize(f.readLine())
  23. f.close()
  24.  
  25. local function encrypt(plain)
  26.     plain = utils.stringToByteArray(plain)
  27.     local iv = utils.keygen(16)
  28.     local c_text = aes.encrypt_cbc(plain, key, iv)
  29.  
  30.     return {c_text, iv}
  31. end
  32.  
  33. local function decrypt(c_text, iv)
  34.     local plain = aes.decrypt_cbc(c_text, key, iv)
  35.  
  36.     return utils.byteArrayToString(plain)
  37. end
  38.  
  39. term.clear()
  40. term.setCursorPos(1,1)
  41.  
  42. print("Welcome to Chat!")
  43. print("IDs im Chatraum:")
  44.  
  45. for i = 1,#flines do
  46.     print(flines[i])
  47.     m.open(tonumber(flines[i]))
  48. end
  49.  
  50. sleep(3)
  51. term.clear()
  52.  
  53. while true do
  54.     term.setCursorPos(1, y)
  55.  term.setBackgroundColor(colors.gray)
  56.     term.clearLine()
  57.     term.write("Message:"..msg)
  58.  term.setBackgroundColor(32768)
  59.     local e = {os.pullEvent()}
  60.     if e[1] == "char" then
  61.  if nick:len()+2 > 8 then
  62.    add = nick:len()+2
  63.  else
  64.    add = 8
  65.  end
  66.   if msg:len()+add < x then
  67.         msg = msg..e[2]
  68.   end
  69.     elseif e[1] == "modem_message" then
  70.         local c_text, iv = aes.validate(e[5])
  71.         if c_text then
  72.             if line == y -1 then
  73.                 term.clear()
  74.                 line = 1
  75.             end
  76.    rs.setOutput("left",true)
  77.    sleep(0.1)
  78.    rs.setOutput("left",false)
  79.    term.setCursorPos(1, line)
  80.             local plain = decrypt(c_text, iv)
  81.     term.write(plain)
  82.             line = line + 1
  83.         end
  84.  
  85.     elseif e[1] == "key" then
  86.         if e[2] == 14 then
  87.             term.setCursorPos(1,1)
  88.             msg = msg:sub(0,#msg-1)
  89.  
  90.         elseif e[2] == 28 then
  91.    hist[#hist+1] = msg
  92.    if msg:sub(1,1) == "/" then
  93.     local cmd = msg:sub(2,#msg)
  94.     if cmd == "help" then
  95.      lines = {"/logout: Will quit you from the chat","/help: Will list the commands","/nick: Change your Nick","/me: Tell the others what you are doing"}
  96.      if #lines + line >=y then
  97.        term.clear()
  98.        term.setCursorPos(1,1)
  99.        line = 1
  100.      end
  101.      for i = 1,#lines do
  102.      term.setCursorPos(1,line)
  103.      print(lines[i])
  104.      line = line + 1
  105.      end
  106.     elseif cmd:sub(1,4) == "nick" then
  107.      if cmd:sub(6) == "" then
  108.    
  109.      else
  110.       nick = cmd:sub(6)
  111.      end
  112.     elseif cmd == "logout" then
  113.       local left = "<"..nick.."> left the chat"
  114.       left = encrypt(left)
  115.       m.transmit(os.getComputerID(),os.getComputerID(),left)
  116.       hist = {}
  117.       shell.run("clear")
  118.       break
  119.     elseif cmd:sub(1,2) == "me" then
  120.      if cmd:sub(4) == "" then
  121.      
  122.      else
  123.       local me = "*"..nick.." "..cmd:sub(4)
  124.       if line == y-1 then
  125.        term.clear()
  126.        line = 1
  127.       end
  128.       term.setCursorPos(1,line)
  129.       print(me)
  130.       line = line + 1
  131.       me = encrypt(me)
  132.       m.transmit(os.getComputerID(),os.getComputerID(),me)
  133.      end
  134.     end
  135.    else
  136.     msg = "<"..nick..">"..msg
  137.             if line == y -1 then
  138.                  term.clear()
  139.                 line = 1
  140.             end
  141.             term.setCursorPos(1, line)
  142.             print(msg)
  143.             line = line + 1
  144.             msg = encrypt(msg)
  145.             m.transmit(os.getComputerID(),os.getComputerID(),msg)
  146.    end
  147.    histc = 0
  148.             msg = ""
  149.   elseif e[2] == 200 then
  150.    histc = histc + 1
  151.    if histc > #hist then
  152.     histc = histc - 1
  153.    else
  154.     msg = hist[#hist-histc+1]
  155.    end
  156.   elseif e[2] == 208 then
  157.  
  158.    histc = histc - 1
  159.    if histc == 0 then
  160.     msg = ""
  161.    elseif histc < 0 then
  162.     histc = histc + 1
  163.    else
  164.     msg = hist[#hist-histc+1]
  165.    end
  166.   end
  167.  end
  168. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement