Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Client-side program with black and white UI and email scrolling functionality
- -- Doggy Mail Branding
- -- 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()
- -- Try to find a modem connected to the system
- 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 a modem is found, open it with rednet
- 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 ask for the server ID and save it to a file
- local function setupServerID()
- local serverIDFile = "/dogchat/services/email/server.ID"
- if not fs.exists(serverIDFile) then
- -- If it doesn't exist, prompt the user for the server ID
- displayHeader("Setup Doggy Mail Server ID")
- print("Server ID not found. Please enter the server ID:")
- local serverID = read()
- -- Save the server ID to the file
- local file = fs.open(serverIDFile, "w")
- file.write(serverID)
- file.close()
- print("\nServer ID saved.")
- else
- print("Server ID already exists.")
- end
- 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 send email to the server
- local function sendEmailToServer()
- -- Check for server ID before sending email
- setupServerID()
- -- Read the server ID from the saved file
- local serverIDFile = "/dogchat/services/email/server.ID"
- local serverID = fs.open(serverIDFile, "r").readLine()
- -- 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 = getClientID(),
- recipientID = recipientID,
- emailContent = emailContent
- }
- -- Debugging message before sending
- print("\nPreparing to send email to server ID: " .. serverID)
- print("Sender: " .. emailData.senderID)
- print("Recipient: " .. emailData.recipientID)
- print("Content: \n" .. emailData.emailContent)
- -- Send the email to the server
- print("\nSending email to server (ID: " .. serverID .. ") via Doggy Mail...")
- rednet.send(tonumber(serverID), emailData)
- -- Debug message after sending
- print("\nEmail sent via Doggy Mail.")
- end
- -- Function to check and read emails from the server
- local function checkAndReadEmails()
- -- Check for server ID before checking emails
- setupServerID()
- -- Read the server ID from the saved file
- local serverIDFile = "/dogchat/services/email/server.ID"
- local serverID = fs.open(serverIDFile, "r").readLine()
- -- Get the client ID automatically from the computer's ID
- local clientID = getClientID()
- displayHeader("Check for New Emails - Doggy Mail")
- 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 .. " - Doggy Mail")
- -- 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.recipientID == clientID then
- print("\nFrom: " .. email.senderID)
- print("To: " .. email.recipientID)
- 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()
- displayHeader("Doggy Mail Client")
- print("Welcome to Doggy Mail!")
- print("1. Send Email")
- print("2. Read Emails")
- print("3. Exit")
- local choice = tonumber(read())
- if choice == 1 then
- sendEmailToServer()
- elseif choice == 2 then
- checkAndReadEmails()
- elseif choice == 3 then
- print("\nExiting Doggy Mail...")
- return
- else
- print("Invalid choice, please try again.")
- mainMenu()
- 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
- -- Start the main menu
- mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement