Advertisement
DOGGYWOOF

Doggy Netowork Email Service

Dec 15th, 2024 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. -- Client-side program with black and white UI and email scrolling functionality
  2. -- Doggy Mail Branding
  3.  
  4. -- Function to display a header with a simple divider
  5. local function displayHeader(title)
  6. term.clear()
  7. term.setCursorPos(1, 1)
  8. term.setBackgroundColor(colors.black)
  9. term.setTextColor(colors.white)
  10. term.write(string.rep("=", 50)) -- Top divider line
  11. term.setCursorPos(2, 2)
  12. term.write(" " .. title)
  13. term.setCursorPos(50, 2)
  14. term.write("v1.0")
  15. term.setCursorPos(1, 3)
  16. term.write(string.rep("=", 50)) -- Bottom divider line
  17. term.setCursorPos(1, 5)
  18. end
  19.  
  20. -- Function to setup and open the modem
  21. local function setupModem()
  22. -- Try to find a modem connected to the system
  23. local modemSide = nil
  24. for _, side in pairs({"left", "right", "top", "bottom", "front", "back"}) do
  25. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  26. modemSide = side
  27. break
  28. end
  29. end
  30.  
  31. -- If a modem is found, open it with rednet
  32. if modemSide then
  33. rednet.open(modemSide)
  34. print("Modem found on " .. modemSide .. " and rednet opened.")
  35. else
  36. print("No modem found.")
  37. return false
  38. end
  39. return true
  40. end
  41.  
  42. -- Function to ask for the server ID and save it to a file
  43. local function setupServerID()
  44. local serverIDFile = "/dogchat/services/email/server.ID"
  45. if not fs.exists(serverIDFile) then
  46. -- If it doesn't exist, prompt the user for the server ID
  47. displayHeader("Setup Doggy Mail Server ID")
  48. print("Server ID not found. Please enter the server ID:")
  49. local serverID = read()
  50.  
  51. -- Save the server ID to the file
  52. local file = fs.open(serverIDFile, "w")
  53. file.write(serverID)
  54. file.close()
  55.  
  56. print("\nServer ID saved.")
  57. else
  58. print("Server ID already exists.")
  59. end
  60. end
  61.  
  62. -- Function to get the Client ID from the computer ID
  63. local function getClientID()
  64. return tostring(os.getComputerID()) -- Use the computer's ID as the client ID
  65. end
  66.  
  67. -- Function to send email to the server
  68. local function sendEmailToServer()
  69. -- Check for server ID before sending email
  70. setupServerID()
  71.  
  72. -- Read the server ID from the saved file
  73. local serverIDFile = "/dogchat/services/email/server.ID"
  74. local serverID = fs.open(serverIDFile, "r").readLine()
  75.  
  76. -- Get recipient ID and sender ID
  77. displayHeader("Send Email via Doggy Mail")
  78. print("Enter recipient's ID:")
  79. local recipientID = read()
  80. print("Enter your email content:")
  81. local emailContent = read()
  82.  
  83. -- Prepare the email data to send to the server
  84. local emailData = {
  85. senderID = getClientID(),
  86. recipientID = recipientID,
  87. emailContent = emailContent
  88. }
  89.  
  90. -- Debugging message before sending
  91. print("\nPreparing to send email to server ID: " .. serverID)
  92. print("Sender: " .. emailData.senderID)
  93. print("Recipient: " .. emailData.recipientID)
  94. print("Content: \n" .. emailData.emailContent)
  95.  
  96. -- Send the email to the server
  97. print("\nSending email to server (ID: " .. serverID .. ") via Doggy Mail...")
  98. rednet.send(tonumber(serverID), emailData)
  99.  
  100. -- Debug message after sending
  101. print("\nEmail sent via Doggy Mail.")
  102. end
  103.  
  104. -- Function to check and read emails from the server
  105. local function checkAndReadEmails()
  106. -- Check for server ID before checking emails
  107. setupServerID()
  108.  
  109. -- Read the server ID from the saved file
  110. local serverIDFile = "/dogchat/services/email/server.ID"
  111. local serverID = fs.open(serverIDFile, "r").readLine()
  112.  
  113. -- Get the client ID automatically from the computer's ID
  114. local clientID = getClientID()
  115.  
  116. displayHeader("Check for New Emails - Doggy Mail")
  117. print("Checking for new emails for client ID: " .. clientID)
  118.  
  119. -- Request emails from the server for this client
  120. rednet.send(tonumber(serverID), {request = "getEmails", clientID = clientID})
  121.  
  122. -- Wait for the server to send emails
  123. local senderID, emailData = rednet.receive()
  124.  
  125. -- Check if the server sent any emails
  126. if emailData then
  127. print("\n--- New Emails ---")
  128. local page = 1
  129. local emailsPerPage = 3
  130. local totalEmails = #emailData
  131. local totalPages = math.ceil(totalEmails / emailsPerPage)
  132.  
  133. while true do
  134. term.clear()
  135. displayHeader("Emails - Page " .. page .. "/" .. totalPages .. " - Doggy Mail")
  136.  
  137. -- Calculate the start and end indices for the emails to display
  138. local startIndex = (page - 1) * emailsPerPage + 1
  139. local endIndex = math.min(page * emailsPerPage, totalEmails)
  140.  
  141. -- Display the emails for this page
  142. for i = startIndex, endIndex do
  143. local email = emailData[i]
  144. if email.recipientID == clientID then
  145. print("\nFrom: " .. email.senderID)
  146. print("To: " .. email.recipientID)
  147. print("Content: \n" .. email.emailContent)
  148. print("-------------------")
  149. end
  150. end
  151.  
  152. -- Prompt user for navigation
  153. print("\nPage " .. page .. "/" .. totalPages)
  154. print("Press 'n' for next page, 'p' for previous page, or 'q' to exit.")
  155. local choice = read()
  156.  
  157. if choice == "n" and page < totalPages then
  158. page = page + 1
  159. elseif choice == "p" and page > 1 then
  160. page = page - 1
  161. elseif choice == "q" then
  162. break
  163. end
  164. end
  165. else
  166. print("\nNo new emails.")
  167. end
  168. end
  169.  
  170. -- Main logic
  171. local function mainMenu()
  172. displayHeader("Doggy Mail Client")
  173. print("Welcome to Doggy Mail!")
  174. print("1. Send Email")
  175. print("2. Read Emails")
  176. print("3. Exit")
  177.  
  178. local choice = tonumber(read())
  179.  
  180. if choice == 1 then
  181. sendEmailToServer()
  182. elseif choice == 2 then
  183. checkAndReadEmails()
  184. elseif choice == 3 then
  185. print("\nExiting Doggy Mail...")
  186. return
  187. else
  188. print("Invalid choice, please try again.")
  189. mainMenu()
  190. end
  191. end
  192.  
  193. -- Ensure modem setup is correct before starting
  194. if not setupModem() then
  195. print("Modem setup failed. Please connect a modem and try again.")
  196. return
  197. end
  198.  
  199. -- Start the main menu
  200. mainMenu()
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement