Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. function drawLogo()
  56. printCenter("Twitrix Mailing Software", 3)
  57. end
  58.  
  59. function drawBackground()
  60. term.setBackgroundColor(colors.orange)
  61. term.setTextColor(colors.black)
  62. term.clear()
  63. end
  64.  
  65. username = nil
  66. password = nil
  67.  
  68. -- States: -1 = register; 0 = menu; 1 = login; 2 = send; 3 = read page; 4 = read message;
  69. state = 0
  70. menuOption = 0
  71.  
  72. readPage = 1
  73. mails = nil
  74. selectedMail = nil
  75.  
  76. function drawMenu()
  77. drawLogo()
  78. term.setBackgroundColor(colors.orange)
  79. term.setTextColor(colors.black)
  80. if password ~= nil then
  81. if menuOption == 0 then
  82. term.setBackgroundColor(colors.lightGray)
  83. end
  84. btnSendX = printCenter("[ SEND ]", h / 2 + 3)
  85. term.setBackgroundColor(colors.orange)
  86. if menuOption == 1 then
  87. term.setBackgroundColor(colors.lightGray)
  88. end
  89. btnReadX = printCenter("[ READ ]", h / 2 + 4)
  90. term.setBackgroundColor(colors.orange)
  91. if menuOption == 2 then
  92. term.setBackgroundColor(colors.lightGray)
  93. end
  94. btnExitX = printCenter("[ EXIT ]", h / 2 + 5)
  95. term.setBackgroundColor(colors.orange)
  96. else
  97. if menuOption == 0 then
  98. term.setBackgroundColor(colors.lightGray)
  99. end
  100. btnSendX = printCenter("[ SIGN IN ]", h / 2 + 3)
  101. term.setBackgroundColor(colors.orange)
  102. if menuOption == 1 then
  103. term.setBackgroundColor(colors.lightGray)
  104. end
  105. btnReadX = printCenter("[ SIGN UP ]", h / 2 + 4)
  106. term.setBackgroundColor(colors.orange)
  107. if menuOption == 2 then
  108. term.setBackgroundColor(colors.lightGray)
  109. end
  110. btnReadX = printCenter("[ EXIT ]", h / 2 + 5)
  111. term.setBackgroundColor(colors.orange)
  112. end
  113. end
  114.  
  115. function drawLabel()
  116. term.setBackgroundColor(colors.lightGray)
  117. term.setCursorPos(1, 1)
  118. term.clearLine()
  119. printCenter("CRAFTMAIL CLIENT", 1)
  120. term.setBackgroundColor(colors.red)
  121. term.setTextColor(colors.black)
  122. term.setCursorPos(w,1)
  123. write("X")
  124. end
  125.  
  126. function drawState(id)
  127. if id == 2 then
  128. term.setBackgroundColor(colors.orange)
  129. term.setTextColor(colors.black)
  130. term.setCursorPos(w / 2 - 5, 6)
  131. write("RECIPIENT:")
  132. paintutils.drawFilledBox(w / 2 - 5, 7, w / 2 + 5, 7, colors.lightGray)
  133. term.setCursorPos(w / 2 - 5, 8)
  134. term.setBackgroundColor(colors.orange)
  135. write("SUBJECT:")
  136. paintutils.drawFilledBox(w / 2 - 10, 9, w / 2 + 10, 9, colors.lightGray)
  137. term.setCursorPos(1, 10)
  138. term.setBackgroundColor(colors.orange)
  139. write("MESSAGE (MULTILINE NOT SUPPORTED):")
  140. term.setCursorPos(1, 11)
  141. elseif id == 1 then
  142. drawLogo()
  143. paintutils.drawFilledBox(w / 2 - 5, h - 6, w / 2 + 5, h - 6, colors.lightGray)
  144. elseif id == -1 then
  145. drawLogo()
  146. elseif id == 3 then
  147. line = 3
  148. term.setBackgroundColor(colors.orange)
  149. term.setTextColor(colors.black)
  150. term.setCursorPos(1, 1)
  151. term.clearLine()
  152. write("BACK - BACKSPACE | SCROLL = LEFT & RIGHT ARROWS")
  153. term.setBackgroundColor(colors.lightGray)
  154. for i = readPage * 8 - 8, readPage * 8, 1 do
  155. local v = mails[i + 1]
  156. if v ~= nil then
  157. term.setCursorPos(1, line)
  158. term.clearLine()
  159. print(v[2] .. " - " .. string.sub(v[5], 0, w / 2))
  160. line = line + 1
  161. end
  162. end
  163. term.setBackgroundColor(colors.orange)
  164. term.setCursorPos(1, h)
  165. write(readPage .. "/" .. math.floor(table.getn(mails) / 8) + 1)
  166. elseif id == 4 then
  167. local v = mails[selectedMail + 1]
  168. term.setCursorPos(1, 1)
  169. term.setBackgroundColor(colors.orange)
  170. term.setTextColor(colors.black)
  171. print("Press BACKSPACE to leave.");
  172. print("Mail ID: " .. v[1])
  173. print("From: " .. v[2])
  174. print()
  175. print(v[4])
  176. print(string.sub("----------------------------------------------------------------------------", 0, w))
  177. print(v[5])
  178. end
  179. end
  180.  
  181. function draw()
  182. drawBackground()
  183. if state == 0 then
  184. drawMenu()
  185. else
  186. drawState(state)
  187. end
  188. end
  189.  
  190. drawBackground()
  191. drawMenu()
  192.  
  193. while true do
  194. local event, param1, param2, param3 = os.pullEvent()
  195. if event == "key" then
  196. if state == 0 then
  197. if param1 == keys.down then
  198. if menuOption == 0 then
  199. menuOption = 1
  200. elseif menuOption == 1 then
  201. menuOption = 2
  202. end
  203. drawMenu()
  204. elseif param1 == keys.up then
  205. if menuOption == 2 then
  206. menuOption = 1
  207. elseif menuOption == 1 then
  208. menuOption = 0
  209. end
  210. drawMenu()
  211. elseif param1 == keys.enter then
  212. if menuOption == 2 then
  213. break
  214. end
  215. if password ~= nil then
  216. if menuOption == 0 then
  217. state = 2
  218. draw()
  219. term.setCursorPos(w / 2 - 5, 7)
  220. term.setBackgroundColor(colors.lightGray)
  221. local recipient = limitRead(10, false)
  222. term.setCursorPos(w / 2 - 10, 9)
  223. local subject = limitRead(20, true)
  224. term.setCursorPos(1, 11)
  225. term.setBackgroundColor(colors.orange)
  226. local message = limitRead((h - 11) * w, true)
  227. term.setBackgroundColor(colors.orange)
  228. term.clear()
  229. printCenter("Accessing remote database", h / 2)
  230. 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))
  231. local pageContent = page.readAll()
  232. if string.find(pageContent, "1") then
  233. printCenter("Mail sent", h / 2 + 1)
  234. elseif string.find(pageContent, "0") then
  235. printCenter("Error found", h / 2 + 1)
  236. end
  237. printCenter("Press any key to continue", h / 2 + 2)
  238. os.pullEvent("key")
  239. state = 0
  240. draw()
  241. elseif menuOption == 1 then
  242. term.setBackgroundColor(colors.orange)
  243. term.clear()
  244. printCenter("Accessing remote database", h / 2)
  245. local page = http.get("http://midnightasapi.vapr.cc/?request=read&username=" .. username .. "&password=" .. textutils.urlEncode(password))
  246. mails = {}
  247. local obj = json.decode(page.readAll())
  248. for k in pairs(obj) do
  249. local v = obj[k]
  250. table.insert(mails, obj[k])
  251. end
  252. printCenter("Request successful", h / 2 + 1)
  253. printCenter("Press any key to continue", h / 2 + 2)
  254. os.pullEvent("key")
  255. state = 3
  256. draw()
  257. end
  258. else
  259. if menuOption == 0 then
  260. state = 1
  261. draw()
  262. term.setCursorPos(w / 2 - 5, h - 7)
  263. term.setBackgroundColor(colors.orange)
  264. term.setTextColor(colors.black)
  265. write("USERNAME:")
  266. term.setBackgroundColor(colors.lightGray)
  267. term.setCursorPos(w / 2 - 5, h - 6)
  268. local newUsername = limitRead(10, false)
  269. term.setBackgroundColor(colors.orange)
  270. term.setCursorPos(w / 2 - 5, h - 5)
  271. write("PASSWORD:")
  272. paintutils.drawFilledBox(w / 2 - 5, h - 4, w / 2 + 5, h - 4, colors.lightGray)
  273. term.setBackgroundColor(colors.lightGray)
  274. term.setCursorPos(w / 2 - 5, h - 4)
  275. newPassword = limitRead(10, true, "*")
  276. term.setBackgroundColor(colors.orange)
  277. term.clear()
  278. printCenter("Accessing remote database", h / 2)
  279. local page = http.get("http://midnightasapi.vapr.cc/?request=login&username=" .. newUsername .. "&password=" .. textutils.urlEncode(newPassword))
  280. local pageContent = page.readAll()
  281. if string.find(pageContent, "1") then
  282. printCenter("Request successful", h / 2 + 1)
  283. username = newUsername
  284. password = newPassword
  285. elseif string.find(pageContent, "0") then
  286. printCenter("Invalid credentials", h / 2 + 1)
  287. end
  288. printCenter("Press any key to continue", h / 2 + 2)
  289. os.pullEvent("key")
  290. state = 0
  291. draw()
  292. elseif menuOption == 1 then
  293. state = -1
  294. draw()
  295. paintutils.drawFilledBox(w / 2 - 5, h - 6, w / 2 + 5, h - 6, colors.lightGray)
  296. term.setBackgroundColor(colors.orange)
  297. term.setTextColor(colors.black)
  298. term.setCursorPos(w / 2 - 5, h - 7)
  299. write("USERNAME:")
  300. term.setBackgroundColor(colors.lightGray)
  301. term.setCursorPos(w / 2 - 5, h - 6)
  302. local newUsername = limitRead(10, false)
  303. term.setBackgroundColor(colors.orange)
  304. term.setCursorPos(w / 2 - 5, h - 5)
  305. write("PASSWORD:")
  306. paintutils.drawFilledBox(w / 2 - 5, h - 4, w / 2 + 5, h - 4, colors.lightGray)
  307. term.setBackgroundColor(colors.lightGray)
  308. term.setCursorPos(w / 2 - 5, h - 4)
  309. local newPassword = limitRead(10, true, "*")
  310. term.setBackgroundColor(colors.orange)
  311. term.clear()
  312. printCenter("Accessing remote database", h / 2)
  313. local page = http.get("http://midnightasapi.vapr.cc/?request=register&username=" .. newUsername .. "&password=" .. textutils.urlEncode(newPassword))
  314. local pageContent = page.readAll()
  315. if string.find(pageContent, "1") then
  316. printCenter("Registration successful", h / 2 + 1)
  317. elseif string.find(pageContent, "0") then
  318. printCenter("Username already is registered", h / 2 + 1)
  319. elseif string.find(pageContent, "2") then
  320. printCenter("Using hacked client, cancelling registration.", h / 2 + 1)
  321. end
  322. printCenter("Press any key to continue", h / 2 + 2)
  323. os.pullEvent("key")
  324. state = 0
  325. draw()
  326. end
  327. end
  328. end
  329. elseif state == 3 then
  330. if param1 == keys.backspace then
  331. state = 0
  332. draw()
  333. elseif param1 == keys.left then
  334. if readPage > 1 then
  335. readPage = readPage - 1
  336. end
  337. elseif param1 == keys.right then
  338. if readPage < math.floor(table.getn(mails) / 8) + 1 then
  339. readPage = readPage + 1
  340. end
  341. end
  342. elseif state == 4 then
  343. if param1 == keys.backspace then
  344. state = 3
  345. draw()
  346. elseif param1 == keys.delete then
  347. term.setBackgroundColor(colors.orange)
  348. term.clear()
  349. printCenter("Accessing remote database", h / 2)
  350. local page = http.get("http://midnightasapi.vapr.cc/?request=deletemail&username=" .. username .. "&password=" .. textutils.urlEncode(password) .. "&id=" .. mails[selectedMail + 1][1])
  351. local pageContent = page.readAll()
  352. if string.find(pageContent, "1") then
  353. printCenter("Mail deleted", h / 2 + 1)
  354. elseif string.find(pageContent, "0") then
  355. printCenter("Failed to delete mail.", h / 2 + 1)
  356. end
  357. printCenter("Press any key to continue", h / 2 + 2)
  358. os.pullEvent("key")
  359. state = 0
  360. draw()
  361. end
  362. end
  363. elseif event == "mouse_click" then
  364. if state == 3 then
  365. if param3 >= 3 and param3 <= 11 then
  366. selectedMail = (8 * readPage) - 8 + param3 - 3
  367. if mails[selectedMail + 1] ~= nil then
  368. state = 4
  369. draw()
  370. end
  371. end
  372. end
  373. end
  374. end
  375. term.setBackgroundColor(colors.black)
  376. term.setTextColor(colors.yellow)
  377. term.clear()
  378. term.setCursorPos(1, 1)
  379. print("Thank you for using CraftMail service.")
  380. print("Project by Midnightas")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement