Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 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. function myFunctionWithoutName(_,_,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. event.listen("modem_message", myFunctionWithoutName)
  53.  
  54. -- send a message
  55. term.write("is this going to be encrypted? (yes/no) \n")
  56.  
  57. -- register cipher
  58. local cipher = "1234567890qwertyuiopasdfghjklzxcvbnm"
  59.  
  60. local function encrypt(str)
  61. str = str:gsub("%d",function(a) a=a:byte()-47 return cipher:sub(a,a) end)
  62. str = str:gsub("%l",function(a) a=a:byte()-86 return cipher:sub(a,a) end)
  63. return str
  64. end
  65.  
  66. -- determine whether encryption should be used
  67. local useEC=io.read()
  68. if useEC=="yes" then
  69. term.clear()
  70. term.write("Encryption activated \n")
  71. sleep(5)
  72. term.clear()
  73. end
  74. while true do
  75. str= user .. ": " .. io.read()
  76. if tostring(str)== user .. ": " .. "endme" then
  77. term.clear()
  78. event.ignore("modem_message", myFunctionWithoutName)
  79. os.exit()
  80. end
  81. if useEC=="yes" then
  82. str= user .. ": " .. encrypt(str)
  83. str="encrypted" .. " " .. str
  84. end
  85.  
  86. -- send message
  87. m.broadcast(tonumber(channel), str)
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement