Advertisement
Guest User

chat.lua

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