Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---messenger server
- if fs.exists("API") == false then
- shell.run("pastebin", "get", "EzkfU5ZM", "API")
- end
- shaka = require("API")
- shaka.connectModem()
- shaka.clearScreen()
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.black)
- term.clearLine()
- shaka.centerText("Server up and running.", 1)
- shaka.resetColors()
- term.setCursorPos(1, 2)
- local function saveNotifications(name)
- local notifications = shaka.readFile(".notifications", false)
- if notifications == false then
- notifications = {}
- end
- if notifications[name] then
- notifications[name] = notifications[name] + 1
- else
- notifications[name] = 1
- end
- shaka.writeFile(".notifications", notifications)
- end
- local function deleteNotifications(name)
- local notifications = shaka.readFile(".notifications")
- if notifications ~= false then
- notifications[name] = nil
- shaka.writeFile(".notifications", notifications)
- end
- end
- function saveUsers(userName, senderID)
- -- Check if the ".users" file exists
- if fs.exists(".users") then
- -- If it does, load the existing table of users
- local file = fs.open(".users", "r")
- local users = textutils.unserialize(file.readAll())
- file.close()
- if users == nil then
- users = {}
- end
- -- Check if the userName already exists in the table with a different ID
- for _, user in ipairs(users) do
- if user.name == userName and user.id ~= senderID then
- -- If it does, return false
- return false
- end
- end
- -- Check if the senderID is already in the table
- local found = false
- for i, user in ipairs(users) do
- if user.id == senderID then
- users[i] = {name=userName, id=senderID} -- Replace the whole entry
- found = true
- break
- end
- end
- -- If the senderID is not already in the table, insert the new user into the table
- if not found then
- table.insert(users, {name=userName, id=senderID})
- end
- -- Save the updated table to the file
- file = fs.open(".users", "w")
- file.write("{\n")
- for i, user in ipairs(users) do
- file.writeLine(textutils.serialize(user) .. (i == #users and "" or ","))
- end
- file.write("}\n")
- file.close()
- print("Saved " ..userName.. ": " ..senderID)
- -- Return true to indicate success
- return true
- else
- -- If the ".users" file does not exist, create a new table with the new user and save it to the file
- local users = {{name=userName, id=senderID}}
- local file = fs.open(".users", "w")
- file.write("{\n")
- for i, user in ipairs(users) do
- file.writeLine(textutils.serialize(user) .. (i == #users and "" or ","))
- end
- file.write("}\n")
- file.close()
- -- Return true to indicate success
- print("Saved " ..userName.. ": " ..senderID)
- return true
- end
- end
- function supplyUsers()
- if fs.exists(".users") then
- local file = fs.open(".users", "r")
- local users = file.readAll()
- rednet.broadcast(users, "userRequestAnswer")
- file.close()
- else
- rednet.broadcast("noUsers", "userRequestAnswer")
- end
- end
- function filterTable(name, id)
- local result = {}
- if fs.exists(".messages") then
- local file = fs.open(".messages", "r")
- local contents = file.readAll()
- file.close()
- result = textutils.unserialize(contents)
- end
- local filtered = {}
- local savedID = findSenderIdByName(name)
- for _, message in ipairs(result) do
- if message.sender == name or message.receiver == name or id == savedID then
- table.insert(filtered, message)
- end
- end
- return filtered
- end
- function findSenderIdByName(name)
- if fs.exists(".users") then
- local file = fs.open(".users", "r")
- existingNames = file.readAll()
- existingNames = textutils.unserialize(existingNames)
- file.close()
- end
- for _, user in ipairs(existingNames) do
- if user.name == name then
- return user.id
- end
- end
- return nil
- end
- function sendFilteredTable(name, id)
- local tableToSend = filterTable(name, id)
- tableToSend = textutils.serialize(tableToSend)
- if id ~= nil then
- print("Sending messageList to ID " ..id.. ": " ..name)
- rednet.send(id, tableToSend, "allMessages")
- end
- end
- function deleteNotifications(name)
- local notis = shaka.readFile(".notifications")
- if notis[name] then
- notis[name] = nil
- end
- shaka.writeFile(".notifications", notis)
- end
- local function remindNotifications(name)
- local list = shaka.readFile(".notifications")
- if type(list) == "table" then
- for k, v in pairs(list) do
- if k == name then
- local id = findSenderIdByName(k)
- rednet.send(id, v, "notificationReminder")
- print("Sent notification reminder to " ..name.. ": " ..v)
- return
- end
- end
- end
- local id = findSenderIdByName(name)
- if id ~= nil then
- rednet.send(id, "0", "notificationReminder")
- end
- end
- function receiveMessages()
- -- Initialize the messages table
- local messages = {}
- -- Check if the ".messages" file exists and load its contents into the messages table
- if fs.exists(".messages") then
- local file = fs.open(".messages", "r")
- local contents = file.readAll()
- file.close()
- messages = textutils.unserialize(contents)
- end
- -- Split the message into sender, text, and receiver
- local result = shaka.split(message, "+|")
- local sender = result[1]
- local text = result[2]
- local receiver = result[3]
- local function checkExistingUser(userName)
- -- Check if the ".users" file exists and read it
- if fs.exists(".users") then
- -- If it does, load the existing table of users
- local file = fs.open(".users", "r")
- local users = textutils.unserialize(file.readAll())
- file.close()
- -- Check if the userName already exists in the table
- for _, user in ipairs(users) do
- if user.name == userName then
- -- If it does, return false
- return true
- end
- end
- end
- return false
- end
- if checkExistingUser(sender) == false then
- return false
- end
- saveNotifications(receiver)
- local now = os.date("*t")
- local date = string.format("%02d.%02d.%04d", now.day, now.month, now.year)
- local time = string.format("%02d:%02d", now.hour, now.min)
- table.insert(messages, {sender=sender, text=text, receiver=receiver, date=date, time=time})
- -- print("\nMessage received from " .. sender .. " to " .. receiver .. ":")
- -- print(text)
- -- Save the updated messages table to the ".messages" file
- local file = fs.open(".messages", "w")
- plzWrite = textutils.serialize(messages)
- file.write(plzWrite)
- file.close()
- return true
- end
- -- Enter the main program loop
- local function main()
- sender, message, protocol = rednet.receive()
- -------------------------------------------
- if protocol == "newMessage" then
- if receiveMessages() then
- rednet.broadcast("worked", "messageConfirm")
- else
- rednet.broadcast("failed", "messageConfirm")
- end
- elseif protocol == "newUser" then
- if saveUsers(message, sender) then
- rednet.broadcast("worked", "userConfirm")
- else
- rednet.broadcast("fail", "userConfirm")
- end
- elseif protocol == "requestUserList" then
- supplyUsers()
- elseif protocol == "messageUpdate" then
- sendFilteredTable(message, sender)
- elseif protocol == "serverID" then
- rednet.broadcast("yeah yeah", "idSupply")
- elseif protocol == "delNotifications" then
- deleteNotifications(message)
- elseif protocol == "anyMessages?" then
- remindNotifications(message)
- end
- end
- while true do
- main()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement