Advertisement
DOGGYWOOF

Untitled

Dec 15th, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. -- Server to receive, store, and send emails to clients
  2.  
  3. local emails = {} -- Table to store emails by recipientID
  4. local accounts = {} -- Table to store account credentials
  5.  
  6. -- Function to store an email
  7. local function storeEmail(emailData)
  8. if not emails[emailData.recipientID] then
  9. emails[emailData.recipientID] = {}
  10. end
  11. table.insert(emails[emailData.recipientID], emailData)
  12. print("DEBUG: Email stored for recipient: " .. emailData.recipientID)
  13. print("DEBUG: Email Content: " .. emailData.emailContent)
  14. end
  15.  
  16. -- Function to register a new account
  17. local function registerAccount(accountData)
  18. if accounts[accountData.username] then
  19. print("DEBUG: Account already exists for username: " .. accountData.username)
  20. return false -- Account already exists
  21. end
  22. accounts[accountData.username] = {
  23. password = accountData.password,
  24. clientID = accountData.clientID
  25. }
  26. print("DEBUG: Account registered for username: " .. accountData.username)
  27. return true
  28. end
  29.  
  30. -- Function to authenticate an account
  31. local function authenticateAccount(accountData)
  32. local account = accounts[accountData.username]
  33. if account and account.password == accountData.password then
  34. print("DEBUG: Authentication successful for username: " .. accountData.username)
  35. return true
  36. else
  37. print("DEBUG: Authentication failed for username: " .. accountData.username)
  38. return false
  39. end
  40. end
  41.  
  42. -- Function to handle incoming requests from clients
  43. local function handleClientRequests()
  44. while true do
  45. -- Receive messages from clients
  46. local senderID, requestData = rednet.receive()
  47.  
  48. if requestData then
  49. -- Debug message to show incoming request data
  50. print("DEBUG: Received data from client (" .. senderID .. "):")
  51. print("DEBUG: requestData: " .. textutils.serialize(requestData))
  52.  
  53. if requestData.request == "getEmails" then
  54. local clientID = requestData.clientID
  55. print("DEBUG: Client " .. clientID .. " is requesting emails.")
  56.  
  57. -- Check if there are emails for the requesting client
  58. if emails[clientID] then
  59. print("DEBUG: Found emails for client " .. clientID .. ". Sending emails...")
  60. -- Send all emails for the client to the requesting client
  61. rednet.send(senderID, emails[clientID])
  62. print("DEBUG: Emails sent to client " .. clientID)
  63. else
  64. -- No emails for this client
  65. print("DEBUG: No emails found for client " .. clientID)
  66. rednet.send(senderID, nil)
  67. end
  68. elseif requestData.request == "registerAccount" then
  69. -- Register a new account
  70. local success = registerAccount(requestData.accountData)
  71. rednet.send(senderID, {success = success})
  72. elseif requestData.request == "authenticateAccount" then
  73. -- Authenticate an account
  74. local success = authenticateAccount(requestData.accountData)
  75. rednet.send(senderID, {success = success})
  76. elseif requestData.senderID and requestData.recipientID then
  77. -- This is an email, store it
  78. print("DEBUG: Storing email from " .. requestData.senderID .. " to " .. requestData.recipientID)
  79. storeEmail(requestData)
  80. else
  81. print("DEBUG: Unknown request from client.")
  82. end
  83. else
  84. print("DEBUG: No data received.")
  85. end
  86. end
  87. end
  88.  
  89. -- Start listening for client requests
  90. rednet.open("left") -- Replace with the correct modem side
  91.  
  92. -- Call the function to handle client requests
  93. print("DEBUG: Server is now listening for requests.")
  94. handleClientRequests()
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement