-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaxNew.lua
More file actions
209 lines (172 loc) · 6.09 KB
/
faxNew.lua
File metadata and controls
209 lines (172 loc) · 6.09 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
-- Minefax client v2
-- Unfinished rewrite, does not currently work
local modem = peripheral.find("modem") or error("Error: No modem attached", 0)
local printer = peripheral.find("printer") or error("Error: No printer attached", 0)
local rsa = require("rsa") -- Modified version of RSA encryption library from 1lann
local pageWidth = 25
local pageHeight = 21
local computerId = os.getComputerID()
local computerName = os.getComputerLabel() or computerId
local faxServerId = 2 -- Must be manually set
local w, h = term.getSize()
local timeoutId = nil
local encrypted = false
local input = ""
local contacts = {}
modem.open(3)
-- Redraws UI
local function reDraw()
term.setCursorPos(1,1) term.clearLine()
term.setCursorPos(1,2) term.clearLine() term.write(" *********************")
term.setCursorPos(1,3) term.clearLine() term.write(" * Minefax client *")
term.setCursorPos(1,4) term.clearLine() term.write(" *********************")
term.setCursorPos(1,5) term.clearLine()
term.setCursorPos(1,6) term.clearLine() term.write(" The Premiere Hyperborean Fax Client")
term.setCursorPos(1,7) term.clearLine() term.write(" Type halp for commands list")
term.setCursorPos(1,8) term.clearLine()
term.setCursorPos(1,h) term.clearLine() term.write("Minefax> ".. input)
term.setCursorBlink(true)
end
-- Prints to terminal with correct formatting
local function output(text)
term.scroll(1)
term.setCursorPos(1, h - 1)
term.clearLine()
term.write(text)
reDraw()
end
-- Request contacts list from server
local function requestContacts()
local packet = {
protocol = "fomp",
type = "contactsListRequest",
sender = computerId,
receiver = faxServerId,
data = nil
}
modem.transmit(3, 3, packet)
timeoutId = os.startTimer(2)
end
-- Wrap text to correctly fit on printed page
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
-- Main loop
local function main()
requestContacts()
while true do
local event, a, b, c, d = os.pullEvent()
-- Handle incoming modem messages
if event == "modem_message" then
local msgContent = d
if (msgContent["receiver"] == computerId) and (msgContent["sender"] == faxServerId) and (msgContent["protocol"] == "fomp") then
if msgContent["type"] == "contactsListResponse" then
os.cancelTimer(timeoutId)
timeoutId = nil
contacts = msgContent["data"]
end
end
-- Add character to input buffer
elseif event == "char" then
local char = a
input = input .. char
reDraw()
-- Handle non-alphanumeric keyboard input
elseif event == "key" then
local key = a
-- Delete character from input buffer
if key == keys.backspace then
input = input:sub(1,-2)
-- Determine command from input buffer
elseif key == keys.enter then
if input ~= "" then
if input == "send" then
elseif input:match("^send%s+") then
local message = input:match("^send%s+(.+)")
local packet
if encrypted then
packet = {
protocol = "fomp",
type = "faxSend",
sender = computerId,
receiver = faxServerId,
data = rsa.encryptString()
}
else
end
-- Allow toggling encryption
elseif input:match("^encrypt%s+") then
local message = input:match("^encrypt%s+(.+)")
if message == "on" then
encrypted = true
elseif message == "off" then
encrypted = false
end
else
output("Error: Invalid command.")
end
input = ""
end
end
reDraw()
elseif event == "timer" then
error("Error: Server connection timed out.", 0)
end
end
end
term.clear()
reDraw()
main()