Advertisement
DOGGYWOOF

client

Dec 15th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. -- Server Code for Handling Emails
  2. -- Stores and retrieves emails for clients
  3.  
  4. -- Constants
  5. local emailStorageFile = "/dogchat/services/email/emails.db"
  6. local serverIDFile = "/dogchat/services/email/server.ID"
  7.  
  8. -- Function to display a header
  9. local function displayHeader(title)
  10. term.clear()
  11. term.setCursorPos(1, 1)
  12. term.setBackgroundColor(colors.black)
  13. term.setTextColor(colors.white)
  14. term.write(string.rep("=", 50))
  15. term.setCursorPos(2, 2)
  16. term.write(" " .. title)
  17. term.setCursorPos(1, 3)
  18. term.write(string.rep("=", 50))
  19. end
  20.  
  21. -- Function to initialize email storage
  22. local function initializeStorage()
  23. if not fs.exists(emailStorageFile) then
  24. local file = fs.open(emailStorageFile, "w")
  25. file.write(textutils.serialize({}))
  26. file.close()
  27. print("Email storage initialized.")
  28. end
  29. end
  30.  
  31. -- Function to load emails from storage
  32. local function loadEmails()
  33. local file = fs.open(emailStorageFile, "r")
  34. local data = textutils.unserialize(file.readAll())
  35. file.close()
  36. return data
  37. end
  38.  
  39. -- Function to save emails to storage
  40. local function saveEmails(emails)
  41. local file = fs.open(emailStorageFile, "w")
  42. file.write(textutils.serialize(emails))
  43. file.close()
  44. end
  45.  
  46. -- Function to handle incoming messages
  47. local function handleMessage(senderID, message)
  48. local emails = loadEmails()
  49.  
  50. if message.request == "sendEmail" then
  51. -- Save the incoming email
  52. local email = {
  53. senderID = message.senderID,
  54. recipientID = message.recipientID,
  55. emailContent = message.emailContent
  56. }
  57.  
  58. if not emails[message.recipientID] then
  59. emails[message.recipientID] = {}
  60. end
  61.  
  62. table.insert(emails[message.recipientID], email)
  63. saveEmails(emails)
  64.  
  65. print("Email received from " .. message.senderID .. " to " .. message.recipientID)
  66.  
  67. elseif message.request == "getEmails" then
  68. -- Retrieve emails for the requesting client
  69. local clientEmails = emails[message.clientID] or {}
  70. rednet.send(senderID, clientEmails)
  71.  
  72. print("Sent emails to client ID: " .. message.clientID)
  73. else
  74. print("Unknown request received.")
  75. end
  76. end
  77.  
  78. -- Function to setup server ID
  79. local function setupServerID()
  80. if not fs.exists(serverIDFile) then
  81. displayHeader("Server ID Setup")
  82. print("Enter a unique server ID:")
  83. local serverID = tonumber(read())
  84.  
  85. local file = fs.open(serverIDFile, "w")
  86. file.write(tostring(serverID))
  87. file.close()
  88.  
  89. print("Server ID saved.")
  90. else
  91. print("Server ID already exists.")
  92. end
  93. end
  94.  
  95. -- Function to get the server ID
  96. local function getServerID()
  97. local file = fs.open(serverIDFile, "r")
  98. local serverID = tonumber(file.readLine())
  99. file.close()
  100. return serverID
  101. end
  102.  
  103. -- Function to setup and open the modem
  104. local function setupModem()
  105. local modemSide = nil
  106. for _, side in pairs({"left", "right", "top", "bottom", "front", "back"}) do
  107. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  108. modemSide = side
  109. break
  110. end
  111. end
  112.  
  113. if modemSide then
  114. rednet.open(modemSide)
  115. print("Modem found on " .. modemSide .. " and rednet opened.")
  116. return true
  117. else
  118. print("No modem found. Please connect a modem.")
  119. return false
  120. end
  121. end
  122.  
  123. -- Main server loop
  124. local function startServer()
  125. initializeStorage()
  126. setupServerID()
  127.  
  128. local serverID = getServerID()
  129. print("Server ID: " .. serverID)
  130.  
  131. if not setupModem() then
  132. return
  133. end
  134.  
  135. print("Server is running. Listening for messages...")
  136.  
  137. while true do
  138. local senderID, message = rednet.receive()
  139. handleMessage(senderID, message)
  140. end
  141. end
  142.  
  143. -- Start the server
  144. startServer()
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement