Advertisement
DOGGYWOOF

Untitled

Dec 14th, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.04 KB | None | 0 0
  1. -- Custom protocol (default)
  2. local chatProtocol = "doggyChat"
  3.  
  4. -- Function to check if a file exists
  5. local function fileExists(path)
  6. local file = fs.open(path, "r")
  7. if file then
  8. file.close()
  9. return true
  10. end
  11. return false
  12. end
  13.  
  14. -- Function to list all profiles
  15. local function listProfiles()
  16. local profilesDir = "./dogchat/profiles/"
  17. if not fs.isDir(profilesDir) then
  18. return {}
  19. end
  20.  
  21. local profiles = fs.list(profilesDir)
  22. return profiles
  23. end
  24.  
  25. -- Function to download or create the XOR.key file
  26. local function downloadOrCreateKey()
  27. local keyFilePath = "XOR.key"
  28.  
  29. if fileExists(keyFilePath) then
  30. term.setTextColor(colors.green)
  31. print("Encryption key found.")
  32. term.setTextColor(colors.white)
  33. else
  34. term.setTextColor(colors.yellow)
  35. print("Downloading encryption key...")
  36. local key = "x3A7rB8zD2L5qW4nH1"
  37. local file = fs.open(keyFilePath, "w")
  38. file.write(key)
  39. file.close()
  40. print("Encryption key downloaded and saved as XOR.key.")
  41. term.setTextColor(colors.white)
  42. end
  43.  
  44. local file = fs.open(keyFilePath, "r")
  45. local key = file.readAll()
  46. file.close()
  47.  
  48. return key
  49. end
  50.  
  51. -- XOR encryption function
  52. local function xorEncryptDecrypt(message, key)
  53. local encrypted = {}
  54. for i = 1, #message do
  55. local char = string.byte(message, i)
  56. local keyChar = string.byte(key, (i - 1) % #key + 1)
  57. table.insert(encrypted, string.char(bit.bxor(char, keyChar)))
  58. end
  59. return table.concat(encrypted)
  60. end
  61.  
  62. -- Function to create a new profile
  63. local function createProfile()
  64. term.clear()
  65. term.setCursorPos(1, 1)
  66. term.setTextColor(colors.cyan)
  67. print("Enter a username for the new profile:")
  68. term.setTextColor(colors.white)
  69. local username = read()
  70.  
  71. local profilePath = "./dogchat/profiles/" .. username
  72. if fileExists(profilePath) then
  73. term.setTextColor(colors.red)
  74. print("Profile already exists.")
  75. term.setTextColor(colors.white)
  76. return
  77. end
  78.  
  79. term.setTextColor(colors.cyan)
  80. print("Enter a password for the profile:")
  81. term.setTextColor(colors.white)
  82. local password = read("*")
  83.  
  84. local encryptionKey = downloadOrCreateKey()
  85. local hashedPassword = xorEncryptDecrypt(password, encryptionKey)
  86.  
  87. fs.makeDir(profilePath)
  88. local passwordFile = fs.open(profilePath .. "/password.txt", "w")
  89. passwordFile.write(hashedPassword)
  90. passwordFile.close()
  91.  
  92. term.setTextColor(colors.green)
  93. print("Profile created successfully.")
  94. term.setTextColor(colors.white)
  95. end
  96.  
  97. -- Function to delete a profile
  98. local function deleteProfile()
  99. term.clear()
  100. term.setCursorPos(1, 1)
  101. local profiles = listProfiles()
  102. if #profiles == 0 then
  103. term.setTextColor(colors.red)
  104. print("No profiles found.")
  105. term.setTextColor(colors.white)
  106. return
  107. end
  108.  
  109. term.setTextColor(colors.cyan)
  110. print("Select a profile to delete:")
  111. term.setTextColor(colors.white)
  112. for i, profile in ipairs(profiles) do
  113. print(i .. ". " .. profile)
  114. end
  115.  
  116. local choice = tonumber(read())
  117. if not choice or choice < 1 or choice > #profiles then
  118. term.setTextColor(colors.red)
  119. print("Invalid choice.")
  120. term.setTextColor(colors.white)
  121. return
  122. end
  123.  
  124. local username = profiles[choice]
  125. local profilePath = "./dogchat/profiles/" .. username
  126.  
  127. term.setTextColor(colors.cyan)
  128. print("Enter the password for " .. username .. " to confirm deletion:")
  129. term.setTextColor(colors.white)
  130. local password = read("*")
  131.  
  132. local encryptionKey = downloadOrCreateKey()
  133. local passwordFile = fs.open(profilePath .. "/password.txt", "r")
  134. local storedPassword = passwordFile.readAll()
  135. passwordFile.close()
  136.  
  137. if xorEncryptDecrypt(password, encryptionKey) == storedPassword then
  138. fs.delete(profilePath)
  139. term.setTextColor(colors.green)
  140. print("Profile deleted successfully.")
  141. term.setTextColor(colors.white)
  142. else
  143. term.setTextColor(colors.red)
  144. print("Incorrect password. Deletion canceled.")
  145. term.setTextColor(colors.white)
  146. end
  147. end
  148.  
  149. -- Function to load an existing profile
  150. local function loadProfile()
  151. term.clear()
  152. term.setCursorPos(1, 1)
  153. local profiles = listProfiles()
  154. if #profiles == 0 then
  155. term.setTextColor(colors.red)
  156. print("No profiles found.")
  157. term.setTextColor(colors.white)
  158. return nil
  159. end
  160.  
  161. term.setTextColor(colors.cyan)
  162. print("Select a profile to log in:")
  163. term.setTextColor(colors.white)
  164. for i, profile in ipairs(profiles) do
  165. print(i .. ". " .. profile)
  166. end
  167.  
  168. local choice = tonumber(read())
  169. if not choice or choice < 1 or choice > #profiles then
  170. term.setTextColor(colors.red)
  171. print("Invalid choice.")
  172. term.setTextColor(colors.white)
  173. return nil
  174. end
  175.  
  176. local username = profiles[choice]
  177. local profilePath = "./dogchat/profiles/" .. username
  178.  
  179. term.setTextColor(colors.cyan)
  180. print("Enter the password for " .. username .. ":")
  181. term.setTextColor(colors.white)
  182. local password = read("*")
  183.  
  184. local encryptionKey = downloadOrCreateKey()
  185. local passwordFile = fs.open(profilePath .. "/password.txt", "r")
  186. local storedPassword = passwordFile.readAll()
  187. passwordFile.close()
  188.  
  189. if xorEncryptDecrypt(password, encryptionKey) == storedPassword then
  190. term.setTextColor(colors.green)
  191. print("Login successful.")
  192. term.setTextColor(colors.white)
  193. return username
  194. else
  195. term.setTextColor(colors.red)
  196. print("Incorrect password.")
  197. term.setTextColor(colors.white)
  198. return nil
  199. end
  200. end
  201.  
  202. -- Function to manage settings
  203. local function settingsMenu()
  204. term.clear()
  205. term.setCursorPos(1, 1)
  206. while true do
  207. term.setTextColor(colors.cyan)
  208. print("Settings Menu:")
  209. term.setTextColor(colors.white)
  210. print("1. Create a new profile")
  211. print("2. Delete a profile")
  212. print("3. Back to main menu")
  213. local choice = read()
  214.  
  215. if choice == "1" then
  216. createProfile()
  217. elseif choice == "2" then
  218. deleteProfile()
  219. elseif choice == "3" then
  220. break
  221. else
  222. term.setTextColor(colors.red)
  223. print("Invalid choice. Please try again.")
  224. term.setTextColor(colors.white)
  225. end
  226. end
  227. end
  228.  
  229. -- Function to list all connected modems
  230. local function listModems()
  231. local modems = {}
  232. for _, side in ipairs(peripheral.getNames()) do
  233. if peripheral.getType(side) == "modem" then
  234. table.insert(modems, side)
  235. end
  236. end
  237. return modems
  238. end
  239.  
  240. -- Function to setup the modem
  241. local function setupModem()
  242. local modems = listModems()
  243. if #modems == 0 then
  244. term.setTextColor(colors.red)
  245. print("No modems found! Please connect a modem and restart.")
  246. term.setTextColor(colors.white)
  247. return nil
  248. end
  249.  
  250. term.setTextColor(colors.cyan)
  251. print("Connected modems:")
  252. term.setTextColor(colors.white)
  253. for i, side in ipairs(modems) do
  254. print(i .. ": " .. side)
  255. end
  256.  
  257. rednet.open(modems[1])
  258. term.setTextColor(colors.green)
  259. print("Using modem on " .. modems[1])
  260. term.setTextColor(colors.white)
  261. return modems[1]
  262. end
  263.  
  264. -- Function to display a message in the chat with history support
  265. local messageHistory = {}
  266.  
  267. local function displayMessage(senderId, message, isOlderClient)
  268. term.setTextColor(colors.yellow)
  269. if isOlderClient then
  270. print("[Unknown User (" .. senderId .. ")]: " .. message)
  271. else
  272. print("[" .. senderId .. "]: " .. message)
  273. end
  274. table.insert(messageHistory, {senderId = senderId, message = message})
  275. term.setTextColor(colors.white)
  276.  
  277. -- Display last 5 messages
  278. print("\n--- Message History ---")
  279. for i = math.max(#messageHistory - 5, 1), #messageHistory do
  280. print("[" .. messageHistory[i].senderId .. "]: " .. messageHistory[i].message)
  281. end
  282. end
  283.  
  284. -- Function to handle incoming messages
  285. local function handleIncomingMessages(encryptionKey)
  286. while true do
  287. local senderId, encryptedMessage, protocol = rednet.receive(chatProtocol)
  288. local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
  289.  
  290. local username, chatMessage = decryptedMessage:match("^(.-): (.+)$")
  291. if username and chatMessage then
  292. displayMessage(username, chatMessage, false)
  293. else
  294. displayMessage(senderId, decryptedMessage, true)
  295. end
  296. end
  297. end
  298.  
  299. -- Function to send a message
  300. local function sendMessage(username, encryptionKey)
  301. while true do
  302. term.setTextColor(colors.cyan)
  303. print("Type your message (or 'exit' to quit): ")
  304. term.setTextColor(colors.white)
  305. local userInput = read()
  306. if userInput == "exit" then
  307. term.setTextColor(colors.green)
  308. print("Exiting chat...")
  309. term.setTextColor(colors.white)
  310. shell.run("/disk/os/gui")
  311. break
  312. end
  313.  
  314. local formattedMessage = username .. ": " .. userInput
  315. local encryptedMessage = xorEncryptDecrypt(formattedMessage, encryptionKey)
  316.  
  317. rednet.broadcast(encryptedMessage, chatProtocol)
  318. displayMessage(username, userInput, false)
  319. end
  320. end
  321.  
  322. -- Main function
  323. local function main()
  324. term.clear()
  325. term.setCursorPos(1, 1)
  326. print("Doggy Chat - Secure Chat")
  327. local username = loadProfile()
  328.  
  329. if username then
  330. local encryptionKey = downloadOrCreateKey()
  331. setupModem()
  332.  
  333. parallel.waitForAny(
  334. function() handleIncomingMessages(encryptionKey) end,
  335. function() sendMessage(username, encryptionKey) end
  336. )
  337. end
  338. end
  339.  
  340. -- Run the program
  341. main()
  342.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement