Guest User

Untitled

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