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