SHOW:
|
|
- or go back to the newest paste.
1 | -- ORIGINAL PROGRAM BY MIDNIGHTAS -- | |
2 | -- MODIFIED BY X3ME03 AKA hiperbolt -- | |
3 | ||
4 | os.loadAPI("disk/json") | |
5 | ||
6 | w, h = term.getSize() | |
7 | ||
8 | -- limitRead: Source by remiX on the ComputerCraft forums. | |
9 | function limitRead(nLimit, specialChars, replaceChar) | |
10 | term.setCursorBlink(true) | |
11 | local cX, cY = term.getCursorPos() | |
12 | local rString = "" | |
13 | if replaceChar == "" then replaceChar = nil end | |
14 | repeat | |
15 | local event, p1 = os.pullEvent() | |
16 | if event == "char" then | |
17 | -- Character event | |
18 | if #rString + 1 <= nLimit then | |
19 | if string.match(p1, "%W") and specialChars == false then | |
20 | p1 = "" | |
21 | end | |
22 | rString = rString .. p1 | |
23 | write(replaceChar or p1) | |
24 | end | |
25 | elseif event == "key" and p1 == keys.backspace and #rString >= 1 then | |
26 | -- Backspace | |
27 | rString = string.sub(rString, 1, #rString-1) | |
28 | xPos, yPos = term.getCursorPos() | |
29 | if xPos > 1 then | |
30 | term.setCursorPos(xPos - 1, yPos) | |
31 | write(" ") | |
32 | term.setCursorPos(xPos - 1, yPos) | |
33 | else | |
34 | if #rString ~= 0 then | |
35 | term.setCursorPos(w, yPos - 1) | |
36 | write(" ") | |
37 | term.setCursorPos(w, yPos - 1) | |
38 | end | |
39 | end | |
40 | end | |
41 | until event == "key" and p1 == keys.enter and #rString > 0 | |
42 | term.setCursorBlink(false) | |
43 | print() -- Skip to the next line after clicking enter. | |
44 | return rString | |
45 | end | |
46 | ||
47 | function printCenter(text, y, offset) | |
48 | offset = offset or 0 | |
49 | local x = math.floor(w - string.len(text)) / 2 + offset | |
50 | term.setCursorPos(x, y) | |
51 | print(text) | |
52 | return x | |
53 | end | |
54 | ||
55 | - | printCenter(" _____ __ _ _ _ ", 3) |
55 | + | |
56 | - | printCenter(" / ____| / _| | (_| |", 4) |
56 | + | printCenter(" _____ _ _ _ ", 3) |
57 | - | printCenter(" | | _ __ __ _| |_| |_ __ ___ __ _ _| |", 5) |
57 | + | printCenter(" |_ _| (_) | (_)", 4) |
58 | - | printCenter(" | | | '__/ _` | _| __| /_ ` _ \\ / _` | | |", 6) |
58 | + | printCenter(" | |_ ___| |_ _ __ ", 5) |
59 | - | printCenter(" | |____| | | (_| | | | |_ | | | | | (_| | | |", 7) |
59 | + | printCenter(" | \ \ /\ / / | __| '__| \ \/ /", 6) |
60 | - | printCenter(" \\_____|_| \\__,_|_| \\_ _| |_| |_| \\__,_|_|_|", 8) |
60 | + | printCenter(" | |\ V V /| | |_| | | |> <", 7) |
61 | printCenter(" \_/ \_/\_/ |_|\__|_| |_/_/\_\", 8) | |
62 | end | |
63 | ||
64 | function drawBackground() | |
65 | term.setBackgroundColor(colors.orange) | |
66 | term.setTextColor(colors.black) | |
67 | term.clear() | |
68 | end | |
69 | ||
70 | username = nil | |
71 | password = nil | |
72 | ||
73 | -- States: -1 = register; 0 = menu; 1 = login; 2 = send; 3 = read page; 4 = read message; | |
74 | state = 0 | |
75 | menuOption = 0 | |
76 | ||
77 | readPage = 1 | |
78 | mails = nil | |
79 | selectedMail = nil | |
80 | ||
81 | function drawMenu() | |
82 | drawLogo() | |
83 | term.setBackgroundColor(colors.orange) | |
84 | term.setTextColor(colors.black) | |
85 | if password ~= nil then | |
86 | if menuOption == 0 then | |
87 | term.setBackgroundColor(colors.lightGray) | |
88 | end | |
89 | btnSendX = printCenter("[ SEND ]", h / 2 + 3) | |
90 | term.setBackgroundColor(colors.orange) | |
91 | if menuOption == 1 then | |
92 | term.setBackgroundColor(colors.lightGray) | |
93 | end | |
94 | btnReadX = printCenter("[ READ ]", h / 2 + 4) | |
95 | term.setBackgroundColor(colors.orange) | |
96 | if menuOption == 2 then | |
97 | term.setBackgroundColor(colors.lightGray) | |
98 | end | |
99 | btnExitX = printCenter("[ EXIT ]", h / 2 + 5) | |
100 | term.setBackgroundColor(colors.orange) | |
101 | else | |
102 | if menuOption == 0 then | |
103 | term.setBackgroundColor(colors.lightGray) | |
104 | end | |
105 | btnSendX = printCenter("[ SIGN IN ]", h / 2 + 3) | |
106 | term.setBackgroundColor(colors.orange) | |
107 | if menuOption == 1 then | |
108 | term.setBackgroundColor(colors.lightGray) | |
109 | end | |
110 | btnReadX = printCenter("[ SIGN UP ]", h / 2 + 4) | |
111 | term.setBackgroundColor(colors.orange) | |
112 | if menuOption == 2 then | |
113 | term.setBackgroundColor(colors.lightGray) | |
114 | end | |
115 | btnReadX = printCenter("[ EXIT ]", h / 2 + 5) | |
116 | term.setBackgroundColor(colors.orange) | |
117 | end | |
118 | end | |
119 | ||
120 | function drawLabel() | |
121 | term.setBackgroundColor(colors.lightGray) | |
122 | term.setCursorPos(1, 1) | |
123 | term.clearLine() | |
124 | printCenter("CRAFTMAIL CLIENT", 1) | |
125 | term.setBackgroundColor(colors.red) | |
126 | term.setTextColor(colors.black) | |
127 | term.setCursorPos(w,1) | |
128 | write("X") | |
129 | end | |
130 | ||
131 | function drawState(id) | |
132 | if id == 2 then | |
133 | term.setBackgroundColor(colors.orange) | |
134 | term.setTextColor(colors.black) | |
135 | term.setCursorPos(w / 2 - 5, 6) | |
136 | write("RECIPIENT:") | |
137 | paintutils.drawFilledBox(w / 2 - 5, 7, w / 2 + 5, 7, colors.lightGray) | |
138 | term.setCursorPos(w / 2 - 5, 8) | |
139 | term.setBackgroundColor(colors.orange) | |
140 | write("SUBJECT:") | |
141 | paintutils.drawFilledBox(w / 2 - 10, 9, w / 2 + 10, 9, colors.lightGray) | |
142 | term.setCursorPos(1, 10) | |
143 | term.setBackgroundColor(colors.orange) | |
144 | write("MESSAGE (MULTILINE NOT SUPPORTED):") | |
145 | term.setCursorPos(1, 11) | |
146 | elseif id == 1 then | |
147 | drawLogo() | |
148 | paintutils.drawFilledBox(w / 2 - 5, h - 6, w / 2 + 5, h - 6, colors.lightGray) | |
149 | elseif id == -1 then | |
150 | drawLogo() | |
151 | elseif id == 3 then | |
152 | line = 3 | |
153 | term.setBackgroundColor(colors.orange) | |
154 | term.setTextColor(colors.black) | |
155 | term.setCursorPos(1, 1) | |
156 | term.clearLine() | |
157 | write("BACK - BACKSPACE | SCROLL = LEFT & RIGHT ARROWS") | |
158 | term.setBackgroundColor(colors.lightGray) | |
159 | for i = readPage * 8 - 8, readPage * 8, 1 do | |
160 | local v = mails[i + 1] | |
161 | if v ~= nil then | |
162 | term.setCursorPos(1, line) | |
163 | term.clearLine() | |
164 | print(v[2] .. " - " .. string.sub(v[5], 0, w / 2)) | |
165 | line = line + 1 | |
166 | end | |
167 | end | |
168 | term.setBackgroundColor(colors.orange) | |
169 | term.setCursorPos(1, h) | |
170 | write(readPage .. "/" .. math.floor(table.getn(mails) / 8) + 1) | |
171 | elseif id == 4 then | |
172 | local v = mails[selectedMail + 1] | |
173 | term.setCursorPos(1, 1) | |
174 | term.setBackgroundColor(colors.orange) | |
175 | term.setTextColor(colors.black) | |
176 | print("Press BACKSPACE to leave."); | |
177 | print("Mail ID: " .. v[1]) | |
178 | print("From: " .. v[2]) | |
179 | print() | |
180 | print(v[4]) | |
181 | print(string.sub("----------------------------------------------------------------------------", 0, w)) | |
182 | print(v[5]) | |
183 | end | |
184 | end | |
185 | ||
186 | function draw() | |
187 | drawBackground() | |
188 | if state == 0 then | |
189 | drawMenu() | |
190 | else | |
191 | drawState(state) | |
192 | end | |
193 | end | |
194 | ||
195 | drawBackground() | |
196 | drawMenu() | |
197 | ||
198 | while true do | |
199 | local event, param1, param2, param3 = os.pullEvent() | |
200 | if event == "key" then | |
201 | if state == 0 then | |
202 | if param1 == keys.down then | |
203 | if menuOption == 0 then | |
204 | menuOption = 1 | |
205 | elseif menuOption == 1 then | |
206 | menuOption = 2 | |
207 | end | |
208 | drawMenu() | |
209 | elseif param1 == keys.up then | |
210 | if menuOption == 2 then | |
211 | menuOption = 1 | |
212 | elseif menuOption == 1 then | |
213 | menuOption = 0 | |
214 | end | |
215 | drawMenu() | |
216 | elseif param1 == keys.enter then | |
217 | if menuOption == 2 then | |
218 | break | |
219 | end | |
220 | if password ~= nil then | |
221 | if menuOption == 0 then | |
222 | state = 2 | |
223 | draw() | |
224 | term.setCursorPos(w / 2 - 5, 7) | |
225 | term.setBackgroundColor(colors.lightGray) | |
226 | local recipient = limitRead(10, false) | |
227 | term.setCursorPos(w / 2 - 10, 9) | |
228 | local subject = limitRead(20, true) | |
229 | term.setCursorPos(1, 11) | |
230 | term.setBackgroundColor(colors.orange) | |
231 | local message = limitRead((h - 11) * w, true) | |
232 | term.setBackgroundColor(colors.orange) | |
233 | term.clear() | |
234 | printCenter("Accessing remote database", h / 2) | |
235 | local page = http.get("http://midnightasapi.vapr.cc/?request=mail&username=" .. username .. "&password=" .. textutils.urlEncode(password) .. "&recipient=" .. recipient .. "&subject=" .. textutils.urlEncode(subject) .. "&message=" .. textutils.urlEncode(message)) | |
236 | local pageContent = page.readAll() | |
237 | if string.find(pageContent, "1") then | |
238 | printCenter("Mail sent", h / 2 + 1) | |
239 | elseif string.find(pageContent, "0") then | |
240 | printCenter("Error found", h / 2 + 1) | |
241 | end | |
242 | printCenter("Press any key to continue", h / 2 + 2) | |
243 | os.pullEvent("key") | |
244 | state = 0 | |
245 | draw() | |
246 | elseif menuOption == 1 then | |
247 | term.setBackgroundColor(colors.orange) | |
248 | term.clear() | |
249 | printCenter("Accessing remote database", h / 2) | |
250 | local page = http.get("http://midnightasapi.vapr.cc/?request=read&username=" .. username .. "&password=" .. textutils.urlEncode(password)) | |
251 | mails = {} | |
252 | local obj = json.decode(page.readAll()) | |
253 | for k in pairs(obj) do | |
254 | local v = obj[k] | |
255 | table.insert(mails, obj[k]) | |
256 | end | |
257 | printCenter("Request successful", h / 2 + 1) | |
258 | printCenter("Press any key to continue", h / 2 + 2) | |
259 | os.pullEvent("key") | |
260 | state = 3 | |
261 | draw() | |
262 | end | |
263 | else | |
264 | if menuOption == 0 then | |
265 | state = 1 | |
266 | draw() | |
267 | term.setCursorPos(w / 2 - 5, h - 7) | |
268 | term.setBackgroundColor(colors.orange) | |
269 | term.setTextColor(colors.black) | |
270 | write("USERNAME:") | |
271 | term.setBackgroundColor(colors.lightGray) | |
272 | term.setCursorPos(w / 2 - 5, h - 6) | |
273 | local newUsername = limitRead(10, false) | |
274 | term.setBackgroundColor(colors.orange) | |
275 | term.setCursorPos(w / 2 - 5, h - 5) | |
276 | write("PASSWORD:") | |
277 | paintutils.drawFilledBox(w / 2 - 5, h - 4, w / 2 + 5, h - 4, colors.lightGray) | |
278 | term.setBackgroundColor(colors.lightGray) | |
279 | term.setCursorPos(w / 2 - 5, h - 4) | |
280 | newPassword = limitRead(10, true, "*") | |
281 | term.setBackgroundColor(colors.orange) | |
282 | term.clear() | |
283 | printCenter("Accessing remote database", h / 2) | |
284 | local page = http.get("http://midnightasapi.vapr.cc/?request=login&username=" .. newUsername .. "&password=" .. textutils.urlEncode(newPassword)) | |
285 | local pageContent = page.readAll() | |
286 | if string.find(pageContent, "1") then | |
287 | printCenter("Request successful", h / 2 + 1) | |
288 | username = newUsername | |
289 | password = newPassword | |
290 | elseif string.find(pageContent, "0") then | |
291 | printCenter("Invalid credentials", h / 2 + 1) | |
292 | end | |
293 | printCenter("Press any key to continue", h / 2 + 2) | |
294 | os.pullEvent("key") | |
295 | state = 0 | |
296 | draw() | |
297 | elseif menuOption == 1 then | |
298 | state = -1 | |
299 | draw() | |
300 | paintutils.drawFilledBox(w / 2 - 5, h - 6, w / 2 + 5, h - 6, colors.lightGray) | |
301 | term.setBackgroundColor(colors.orange) | |
302 | term.setTextColor(colors.black) | |
303 | term.setCursorPos(w / 2 - 5, h - 7) | |
304 | write("USERNAME:") | |
305 | term.setBackgroundColor(colors.lightGray) | |
306 | term.setCursorPos(w / 2 - 5, h - 6) | |
307 | local newUsername = limitRead(10, false) | |
308 | term.setBackgroundColor(colors.orange) | |
309 | term.setCursorPos(w / 2 - 5, h - 5) | |
310 | write("PASSWORD:") | |
311 | paintutils.drawFilledBox(w / 2 - 5, h - 4, w / 2 + 5, h - 4, colors.lightGray) | |
312 | term.setBackgroundColor(colors.lightGray) | |
313 | term.setCursorPos(w / 2 - 5, h - 4) | |
314 | local newPassword = limitRead(10, true, "*") | |
315 | term.setBackgroundColor(colors.orange) | |
316 | term.clear() | |
317 | printCenter("Accessing remote database", h / 2) | |
318 | local page = http.get("http://midnightasapi.vapr.cc/?request=register&username=" .. newUsername .. "&password=" .. textutils.urlEncode(newPassword)) | |
319 | local pageContent = page.readAll() | |
320 | if string.find(pageContent, "1") then | |
321 | printCenter("Registration successful", h / 2 + 1) | |
322 | elseif string.find(pageContent, "0") then | |
323 | printCenter("Username already is registered", h / 2 + 1) | |
324 | elseif string.find(pageContent, "2") then | |
325 | printCenter("Using hacked client, cancelling registration.", h / 2 + 1) | |
326 | end | |
327 | printCenter("Press any key to continue", h / 2 + 2) | |
328 | os.pullEvent("key") | |
329 | state = 0 | |
330 | draw() | |
331 | end | |
332 | end | |
333 | end | |
334 | elseif state == 3 then | |
335 | if param1 == keys.backspace then | |
336 | state = 0 | |
337 | draw() | |
338 | elseif param1 == keys.left then | |
339 | if readPage > 1 then | |
340 | readPage = readPage - 1 | |
341 | end | |
342 | elseif param1 == keys.right then | |
343 | if readPage < math.floor(table.getn(mails) / 8) + 1 then | |
344 | readPage = readPage + 1 | |
345 | end | |
346 | end | |
347 | elseif state == 4 then | |
348 | if param1 == keys.backspace then | |
349 | state = 3 | |
350 | draw() | |
351 | elseif param1 == keys.delete then | |
352 | term.setBackgroundColor(colors.orange) | |
353 | term.clear() | |
354 | printCenter("Accessing remote database", h / 2) | |
355 | local page = http.get("http://midnightasapi.vapr.cc/?request=deletemail&username=" .. username .. "&password=" .. textutils.urlEncode(password) .. "&id=" .. mails[selectedMail + 1][1]) | |
356 | local pageContent = page.readAll() | |
357 | if string.find(pageContent, "1") then | |
358 | printCenter("Mail deleted", h / 2 + 1) | |
359 | elseif string.find(pageContent, "0") then | |
360 | printCenter("Failed to delete mail.", h / 2 + 1) | |
361 | end | |
362 | printCenter("Press any key to continue", h / 2 + 2) | |
363 | os.pullEvent("key") | |
364 | state = 0 | |
365 | draw() | |
366 | end | |
367 | end | |
368 | elseif event == "mouse_click" then | |
369 | if state == 3 then | |
370 | if param3 >= 3 and param3 <= 11 then | |
371 | selectedMail = (8 * readPage) - 8 + param3 - 3 | |
372 | if mails[selectedMail + 1] ~= nil then | |
373 | state = 4 | |
374 | draw() | |
375 | end | |
376 | end | |
377 | end | |
378 | end | |
379 | end | |
380 | term.setBackgroundColor(colors.black) | |
381 | term.setTextColor(colors.yellow) | |
382 | term.clear() | |
383 | term.setCursorPos(1, 1) | |
384 | print("Thank you for using CraftMail service.") | |
385 | print("Project by Midnightas") |