Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Server Code for Handling Emails
- -- Stores and retrieves emails for clients
- -- Constants
- local emailStorageFile = "/dogchat/services/email/emails.db"
- local serverIDFile = "/dogchat/services/email/server.ID"
- -- Function to display a header
- local function displayHeader(title)
- term.clear()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write(string.rep("=", 50))
- term.setCursorPos(2, 2)
- term.write(" " .. title)
- term.setCursorPos(1, 3)
- term.write(string.rep("=", 50))
- end
- -- Function to initialize email storage
- local function initializeStorage()
- if not fs.exists(emailStorageFile) then
- local file = fs.open(emailStorageFile, "w")
- file.write(textutils.serialize({}))
- file.close()
- print("Email storage initialized.")
- end
- end
- -- Function to load emails from storage
- local function loadEmails()
- local file = fs.open(emailStorageFile, "r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- return data
- end
- -- Function to save emails to storage
- local function saveEmails(emails)
- local file = fs.open(emailStorageFile, "w")
- file.write(textutils.serialize(emails))
- file.close()
- end
- -- Function to handle incoming messages
- local function handleMessage(senderID, message)
- local emails = loadEmails()
- if message.request == "sendEmail" then
- -- Save the incoming email
- local email = {
- senderID = message.senderID,
- recipientID = message.recipientID,
- emailContent = message.emailContent
- }
- if not emails[message.recipientID] then
- emails[message.recipientID] = {}
- end
- table.insert(emails[message.recipientID], email)
- saveEmails(emails)
- print("Email received from " .. message.senderID .. " to " .. message.recipientID)
- elseif message.request == "getEmails" then
- -- Retrieve emails for the requesting client
- local clientEmails = emails[message.clientID] or {}
- rednet.send(senderID, clientEmails)
- print("Sent emails to client ID: " .. message.clientID)
- else
- print("Unknown request received.")
- end
- end
- -- Function to setup server ID
- local function setupServerID()
- if not fs.exists(serverIDFile) then
- displayHeader("Server ID Setup")
- print("Enter a unique server ID:")
- local serverID = tonumber(read())
- local file = fs.open(serverIDFile, "w")
- file.write(tostring(serverID))
- file.close()
- print("Server ID saved.")
- else
- print("Server ID already exists.")
- end
- end
- -- Function to get the server ID
- local function getServerID()
- local file = fs.open(serverIDFile, "r")
- local serverID = tonumber(file.readLine())
- file.close()
- return serverID
- end
- -- Function to setup and open the modem
- local function setupModem()
- 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 modemSide then
- rednet.open(modemSide)
- print("Modem found on " .. modemSide .. " and rednet opened.")
- return true
- else
- print("No modem found. Please connect a modem.")
- return false
- end
- end
- -- Main server loop
- local function startServer()
- initializeStorage()
- setupServerID()
- local serverID = getServerID()
- print("Server ID: " .. serverID)
- if not setupModem() then
- return
- end
- print("Server is running. Listening for messages...")
- while true do
- local senderID, message = rednet.receive()
- handleMessage(senderID, message)
- end
- end
- -- Start the server
- startServer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement