Advertisement
Guest User

chat.lua

a guest
Sep 18th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.31 KB | None | 0 0
  1. local term = require("term")
  2. local component = require("component")
  3. local event = require("event")
  4. local m = component.modem
  5. local str="0"
  6. local userfile = "/home/.uname.txt"
  7. local fs = require("filesystem")
  8. local uname = fs.exists(userfile)
  9.  
  10. -- determine what channel people want to listen on
  11. term.clear()
  12. if uname then
  13.   local handler = fs.open(userfile)
  14.   user = handler:read(16)
  15.   handler:close()
  16. else
  17.   term.write("You do not have a user profile. \n")
  18.   term.write("Please enter a 16 character user name: ")
  19.   local handler = fs.open(userfile, "w")
  20.   handler:write(io.read())
  21.   local handler = fs.open(userfile)
  22.   user = handler:read(16)
  23.   term.write("\n Username set to " .. user .. ". \n")
  24.   handler:close()
  25. end
  26. term.write("What channel do you want to chat on: ")
  27. local channel = io.read()
  28. m.open(tonumber(channel))
  29.  
  30. -- begin event
  31. event.listen("modem_message", function(_,_,from,port,_,message)
  32.  
  33. -- decipher, and print
  34. local cipher = "1234567890qwertyuiopasdfghjklzxcvbnm"
  35. local function decrypt(str)
  36.         str = str:gsub("%d",function(a) return string.char(cipher:find(a,nil,true)+47) end)
  37.         str = str:gsub("%l",function(a) return string.char(cipher:find(a,nil,true)+86) end)
  38.         return str
  39. end
  40. if string.sub(tostring(message), 1, 9)=="encrypted" then
  41. str=tostring(message)
  42. message=decrypt(str)
  43. local strlngth=string.len(tostring(message))
  44. message=string.sub(tostring(message), 10, strlngth)
  45. print(tostring(message))
  46.  
  47. else
  48. print(tostring(message))
  49. end
  50. end)
  51.  
  52. -- send a message
  53. term.write("is this going to be encrypted? (yes/no) \n")
  54.  
  55. -- register cipher
  56. local cipher = "1234567890qwertyuiopasdfghjklzxcvbnm"
  57.  
  58. local function encrypt(str)
  59.   str = str:gsub("%d",function(a) a=a:byte()-47 return cipher:sub(a,a) end)
  60.   str = str:gsub("%l",function(a) a=a:byte()-86 return cipher:sub(a,a) end)
  61.   return str
  62. end
  63.  
  64. -- determine whether encryption should be used
  65. local useEC=io.read()
  66. if useEC=="yes" then
  67.   term.clear()
  68.   term.write("Encryption activated \n")
  69.   sleep(5)
  70.   term.clear()
  71. end
  72. while true do
  73.   str= user .. ": " .. io.read()
  74.   if tostring(str)== user .. ": " .. "endme" then
  75.     term.clear()
  76.     event.ignore("modem_message", function(_,_,from,port,_,message)
  77.     os.exit()
  78.   end
  79.   if useEC=="yes" then
  80.     str= user .. ": " .. encrypt(str)
  81.     str="encrypted" .. " " .. str
  82.   end
  83.  
  84. -- send message
  85. m.broadcast(tonumber(channel), str)
  86. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement