Advertisement
Guest User

chat2

a guest
Oct 31st, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.31 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.     write("Message:"..msg)
  58.  term.setBackgroundColor(32768)
  59.     local e = {os.pullEvent()}
  60.     if e[1] == "char" then
  61.         msg = msg..e[2]
  62.  
  63.     elseif e[1] == "modem_message" then
  64.         local c_text, iv = aes.validate(e[5])
  65.         if c_text then
  66.             if line == y -1 then
  67.                 term.clear()
  68.                 line = 1
  69.             end
  70.    rs.setOutput("left",true)
  71.    sleep(0.1)
  72.    rs.setOutput("left",false)
  73.    term.setCursorPos(1, line)
  74.             local plain = decrypt(c_text, iv)
  75.             print(plain)
  76.             line = line + 1
  77.         end
  78.  
  79.     elseif e[1] == "key" then
  80.         if e[2] == 14 then
  81.             term.setCursorPos(1,1)
  82.             msg = msg:sub(0,#msg-1)
  83.  
  84.         elseif e[2] == 28 then
  85.    hist[#hist + 1] = msg
  86.             msg = "<"..nick..">"..msg
  87.             if line == y -1 then
  88.                 term.clear()
  89.                 line = 1
  90.             end
  91.             term.setCursorPos(1, line)
  92.             print(msg)
  93.             line = line + 1
  94.             msg = encrypt(msg)
  95.             m.transmit(os.getComputerID(),os.getComputerID(),msg)
  96.             msg = ""
  97.   elseif e[2] == 200 then
  98.    histc = histc + 1
  99.    if histc > #hist then
  100.     histc = histc - 1
  101.    else
  102.     msg = hist[#hist-histc+1]
  103.    end
  104.   elseif e[2] == 208 then
  105.  
  106.    histc = histc - 1
  107.    if histc == 0 then
  108.     msg = ""
  109.    elseif histc < 0 then
  110.     histc = histc + 1
  111.    else
  112.     msg = hist[#hist-histc+1]
  113.    end
  114.   end
  115.  end
  116. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement