Advertisement
DOGGYWOOF

Email server

Dec 15th, 2024 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. -- Server-Side Code for Doggy Mail Server
  2. -- Manages user accounts, email storage, and handles login/registration.
  3.  
  4. local fs = require("fs")
  5. local emails = {} -- Stores emails indexed by email addresses
  6.  
  7. -- Function to open the modem
  8. local function setupModem()
  9. local modemSide = nil
  10. for _, side in pairs({"left", "right", "top", "bottom", "front", "back"}) do
  11. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  12. modemSide = side
  13. break
  14. end
  15. end
  16.  
  17. if modemSide then
  18. rednet.open(modemSide)
  19. print("Modem found and rednet opened.")
  20. else
  21. print("No modem found.")
  22. return false
  23. end
  24. return true
  25. end
  26.  
  27. -- Function to create a new account
  28. local function createAccount(email, password)
  29. local accountDir = "/users/" .. email
  30. if not fs.exists(accountDir) then
  31. fs.makeDir(accountDir) -- Create the user's directory
  32. -- Store the password in a file
  33. local passwordFile = accountDir .. "/password.txt"
  34. local emailFile = accountDir .. "/email.txt"
  35. fs.writeFile(passwordFile, password)
  36. fs.writeFile(emailFile, email)
  37. emails[email] = {} -- Initialize empty inbox
  38. print("Account created successfully!")
  39. else
  40. print("Account already exists for " .. email)
  41. end
  42. end
  43.  
  44. -- Function to login an account
  45. local function loginAccount(email, password)
  46. local accountDir = "/users/" .. email
  47. if fs.exists(accountDir) then
  48. local storedPassword = fs.readFile(accountDir .. "/password.txt")
  49. if storedPassword == password then
  50. print("Login successful!")
  51. return true
  52. else
  53. print("Incorrect password.")
  54. return false
  55. end
  56. else
  57. print("Account does not exist.")
  58. return false
  59. end
  60. end
  61.  
  62. -- Function to send an email
  63. local function sendEmail(sender, recipient, content)
  64. -- If recipient doesn't exist, create an account for them
  65. if not emails[recipient] then
  66. createAccount(recipient, "") -- Create an account with an empty password
  67. end
  68.  
  69. -- Store the email in the recipient's inbox
  70. table.insert(emails[recipient], {sender = sender, content = content})
  71. print("Email sent from " .. sender .. " to " .. recipient)
  72. end
  73.  
  74. -- Function to check for emails for a specific account
  75. local function checkEmails(recipient)
  76. if emails[recipient] then
  77. print("\n--- New Emails for " .. recipient .. " ---")
  78. local page = 1
  79. local emailsPerPage = 3
  80. local totalEmails = #emails[recipient]
  81. local totalPages = math.ceil(totalEmails / emailsPerPage)
  82.  
  83. while true do
  84. term.clear()
  85. print("Checking emails for " .. recipient .. " - Page " .. page .. "/" .. totalPages)
  86.  
  87. -- Calculate the start and end indices for the emails to display
  88. local startIndex = (page - 1) * emailsPerPage + 1
  89. local endIndex = math.min(page * emailsPerPage, totalEmails)
  90.  
  91. -- Display the emails for this page
  92. for i = startIndex, endIndex do
  93. local email = emails[recipient][i]
  94. print("\nFrom: " .. email.sender)
  95. print("Content: \n" .. email.content)
  96. print("-------------------")
  97. end
  98.  
  99. -- Prompt user for navigation
  100. print("\nPage " .. page .. "/" .. totalPages)
  101. print("Press 'n' for next page, 'p' for previous page, or 'q' to exit.")
  102. local choice = read()
  103.  
  104. if choice == "n" and page < totalPages then
  105. page = page + 1
  106. elseif choice == "p" and page > 1 then
  107. page = page - 1
  108. elseif choice == "q" then
  109. break
  110. end
  111. end
  112. else
  113. print("\nNo emails found for " .. recipient)
  114. end
  115. end
  116.  
  117. -- Main loop to handle requests from clients
  118. while true do
  119. local senderID, message = rednet.receive()
  120. if message.request == "register" then
  121. -- Register a new account
  122. createAccount(message.email, message.password)
  123. elseif message.request == "login" then
  124. -- Login to an account
  125. if loginAccount(message.email, message.password) then
  126. rednet.send(senderID, {status = "success"})
  127. else
  128. rednet.send(senderID, {status = "failure"})
  129. end
  130. elseif message.request == "sendEmail" then
  131. -- Send an email
  132. sendEmail(message.sender, message.recipient, message.content)
  133. elseif message.request == "getEmails" then
  134. -- Send emails to client
  135. rednet.send(senderID, emails[message.recipient] or {})
  136. end
  137. end
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement