Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Server to receive, store, and send emails to clients
- local emails = {} -- Table to store emails by recipientID
- local accounts = {} -- Table to store account credentials
- -- Function to store an email
- local function storeEmail(emailData)
- if not emails[emailData.recipientID] then
- emails[emailData.recipientID] = {}
- end
- table.insert(emails[emailData.recipientID], emailData)
- print("DEBUG: Email stored for recipient: " .. emailData.recipientID)
- print("DEBUG: Email Content: " .. emailData.emailContent)
- end
- -- Function to register a new account
- local function registerAccount(accountData)
- if accounts[accountData.username] then
- print("DEBUG: Account already exists for username: " .. accountData.username)
- return false -- Account already exists
- end
- accounts[accountData.username] = {
- password = accountData.password,
- clientID = accountData.clientID
- }
- print("DEBUG: Account registered for username: " .. accountData.username)
- return true
- end
- -- Function to authenticate an account
- local function authenticateAccount(accountData)
- local account = accounts[accountData.username]
- if account and account.password == accountData.password then
- print("DEBUG: Authentication successful for username: " .. accountData.username)
- return true
- else
- print("DEBUG: Authentication failed for username: " .. accountData.username)
- return false
- end
- end
- -- Function to handle incoming requests from clients
- local function handleClientRequests()
- while true do
- -- Receive messages from clients
- local senderID, requestData = rednet.receive()
- if requestData then
- -- Debug message to show incoming request data
- print("DEBUG: Received data from client (" .. senderID .. "):")
- print("DEBUG: requestData: " .. textutils.serialize(requestData))
- if requestData.request == "getEmails" then
- local clientID = requestData.clientID
- print("DEBUG: Client " .. clientID .. " is requesting emails.")
- -- Check if there are emails for the requesting client
- if emails[clientID] then
- print("DEBUG: Found emails for client " .. clientID .. ". Sending emails...")
- -- Send all emails for the client to the requesting client
- rednet.send(senderID, emails[clientID])
- print("DEBUG: Emails sent to client " .. clientID)
- else
- -- No emails for this client
- print("DEBUG: No emails found for client " .. clientID)
- rednet.send(senderID, nil)
- end
- elseif requestData.request == "registerAccount" then
- -- Register a new account
- local success = registerAccount(requestData.accountData)
- rednet.send(senderID, {success = success})
- elseif requestData.request == "authenticateAccount" then
- -- Authenticate an account
- local success = authenticateAccount(requestData.accountData)
- rednet.send(senderID, {success = success})
- elseif requestData.senderID and requestData.recipientID then
- -- This is an email, store it
- print("DEBUG: Storing email from " .. requestData.senderID .. " to " .. requestData.recipientID)
- storeEmail(requestData)
- else
- print("DEBUG: Unknown request from client.")
- end
- else
- print("DEBUG: No data received.")
- end
- end
- end
- -- Start listening for client requests
- rednet.open("left") -- Replace with the correct modem side
- -- Call the function to handle client requests
- print("DEBUG: Server is now listening for requests.")
- handleClientRequests()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement