Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Doggy Mail Account System
- -- File paths for storing user data
- local accountsFile = "/dogchat/accounts.txt" -- Stores account information (username and password)
- -- Function to display a header with the program name
- 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 the account system
- local function setupAccountSystem()
- -- Check if the accounts file exists
- if not fs.exists(accountsFile) then
- -- If it doesn't exist, create the file
- local file = fs.open(accountsFile, "w")
- file.close()
- print("Accounts file created.")
- end
- end
- -- Function to register a new account
- local function registerAccount()
- displayHeader("Doggy Mail - Register Account")
- print("Please enter a username (alphanumeric only):")
- local username = read()
- -- Check if the username already exists
- local file = fs.open(accountsFile, "r")
- local accountExists = false
- while true do
- local line = file.readLine()
- if not line then break end
- if string.match(line, "^" .. username .. ":") then
- accountExists = true
- break
- end
- end
- file.close()
- if accountExists then
- print("\nUsername already exists. Please try again.")
- return
- end
- -- Request password
- print("Enter a password:")
- local password = read("*")
- -- Save the username and password
- file = fs.open(accountsFile, "a")
- file.writeLine(username .. ":" .. password)
- file.close()
- print("\nAccount successfully created!")
- end
- -- Function to log in with an existing account
- local function loginAccount()
- displayHeader("Doggy Mail - Log In")
- print("Please enter your username:")
- local username = read()
- -- Check if the username exists and validate password
- local file = fs.open(accountsFile, "r")
- local accountValid = false
- while true do
- local line = file.readLine()
- if not line then break end
- local storedUsername, storedPassword = string.match(line, "(.*):(.*)")
- if storedUsername == username then
- print("Enter your password:")
- local password = read("*")
- if password == storedPassword then
- accountValid = true
- end
- break
- end
- end
- file.close()
- if accountValid then
- print("\nLogin successful! Welcome, " .. username .. "!")
- return username -- Return the logged-in username
- else
- print("\nInvalid username or password.")
- return nil
- end
- end
- -- Main program to handle login or registration
- local function accountMenu()
- displayHeader("Doggy Mail - Account Menu")
- print("1. Register a new account")
- print("2. Log in")
- print("3. Exit")
- local choice = tonumber(read())
- if choice == 1 then
- registerAccount()
- elseif choice == 2 then
- local loggedInUsername = loginAccount()
- if loggedInUsername then
- return loggedInUsername
- end
- elseif choice == 3 then
- print("Exiting Doggy Mail...")
- return nil
- else
- print("Invalid choice. Please try again.")
- return accountMenu()
- end
- end
- -- Function to send email
- local function sendEmailToServer(user)
- -- Setup for the email client (similar to previous)
- setupServerID()
- -- Get recipient ID and sender ID
- displayHeader("Send Email via Doggy Mail")
- print("Enter recipient's ID:")
- local recipientID = read()
- print("Enter your email content:")
- local emailContent = read()
- -- Prepare the email data to send to the server
- local emailData = {
- senderID = user,
- recipientID = recipientID,
- emailContent = emailContent
- }
- -- Send the email to the server
- local serverIDFile = "/dogchat/services/email/server.ID"
- local serverID = fs.open(serverIDFile, "r").readLine()
- print("\nSending email...")
- rednet.send(tonumber(serverID), emailData)
- print("\nEmail sent.")
- end
- -- Function to check and read emails
- local function checkAndReadEmails(user)
- -- Request emails from the server for the logged-in user
- displayHeader("Check Emails - Doggy Mail")
- print("Checking for new emails for " .. user .. "...")
- -- Setup server and check emails (similar to previous)
- setupServerID()
- local serverIDFile = "/dogchat/services/email/server.ID"
- local serverID = fs.open(serverIDFile, "r").readLine()
- -- Request and display emails
- rednet.send(tonumber(serverID), {request = "getEmails", clientID = user})
- local senderID, emailData = rednet.receive()
- if emailData then
- print("\n--- New Emails ---")
- for _, email in ipairs(emailData) do
- if email.recipientID == user then
- print("\nFrom: " .. email.senderID)
- print("Content: \n" .. email.emailContent)
- print("-------------------")
- end
- end
- else
- print("\nNo new emails.")
- end
- end
- -- Main menu
- local function mainMenu(user)
- displayHeader("Doggy Mail Client - Welcome " .. user)
- print("1. Send Email")
- print("2. Read Emails")
- print("3. Logout")
- local choice = tonumber(read())
- if choice == 1 then
- sendEmailToServer(user)
- elseif choice == 2 then
- checkAndReadEmails(user)
- elseif choice == 3 then
- print("\nLogging out...")
- return nil
- else
- print("Invalid choice. Please try again.")
- return mainMenu(user)
- end
- end
- -- Ensure account system is ready
- setupAccountSystem()
- -- Main program logic
- local loggedInUser = nil
- while not loggedInUser do
- loggedInUser = accountMenu()
- end
- -- After login or registration, enter the main menu
- while loggedInUser do
- loggedInUser = mainMenu(loggedInUser)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement