Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local m = peripheral.find("modem") -- Yay for CC1.6
- local hist = {}
- local histc = 0
- local flines = {}
- local nick = "Luca"
- local x, y = term.getSize()
- local line = 1
- local msg = ""
- local aes = dofile("/libs/cryptLib").aes()
- local utils = dofile("/libs/cryptLib").utils()
- local f = fs.open(args[1],"r")
- for fline in f.readLine do
- flines[#flines+1] = fline
- end
- f.close()
- f = fs.open(args[2], "r")
- local key = textutils.unserialize(f.readLine())
- f.close()
- local function encrypt(plain)
- plain = utils.stringToByteArray(plain)
- local iv = utils.keygen(16)
- local c_text = aes.encrypt_cbc(plain, key, iv)
- return {c_text, iv}
- end
- local function decrypt(c_text, iv)
- local plain = aes.decrypt_cbc(c_text, key, iv)
- return utils.byteArrayToString(plain)
- end
- term.clear()
- term.setCursorPos(1,1)
- print("Welcome to Chat!")
- print("IDs im Chatraum:")
- for i = 1,#flines do
- print(flines[i])
- m.open(tonumber(flines[i]))
- end
- sleep(3)
- term.clear()
- while true do
- term.setCursorPos(1, y)
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- term.write("Message:"..msg)
- term.setBackgroundColor(32768)
- local e = {os.pullEvent()}
- if e[1] == "char" then
- if nick:len()+2 > 8 then
- add = nick:len()+2
- else
- add = 8
- end
- if msg:len()+add < x then
- msg = msg..e[2]
- end
- elseif e[1] == "modem_message" then
- local c_text, iv = aes.validate(e[5])
- if c_text then
- if line == y -1 then
- term.clear()
- line = 1
- end
- rs.setOutput("left",true)
- sleep(0.1)
- rs.setOutput("left",false)
- term.setCursorPos(1, line)
- local plain = decrypt(c_text, iv)
- term.write(plain)
- line = line + 1
- end
- elseif e[1] == "key" then
- if e[2] == 14 then
- term.setCursorPos(1,1)
- msg = msg:sub(0,#msg-1)
- elseif e[2] == 28 then
- hist[#hist+1] = msg
- if msg:sub(1,1) == "/" then
- local cmd = msg:sub(2,#msg)
- if cmd == "help" then
- 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"}
- if #lines + line >=y then
- term.clear()
- term.setCursorPos(1,1)
- line = 1
- end
- for i = 1,#lines do
- term.setCursorPos(1,line)
- print(lines[i])
- line = line + 1
- end
- elseif cmd:sub(1,4) == "nick" then
- if cmd:sub(6) == "" then
- else
- nick = cmd:sub(6)
- end
- elseif cmd == "logout" then
- local left = "<"..nick.."> left the chat"
- left = encrypt(left)
- m.transmit(os.getComputerID(),os.getComputerID(),left)
- hist = {}
- shell.run("clear")
- break
- elseif cmd:sub(1,2) == "me" then
- if cmd:sub(4) == "" then
- else
- local me = "*"..nick.." "..cmd:sub(4)
- if line == y-1 then
- term.clear()
- line = 1
- end
- term.setCursorPos(1,line)
- print(me)
- line = line + 1
- me = encrypt(me)
- m.transmit(os.getComputerID(),os.getComputerID(),me)
- end
- end
- else
- msg = "<"..nick..">"..msg
- if line == y -1 then
- term.clear()
- line = 1
- end
- term.setCursorPos(1, line)
- print(msg)
- line = line + 1
- msg = encrypt(msg)
- m.transmit(os.getComputerID(),os.getComputerID(),msg)
- end
- histc = 0
- msg = ""
- elseif e[2] == 200 then
- histc = histc + 1
- if histc > #hist then
- histc = histc - 1
- else
- msg = hist[#hist-histc+1]
- end
- elseif e[2] == 208 then
- histc = histc - 1
- if histc == 0 then
- msg = ""
- elseif histc < 0 then
- histc = histc + 1
- else
- msg = hist[#hist-histc+1]
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement