Advertisement
Guest User

Untitled

a guest
Nov 8th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 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 ModemFunction(_,_,from,port,_,message_)
  32. event.listen("modem_message", ModemFunction)
  33.  
  34. -- decipher, and print
  35. local cipher = "1234567890qwertyuiopasdfghjklzxcvbnm"
  36. local function decrypt(str)
  37. str = str:gsub("%d",function(a) return string.char(cipher:find(a,nil,true)+47) end)
  38. str = str:gsub("%l",function(a) return string.char(cipher:find(a,nil,true)+86) end)
  39. return str
  40. end
  41. if string.sub(tostring(message), 1, 9)=="encrypted" then
  42. str=tostring(message)
  43. message=decrypt(str)
  44. local strlngth=string.len(tostring(message))
  45. message=string.sub(tostring(message), 10, strlngth)
  46. print(tostring(message))
  47.  
  48. else
  49. print(tostring(message))
  50. end
  51. end
  52.  
  53. -- send a message
  54. term.write("is this going to be encrypted? (yes/no) \n")
  55.  
  56. -- register cipher
  57. local cipher = "1234567890qwertyuiopasdfghjklzxcvbnm"
  58.  
  59. local function encrypt(str)
  60. str = str:gsub("%d",function(a) a=a:byte()-47 return cipher:sub(a,a) end)
  61. str = str:gsub("%l",function(a) a=a:byte()-86 return cipher:sub(a,a) end)
  62. return str
  63. end
  64.  
  65. -- determine whether encryption should be used
  66. local useEC=io.read()
  67. if useEC=="yes" then
  68. term.clear()
  69. term.write("Encryption activated \n")
  70. sleep(5)
  71. term.clear()
  72. end
  73. while true do
  74. str= user .. ": " .. io.read()
  75. if tostring(str)== user .. ": " .. "endme" then
  76. term.clear()
  77. event.ignore("modem_message", ModemFunction)
  78. os.exit()
  79. end
  80. if useEC=="yes" then
  81. str= user .. ": " .. encrypt(str)
  82. str="encrypted" .. " " .. str
  83. end
  84.  
  85. -- send message
  86. m.broadcast(tonumber(channel), str)
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement