Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 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 with the display name
- local function sendEmailToServer(args)
- -- 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
- local recipientID = args[2] -- Argument for recipient ID
- local senderDisplayName = args[3] -- Argument for sender's display name
- local emailContent = args[4] -- Argument for email content
- -- Prepare the email data to send to the server
- local emailData = {
- senderID = getClientID(), -- Store the actual client ID
- senderDisplayName = senderDisplayName, -- Store the sender's display name
- recipientID = recipientID,
- emailContent = emailContent
- }
- -- Debugging message before sending
- print("\nPreparing to send email to server ID: " .. serverID)
- print("Sender Display Name: " .. emailData.senderDisplayName)
- print("Sender ID: " .. 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.senderDisplayName) -- Display the sender's display name
- 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 to process the command-line arguments
- local function main()
- -- Ensure modem setup is correct before starting
- if not setupModem() then
- print("Modem setup failed. Please connect a modem and try again.")
- return
- end
- -- Check if the user wants to send or receive emails
- if #arg < 1 then
- print("Usage: [send|read] <args>")
- return
- end
- if arg[1] == "send" then
- if #arg < 4 then
- print("Usage for send: send <recipientID> <senderDisplayName> <emailContent>")
- return
- end
- sendEmailToServer(arg)
- elseif arg[1] == "read" then
- checkAndReadEmails()
- else
- print("Invalid option. Please use 'send' or 'read'.")
- end
- end
- -- Start the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement