Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to display a header with a simple divider
- local function displayHeader(title)
- term.clear()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write(string.rep("=", 50)) -- Top divider line
- term.setCursorPos(2, 2)
- term.write(" " .. title)
- term.setCursorPos(50, 2)
- term.write("v1.0")
- term.setCursorPos(1, 3)
- term.write(string.rep("=", 50)) -- Bottom divider line
- term.setCursorPos(1, 5)
- end
- -- Function to setup and 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 on " .. modemSide .. " and rednet opened.")
- else
- print("No modem found.")
- return false
- end
- return true
- end
- -- Function to load the server ID from the file
- local function loadServerID()
- local filePath = "/dogchat/services/Dmail/ServerID.txt"
- local file = fs.open(filePath, "r")
- if file then
- local serverID = file.readLine()
- file.close()
- return serverID
- else
- return nil
- end
- end
- -- Function to ask the user for the server ID if not found
- local function askForServerID()
- print("Server ID not found.")
- print("Please enter the server ID:")
- local serverID = read()
- -- Save the server ID for future use
- local filePath = "/dogchat/services/Dmail/ServerID.txt"
- local file = fs.open(filePath, "w")
- if file then
- file.writeLine(serverID)
- file.close()
- else
- print("Error saving the ServerID.")
- end
- return serverID
- end
- -- Function to get the server ID (load or ask for it)
- local function getServerID()
- local serverID = loadServerID()
- if not serverID then
- serverID = askForServerID()
- end
- return serverID
- end
- -- Function to get the Client ID from the computer ID
- local function getClientID()
- return tostring(os.getComputerID()) -- Use the computer's ID as the client ID
- end
- -- Function to load account credentials from file
- local function loadCredentials()
- local credsFilePath = "/dogchat/accounts/credentials.txt"
- if fs.exists(credsFilePath) then
- local file = fs.open(credsFilePath, "r")
- local username = file.readLine()
- local password = file.readLine()
- file.close()
- return {username = username, password = password}
- else
- return nil
- end
- end
- -- Function to create a new account
- local function createAccount()
- print("No account found. Please create a new account.")
- print("Enter your username:")
- local username = read()
- print("Enter your password:")
- local password = read("*")
- -- Save credentials to file
- local credsFilePath = "/dogchat/accounts/credentials.txt"
- local file = fs.open(credsFilePath, "w")
- if file then
- file.writeLine(username)
- file.writeLine(password)
- file.close()
- else
- print("Error saving credentials.")
- end
- return {username = username, password = password}
- end
- -- Function to login using credentials
- local function login()
- local creds = loadCredentials()
- if not creds then
- return createAccount() -- If no credentials exist, create a new account
- end
- print("Enter your username:")
- local inputUsername = read()
- print("Enter your password:")
- local inputPassword = read("*")
- if inputUsername == creds.username and inputPassword == creds.password then
- print("Login successful!")
- return creds
- else
- print("Incorrect credentials. Please try again.")
- return login() -- Retry if credentials are incorrect
- end
- end
- -- Function to send an email to the server
- local function sendEmailToServer(user)
- -- Get the server ID (or prompt if it's not available)
- local serverID = getServerID()
- -- Get recipient email and content
- displayHeader("Send Email")
- print("Enter recipient's email address:")
- local recipientEmail = read()
- print("Enter your email content:")
- local emailContent = read()
- -- Prepare the email data to send to the server
- local emailData = {
- senderID = user.username,
- recipientEmail = recipientEmail,
- emailContent = emailContent
- }
- -- Debugging message before sending
- print("\nPreparing to send email to server ID: " .. serverID)
- print("Sender: " .. emailData.senderID)
- print("Recipient: " .. emailData.recipientEmail)
- print("Content: \n" .. emailData.emailContent)
- -- Send the email to the server
- print("\nSending email to server (ID: " .. serverID .. ")...")
- rednet.send(tonumber(serverID), emailData)
- print("\nEmail sent to server.")
- end
- -- Function to check and read emails from the server
- local function checkAndReadEmails(user)
- -- Get the server ID (or prompt if it's not available)
- local serverID = getServerID()
- -- Get the client ID automatically from the computer's ID
- local clientID = getClientID()
- displayHeader("Check for New Emails")
- print("Checking for new emails for client ID: " .. clientID)
- -- Request emails from the server for this client
- rednet.send(tonumber(serverID), {request = "getEmails", clientID = clientID})
- -- Wait for the server to send emails
- local senderID, emailData = rednet.receive()
- -- Check if the server sent any emails
- if emailData then
- print("\n--- New Emails ---")
- local page = 1
- local emailsPerPage = 3
- local totalEmails = #emailData
- local totalPages = math.ceil(totalEmails / emailsPerPage)
- while true do
- term.clear()
- displayHeader("Emails - 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 = emailData[i]
- if email.recipientEmail == user.username then
- print("\nFrom: " .. email.senderID)
- print("To: " .. email.recipientEmail)
- print("Content: \n" .. email.emailContent)
- print("-------------------")
- end
- 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 new emails.")
- end
- end
- -- Main logic
- local function mainMenu(user)
- displayHeader("Doggy Mail Client")
- print("Welcome to Doggy Mail!")
- print("1. Send Email")
- print("2. Check Emails")
- print("3. Exit")
- local choice = tonumber(read())
- if choice == 1 then
- sendEmailToServer(user)
- elseif choice == 2 then
- checkAndReadEmails(user)
- elseif choice == 3 then
- print("\nExiting...")
- return
- else
- print("Invalid choice, please try again.")
- mainMenu(user)
- end
- end
- -- Ensure modem setup is correct before starting
- if not setupModem() then
- print("Modem setup failed. Please connect a modem and try again.")
- return
- end
- -- Login or Create Account
- local user = login()
- -- Start the main menu
- mainMenu(user)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement