Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Doggy Mail Server Code
- -- Function to create or load account information
- local function loadAccountData()
- local accountFile = "/dogchat/accounts.dat"
- if not fs.exists(accountFile) then
- return {}
- end
- local file = fs.open(accountFile, "r")
- local accountData = textutils.unserialize(file.readAll())
- file.close()
- return accountData
- end
- -- Function to save account information
- local function saveAccountData(accountData)
- local accountFile = "/dogchat/accounts.dat"
- local file = fs.open(accountFile, "w")
- file.write(textutils.serialize(accountData))
- file.close()
- end
- -- Function to handle incoming emails from clients
- local function handleIncomingEmail(sender, emailData)
- local accountData = loadAccountData()
- -- Ensure the recipient account exists
- local recipient = emailData.recipient
- if not accountData[recipient] then
- print("Recipient account does not exist.")
- return
- end
- -- Add the email to the recipient's email list
- local userEmails = accountData[recipient].emails
- table.insert(userEmails, emailData)
- accountData[recipient].emails = userEmails
- -- Save the updated account data
- saveAccountData(accountData)
- print("Email received from " .. sender .. " to " .. recipient)
- end
- -- Function to start the server
- local function startServer()
- -- Setup modem
- 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
- -- Check if a modem is found
- if modemSide then
- rednet.open(modemSide)
- print("Server is running. Waiting for messages...")
- else
- print("No modem found. Server cannot start.")
- return
- end
- -- Main loop to listen for incoming messages
- while true do
- local senderID, message = rednet.receive()
- -- Check if the message is an email (table format)
- if type(message) == "table" and message.sender and message.recipient and message.content then
- handleIncomingEmail(senderID, message)
- end
- end
- end
- -- Run the server
- startServer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement