-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfax.lua
More file actions
215 lines (182 loc) · 6.86 KB
/
fax.lua
File metadata and controls
215 lines (182 loc) · 6.86 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
-- Minecraft cc tweaked lua fax client
-- Requires server connection
-- Requires basic wireless modem and printer
local modem = peripheral.find("modem") or error("Error: No modem attached", 0)
local printer = peripheral.find("printer") or error("Error: No printer attached", 0)
modem.open(41)
local pageWidth = 25
local pageHeight = 40
local computerId = os.getComputerID()
-- Parse contacts from received message
local function parseToTable(input)
local result = {}
local words = {}
for word in input:gmatch("%S+") do
table.insert(words, word)
end
for i = 1, #words, 2 do
local key = words[i]
local value = words[i + 1]
if key and value then
result[key] = value
end
end
return result
end
-- Request contacts from server
local function loadContacts()
modem.transmit(40, 0, "REQ" .. string.format("%03d", computerId))
local timerId = os.startTimer(2) -- 2-second timeout
while true do
local event, side, channel, replyChannel, message = os.pullEvent()
if event == "modem_message" and channel == 41 then
local flag = string.sub(message, 1, 3)
local destination = string.sub(message, 4, 6)
message = string.sub(message, 7)
if flag == "CTS" and tonumber(destination) == computerId then
return parseToTable(message)
end
elseif event == "timer" and side == timerId then
error("Error: Timeout waiting for server response.")
end
end
end
-- Wrap text to more lines or pages as neccessary
local function wrapText(text, width)
local lines = {}
local line = ""
local function splitLongWord(word)
local parts = {}
local i = 1
while i <= #word do
table.insert(parts, word:sub(i, i + width - 1))
i = i + width
end
return parts
end
for word in text:gmatch("%S+") do
-- Split the word into chunks if it's longer than width
local wordParts = #word > width and splitLongWord(word) or { word }
for _, part in ipairs(wordParts) do
if #line + #part + 1 <= width then
if line == "" then
line = part
else
line = line .. " " .. part
end
else
table.insert(lines, line)
line = part
end
end
end
if line ~= "" then
table.insert(lines, line)
end
return lines
end
-- Print wrapped text with cursor position and page management
local function printText(text)
local lines = wrapText(text, pageWidth)
local y = 1
printer.setPageTitle("MineFax message")
for _, line in ipairs(lines) do
if y > pageHeight then
printer.endPage()
if not printer.newPage() then
print("\nError: Failed to print, check printer.")
term.write("MineFax> ")
term.setCursorBlink(true)
return false
end
printer.setPageTitle("MineFax message")
y = 1
end
printer.setCursorPos(1, y)
printer.write(line)
y = y + 1
end
printer.endPage()
end
local contacts = loadContacts()
term.clear()
term.setCursorPos(0,1)
print("\n MineFax")
print(" ---------\n")
print(" *************************************")
print(" *The Premiere Hyperborean Fax Client*")
print(" *************************************\n")
print("To send a message write the recipients name followed by the contents of the message.\n")
term.write("MineFax> ")
term.setCursorBlink(true)
while true do
local event, side, channel, replyChannel, message = os.pullEvent()
if event == "modem_message" then --Check for modem message
if channel == 41 then
local flag = string.sub(message, 1, 3)
local destination = string.sub(message, 4, 6)
local sender = string.sub(message, 7, 9)
if (flag == "REC" and tonumber(destination) == computerId) then
message = string.sub(message, 10)
if not printer.newPage() then
print("\nError: Failed to print, check printer.")
term.write("MineFax> ")
term.setCursorBlink(true)
else
print("\nYou got mail!")
local transmitMessage = "SND" .. "ACK" .. sender
modem.transmit(40, 0, transmitMessage)
term.write("MineFax> ")
term.setCursorBlink(true)
printText(message)
end
elseif (flag == "REC" and destination == "BRD") then
if not (tonumber(sender) == computerId) then
message = string.sub(message, 10)
if not printer.newPage() then
print("\nError: Failed to print, check printer.")
term.write("MineFax> ")
term.setCursorBlink(true)
else
print("\nYou got mail!")
term.write("MineFax> ")
term.setCursorBlink(true)
printText(message)
end
end
end
end
elseif event == "key" then --Allow sending fax
local toSend = read()
contacts = loadContacts()
local destination, messageBody = toSend:match("^(%S+)%s*(.*)$")
toSend = messageBody
if destination == "ALL" then
modem.transmit(40, 0, ("SND" .. "BRD" .. string.format("%03d", computerId) .. toSend))
elseif contacts[destination] then
modem.transmit(40, 0, "SND" .. contacts[destination] .. string.format("%03d", computerId) .. toSend)
term.write("Message sending... ")
local timerId = os.startTimer(2) -- 2-second timeout
while true do
local event, side, channel, replyChannel, message = os.pullEvent()
if event == "modem_message" and channel == 41 then
local flag = string.sub(message, 1, 3)
local destination = string.sub(message, 4, 6)
if flag == "ACK" and tonumber(destination) == tonumber(computerId) then
term.write("Success!")
print()
break
end
elseif event == "timer" and side == timerId then
term.write("Failed to send.")
print()
break
end
end
else
print("Error: Contact does not exist")
end
term.write("MineFax> ")
term.setCursorBlink(true)
end
end