View difference between Paste ID: hwMduJJ0 and 7mSFPyGc
SHOW: | | - or go back to the newest paste.
1
-- https://stackoverflow.com/a/7615129/13347795
2-
    
2+
function split(input, maxSplits, sep)
3
	if sep == nil then
4
		sep = ";"
5
	end
6
	splits = {}
7
    splitCount = 0
8-
    h = fs.open("messages.txt", "r")
8+
	for str in string.gmatch(input, "([^"..sep.."]+)") do
9-
    repeat
9+
		if maxSplits ~= nil and splitCount > maxSplits then
10-
        line = h.readLine()
10+
			splits[splitCount] = splits[splitCount]..sep..str
11-
        if line ~= nil then
11+
		else
12-
            print(line)
12+
			table.insert(splits, str)
13-
        end
13+
            splitCount = splitCount + 1
14-
    until line == nil
14+
		end
15
	end
16-
    
16+
	return splits
17-
    
17+
18-
    id, message = rednet.receive()
18+
19-
    h = fs.open("messages.txt", "a")
19+
20-
    h.writeLine(os.date()..os.time()..";"..message)
20+
	h = fs.open("messages.txt", "r")
21
	term.clear()
22
	term.setCursorPos(1, 1)
23
	repeat
24
		line = h.readLine()
25
		if line ~= nil and line ~= "" then
26
            date, time, id, message = table.unpack(split(line, 3))
27
			print(message)
28
		end
29
	until line == nil
30
	h.close()
31
end
32
33
write("Starting...")
34
if not fs.exists("messages.txt") then
35
    local h = fs.open("messages.txt", "w")
36
    h.close()
37
end
38
39
peripheral.find("modem", rednet.open)
40
41
local h = fs.open("messages.txt", "r")
42
rednet.broadcast(h.readAll(), "response-messenger-backlog")
43
h.close()
44
refresh()
45
46
while true do
47
	id, message, protocol = rednet.receive()
48
    if protocol == "request-messenger-backlog" then
49
        local h = fs.open("messages.txt", "r")
50
       	rednet.send(id, h.readAll(), "response-messenger-backlog")
51
        h.close()
52
	elseif protocol == "broadcast-messenger-message" then
53
		local h = fs.open("messages.txt", "a")
54
		h.writeLine(os.date("%F;%T;")..id..";"..message)
55
		h.close()
56
	end
57
	refresh()
58
end
59