Advertisement
Guest User

echat

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