Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Server-Side Code for Doggy Mail Server
- -- Manages user accounts, email storage, and handles login/registration.
- local fs = require("fs")
- local emails = {} -- Stores emails indexed by email addresses
- -- Function to open the modem
- local function setupModem()
- local modemSide = nil
- for _, side in pairs({"left", "right", "top", "bottom", "front", "back"}) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- modemSide = side
- break
- end
- end
- if modemSide then
- rednet.open(modemSide)
- print("Modem found and rednet opened.")
- else
- print("No modem found.")
- return false
- end
- return true
- end
- -- Function to create a new account
- local function createAccount(email, password)
- local accountDir = "/users/" .. email
- if not fs.exists(accountDir) then
- fs.makeDir(accountDir) -- Create the user's directory
- -- Store the password in a file
- local passwordFile = accountDir .. "/password.txt"
- local emailFile = accountDir .. "/email.txt"
- fs.writeFile(passwordFile, password)
- fs.writeFile(emailFile, email)
- emails[email] = {} -- Initialize empty inbox
- print("Account created successfully!")
- else
- print("Account already exists for " .. email)
- end
- end
- -- Function to login an account
- local function loginAccount(email, password)
- local accountDir = "/users/" .. email
- if fs.exists(accountDir) then
- local storedPassword = fs.readFile(accountDir .. "/password.txt")
- if storedPassword == password then
- print("Login successful!")
- return true
- else
- print("Incorrect password.")
- return false
- end
- else
- print("Account does not exist.")
- return false
- end
- end
- -- Function to send an email
- local function sendEmail(sender, recipient, content)
- -- If recipient doesn't exist, create an account for them
- if not emails[recipient] then
- createAccount(recipient, "") -- Create an account with an empty password
- end
- -- Store the email in the recipient's inbox
- table.insert(emails[recipient], {sender = sender, content = content})
- print("Email sent from " .. sender .. " to " .. recipient)
- end
- -- Function to check for emails for a specific account
- local function checkEmails(recipient)
- if emails[recipient] then
- print("\n--- New Emails for " .. recipient .. " ---")
- local page = 1
- local emailsPerPage = 3
- local totalEmails = #emails[recipient]
- local totalPages = math.ceil(totalEmails / emailsPerPage)
- while true do
- term.clear()
- print("Checking emails for " .. recipient .. " - Page " .. page .. "/" .. totalPages)
- -- Calculate the start and end indices for the emails to display
- local startIndex = (page - 1) * emailsPerPage + 1
- local endIndex = math.min(page * emailsPerPage, totalEmails)
- -- Display the emails for this page
- for i = startIndex, endIndex do
- local email = emails[recipient][i]
- print("\nFrom: " .. email.sender)
- print("Content: \n" .. email.content)
- print("-------------------")
- end
- -- Prompt user for navigation
- print("\nPage " .. page .. "/" .. totalPages)
- print("Press 'n' for next page, 'p' for previous page, or 'q' to exit.")
- local choice = read()
- if choice == "n" and page < totalPages then
- page = page + 1
- elseif choice == "p" and page > 1 then
- page = page - 1
- elseif choice == "q" then
- break
- end
- end
- else
- print("\nNo emails found for " .. recipient)
- end
- end
- -- Main loop to handle requests from clients
- while true do
- local senderID, message = rednet.receive()
- if message.request == "register" then
- -- Register a new account
- createAccount(message.email, message.password)
- elseif message.request == "login" then
- -- Login to an account
- if loginAccount(message.email, message.password) then
- rednet.send(senderID, {status = "success"})
- else
- rednet.send(senderID, {status = "failure"})
- end
- elseif message.request == "sendEmail" then
- -- Send an email
- sendEmail(message.sender, message.recipient, message.content)
- elseif message.request == "getEmails" then
- -- Send emails to client
- rednet.send(senderID, emails[message.recipient] or {})
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement