Advertisement
DOGGYWOOF

server

Dec 15th, 2024
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. -- Doggy Mail Server Code
  2.  
  3. -- Function to create or load account information
  4. local function loadAccountData()
  5. local accountFile = "/dogchat/accounts.dat"
  6. if not fs.exists(accountFile) then
  7. return {}
  8. end
  9. local file = fs.open(accountFile, "r")
  10. local accountData = textutils.unserialize(file.readAll())
  11. file.close()
  12. return accountData
  13. end
  14.  
  15. -- Function to save account information
  16. local function saveAccountData(accountData)
  17. local accountFile = "/dogchat/accounts.dat"
  18. local file = fs.open(accountFile, "w")
  19. file.write(textutils.serialize(accountData))
  20. file.close()
  21. end
  22.  
  23. -- Function to handle incoming emails from clients
  24. local function handleIncomingEmail(sender, emailData)
  25. local accountData = loadAccountData()
  26.  
  27. -- Ensure the recipient account exists
  28. local recipient = emailData.recipient
  29. if not accountData[recipient] then
  30. print("Recipient account does not exist.")
  31. return
  32. end
  33.  
  34. -- Add the email to the recipient's email list
  35. local userEmails = accountData[recipient].emails
  36. table.insert(userEmails, emailData)
  37. accountData[recipient].emails = userEmails
  38.  
  39. -- Save the updated account data
  40. saveAccountData(accountData)
  41.  
  42. print("Email received from " .. sender .. " to " .. recipient)
  43. end
  44.  
  45. -- Function to start the server
  46. local function startServer()
  47. -- Setup modem
  48. local modemSide = nil
  49. for _, side in pairs({"left", "right", "top", "bottom", "front", "back"}) do
  50. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  51. modemSide = side
  52. break
  53. end
  54. end
  55.  
  56. -- Check if a modem is found
  57. if modemSide then
  58. rednet.open(modemSide)
  59. print("Server is running. Waiting for messages...")
  60. else
  61. print("No modem found. Server cannot start.")
  62. return
  63. end
  64.  
  65. -- Main loop to listen for incoming messages
  66. while true do
  67. local senderID, message = rednet.receive()
  68.  
  69. -- Check if the message is an email (table format)
  70. if type(message) == "table" and message.sender and message.recipient and message.content then
  71. handleIncomingEmail(senderID, message)
  72. end
  73. end
  74. end
  75.  
  76. -- Run the server
  77. startServer()
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement