Advertisement
Shaka01

Messenger Server / startup

Mar 5th, 2023 (edited)
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.49 KB | None | 0 0
  1. ---messenger server
  2.  
  3. if fs.exists("API") == false then
  4. shell.run("pastebin", "get", "EzkfU5ZM", "API")
  5. end
  6. shaka = require("API")
  7. shaka.connectModem()
  8. shaka.clearScreen()
  9. term.setBackgroundColor(colors.green)
  10. term.setTextColor(colors.black)
  11. term.clearLine()
  12. shaka.centerText("Server up and running.", 1)
  13. shaka.resetColors()
  14. term.setCursorPos(1, 2)
  15.  
  16. local function saveNotifications(name)
  17.     local notifications = shaka.readFile(".notifications", false)
  18.     if notifications == false then
  19.         notifications = {}
  20.     end
  21.     if notifications[name] then
  22.         notifications[name] = notifications[name] + 1
  23.     else
  24.     notifications[name] = 1
  25.     end
  26.     shaka.writeFile(".notifications", notifications)
  27. end
  28.  
  29. local function deleteNotifications(name)
  30.     local notifications = shaka.readFile(".notifications")
  31.     if notifications ~= false then
  32.         notifications[name] = nil
  33.         shaka.writeFile(".notifications", notifications)
  34.     end
  35. end
  36.  
  37.  
  38. function saveUsers(userName, senderID)
  39.   -- Check if the ".users" file exists
  40.   if fs.exists(".users") then
  41.     -- If it does, load the existing table of users
  42.     local file = fs.open(".users", "r")
  43.     local users = textutils.unserialize(file.readAll())
  44.     file.close()
  45.    
  46.     if users == nil then
  47.         users = {}
  48.     end
  49.    
  50.     -- Check if the userName already exists in the table with a different ID
  51.     for _, user in ipairs(users) do
  52.       if user.name == userName and user.id ~= senderID then
  53.         -- If it does, return false
  54.         return false
  55.       end
  56.     end
  57.  
  58.     -- Check if the senderID is already in the table
  59.     local found = false
  60.     for i, user in ipairs(users) do
  61.       if user.id == senderID then
  62.         users[i] = {name=userName, id=senderID} -- Replace the whole entry
  63.         found = true
  64.         break
  65.       end
  66.     end
  67.  
  68.     -- If the senderID is not already in the table, insert the new user into the table
  69.     if not found then
  70.       table.insert(users, {name=userName, id=senderID})
  71.     end
  72.  
  73.     -- Save the updated table to the file
  74.     file = fs.open(".users", "w")
  75.     file.write("{\n")
  76.     for i, user in ipairs(users) do
  77.       file.writeLine(textutils.serialize(user) .. (i == #users and "" or ","))
  78.     end
  79.     file.write("}\n")
  80.     file.close()
  81.     print("Saved " ..userName.. ": " ..senderID)
  82.     -- Return true to indicate success
  83.     return true
  84.   else
  85.     -- If the ".users" file does not exist, create a new table with the new user and save it to the file
  86.     local users = {{name=userName, id=senderID}}
  87.     local file = fs.open(".users", "w")
  88.     file.write("{\n")
  89.     for i, user in ipairs(users) do
  90.       file.writeLine(textutils.serialize(user) .. (i == #users and "" or ","))
  91.     end
  92.     file.write("}\n")
  93.     file.close()
  94.  
  95.     -- Return true to indicate success
  96.     print("Saved " ..userName.. ": " ..senderID)
  97.     return true
  98.   end
  99. end
  100.  
  101.  
  102. function supplyUsers()
  103.     if fs.exists(".users") then
  104.         local file = fs.open(".users", "r")
  105.         local users = file.readAll()
  106.         rednet.broadcast(users, "userRequestAnswer")
  107.         file.close()
  108.     else
  109.         rednet.broadcast("noUsers", "userRequestAnswer")
  110.     end
  111. end
  112.  
  113. function filterTable(name, id)
  114.   local result = {}
  115.   if fs.exists(".messages") then
  116.     local file = fs.open(".messages", "r")
  117.     local contents = file.readAll()
  118.     file.close()
  119.     result = textutils.unserialize(contents)
  120.   end
  121.  
  122.   local filtered = {}
  123.   local savedID = findSenderIdByName(name)
  124.   for _, message in ipairs(result) do
  125.     if message.sender == name or message.receiver == name or id == savedID then
  126.       table.insert(filtered, message)
  127.     end
  128.   end
  129.   return filtered
  130. end
  131.  
  132. function findSenderIdByName(name)
  133.     if fs.exists(".users") then
  134.         local file = fs.open(".users", "r")
  135.         existingNames = file.readAll()
  136.         existingNames = textutils.unserialize(existingNames)
  137.         file.close()
  138.     end
  139.   for _, user in ipairs(existingNames) do
  140.     if user.name == name then
  141.       return user.id
  142.     end
  143.   end
  144.   return nil
  145. end
  146.  
  147.  
  148. function sendFilteredTable(name, id)
  149.     local tableToSend = filterTable(name, id)
  150.     tableToSend = textutils.serialize(tableToSend)
  151.     if id ~= nil then
  152.         print("Sending messageList to ID " ..id.. ": " ..name)
  153.         rednet.send(id, tableToSend, "allMessages")
  154.     end
  155. end
  156.  
  157. function deleteNotifications(name)
  158.     local notis = shaka.readFile(".notifications")
  159.     if notis[name] then
  160.         notis[name] = nil
  161.     end
  162.     shaka.writeFile(".notifications", notis)
  163. end
  164.  
  165. local function remindNotifications(name)
  166.     local list = shaka.readFile(".notifications")
  167.     if type(list) == "table" then
  168.     for k, v in pairs(list) do
  169.         if k == name then
  170.             local id = findSenderIdByName(k)
  171.             rednet.send(id, v, "notificationReminder")
  172.             print("Sent notification reminder to " ..name.. ": " ..v)
  173.             return
  174.         end
  175.     end
  176.     end
  177.     local id = findSenderIdByName(name)
  178.     if id ~= nil then
  179.         rednet.send(id, "0", "notificationReminder")
  180.     end
  181. end
  182.  
  183. function receiveMessages()
  184.    
  185.   -- Initialize the messages table
  186.   local messages = {}
  187.  
  188.   -- Check if the ".messages" file exists and load its contents into the messages table
  189.   if fs.exists(".messages") then
  190.     local file = fs.open(".messages", "r")
  191.     local contents = file.readAll()
  192.     file.close()
  193.     messages = textutils.unserialize(contents)
  194.   end
  195.  
  196.  
  197.   -- Split the message into sender, text, and receiver
  198.   local result = shaka.split(message, "+|")
  199.   local sender = result[1]
  200.   local text = result[2]
  201.   local receiver = result[3]
  202.  
  203.  
  204.             local function checkExistingUser(userName)
  205.             -- Check if the ".users" file exists and read it
  206.                 if fs.exists(".users") then
  207.                 -- If it does, load the existing table of users
  208.                 local file = fs.open(".users", "r")
  209.                 local users = textutils.unserialize(file.readAll())
  210.                 file.close()
  211.  
  212.                 -- Check if the userName already exists in the table
  213.                 for _, user in ipairs(users) do
  214.                   if user.name == userName then
  215.                     -- If it does, return false
  216.                     return true
  217.                   end
  218.                 end
  219.             end
  220.             return false
  221.             end
  222.     if checkExistingUser(sender) == false then
  223.         return false
  224.     end
  225.  
  226. saveNotifications(receiver)
  227.  
  228.   local now = os.date("*t")
  229.   local date = string.format("%02d.%02d.%04d", now.day, now.month, now.year)
  230.   local time = string.format("%02d:%02d", now.hour, now.min)
  231.  
  232.   table.insert(messages, {sender=sender, text=text, receiver=receiver, date=date, time=time})
  233.   -- print("\nMessage received from " .. sender .. " to " .. receiver .. ":")
  234.   -- print(text)
  235.  
  236.  
  237.    
  238.   -- Save the updated messages table to the ".messages" file
  239.   local file = fs.open(".messages", "w")
  240.   plzWrite = textutils.serialize(messages)
  241.   file.write(plzWrite)
  242.   file.close()
  243.   return true
  244. end
  245.  
  246.  
  247. -- Enter the main program loop
  248. local function main()
  249.         sender, message, protocol = rednet.receive()
  250.         -------------------------------------------
  251.         if protocol == "newMessage" then
  252.             if receiveMessages() then
  253.                 rednet.broadcast("worked", "messageConfirm")
  254.             else
  255.                 rednet.broadcast("failed", "messageConfirm")
  256.             end
  257.         elseif protocol == "newUser" then
  258.             if saveUsers(message, sender) then
  259.                 rednet.broadcast("worked", "userConfirm")
  260.             else
  261.                 rednet.broadcast("fail", "userConfirm")
  262.             end
  263.         elseif protocol == "requestUserList" then
  264.             supplyUsers()
  265.         elseif protocol == "messageUpdate" then
  266.             sendFilteredTable(message, sender)
  267.         elseif protocol == "serverID" then
  268.             rednet.broadcast("yeah yeah", "idSupply")
  269.         elseif protocol == "delNotifications" then
  270.             deleteNotifications(message)
  271.         elseif protocol == "anyMessages?" then
  272.             remindNotifications(message)
  273.         end
  274. end
  275.  
  276.  
  277. while true do
  278.  
  279. main()
  280.  
  281. end
  282.  
  283.  
  284.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement