Advertisement
DOGGYWOOF

Email client

Dec 15th, 2024 (edited)
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. -- Function to display a header with a simple divider
  2. local function displayHeader(title)
  3. term.clear()
  4. term.setCursorPos(1, 1)
  5. term.setBackgroundColor(colors.black)
  6. term.setTextColor(colors.white)
  7. term.write(string.rep("=", 50)) -- Top divider line
  8. term.setCursorPos(2, 2)
  9. term.write(" " .. title)
  10. term.setCursorPos(50, 2)
  11. term.write("v1.0")
  12. term.setCursorPos(1, 3)
  13. term.write(string.rep("=", 50)) -- Bottom divider line
  14. term.setCursorPos(1, 5)
  15. end
  16.  
  17. -- Function to setup and open the modem
  18. local function setupModem()
  19. local modemSide = nil
  20. for _, side in pairs({"left", "right", "top", "bottom", "front", "back"}) do
  21. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  22. modemSide = side
  23. break
  24. end
  25. end
  26.  
  27. if modemSide then
  28. rednet.open(modemSide)
  29. print("Modem found on " .. modemSide .. " and rednet opened.")
  30. else
  31. print("No modem found.")
  32. return false
  33. end
  34. return true
  35. end
  36.  
  37. -- Function to load the server ID from the file
  38. local function loadServerID()
  39. local filePath = "/dogchat/services/Dmail/ServerID.txt"
  40. local file = fs.open(filePath, "r")
  41. if file then
  42. local serverID = file.readLine()
  43. file.close()
  44. return serverID
  45. else
  46. return nil
  47. end
  48. end
  49.  
  50. -- Function to ask the user for the server ID if not found
  51. local function askForServerID()
  52. print("Server ID not found.")
  53. print("Please enter the server ID:")
  54. local serverID = read()
  55.  
  56. -- Save the server ID for future use
  57. local filePath = "/dogchat/services/Dmail/ServerID.txt"
  58. local file = fs.open(filePath, "w")
  59. if file then
  60. file.writeLine(serverID)
  61. file.close()
  62. else
  63. print("Error saving the ServerID.")
  64. end
  65.  
  66. return serverID
  67. end
  68.  
  69. -- Function to get the server ID (load or ask for it)
  70. local function getServerID()
  71. local serverID = loadServerID()
  72. if not serverID then
  73. serverID = askForServerID()
  74. end
  75. return serverID
  76. end
  77.  
  78. -- Function to get the Client ID from the computer ID
  79. local function getClientID()
  80. return tostring(os.getComputerID()) -- Use the computer's ID as the client ID
  81. end
  82.  
  83. -- Function to load account credentials from file
  84. local function loadCredentials()
  85. local credsFilePath = "/dogchat/accounts/credentials.txt"
  86. if fs.exists(credsFilePath) then
  87. local file = fs.open(credsFilePath, "r")
  88. local username = file.readLine()
  89. local password = file.readLine()
  90. file.close()
  91. return {username = username, password = password}
  92. else
  93. return nil
  94. end
  95. end
  96.  
  97. -- Function to create a new account
  98. local function createAccount()
  99. print("No account found. Please create a new account.")
  100. print("Enter your username:")
  101. local username = read()
  102. print("Enter your password:")
  103. local password = read("*")
  104.  
  105. -- Save credentials to file
  106. local credsFilePath = "/dogchat/accounts/credentials.txt"
  107. local file = fs.open(credsFilePath, "w")
  108. if file then
  109. file.writeLine(username)
  110. file.writeLine(password)
  111. file.close()
  112. else
  113. print("Error saving credentials.")
  114. end
  115.  
  116. return {username = username, password = password}
  117. end
  118.  
  119. -- Function to login using credentials
  120. local function login()
  121. local creds = loadCredentials()
  122. if not creds then
  123. return createAccount() -- If no credentials exist, create a new account
  124. end
  125.  
  126. print("Enter your username:")
  127. local inputUsername = read()
  128. print("Enter your password:")
  129. local inputPassword = read("*")
  130.  
  131. if inputUsername == creds.username and inputPassword == creds.password then
  132. print("Login successful!")
  133. return creds
  134. else
  135. print("Incorrect credentials. Please try again.")
  136. return login() -- Retry if credentials are incorrect
  137. end
  138. end
  139.  
  140. -- Function to send an email to the server
  141. local function sendEmailToServer(user)
  142. -- Get the server ID (or prompt if it's not available)
  143. local serverID = getServerID()
  144.  
  145. -- Get recipient email and content
  146. displayHeader("Send Email")
  147. print("Enter recipient's email address:")
  148. local recipientEmail = read()
  149. print("Enter your email content:")
  150. local emailContent = read()
  151.  
  152. -- Prepare the email data to send to the server
  153. local emailData = {
  154. senderID = user.username,
  155. recipientEmail = recipientEmail,
  156. emailContent = emailContent
  157. }
  158.  
  159. -- Debugging message before sending
  160. print("\nPreparing to send email to server ID: " .. serverID)
  161. print("Sender: " .. emailData.senderID)
  162. print("Recipient: " .. emailData.recipientEmail)
  163. print("Content: \n" .. emailData.emailContent)
  164.  
  165. -- Send the email to the server
  166. print("\nSending email to server (ID: " .. serverID .. ")...")
  167. rednet.send(tonumber(serverID), emailData)
  168. print("\nEmail sent to server.")
  169. end
  170.  
  171. -- Function to check and read emails from the server
  172. local function checkAndReadEmails(user)
  173. -- Get the server ID (or prompt if it's not available)
  174. local serverID = getServerID()
  175.  
  176. -- Get the client ID automatically from the computer's ID
  177. local clientID = getClientID()
  178.  
  179. displayHeader("Check for New Emails")
  180. print("Checking for new emails for client ID: " .. clientID)
  181.  
  182. -- Request emails from the server for this client
  183. rednet.send(tonumber(serverID), {request = "getEmails", clientID = clientID})
  184.  
  185. -- Wait for the server to send emails
  186. local senderID, emailData = rednet.receive()
  187.  
  188. -- Check if the server sent any emails
  189. if emailData then
  190. print("\n--- New Emails ---")
  191. local page = 1
  192. local emailsPerPage = 3
  193. local totalEmails = #emailData
  194. local totalPages = math.ceil(totalEmails / emailsPerPage)
  195.  
  196. while true do
  197. term.clear()
  198. displayHeader("Emails - Page " .. page .. "/" .. totalPages)
  199.  
  200. -- Calculate the start and end indices for the emails to display
  201. local startIndex = (page - 1) * emailsPerPage + 1
  202. local endIndex = math.min(page * emailsPerPage, totalEmails)
  203.  
  204. -- Display the emails for this page
  205. for i = startIndex, endIndex do
  206. local email = emailData[i]
  207. if email.recipientEmail == user.username then
  208. print("\nFrom: " .. email.senderID)
  209. print("To: " .. email.recipientEmail)
  210. print("Content: \n" .. email.emailContent)
  211. print("-------------------")
  212. end
  213. end
  214.  
  215. -- Prompt user for navigation
  216. print("\nPage " .. page .. "/" .. totalPages)
  217. print("Press 'n' for next page, 'p' for previous page, or 'q' to exit.")
  218. local choice = read()
  219.  
  220. if choice == "n" and page < totalPages then
  221. page = page + 1
  222. elseif choice == "p" and page > 1 then
  223. page = page - 1
  224. elseif choice == "q" then
  225. break
  226. end
  227. end
  228. else
  229. print("\nNo new emails.")
  230. end
  231. end
  232.  
  233. -- Main logic
  234. local function mainMenu(user)
  235. displayHeader("Doggy Mail Client")
  236. print("Welcome to Doggy Mail!")
  237. print("1. Send Email")
  238. print("2. Check Emails")
  239. print("3. Exit")
  240.  
  241. local choice = tonumber(read())
  242.  
  243. if choice == 1 then
  244. sendEmailToServer(user)
  245. elseif choice == 2 then
  246. checkAndReadEmails(user)
  247. elseif choice == 3 then
  248. print("\nExiting...")
  249. return
  250. else
  251. print("Invalid choice, please try again.")
  252. mainMenu(user)
  253. end
  254. end
  255.  
  256. -- Ensure modem setup is correct before starting
  257. if not setupModem() then
  258. print("Modem setup failed. Please connect a modem and try again.")
  259. return
  260. end
  261.  
  262. -- Login or Create Account
  263. local user = login()
  264.  
  265. -- Start the main menu
  266. mainMenu(user)
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement