-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaxServer.lua
More file actions
66 lines (55 loc) · 2.07 KB
/
faxServer.lua
File metadata and controls
66 lines (55 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- Server program for minefax
-- requires ender modem
local modem = peripheral.find("modem") or error("No modem attached", 0)
modem.open(40)
local function loadContacts()
local file = fs.open("contacts.txt", "r")
if not file then
error("Error: Failed to open contacts")
end
local content = ""
while true do
local line = file.readLine()
if not line then break end
content = content .. line .. " "
end
file.close()
return content:match("^%s*(.-)%s*$") -- Trim any leading/trailing whitespace
end
local contacts = loadContacts()
term.clear()
term.setCursorPos(1, 1)
print("Relaying fax...")
print("I/O stream:")
print("\n")
while true do
local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
if channel == 40 then
print(message)
local flag = string.sub(message, 1, 3)
local destination = string.sub(message, 4, 6)
if flag == "SND" then --Code for relaying fax messags
if destination == "BRD" then
local sender = string.sub(message, 7, 9)
message = string.sub(message, 10)
local transmitMessage = "REC" .. "BRD" .. sender .. message
print(transmitMessage)
modem.transmit(41, 0, transmitMessage)
elseif destination == "ACK" then
local newDestination = string.sub(message, 7, 9)
local transmitMessage = "ACK" .. newDestination
print(transmitMessage)
modem.transmit(41, 0, transmitMessage)
else
message = string.sub(message, 7)
local transmitMessage = "REC" .. destination .. message
print(transmitMessage)
modem.transmit(41, 0, transmitMessage)
end
elseif flag == "REQ" then --Code for sending contact information
local transmitMessage = "CTS" .. destination .. contacts
print(transmitMessage)
modem.transmit(41, 0, transmitMessage)
end
end
end