Advertisement
DOGGYWOOF

Doggy Network Email Service command line for apps

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