Advertisement
DOGGYWOOF

Untitled

Dec 15th, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. -- Doggy Mail Account System
  2.  
  3. -- File paths for storing user data
  4. local accountsFile = "/dogchat/accounts.txt" -- Stores account information (username and password)
  5.  
  6. -- Function to display a header with the program name
  7. local function displayHeader(title)
  8. term.clear()
  9. term.setCursorPos(1, 1)
  10. term.setBackgroundColor(colors.black)
  11. term.setTextColor(colors.white)
  12. term.write(string.rep("=", 50)) -- Top divider line
  13. term.setCursorPos(2, 2)
  14. term.write(" " .. title)
  15. term.setCursorPos(50, 2)
  16. term.write("v1.0")
  17. term.setCursorPos(1, 3)
  18. term.write(string.rep("=", 50)) -- Bottom divider line
  19. term.setCursorPos(1, 5)
  20. end
  21.  
  22. -- Function to setup the account system
  23. local function setupAccountSystem()
  24. -- Check if the accounts file exists
  25. if not fs.exists(accountsFile) then
  26. -- If it doesn't exist, create the file
  27. local file = fs.open(accountsFile, "w")
  28. file.close()
  29. print("Accounts file created.")
  30. end
  31. end
  32.  
  33. -- Function to register a new account
  34. local function registerAccount()
  35. displayHeader("Doggy Mail - Register Account")
  36. print("Please enter a username (alphanumeric only):")
  37. local username = read()
  38.  
  39. -- Check if the username already exists
  40. local file = fs.open(accountsFile, "r")
  41. local accountExists = false
  42. while true do
  43. local line = file.readLine()
  44. if not line then break end
  45. if string.match(line, "^" .. username .. ":") then
  46. accountExists = true
  47. break
  48. end
  49. end
  50. file.close()
  51.  
  52. if accountExists then
  53. print("\nUsername already exists. Please try again.")
  54. return
  55. end
  56.  
  57. -- Request password
  58. print("Enter a password:")
  59. local password = read("*")
  60.  
  61. -- Save the username and password
  62. file = fs.open(accountsFile, "a")
  63. file.writeLine(username .. ":" .. password)
  64. file.close()
  65.  
  66. print("\nAccount successfully created!")
  67. end
  68.  
  69. -- Function to log in with an existing account
  70. local function loginAccount()
  71. displayHeader("Doggy Mail - Log In")
  72. print("Please enter your username:")
  73. local username = read()
  74.  
  75. -- Check if the username exists and validate password
  76. local file = fs.open(accountsFile, "r")
  77. local accountValid = false
  78. while true do
  79. local line = file.readLine()
  80. if not line then break end
  81. local storedUsername, storedPassword = string.match(line, "(.*):(.*)")
  82. if storedUsername == username then
  83. print("Enter your password:")
  84. local password = read("*")
  85. if password == storedPassword then
  86. accountValid = true
  87. end
  88. break
  89. end
  90. end
  91. file.close()
  92.  
  93. if accountValid then
  94. print("\nLogin successful! Welcome, " .. username .. "!")
  95. return username -- Return the logged-in username
  96. else
  97. print("\nInvalid username or password.")
  98. return nil
  99. end
  100. end
  101.  
  102. -- Main program to handle login or registration
  103. local function accountMenu()
  104. displayHeader("Doggy Mail - Account Menu")
  105. print("1. Register a new account")
  106. print("2. Log in")
  107. print("3. Exit")
  108.  
  109. local choice = tonumber(read())
  110.  
  111. if choice == 1 then
  112. registerAccount()
  113. elseif choice == 2 then
  114. local loggedInUsername = loginAccount()
  115. if loggedInUsername then
  116. return loggedInUsername
  117. end
  118. elseif choice == 3 then
  119. print("Exiting Doggy Mail...")
  120. return nil
  121. else
  122. print("Invalid choice. Please try again.")
  123. return accountMenu()
  124. end
  125. end
  126.  
  127. -- Function to send email
  128. local function sendEmailToServer(user)
  129. -- Setup for the email client (similar to previous)
  130. setupServerID()
  131.  
  132. -- Get recipient ID and sender ID
  133. displayHeader("Send Email via Doggy Mail")
  134. print("Enter recipient's ID:")
  135. local recipientID = read()
  136. print("Enter your email content:")
  137. local emailContent = read()
  138.  
  139. -- Prepare the email data to send to the server
  140. local emailData = {
  141. senderID = user,
  142. recipientID = recipientID,
  143. emailContent = emailContent
  144. }
  145.  
  146. -- Send the email to the server
  147. local serverIDFile = "/dogchat/services/email/server.ID"
  148. local serverID = fs.open(serverIDFile, "r").readLine()
  149.  
  150. print("\nSending email...")
  151. rednet.send(tonumber(serverID), emailData)
  152. print("\nEmail sent.")
  153. end
  154.  
  155. -- Function to check and read emails
  156. local function checkAndReadEmails(user)
  157. -- Request emails from the server for the logged-in user
  158. displayHeader("Check Emails - Doggy Mail")
  159. print("Checking for new emails for " .. user .. "...")
  160.  
  161. -- Setup server and check emails (similar to previous)
  162. setupServerID()
  163. local serverIDFile = "/dogchat/services/email/server.ID"
  164. local serverID = fs.open(serverIDFile, "r").readLine()
  165.  
  166. -- Request and display emails
  167. rednet.send(tonumber(serverID), {request = "getEmails", clientID = user})
  168. local senderID, emailData = rednet.receive()
  169.  
  170. if emailData then
  171. print("\n--- New Emails ---")
  172. for _, email in ipairs(emailData) do
  173. if email.recipientID == user then
  174. print("\nFrom: " .. email.senderID)
  175. print("Content: \n" .. email.emailContent)
  176. print("-------------------")
  177. end
  178. end
  179. else
  180. print("\nNo new emails.")
  181. end
  182. end
  183.  
  184. -- Main menu
  185. local function mainMenu(user)
  186. displayHeader("Doggy Mail Client - Welcome " .. user)
  187. print("1. Send Email")
  188. print("2. Read Emails")
  189. print("3. Logout")
  190.  
  191. local choice = tonumber(read())
  192.  
  193. if choice == 1 then
  194. sendEmailToServer(user)
  195. elseif choice == 2 then
  196. checkAndReadEmails(user)
  197. elseif choice == 3 then
  198. print("\nLogging out...")
  199. return nil
  200. else
  201. print("Invalid choice. Please try again.")
  202. return mainMenu(user)
  203. end
  204. end
  205.  
  206. -- Ensure account system is ready
  207. setupAccountSystem()
  208.  
  209. -- Main program logic
  210. local loggedInUser = nil
  211.  
  212. while not loggedInUser do
  213. loggedInUser = accountMenu()
  214. end
  215.  
  216. -- After login or registration, enter the main menu
  217. while loggedInUser do
  218. loggedInUser = mainMenu(loggedInUser)
  219. end
  220.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement