Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 45.01 KB | None | 0 0
  1. oldpullEvent = os.pullEvent
  2. os.pullEvent = os.pullEventRaw
  3.  
  4. -- Cannon OS by PaymentOption --
  5. VERSION = 1.85
  6. --------------------------------
  7.  
  8. rednet.open("top")
  9. rednet.open("bottom")
  10. rednet.open("back")
  11. rednet.open("front")
  12. rednet.open("right")
  13. rednet.open("left")
  14.  
  15. --[[ Notes:
  16. User information like passwords are located in the User/<computerID>/.info.
  17. E-Mails are stored in the /Mail Directory.
  18. E-Mail preferences are stored in /Mail/.prefs.
  19.  
  20. All printing functions like PrintMenu and PrintMainScreen do not clear the screen first; you must clear it before using them.
  21. --]]
  22.  
  23.  
  24. -- VARIABLES --
  25.  
  26. -- Update and Service Variables --
  27. nServerID = 12 -- Update and key check server ID
  28. ----------------------------------
  29.  
  30. -- Screen Variables --
  31. screenWidth, screenHeight = term.getSize()
  32. selection = 1
  33.  
  34. sMenuState = "main"
  35. ----------------------
  36.  
  37. -- Account Variables --
  38. sUserName = ""
  39. sPassword = ""
  40.  
  41. bNewUser = true
  42. bLogin = true
  43.  
  44. bAdmin = true -- Switch to false to disable the following features: CraftOS, Computer, Email, Account Manipulation
  45. -----------------------
  46.  
  47. -- Authenticity Variables --
  48. sLocalKey = ""
  49. bIsAuthentic = true -- If the key entered by the user upon installation is correct, then this should be true
  50. ----------------------------
  51.  
  52. -- Mail --
  53. if(fs.isDir("Mail")) then nMail = #(fs.list("Mail"))-1
  54. else nMailCount = 0 end
  55. ----------
  56. ---------------
  57.  
  58. -- Software Validation --
  59. function checkKey(sKey)
  60. rednet.send(nServerID, "CHECKMYKEY")
  61. rednet.send(nServerID, tostring(sKey))
  62.  
  63. sender, message = rednet.receive(1)
  64.  
  65. if sender == nServerID then
  66. if message ~= "authentic" then bAuthentic = false; return false
  67. else bAuthentic = true; return true end
  68. end
  69. end
  70.  
  71. function getKey()
  72. if fs.exists(".key") then return end
  73. if bIsAuthentic == true then return end -- To avoid embarassing double authentication!
  74.  
  75. clear()
  76. printMainScreen()
  77. sleep(2)
  78.  
  79. clear()
  80. printBorder()
  81.  
  82. cPrint(3, "Welcome!")
  83. cPrint(5, "You're about to install:")
  84. cPrint(6, "Cannon OS V:"..VERSION.."!")
  85. cPrint(7, " The installation wizard will walk you")
  86. cPrint(8, " through product validation to installation.")
  87. cPrint(10, "Before you continue, make sure that you have")
  88. cPrint(11, " the product key given by your distributor.")
  89.  
  90. cPrint(15, "[ Continue ]")
  91. while true do
  92. event, key = os.pullEvent("key")
  93.  
  94. if key == 28 then break end
  95. end
  96.  
  97. clear()
  98. printBorder()
  99.  
  100. rPrint(17, "Cannon OS Version: "..VERSION.."*")
  101. rPrint(16, "Software: LOCKED*")
  102.  
  103. cPrint(8, "Please enter your product key here:")
  104. term.setCursorPos(15, 9); write("Key: ")
  105. local sProductKey = newRead()
  106.  
  107. if checkKey(sProductKey) then
  108. clear()
  109. printBorder()
  110.  
  111. cPrint(9, "Congradulations,")
  112. cPrint(10, "Your software has successfully been")
  113. cPrint(11, "VALIDATED!")
  114. sleep(3)
  115.  
  116. local file = fs.open(".key", "w")
  117. file.writeLine(sProductKey)
  118. file.close()
  119. bIsAuthentic = true
  120. else
  121. clear()
  122. printBorder()
  123.  
  124. cPrint(8, "I'm sorry, but the key")
  125. cPrint(9, "entered doesn't seem to be valid.")
  126. sleep(3)
  127. os.shutdown()
  128. end
  129. end
  130. -------------------------
  131.  
  132.  
  133. -- Helper Functions --
  134. function cPrint(height, string)
  135. local xPos = screenWidth/2 - string.len(string)/2
  136. term.setCursorPos(xPos, height); term.write(string)
  137. end
  138.  
  139. function rPrint(height, string)
  140. local xPos = screenWidth - string.len(string)
  141. term.setCursorPos(xPos, height); term.write(string)
  142. end
  143.  
  144. function clear() term.clear(); term.setCursorPos(1,1) end
  145.  
  146. function printBorder()
  147. write(" "..string.rep("*", screenWidth-2).."n")
  148. for i=1, screenHeight-2 do
  149. write(" *"..string.rep(" ", screenWidth-4).."*n")
  150. end
  151. write(" "..string.rep("*", screenWidth-2))
  152. end
  153.  
  154. function customWrite(height, string)
  155. term.setCursorPos(3, height)
  156. term.write(string)
  157. end
  158.  
  159. function clearCenter()
  160. cPrint(2, "* "..sUserName.." *")
  161. cPrint(3, "*"..string.rep("*", string.len(sUserName)+2).."*")
  162. rPrint(17, "Cannon OS Version: "..VERSION.. "*")
  163.  
  164. local line = 4
  165. term.setCursorPos(34, 3); term.write(" ") -- Remove the very top part of the cannon logo
  166. repeat
  167. term.setCursorPos(1, line); term.clearLine()
  168. line = line+1
  169. until line == 12
  170.  
  171. line = 4
  172. term.setCursorPos(1, 4)
  173.  
  174. repeat
  175. write(" *"..string.rep(" ", screenWidth-4).."*n")
  176. line = line+1
  177. until line == 12
  178. end
  179.  
  180. function printContainer()
  181. term.setCursorPos(4, 5); term.write(string.rep("*", screenWidth-6))
  182. local y = 6
  183. for i=1, screenHeight - 13 do
  184. term.setCursorPos(4, y); term.write("*"..string.rep(" ", screenWidth-8).."*")
  185. y = y+1
  186. end
  187. term.setCursorPos(4, 11); term.write(string.rep("*", screenWidth-6))
  188. end
  189.  
  190. -- Perfecting the pullEvent backspace attempt 1! --
  191.  
  192. function newRead( _charLimit, _ReplaceChar) -- The filter can either be a 1, 0, or nil. 1 is Numbers only, 0 is letters only, and nil is both
  193. local sMessage = ""
  194. term.setCursorBlink(true)
  195. local originalCursorX, originalCursorY = term.getCursorPos()
  196.  
  197. while true do
  198. local cursorX, cursorY = term.getCursorPos()
  199. local event, p1 = os.pullEvent()
  200.  
  201. if _charLimit ~= nil then
  202. if string.len(sMessage) == _charLimit then term.setCursorBlink(false); return sMessage end
  203. end
  204.  
  205. if event == "char" then sMessage = sMessage..p1
  206. --[[elseif event == "key" then -- The escape key has keyID 1 so: 0 = keyID: 11, 1 = keyID: 2, 2 = keyID : 3 etc etc....
  207. if p1 == 11 or p1 == 2 or p1 == 3 or p1 == 4 or p1 == 5 or p1 == 6 or p1 == 7 or p1 == 8 or p1 == 9 or p1 == 10 then
  208. -- 0 1 2 3 4 5 6 7 8 9
  209. sMessage = sMessage..(p1-1)
  210. end--]]
  211. end
  212.  
  213. if event == "key" and p1 == 28 then term.setCursorBlink(false); return sMessage end-- Enter key; end the loop and let's see the string
  214.  
  215. if event == "key" and p1 == 14 then -- Backspace...
  216. sMessage = string.sub(sMessage, 1, string.len(sMessage)-1)
  217. -- Set our message to the substring of the first charcter
  218. -- To the length of the message minus 1 to achieve one less letter.
  219.  
  220. term.setCursorPos(originalCursorX, cursorY)
  221. term.write(sMessage.." ") -- Adding this empty space clears the removed character from the line, but may pose future issues
  222. end
  223.  
  224. term.setCursorPos(originalCursorX, cursorY); term.write(string.rep(" ", string.len(sMessage)))
  225. -- Clear the previous line just in case we get a new line
  226. -- Now write the message on the new line
  227. if _ReplaceChar ~= nil then
  228. term.setCursorPos(originalCursorX, cursorY)
  229. term.write(string.rep( _ReplaceChar, string.len(sMessage) ))
  230. else
  231. term.setCursorPos(originalCursorX, cursorY)
  232. term.write(sMessage)
  233. end
  234. end
  235. end
  236. ----------------------
  237.  
  238.  
  239. -- System Functions --
  240. function checkInstalltion()
  241. if fs.exists("SYSTEM/.installed") then return true
  242. else return false end
  243. end
  244.  
  245. function runShell() shell.run("/rom/programs/shell") end
  246.  
  247. function getVersion() return VERSION end
  248.  
  249. function checkVersion()
  250. local sCommand = "get"
  251. if sCommand == "get" then
  252. local sCode = "c17Hgq1A"
  253. local sPath = ".tempVersion"
  254.  
  255. while true do
  256. local response = http.get(
  257. "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  258. )
  259.  
  260. if response then
  261. local sResponse = response.readAll()
  262. response.close()
  263. if tonumber(sResponse) > VERSION then return true
  264. else return false end
  265. break
  266. else return false end
  267. end
  268. end
  269. end
  270.  
  271. function updateSelf()
  272. local sCommand = "get"
  273. if sCommand == "get" then
  274. local sCode = "6a92bGGZ"
  275. local sFile = "startup"
  276. local sPath = shell.resolve( sFile )
  277.  
  278. while true do
  279. local response = http.get(
  280. "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  281. )
  282.  
  283. if response then
  284. local sResponse = response.readAll()
  285. response.close()
  286.  
  287. local file = fs.open( sPath, "w" )
  288. file.write( sResponse )
  289. file.close()
  290. break
  291. else return false end
  292. end
  293. end
  294. end
  295.  
  296. function checkDirs()
  297. if not fs.isDir("Mail") then shell.run("rom/programs/mkdir", "Mail"); nMailCount = (#(fs.list("/Mail"))) end
  298. if not fs.isDir("User") then shell.run("rom/programs/mkdir", "User"); shell.run("rom/programs/mkdir", "User/"..tostring(os.getComputerID())) end
  299. if not fs.isDir("User/"..tostring(os.getComputerID()).."/Projects") then shell.run("rom/programs/mkdir", "User/"..tostring(os.getComputerID()).."/Projects") end
  300. if not fs.isDir("User/"..tostring(os.getComputerID()).."/Desktop") then shell.run("rom/programs/mkdir", "User/"..tostring(os.getComputerID()).."/Desktop") end
  301. if not fs.isDir("User/"..tostring(os.getComputerID()).."/Desktop/Shortcuts") then shell.run("rom/programs/mkdir", "User/"..tostring(os.getComputerID()).."/Desktop/Shortcuts") end
  302. if not fs.isDir("User/"..tostring(os.getComputerID()).."/Files") then shell.run("rom/programs/mkdir", "User/"..tostring(os.getComputerID()).."/Files") end
  303.  
  304. if not fs.isDir("Mail/Contacts") then shell.run("rom/programs/mkdir", "Mail/Contacts") end
  305. if not fs.isDir("SYSTEM") then shell.run("rom/programs/mkdir", "SYSTEM") end
  306. if not fs.exists("SYSTEM/Version") then
  307. local file = fs.open("SYSTEM/Version", "w"); file.writeLine(tostring(VERSION))
  308. file.close()
  309. end
  310. file = fs.open("SYSTEM/.installed", "w")
  311. file.writeLine("TRUE")
  312. file.close()
  313. end
  314.  
  315.  
  316. function checkUserInformation()
  317. if fs.exists("User/"..os.getComputerID().."/.info") then
  318. local file = fs.open("User/"..os.getComputerID().."/.info", "r")
  319. sUserName = file.readLine()
  320. sPassword = file.readLine()
  321. file.close()
  322. bNewUser = false
  323. else
  324. bNewUser = true
  325. end
  326. end
  327.  
  328. function getUserInformation()
  329. if bNewUser == false then return nil -- This is to prevent accidental account resets
  330. else
  331. printNewUserScreen()
  332. end
  333. end
  334.  
  335. function handleLogin()
  336. if fs.exists(".login") then
  337. file = fs.open(".login", "r")
  338. fileContents = file.readAll()
  339. file.close()
  340. fs.delete(".login")
  341. bLogin = false
  342.  
  343. if fileContents == "false" then return end
  344. end
  345.  
  346. if bNewUser == false then
  347. printMainScreen(); sleep(2)
  348. clear()
  349.  
  350. printBorder()
  351. cPrint(4, "***************************")
  352. cPrint(5, "* Identification required *")
  353. cPrint(6, "***************************")
  354.  
  355. cPrint(8, "Username: ")
  356. cPrint(9, "Password: ")
  357. rPrint(17, "Cannon OS Version: "..VERSION.."*")
  358. rPrint(16, "LOCKED*")
  359.  
  360. term.setCursorPos(screenWidth/2 - string.len("Username: ")/2 + string.len("Username: "), 8)
  361. local tempUserName = newRead(14, nil)
  362. term.setCursorPos(screenWidth/2 - string.len("Password: ")/2 + string.len("Password: "), 9)
  363. local tempPassword = newRead(14, "*")
  364.  
  365. if tempUserName == sUserName and tempPassword == sPassword then
  366. rPrint(16, "UNLOCKED*")
  367. cPrint(11, "**********************")
  368. cPrint(12, "* Successfull login! *")
  369. cPrint(13, "**********************")
  370. sleep(2)
  371. return
  372. else
  373. cPrint(11, "************************")
  374. cPrint(12, "* Unsuccessfull login! *")
  375. cPrint(13, "************************")
  376. sleep(2)
  377. os.shutdown()
  378. end
  379. end
  380. end
  381.  
  382. function printInstallation()
  383. if checkInstalltion() then return end
  384. printBorder()
  385. cPrint(4, "*************************")
  386. cPrint(5, "* Installation Progress *")
  387. cPrint(6, "*************************")
  388.  
  389. local progressPosition = 6; -- X position to represent where the progress bars will be drawn
  390. local nProgressPercent = 0;
  391.  
  392. cPrint(8, " * "..string.rep("*", screenWidth - 6))
  393. cPrint(9, " * *"..string.rep(" ", screenWidth - 8).."*")
  394. cPrint(10, " * "..string.rep("*", screenWidth - 6))
  395.  
  396. cPrint(12, "********")
  397. cPrint(13, "* "..string.rep(" ", string.len("100%")).." *")
  398. cPrint(14, "********")
  399.  
  400. for i=1, 80 do
  401. if i%2 == 0 then
  402. term.setCursorPos(progressPosition, 9); term.write("|")
  403. progressPosition = progressPosition+1
  404. sleep(0.2)
  405. end
  406. nProgressPercent = nProgressPercent+1
  407. if nProgressPercent == 75 then nProgressPercent = 95 end
  408. if nProgressPercent == 95 then sleep(1.1) end
  409. term.setCursorPos(24, 13); term.write(nProgressPercent)
  410. end
  411. checkDirs()
  412. end
  413.  
  414. -- Background Processes --
  415. function checkMail(sender, message)
  416. if string.find(message, "CANNONMAILMESSAGE") then
  417. local file = fs.open("Mail/Unread"..nMailCount, "w")
  418. message = string.gsub(message, "CANNONMAILMESSAGE", "")
  419. file.write(tostring(sender).."n"..tostring(message))
  420. file.close()
  421.  
  422. nMailCount = nMailCount+1
  423. end
  424. end
  425. --------------------------
  426. ----------------------
  427.  
  428.  
  429. -- Menues and other screens --
  430. function printNewUserScreen()
  431. if bNewUser == false then return end
  432. local bPasswordsMatch = false
  433.  
  434. printBorder()
  435. printWelcome()
  436. sleep(3)
  437. clear()
  438.  
  439. printBorder()
  440. cPrint(2, "* New User *")
  441. cPrint(3, "************")
  442.  
  443. rPrint(17, "Cannon OS Version: "..VERSION.."*")
  444.  
  445. cPrint(6, "Username: ")
  446. cPrint(7, "Password: ")
  447. cPrint(8, " Confirm: ")
  448. cPrint(12, "************************")
  449. cPrint(13, "*Password not optional!*")
  450. cPrint(14, "************************")
  451.  
  452. term.setCursorPos(screenWidth/2-string.len("Username: ")/2+string.len("Username: "), 6)
  453. sUserName = newRead(14, nil)
  454. if string.len(sUserName) > 14 then
  455. cPrint(10, "Username is too long!"); sleep(1.3)
  456. term.setCursorPos(1, 10); term.clearLine() -- Clear the line 10
  457. term.write(" *"..string.rep(" ", screenWidth-4).."*")
  458.  
  459. term.setCursorPos(1, 6); term.clearLine() -- Clear the line
  460. term.write(" *"..string.rep(" ", screenWidth-4).."*")
  461. cPrint(6, "Username: ")
  462. term.setCursorPos(screenWidth/2-string.len("Username: ")/2+string.len("Username: "), 6)
  463. sUserName = newRead(14, nil)
  464. end
  465. while bPasswordsMatch == false do
  466. term.setCursorPos(screenWidth/2-string.len("Password: ")/2+string.len("Password: "), 7)
  467. sPassword = newRead(14, "*")
  468. if sPassword ~= nil and sPassword ~= "" then
  469. term.setCursorPos(screenWidth/2-string.len(" Confirm: ")/2+string.len(" Confirm: "), 8)
  470. local sConfirm = newRead(14, "*")
  471. if sConfirm ~= sPassword then
  472. cPrint(10, "Passwords do not match!"); sleep(1.3)
  473. term.setCursorPos(1, 10); term.clearLine() -- Clear the line 10
  474. term.write(" *"..string.rep(" ", screenWidth-4).."*")
  475.  
  476. term.setCursorPos(1, 7); term.clearLine() -- Clear the line 7
  477. term.write(" *"..string.rep(" ", screenWidth-4).."*")
  478. cPrint(7, "Password: ")
  479.  
  480. term.setCursorPos(1, 8); term.clearLine() -- Clear the line 8
  481. term.write(" *"..string.rep(" ", screenWidth-4).."*")
  482. cPrint(8, " Confirm: ")
  483. else sPassword = sConfirm; bPasswordsMatch = true end
  484. else sPassword = ""
  485. end
  486. end
  487.  
  488. local file = fs.open("User/"..os.getComputerID().."/.info", "w")
  489. file.writeLine(tostring(sUserName))
  490. if sPassword == "" then file.writeLine("N/A")
  491. else file.writeLine(tostring(sPassword))
  492. end
  493. file.close()
  494.  
  495. cPrint(10, "Account '"..sUserName.. "' succesfully created!"); sleep(1.3)
  496. end
  497.  
  498. function printWelcome()
  499. customWrite(3, "__ __ _ ")
  500. customWrite(4, " / / | | ")
  501. customWrite(5, " / / /___| | ___ ___ _ __ ___ ___ ")
  502. customWrite(6, " / / // _ |/ __|/ _ | '_ ' _ / _ ")
  503. customWrite(7, " / /| __/ | (__| (_) | | | | | | __/")
  504. customWrite(8, " / / ___|_|___|___/|_| |_| |_|___|")
  505. customWrite(9, " _ _ ")
  506. customWrite(10, " | | | | ")
  507. customWrite(11, " | | | |___ ___ _ __ ")
  508. customWrite(12, " | | | / __|/ _ '__|")
  509. customWrite(13, " | |__| __ __/ | ")
  510. customWrite(14, " ____/|___/___|_| ")
  511.  
  512. rPrint(17, "Cannon OS Version: "..VERSION.."*")
  513. end
  514.  
  515. function printMainScreen()
  516. printBorder()
  517. customWrite(2, " ________")
  518. customWrite(3, " | ______o|")
  519. customWrite(4, " _______________ ||__---_||")
  520. customWrite(5, " | ___________ || ______ |")
  521. customWrite(6, " | | | |||______||")
  522. customWrite(7, " | | Cannon OS | ||--------|")
  523. customWrite(8, " | | By | || O |")
  524. customWrite(9, " | | Payment | || | |")
  525. customWrite(10, " | '-----------' || | |")
  526. customWrite(11, " |_____________-_|| | |")
  527. customWrite(12, " __/_________ |::::::::|")
  528. customWrite(13, " ________________'-.__")
  529. customWrite(14, " /:::::::::':::':: .---.")
  530. customWrite(15, " /::======::: .:.::: _) ")
  531. customWrite(16, ' """""""""""""""""""" -----')
  532.  
  533. term.setCursorPos(3, 17); term.write("By PaymentOption")
  534. rPrint(17, "Version: "..VERSION.. "*")
  535. end
  536.  
  537. function printLogo()
  538. term.setCursorPos(34, 3); term.write("__")
  539. customWrite(4, " / ")
  540. customWrite(5, " .-. | |")
  541. customWrite(6, " * _.-' __/")
  542. customWrite(7, " .-' ")
  543. customWrite(8, " / _/")
  544. customWrite(9, " | _ /")
  545. customWrite(10, " | /_'")
  546. customWrite(11, " _____/")
  547. end
  548.  
  549. function printLogout()
  550. term.setCursorPos(34, 3); term.write(" ")
  551.  
  552. clearCenter()
  553.  
  554. cPrint(5, "********")
  555. cPrint(6, "*Logout*")
  556. cPrint(7, "********")
  557. cPrint(8, "/ ")
  558. cPrint(9, "***************")
  559. cPrint(10, "*[ Yes ] No *")
  560. cPrint(11, "***************")
  561.  
  562. while true do
  563. event, key = os.pullEvent("key")
  564.  
  565. if key == 205 and selection < 2 then selection = 2
  566. elseif key == 203 and selection > 1 then selection = 1
  567.  
  568. elseif key == 28 and selection == 1 then os.reboot()
  569. elseif key == 28 and selection == 2 then sMenuState = "main"; selection = 6; break
  570. end
  571.  
  572. if selection == 1 then cPrint(10, "*[ Yes ] No *")
  573. else cPrint(10, "* Yes [ No ]*") end
  574. end
  575. end
  576.  
  577. function printMainMenu()
  578. printBorder()
  579. cPrint(2, "* "..sUserName.." *")
  580. cPrint(3, "*"..string.rep("*", string.len(sUserName)+2).."*")
  581. rPrint(17, "Cannon OS Version: "..VERSION.. "*")
  582.  
  583. if sMenuState == "main" then printLogo(); rPrint(2, "Mail: "..nMailCount.."*") end
  584.  
  585. if selection == 1 then cPrint(13, "[ Computer ] Craft OS IRC ")
  586. cPrint(15, " E-Mail Account Update ")
  587. end
  588.  
  589. if selection == 2 then cPrint(13, " Computer [ Craft OS ] IRC ")
  590. cPrint(15, " E-Mail Account Update ")
  591. end
  592.  
  593. if selection == 3 then cPrint(13, " Computer Craft OS [ IRC ] ")
  594. cPrint(15, " E-Mail Account Update ")
  595. end
  596.  
  597. if selection == 4 then cPrint(13, " Computer Craft OS IRC ")
  598. cPrint(15, "[ E-Mail ] Account Update ")
  599. end
  600.  
  601. if selection == 5 then cPrint(13, " Computer Craft OS IRC ")
  602. cPrint(15, " E-Mail [ Account ] Update ")
  603. end
  604.  
  605. if selection == 6 then cPrint(13, " Computer Craft OS IRC ")
  606. cPrint(15, " E-Mail Account [ Update ] ")
  607. end
  608. end
  609. ------------------------------
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622. -- Mail related things --
  623. function loadContacts()
  624. local tContacts = fs.list("Mail/Contacts")
  625. for i=1, #tContacts do -- Set the correct computer adress found
  626. local file = fs.open("Mail/Contacts/"..tContacts[i], "r")
  627. local fileContents = file.readAll()
  628. file.close()
  629. tContacts[i] = { name = tContacts[i], id = fileContents }
  630. end
  631.  
  632. return tContacts
  633. end
  634.  
  635. function mailMenu()
  636. recipientID = 0
  637. sLine1 = "" -- The message will be split into two lines due to lack of multiline functionality of newread()
  638. sLine2 = ""
  639. sMessage = "" -- The two lines will be combined into the whole message in this variable
  640.  
  641. selection = 1
  642. nMailCount = 0
  643. screenWidth, screenHeight = term.getSize()
  644.  
  645. function cPrint(height, string)
  646. local xPos = screenWidth/2 - string.len(string)/2
  647. term.setCursorPos(xPos, height)
  648. term.write(string)
  649. end
  650.  
  651. function rPrint(height, string)
  652. local xPos = screenWidth - string.len(string)
  653. term.setCursorPos(xPos, height)
  654. term.write(string)
  655. end
  656.  
  657. clearCenter() -- Remove the cannon logo
  658.  
  659. local function sendMessage()
  660. sMessage = ";alkdjien".."n"..sMessage
  661. rednet.send(tonumber(recipientID), sMessage)
  662. end
  663.  
  664. local function printMailMenu()
  665. rPrint(2, "Mail: "..tostring((#(fs.list("Mail"))-1).."*"))
  666.  
  667. cPrint(5, "***********")
  668. if selection == 1 then cPrint(6, "*[ Write ]*")
  669. else cPrint(6, "* Write *") end
  670.  
  671. if selection == 2 then cPrint(7, "*[ eMail ]*")
  672. else cPrint(7, "* eMail *") end
  673.  
  674. if selection == 3 then cPrint(8, "*[ Cont. ]*")
  675. else cPrint(8, "* Cont. *") end
  676.  
  677. if selection == 4 then cPrint(9, "*[ Leave ]*")
  678. else cPrint(9, "* Leave *") end
  679. cPrint(10, "***********")
  680. -- End these damn lines ^^
  681. end
  682.  
  683. local function printRevise()
  684. local nSelect = 1 -- Too much sharing of the 'select' variable
  685.  
  686. local function cDraw(width, height, string)
  687. term.setCursorPos(width, height)
  688. term.write(string)
  689. end
  690.  
  691. local function printRevMenu()
  692. cDraw(29, 5, "*****************")
  693. if nSelect == 1 then cDraw(29, 6, "*[ Send ] Exit *")
  694. else cDraw(29, 6, "* Send [ Exit ]*") end
  695. cDraw(29, 7, "*****************")
  696. end
  697.  
  698. while true do
  699. printRevMenu()
  700. event, key = os.pullEvent("key")
  701. -- Freakin' lines man ^^
  702.  
  703. if key == 205 and nSelect == 1 then nSelect = nSelect+1
  704. elseif key == 203 and nSelect == 2 then nSelect = nSelect-1
  705.  
  706. elseif key == 28 and nSelect == 1 then sendMessage(); clearCenter(); term.setCursorPos(4, 12); term.write(string.rep(" ", 42)); break
  707. elseif key == 28 and nSelect == 2 then clearCenter(); break
  708. end
  709. end
  710. end
  711.  
  712. local function composeMail()
  713. clearCenter()
  714.  
  715. term.setCursorPos(4, 5); term.write("****************************")
  716. term.setCursorPos(4, 6); term.write("*Recipient: *") -- 15 is the x position for the retrieving of 'recipietID'
  717. term.setCursorPos(4, 7); term.write("****************************")
  718.  
  719. term.setCursorPos(4, 9); term.write(string.rep("*", 42))
  720. term.setCursorPos(4, 10); term.write("*"..string.rep(" ", 40).."*") -- 2 lines to write the body of the message; 38 spaces per line
  721. term.setCursorPos(4, 11); term.write("*"..string.rep(" ", 40).."*")
  722. term.setCursorPos(4, 12); term.write(string.rep("*", 42))
  723.  
  724. term.setCursorPos(15, 6) -- Get the recipient ID
  725. recipientID = newRead(14) -- If they entered a contact name then find the id
  726. local tContacts = loadContacts()
  727.  
  728. for i=1, #tContacts do
  729. if tostring(recipientID) == tContacts[i].name then recipientID = tContacts[i].id end
  730. end
  731.  
  732. term.setCursorPos(6, 10); sLine1 = newRead(38) -- Character only filter
  733. term.setCursorPos(6, 11); sLine2 = newRead(38)
  734.  
  735. sMessage = sLine1.." "..sLine2
  736.  
  737. printRevise()
  738. end
  739.  
  740. local function contacts()
  741. local nSelect = 1
  742. local scroll = 0;
  743. local tContacts = fs.list("Mail/Contacts")
  744.  
  745. local function printContacts()
  746. local y = 6
  747. local tContacts = loadContacts()
  748.  
  749. for i = 1+scroll, #tContacts do
  750. term.setCursorPos(5, y)
  751. if nSelect == i then term.write("[ "..tContacts[i].name.." ]")
  752. else term.write(" "..tContacts[i].name.." ") end
  753.  
  754. term.setCursorPos(43, y); term.write(tContacts[i].id)
  755. y = y+1
  756. if y == 11 then break end
  757. end
  758.  
  759. term.setCursorPos(4, 3); term.write("********************")
  760. term.setCursorPos(4, 4); term.write("* Ctrl:New Contact *")
  761. end
  762.  
  763. while true do
  764. clearCenter()
  765. printContainer()
  766. printContacts()
  767.  
  768. local event, key = os.pullEvent("key")
  769. if key == 208 and nSelect < #(fs.list("Mail/Contacts")) then
  770. if nSelect >= 4 and scroll < #tContacts then scroll = scroll+1; nSelect = nSelect+1
  771. else nSelect = nSelect+1 end
  772. elseif key == 200 and nSelect > 1 then
  773. if nSelect >= 4 and scroll > 0 then scroll = scroll-1; nSelect = nSelect-1
  774. else nSelect = nSelect-1 end
  775. elseif key == 29 or key == 157 then
  776. term.setCursorPos(5, 6); term.write(string.rep(" ", screenWidth-8))
  777. term.setCursorPos(5, 6); term.write("Contact Name: ")
  778. local sContact = newRead(14)
  779.  
  780. term.setCursorPos(5, 6); term.write(string.rep(" ", screenWidth-8))
  781. term.setCursorPos(5, 6); term.write("Contact ID: ")
  782. local nContactID = newRead(14)
  783.  
  784. local file = fs.open("Mail/Contacts/"..sContact, "w")
  785. file.write(tostring(nContactID))
  786. file.close()
  787. elseif key == 14 then break
  788.  
  789. elseif key == 28 then
  790. nSelected = 1
  791. while true do
  792. if nSelected == 1 then
  793. term.setCursorPos(5, 6)
  794. term.write("Delete? [ Yes ] No ")
  795. else
  796. term.setCursorPos(5, 6)
  797. term.write("Delete? Yes [ No ]")
  798. end
  799.  
  800. local event, key = os.pullEvent("key")
  801.  
  802. if key == 205 and nSelected == 1 then nSelected = nSelected+1
  803. elseif key == 203 and nSelected == 2 then nSelected = nSelected-1
  804.  
  805. elseif key == 28 and nSelected == 1 then
  806. local tContacts = loadContacts()
  807.  
  808. for i=1, #tContacts do
  809. if nSelected == i then fs.delete("Mail/Contacts/"..tContacts[i].name) end
  810. end
  811. break
  812. elseif key == 28 and nSelected == 2 then break
  813. end
  814. end
  815. end
  816. end
  817. end
  818.  
  819. while true do
  820. printMailMenu()
  821.  
  822. event, key = os.pullEvent("key")
  823.  
  824. if key == 200 and selection > 1 then selection = selection-1
  825. elseif key == 208 and selection < 4 then selection = selection+1
  826.  
  827. elseif key == 28 and selection == 1 then composeMail(); sMenuState = "main"; break
  828. elseif key == 28 and selection == 2 then readMail(); sMenuState = "main"; break
  829. elseif key == 28 and selection == 3 then contacts(); selection = 1; term.setCursorPos(4, 3); term.write(string.rep(" ", 17)); clearCenter();
  830. elseif key == 28 and selection == 4 then selection = 4; sMenuState = "main"; break
  831. end
  832. end
  833. end
  834.  
  835. function newMessage(nSender, sContents)
  836. rPrint(2, "Mail: "..nMailCount.."*")
  837. sMenuState = "newMessage"
  838. local nSelect = 1
  839. local bReading = false -- This is to handle whether or not we should print the reception menu
  840. clearCenter()
  841.  
  842. local function printMessageMenu()
  843. cPrint(5, "*****************")
  844. cPrint(6, "* New Message *")
  845. cPrint(7, "*****************")
  846. if nSelect == 1 then cPrint(8, "*[ Read ] Exit *")
  847. else cPrint(8, "* Read [ Exit ]*") end
  848. cPrint(9, "*****************")
  849. end
  850.  
  851. while true do
  852. if not bReading then printMessageMenu() end
  853. local event, key = os.pullEvent("key")
  854.  
  855. if key == 205 then nSelect = nSelect+1
  856. elseif key == 203 then nSelect = nSelect-1
  857.  
  858. elseif key == 28 and nSelect == 1 then
  859. nMailCount = nMailCount-1
  860. bReading = true
  861. clearCenter()
  862. term.setCursorPos(4, 5); term.write("From: "..nSender)
  863. term.setCursorPos(4, 6); term.write("******"..string.rep("*", string.len(nSender)))
  864.  
  865. term.setCursorPos(4, 7)
  866. if string.len(sContents) > 42 then
  867. local sContentsLine1 = string.sub(sContents, 1, 42) -- Split the message into two lines if it is greater than 38
  868. local sContentsLine2 = string.sub(sContents, 43, string.len(sContents))
  869.  
  870. term.write(sContentsLine1)
  871. term.setCursorPos(4, 9)
  872. term.write(sContentsLine2)
  873. else
  874. term.write(sContents)
  875. end
  876. break -- Print the message then end
  877. elseif key == 28 and nSelect == 2 then
  878. clearCenter()
  879. sMenuState = "main"
  880. return
  881. end
  882. end
  883.  
  884. while true do
  885. cPrint(9, "**********")
  886. cPrint(10, "*[ Exit ]*")
  887. cPrint(11, "**********")
  888.  
  889. local event, key = os.pullEvent("key")
  890.  
  891. if key == 28 then sMenuState = "main"; break end
  892. end
  893. end
  894.  
  895. function readMail()
  896. local sFileOpen = ""
  897. sMenuState = "archive"
  898. clearCenter()
  899.  
  900. if #(fs.list("Mail"))-1 == 0 then
  901. cPrint(6, "***************")
  902. cPrint(7, "* No messages *")
  903. cPrint(8, "***************")
  904. sleep(1.3)
  905. clearCenter()
  906. cPrint(12, " ") -- Clear line 12
  907. return
  908. end
  909.  
  910. term.setCursorPos(4, 5); term.write(string.rep("*", screenWidth-6))
  911. local y = 6
  912. for i=1, screenHeight - 13 do
  913. term.setCursorPos(4, y); term.write("*"..string.rep(" ", screenWidth-8).."*")
  914. y = y+1
  915. end
  916. term.setCursorPos(4, 11); term.write(string.rep("*", screenWidth-6))
  917.  
  918. local tMessages = fs.list("Mail")
  919. local nSelections = #tMessages -- How many selections for mail we have
  920. local nSelected = 1
  921.  
  922. local function clearMessageRectangle()
  923. local y = 6
  924. for i=1, 5 do
  925. term.setCursorPos(5, y); term.write(string.rep(" ", screenWidth - 8))
  926. y = y+1
  927. end
  928. end
  929.  
  930. local function printMessages()
  931. term.setCursorPos(5, 13); term.write(string.rep(" ", screenWidth - (string.len("*********************")+2)))
  932. term.setCursorPos(screenWidth - (string.len("*********************")+2), 13)
  933. term.write("*********************")
  934. term.setCursorPos(screenWidth - (string.len("* Backspace to exit *")+2), 12)
  935. term.write("* Backspace to exit *")
  936.  
  937. tMessages = fs.list("Mail")
  938. for i=1, #tMessages do
  939. if tMessages[i] == "Contacts" then table.remove(tMessages, i) end
  940. end -- Remove the 'Contacts' directory from the list
  941.  
  942. term.setCursorPos(4, 5); term.write(string.rep("*", screenWidth-6))
  943. local y = 6
  944. for i=1, screenHeight - 13 do
  945. term.setCursorPos(4, y); term.write("*"..string.rep(" ", screenWidth-8).."*")
  946. y = y+1
  947. end
  948. term.setCursorPos(4, 11); term.write(string.rep("*", screenWidth-6))
  949.  
  950.  
  951. local recX = 6
  952. local recY = 6 -- Positions to hold the x and y coords of where we'll draw the mail
  953. for i=1, #tMessages do
  954. if recX > 41 or recX+string.len(tMessages[i]) > 41 then
  955. recY = recY+2
  956. recX = 6
  957. end
  958. if recY >= 10 then break end
  959.  
  960. if nSelected == i then recX = recX-1 end
  961. term.setCursorPos(recX, recY)
  962. if nSelected == i then term.write("["..tMessages[i].."]"); recX = recX+1 end
  963. if nSelected ~= i then term.write(tMessages[i]) end
  964.  
  965. if nSelected == i then
  966. recX = recX + string.len(tMessages[i]) + 2 -- The 2 is for the spaces between the options
  967. else recX = recX + string.len(tMessages[i]) + 2 end
  968. end
  969. end
  970.  
  971. while true do
  972. cPrint(12, " ") -- Clear the last line from the 'return' or 'delete' menu from reading a message
  973. printMessages()
  974.  
  975. local event, key = os.pullEvent("key")
  976.  
  977. if key == 205 and nSelected < #tMessages then nSelected = nSelected+1
  978. elseif key == 203 and nSelected > 1 then nSelected = nSelected-1
  979.  
  980. elseif key == 14 then clearCenter(); term.setCursorPos(5, 12); term.write(string.rep(" ", screenWidth - 7)); term.setCursorPos(5, 13); term.write(string.rep(" ", screenWidth - 7)); cPrint(13, " Computer Craft OS File Browser "); return
  981. elseif key == 28 then
  982. clearCenter()
  983. local sFileOpen = "Mail/"..tMessages[nSelected]
  984. local file = fs.open("Mail/"..tMessages[nSelected], "r")
  985. local nSender = file.readLine()
  986. local sMessage = file.readAll(); -- We'll be using the File Contents as the message to read
  987. file.close()
  988.  
  989. sMessage = string.gsub(sMessage, tostring(nSender), "") -- Get the message string without the sender
  990. if string.len(sMessage) > 38 then
  991. local sMessageLine1 = string.sub(sMessage, 1, 19)
  992. local sMessageLine2 = string.sub(sMessage, 20, 38)
  993. end
  994.  
  995. term.setCursorPos(4, 3); term.write("From: "..nSender)
  996. term.setCursorPos(4, 4); term.write("******"..string.rep("*", string.len(tostring(nSender))))
  997. term.setCursorPos(4, 5)
  998. if sMessageLine1 ~= nil then
  999. term.write(sMessageLine1)
  1000. term.setCursorPos(4, 6)
  1001. term.write(sMessageLine2)
  1002. else
  1003. term.write(sMessage)
  1004. end
  1005.  
  1006. nSelected = 1 -- Reset the selection for the 'back' or 'delete' option
  1007. term.setCursorPos(5, 12); term.write(string.rep(" ", screenWidth - 7)); term.setCursorPos(5, 13); term.write(string.rep(" ", screenWidth - 7))
  1008. while true do
  1009. cPrint(10, "*********************")
  1010. if nSelected == 1 then cPrint(11, "*[ Return ] Delete *")
  1011. else cPrint(11, "* Return [ Delete ]*") end
  1012. cPrint(12, "*********************")
  1013. --
  1014.  
  1015. local event, key = os.pullEvent("key")
  1016.  
  1017. if key == 205 and nSelected == 1 then nSelected = 2
  1018. elseif key == 203 and nSelected == 2 then nSelected = 1
  1019.  
  1020. elseif key == 28 and nSelected == 1 then term.setCursorPos(4, 3); term.write(string.rep(" ", string.len("From: "..nSender))); term.setCursorPos(4, 4); term.write(string.rep(" ", string.len("From: "..nSender))) break
  1021. elseif key == 28 and nSelected == 2 then
  1022. fs.delete(sFileOpen)
  1023. sFileOpened = ""
  1024. nSelected = 1
  1025. term.setCursorPos(4, 3); term.write(string.rep(" ", string.len("From: "..nSender)))
  1026. term.setCursorPos(4, 4); term.write(string.rep(" ", string.len("From: "..nSender)))
  1027. break
  1028. end
  1029. end
  1030. end
  1031. end
  1032. end
  1033. -------------------------
  1034.  
  1035.  
  1036. -- Filbrowser --
  1037. function fileBrowser()
  1038. sMenuState = "browseing"
  1039. clearCenter()
  1040.  
  1041. local scroll = 0
  1042. local y = 0
  1043. local sObjectName = ""
  1044. local sObjectType = ""
  1045. local nObjectHeight = 0
  1046.  
  1047. local nSelect = 1
  1048. local tFiles = {}
  1049.  
  1050. local function printOptionMenu()
  1051. local previousSelection = nSelect
  1052. nSelect = 1
  1053.  
  1054. while true do
  1055. term.setCursorPos(6, 8); term.write(string.rep(" ", 40))
  1056. if nSelect == 1 and sObjectType == "File" then cPrint(8, "[Run] Edit Delete Exit ")
  1057. elseif nSelect == 2 and sObjectType == "File" then cPrint(8, " Run [Edit] Delete Exit ")
  1058. elseif nSelect == 3 and sObjectType == "File" then cPrint(8, " Run Edit [Delete] Exit ")
  1059. elseif nSelect == 4 and sObjectType == "File" then cPrint(8, " Run Edit Delete [Exit]")
  1060. end
  1061.  
  1062. local event, key = os.pullEvent("key")
  1063.  
  1064. if key == 205 and nSelect < 4 then nSelect = nSelect+1
  1065. elseif key == 203 and nSelect > 1 then nSelect = nSelect-1
  1066. elseif key == 14 or key == 28 and nSelect == 4 then nSelect = previousSelection; return false
  1067. elseif nSelect == 1 and key == 28 then clear(); shell.run(sObjectName); sMenuState = "main"; return true
  1068. elseif nSelect == 2 and key == 28 then clear(); shell.run("edit", sObjectName); sMenuState = "main"; return true
  1069. elseif nSelect == 3 and key == 28 then fs.delete(shell.dir().."/"..sObjectName); sMenuState = "main"; return true
  1070. end
  1071. end
  1072. end
  1073.  
  1074. local function printFileContainer()
  1075. term.setCursorPos(4, 5); term.write(string.rep("*", screenWidth-6))
  1076. local y = 6
  1077. for i=1, screenHeight - 13 do
  1078. term.setCursorPos(4, y); term.write("*"..string.rep(" ", screenWidth-8).."*")
  1079. y = y+1
  1080. end
  1081. term.setCursorPos(4, 11); term.write(string.rep("*", screenWidth-6))
  1082. end
  1083.  
  1084. local function printFiles()
  1085. local sDir = shell.dir()
  1086. clearCenter()
  1087. printFileContainer() -- This is the rectangle we'll be displaying the options in
  1088. y = 6
  1089. tFiles = fs.list( sDir )
  1090.  
  1091.  
  1092. for i=1+scroll, #tFiles do
  1093. if i == nSelect then nObjectHeight = y end
  1094. sObjectName = tostring(shell.dir()).."/"..tFiles[nSelect]
  1095. if fs.isDir(tostring(shell.dir()).."/"..tFiles[nSelect]) then sObjectType = "Dir"
  1096. else sObjectType = "File"
  1097. end
  1098.  
  1099. term.setCursorPos(6, y)
  1100.  
  1101. if nSelect == i then term.write("["..tFiles[i].."]")
  1102. else term.write(" "..tFiles[i].." ") end
  1103.  
  1104. if fs.isDir(tostring(shell.dir()).."/"..tFiles[i]) then term.setCursorPos(42, y); term.write("D ")
  1105. else term.setCursorPos(42, y); term.write("F ") end
  1106.  
  1107. y = y+1
  1108. if y == 11 then break end
  1109. end
  1110. end
  1111.  
  1112. while true do
  1113. printFiles()
  1114.  
  1115. local event, key = os.pullEvent("key")
  1116.  
  1117. if key == 200 and nSelect == 1 then nSelect = 1
  1118. elseif key == 208 and nSelect == #tFiles then nSelect = #tFiles
  1119.  
  1120. elseif key == 208 and nSelect < #tFiles then
  1121. if nSelect >= 5 and scroll < #tFiles then scroll = scroll+1; nSelect = nSelect+1
  1122. else nSelect = nSelect+1 end
  1123. elseif key == 200 and nSelect > 1 then
  1124. if nSelect >= 5 and scroll > 0 then scroll = scroll-1; nSelect = nSelect-1
  1125. else nSelect = nSelect-1 end
  1126.  
  1127. elseif key == 28 and sObjectType == "File" then
  1128. if printOptionMenu() then return end
  1129. elseif key == 28 and sObjectType == "Dir" then shell.setDir(sObjectName); scroll = 0; nSelect = 1
  1130. elseif key == 14 and shell.dir() ~= "" then shell.run("cd", ".."); scroll = 0; nSelect = 1
  1131. elseif key == 14 and shell.dir() == "/" or shell.dir() == "" then sMenuState = "main"; break
  1132. end
  1133. end
  1134. end
  1135. ----------------
  1136.  
  1137.  
  1138. -- IRC Client --
  1139. function IRC_Client()
  1140. sMenuState = "irc"
  1141.  
  1142. -- VARS --
  1143. local sServerName = ""
  1144. local nServerID = 0
  1145. local bConnected = false
  1146.  
  1147. local sUserName = ""
  1148. local sText = ""
  1149.  
  1150. nConversationY = 3
  1151. ----------
  1152.  
  1153. -- Helper local functions --
  1154. local function newReadIRC( _charLimit, _ReplaceChar) -- The filter can either be a 1, 0, or nil. 1 is Numbers only, 0 is letters only, and nil is both
  1155. local sMessage = ""
  1156. term.setCursorBlink(true)
  1157. local originalCursorX, originalCursorY = term.getCursorPos()
  1158.  
  1159. while true do
  1160. local cursorX, cursorY = term.getCursorPos()
  1161. local event, p1, p2 = os.pullEvent()
  1162.  
  1163. if _charLimit ~= nil then
  1164. if string.len(sMessage) == _charLimit then term.setCursorBlink(false); return sMessage end
  1165. end
  1166.  
  1167. if event == "char" then sMessage = sMessage..p1
  1168. --[[elseif event == "key" then -- The escape key has keyID 1 so: 0 = keyID: 11, 1 = keyID: 2, 2 = keyID : 3 etc etc....
  1169. if p1 == 11 or p1 == 2 or p1 == 3 or p1 == 4 or p1 == 5 or p1 == 6 or p1 == 7 or p1 == 8 or p1 == 9 or p1 == 10 then
  1170. -- 0 1 2 3 4 5 6 7 8 9
  1171. sMessage = sMessage..(p1-1)
  1172. end--]]
  1173. end
  1174.  
  1175. if event == "key" and p1 == 28 then
  1176. term.setCursorBlink(false)
  1177. term.setCursorPos(1, screenHeight)
  1178. term.write(string.rep(" ", screenWidth))
  1179. return sMessage
  1180. end-- Enter key; end the loop and let's see the string
  1181.  
  1182. if event == "key" and p1 == 14 then -- Backspace...
  1183. sMessage = string.sub(sMessage, 1, string.len(sMessage)-1)
  1184. -- Set our message to the substring of the first charcter
  1185. -- To the length of the message minus 1 to achieve one less letter.
  1186.  
  1187. term.setCursorPos(originalCursorX, cursorY)
  1188. term.write(sMessage.." ") -- Adding this empty space clears the removed character from the line, but may pose future issues
  1189. end
  1190.  
  1191. if event == "rednet_message" and p1 == nServerID then
  1192. if nConversationY == 10 then
  1193. for i=3, 14 do
  1194. term.setCursorPos(3, i); term.write(string.rep(" ", screenWidth-4))
  1195. end
  1196.  
  1197. term.setCursorPos(3, 2); term.write(sServerName..":")
  1198. term.setCursorPos(5, screenHeight-1)
  1199. nConversationY = 3
  1200. end
  1201.  
  1202. if p1 == nServerID and p2 == "dlskjfiejdlijfklsdj" then bConnected = false; return nil end
  1203. term.setCursorPos(3, nConversationY); term.write(p2)
  1204. nConversationY = nConversationY+1
  1205. term.setCursorPos(string.len(sMessage)+6, screenHeight)
  1206. end
  1207.  
  1208. term.setCursorPos(originalCursorX, cursorY); term.write(string.rep(" ", string.len(sMessage)))
  1209. -- Clear the previous line just in case we get a new line
  1210. -- Now write the message on the new line
  1211. if _ReplaceChar ~= nil then
  1212. term.setCursorPos(originalCursorX, cursorY)
  1213. term.write(string.rep( _ReplaceChar, string.len(sMessage) ))
  1214. else
  1215. term.setCursorPos(originalCursorX, cursorY)
  1216. term.write(sMessage)
  1217. end
  1218. end
  1219. end
  1220. ----------------------
  1221.  
  1222. -- Input related local functions --
  1223. local function getInput()
  1224. cPrint(4, "IRC Version: Beta 1.0")
  1225. cPrint(5, "Server ID: ")
  1226. cPrint(6, " Username: ")
  1227.  
  1228. rPrint(17, "Common channels: 146, 174, 175, 176, 177*")
  1229.  
  1230. term.setCursorPos(screenWidth/2 - string.len("Server ID: ")/2 + string.len("Server ID: "), 5)
  1231. nServerID = tonumber(newRead(5))
  1232.  
  1233. term.setCursorPos(screenWidth/2 - string.len(" Username: ")/2 + string.len(" Username: "), 6)
  1234. sUserName = newRead(14)
  1235. end
  1236. -----------------------------
  1237.  
  1238. -- Networking related local functions --
  1239. local function connectToServer()
  1240. if bConnected == true then return nil end
  1241.  
  1242. if nServerID ~= nil then
  1243. rednet.send(tonumber(nServerID), "dlsk;fje3ijoidsjfl")
  1244.  
  1245. sender, message = rednet.receive(0.2)
  1246.  
  1247. if sender == nServerID and message == "a;sodifu39798d0f3" then
  1248. rednet.send(tonumber(nServerID), tostring(sUserName))
  1249.  
  1250. sender, message = rednet.receive(0.1)
  1251.  
  1252. if sender == nServerID and message == "ifjdslfije" then
  1253. bConnected = true
  1254.  
  1255. sender, message = rednet.receive(0.2)
  1256.  
  1257. if sender == nServerID then sServerName = message end
  1258. end
  1259. end
  1260. else os.shutdown() end
  1261. end
  1262.  
  1263. local function sendMessage( _sMessage )
  1264. rednet.send( tonumber(nServerID), _sMessage )
  1265. end
  1266. ----------------------------------
  1267.  
  1268. printBorder()
  1269. getInput()
  1270. connectToServer()
  1271. clear()
  1272. printBorder()
  1273. term.setCursorPos(3, 2); term.write(sServerName..":")
  1274. term.setCursorPos(3, screenHeight-1); term.write("> ")
  1275.  
  1276. while bConnected do
  1277. if nConversationY == 10 then
  1278. for i=3, 14 do
  1279. term.setCursorPos(3, i); term.write(string.rep(" ", screenWidth-4))
  1280. end
  1281.  
  1282. term.setCursorPos(3, 2); term.write(sServerName..":")
  1283. term.setCursorPos(5, screenHeight-1)
  1284. nConversationY = 3
  1285. end
  1286.  
  1287. sText = newReadIRC(screenWidth-7) -- This will be our disconnection message: dslifj
  1288. if sText == "!exit" then clear(); sMenuState = "main"; break end
  1289. if sText == nil then term.setCursorBlink(false); sMenuState = "main"; break end
  1290.  
  1291. if sText ~= sUserName then sendMessage(sText)
  1292. else nConversationY = nConversationY-1 end
  1293. sText = ""
  1294. term.setCursorPos(3, screenHeight-1); term.write(string.rep(" ", screenWidth-4))
  1295. term.setCursorPos(3, screenHeight-1); term.write("> ")
  1296.  
  1297. term.setCursorPos(2, screenHeight); term.write(string.rep("*", screenWidth-2))
  1298. term.setCursorPos(5, screenHeight-1)
  1299. end
  1300. clear()
  1301. sMenuState = "main"
  1302. end
  1303. ---------------------
  1304.  
  1305.  
  1306. -- Account --
  1307. function editAccount()
  1308. sMenuState = "account"
  1309. local nSelect = 1
  1310.  
  1311. clearCenter()
  1312. local function printInfoContainer()
  1313. term.setCursorPos(4, 5); term.write(string.rep("*", screenWidth-6))
  1314. local y = 6
  1315. for i=1, screenHeight - 13 do
  1316. term.setCursorPos(4, y); term.write("*"..string.rep(" ", screenWidth-8).."*")
  1317. y = y+1
  1318. end
  1319. term.setCursorPos(4, 11); term.write(string.rep("*", screenWidth-6))
  1320. end
  1321.  
  1322. printInfoContainer()
  1323. while true do
  1324. if nSelect == 3 then sMenuState = "main"; break end
  1325.  
  1326. term.setCursorPos(5, 7); term.write("Username: "..sUserName)
  1327. term.setCursorPos(5, 8); term.write("Password: "..string.rep("*", string.len(sPassword)))
  1328.  
  1329. if nSelect == 1 then term.setCursorPos((screenWidth-6)-(string.len("[ Change ]*")-4), 7); term.write("[ Change ]*")
  1330. else term.setCursorPos((screenWidth-6)-(string.len("[ Change ]*")-4), 7); term.write(" Change *") end
  1331.  
  1332. if nSelect == 2 then term.setCursorPos((screenWidth-6)-(string.len("[ Change ]*")-4), 8); term.write("[ Change ]*")
  1333. else term.setCursorPos((screenWidth-6)-(string.len("[ Change ]*")-4), 8); term.write(" Change *") end
  1334.  
  1335. local event, key = os.pullEvent("key")
  1336.  
  1337. if key == 208 and nSelect == 1 then nSelect = nSelect+1
  1338. elseif key == 200 and nSelect == 2 then nSelect = nSelect-1
  1339.  
  1340. elseif key == 28 and nSelect == 1 then
  1341. term.setCursorPos(5, 7); term.write(string.rep(" ", screenWidth-8))
  1342. term.setCursorPos(5, 7); term.write("New Username: ")
  1343. sUserName = newRead(14)
  1344. term.setCursorPos(5, 7); term.write(string.rep(" ", screenWidth-8))
  1345.  
  1346. local file = fs.open("User/"..os.getComputerID().."/.info", "w")
  1347. file.write(sUserName.."n"..sPassword)
  1348. file.close()
  1349. elseif key == 28 and nSelect == 2 then
  1350. term.setCursorPos(5, 8); term.write(string.rep(" ", screenWidth-8))
  1351. term.setCursorPos(5, 8); term.write("Old Password: ")
  1352. local sCheckPassword = newRead(14, "*")
  1353.  
  1354. if sCheckPassword == sPassword then
  1355. term.setCursorPos(5, 8); term.write(string.rep(" ", screenWidth-8))
  1356. term.setCursorPos(5, 8); term.write("New Password: ")
  1357. sPassword = newRead(14, "*")
  1358.  
  1359. local file = fs.open("User/"..os.getComputerID().."/.info", "w")
  1360. file.write(sUserName.."n"..sPassword)
  1361. file.close()
  1362. end
  1363. term.setCursorPos(5, 8); term.write(string.rep(" ", screenWidth-8))
  1364. elseif key == 14 then nSelect = 3
  1365. end
  1366. end
  1367. end
  1368. -------------
  1369.  
  1370. function printError()
  1371. sMenuState = "perms"; clearCenter()
  1372. cPrint(6, "*********************")
  1373. cPrint(7, "* Restricted Access *")
  1374. cPrint(8, "*********************")
  1375. sleep(2)
  1376. sMenuState = "main"
  1377. end
  1378.  
  1379.  
  1380.  
  1381. clear()
  1382. getKey() -- b8v2a6b8i1e2j8g91
  1383. clear()
  1384. printInstallation()
  1385. clear()
  1386. checkUserInformation()
  1387. getUserInformation()
  1388. handleLogin()
  1389.  
  1390. if bLogin then
  1391. if checkVersion() then
  1392. sMenuState = "updateScreen"
  1393. clear()
  1394. printBorder()
  1395. rPrint(17, "Cannon OS Version: "..VERSION.."*")
  1396. cPrint(6, "********************")
  1397. cPrint(7, "* Update Available *")
  1398. cPrint(8, "********************")
  1399.  
  1400. local select = 1
  1401. while true do
  1402. if select == 1 then cPrint(9, "* [ Yes ] No *")
  1403. else cPrint(9, "* Yes [ No ] *")
  1404. end
  1405. cPrint(10, "********************")
  1406.  
  1407. local event, key = os.pullEvent("key")
  1408.  
  1409. if key == 205 and select == 1 then select = 2
  1410. elseif key == 203 and select == 2 then select = 1
  1411.  
  1412. elseif key == 28 and select == 1 then clear(); printBorder(); rPrint(17, "Cannon OS Version: "..VERSION.."*"); fs.delete("SYSTEM"); fs.delete("User"); term.setCursorPos(screenWidth/2-string.len("Updating...")/2, 9); textutils.slowPrint("Updating..."); updateSelf(); shell.run("startup")
  1413. elseif key == 28 and select == 2 then sMenuState = "main"; clearCenter(); break
  1414. end
  1415. end
  1416. end
  1417. end
  1418.  
  1419. -- Main OS Loop --
  1420. while true do
  1421. clear()
  1422. nMailCount = #(fs.list("Mail"))-1 -- -1 To eliminate the 'Contacts' directory from the list
  1423. printMainMenu()
  1424.  
  1425. event, key, p2 = os.pullEvent() -- Key will be p1, laziness has struck again
  1426.  
  1427. if event == "key" and key == 205 and selection < 6 and selection ~= 3 then selection = selection+1
  1428. elseif event == "key" and key == 203 and selection > 1 and selection ~= 4 then selection = selection-1
  1429. elseif event == "key" and key == 208 and selection < 6 and selection+3 <= 6 then selection = selection+3
  1430. elseif event == "key" and key == 200 and selection > 1 and selection-3 >= 1 then selection = selection-3
  1431.  
  1432. elseif event == "key" and key == 28 and selection == 6 then
  1433. clear(); printBorder(); rPrint(17, "Cannon OS Version: "..VERSION.."*"); term.setCursorPos(screenWidth/2-string.len("Updating...")/2, 9); textutils.slowPrint("Updating...")
  1434. if not updateSelf() then cPrint(9, "Update Failed"); sleep(1.3); shell.run("startup")
  1435. else fs.delete("SYSTEM"); fs.delete("User"); cPrint(9, "Update Successfull"); sleep(1.3); shell.run("startup")
  1436. end
  1437. elseif event == "key" and key == 28 and selection == 2 and bAdmin then clear(); shell.run("shell")-- If exit to Craft OS
  1438. elseif event == "key" and key == 28 and selection == 2 and not bAdmin then printError()
  1439.  
  1440. elseif event == "key" and key == 28 and selection == 4 and bAdmin then mailMenu()
  1441. elseif event == "key" and key == 28 and selection == 4 and not bAdmin then printError()
  1442.  
  1443. elseif event == "key" and key == 28 and selection == 1 and bAdmin then fileBrowser()
  1444. elseif event == "key" and key == 28 and selection == 1 and not bAdmin then printError()
  1445.  
  1446. elseif event == "key" and key == 28 and selection == 3 then clear(); IRC_Client()
  1447.  
  1448. elseif event == "key" and key == 28 and selection == 5 and bAdmin then editAccount()
  1449. elseif event == "key" and key == 28 and selection == 5 and not bAdmin then printError()
  1450.  
  1451. elseif event == "rednet_message" then
  1452. if string.find( p2, ";alkdjien") then
  1453. p2 = string.sub( p2, string.len(";alkdjien"), string.len( p2 ))
  1454. p2 = string.sub( p2, 3, string.len( p2 )) -- 3 to remove the n from the message
  1455. local file = fs.open("Mail/message"..#(fs.list("Mail"))+1, "w")
  1456. file.writeLine(key)
  1457. file.writeLine(p2)
  1458. file.close()
  1459.  
  1460. nMailCount = nMailCount+1
  1461. newMessage(key, p2) -- Key being the senderID and p2 being the message received
  1462. end
  1463. elseif event == "reboot" then selection = 1; sMenuState = "logout"; printLogout()
  1464. end
  1465. end
  1466. ------------------
  1467.  
  1468. os.pullEvent = oldpullEvent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement