Advertisement
jille_Jr

CC: JagChat

Oct 13th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.93 KB | None | 0 0
  1. local version =
  2. 1.7
  3.  
  4. function detectModem()
  5.   for _,side in ipairs(rs.getSides()) do
  6.     if peripheral.isPresent(side) then
  7.       if peripheral.getType(side) == "modem" then
  8.         if not rednet.isOpen(side) then
  9.           rednet.open(side)
  10.         end
  11.         return side
  12.       end
  13.     end
  14.   end
  15.   return false
  16. end
  17.  
  18. --[[
  19. Huge thanks goes to Cranium
  20. for showing me a updating
  21. technic!
  22. ]]--
  23.  
  24. local modem = detectModem()
  25. if modem == false then
  26.   if term.isColor then
  27.     if term.isColor() then
  28.       term.setTextColor(colors.red)
  29.       term.setBackgroundColor(colors.black)
  30.     end
  31.   end
  32.   print("No modem found!")
  33.   return
  34. else
  35.   rednet.open(modem)
  36. end
  37. local siteUrl = "http://Ninety7Seven.com/CC/prog/JagChat.lua"
  38. local width, height = term.getSize()
  39. local event,param1,param2,param3
  40. local written = ""
  41. local output = ""
  42. local scrolls = 2
  43. local site, siteFile, writeFile
  44. local newVersion
  45. local x,y
  46. local id, message = 0, ""
  47. local table1 = {}
  48. local table2 = {}
  49. local usersTable = {}
  50. local command = ""
  51. local commands = {
  52.   "logout",
  53.   "list",
  54.   "update",
  55.   "help",
  56.   "commands",
  57. }
  58. term.setCursorBlink(true)
  59.  
  60. function setColor(txtColor,bgColor)
  61.   if term.isColor then
  62.     if term.isColor() then
  63.       if txtColor then
  64.         term.setTextColor(txtColor)
  65.       end
  66.       if bgColor then
  67.         term.setBackgroundColor(bgColor)
  68.       end
  69.     end
  70.   end
  71. end
  72.  
  73. function printC(inputString,spaces)
  74.   if not spaces then
  75.     spaces = " "
  76.   else
  77.     spaces = tostring(spaces)
  78.   end
  79.   string_len = string.len(inputString)
  80.   num = math.floor(width/2 - string_len/2)
  81.   for j = 1,num do
  82.     output = output..spaces
  83.   end
  84.   output = output..tostring(inputString)
  85.   for j = 1,num do
  86.     output = output..spaces
  87.   end
  88.   setColor(colors.red,colors.gray)
  89.   term.clearLine()
  90.   write(string.sub(output,1,width-1))
  91.   output = ""
  92. end
  93.  
  94. function printN(name,inputString)
  95.   inputString = tostring(inputString)
  96.   setColor(colors.lightGray,colors.black)
  97.   term.clearLine()
  98.   if name then write(name) end
  99.   setColor(colors.yellow)
  100.   write(inputString.."\n")
  101.   term.clearLine()
  102.   if scrolls > height-2 then
  103.     term.scroll(1)
  104.   else
  105.     scrolls = scrolls + 1
  106.   end
  107. end
  108.  
  109. function listUsers()
  110.   term.setCursorBlink(false)
  111.   usersTable = {}
  112.   rednet.broadcast(textutils.serialize({chat={listAll=true}}))
  113.   repeat
  114.     id, message = 0,""
  115.     id, message = rednet.receive(.5)
  116.     if message == textutils.serialize({chat={listMe=true}}) then
  117.       table.insert(usersTable, id)
  118.     end
  119.   until id == nil
  120.   if #usersTable == 0 then
  121.     clearF()
  122.     term.setCursorPos(x,y)
  123.     printN(_,"No users active!")
  124.   else
  125.     clearF()
  126.     term.setCursorPos(x,y)
  127.     printN(_,"Users active: ")
  128.     for h = 1,#usersTable do
  129.       clearF()
  130.       term.setCursorPos(x,y)
  131.       printN(h..": ",usersTable[h])
  132.     end
  133.   end
  134.   term.setCursorBlink(true)
  135. end
  136.  
  137. function check4update()
  138.   term.setCursorBlink(false)
  139.   term.setCursorPos(x,y)
  140.   printN(_,"Searching for updates...")
  141.   clearF()
  142.   term.setCursorPos(x,y)
  143.   site = http.get(siteUrl)
  144.   if not site then
  145.     printN(_,"Failed! Try again later.")
  146.     term.setCursorBlink(true)
  147.     return false
  148.   end
  149.   site.readLine()
  150.   newVersion = site.readLine()
  151.   if tonumber(newVersion) > version then
  152.     printN(_,"Program is out of date, updating...")
  153.     clearF()
  154.     term.setCursorPos(x,y)
  155.     site = http.get(siteUrl)
  156.     if not site then
  157.       printN(_,"Failed! Try again later.")
  158.       term.setCursorBlink(true)
  159.       return false
  160.     end
  161.     siteFile = site.readAll()
  162.     writeFile = fs.open(shell.getRunningProgram(),"w")
  163.     writeFile.write(siteFile)
  164.     writeFile.close()
  165.     printN(_,"Update successful! Restarting program...")
  166.     clearF()
  167.     sleep(2)
  168.     rednet.broadcast(textutils.serialize({chat={login=false}}))
  169.     rednet.close("top")
  170.     shell.run(shell.getRunningProgram())
  171.     os.reboot()
  172.   else
  173.     printN(_,"Program is up to date!")
  174.   end
  175.   term.setCursorBlink(true)
  176. end
  177.  
  178. function commandList()
  179.   clearF()
  180.   term.setCursorPos(x,y)
  181.   printN(_,"Commands:")
  182.   for o = 1,#commands do
  183.     clearF()
  184.     term.setCursorPos(x,y)
  185.     printN(o..": ","/"..commands[o])
  186.   end
  187. end
  188.  
  189. function clearF()
  190.   term.setCursorPos(1,1)
  191.   printC(" JagChat v"..version.." ","-")
  192.   term.setCursorPos(1,scrolls)
  193.   x,y = term.getCursorPos()
  194.   term.setCursorPos(1,height-1)
  195.   term.clearLine()
  196.   printC(" Your id is #"..os.getComputerID().." ","-")
  197.   term.setCursorPos(1,height)
  198.   setColor(colors.orange,colors.gray)
  199.   term.clearLine()
  200.   write("> ")
  201.   setColor(colors.yellow)
  202.   write(written)
  203. end
  204.  
  205. rednet.broadcast(textutils.serialize({chat={login=true}}))
  206. setColor(_,colors.black)
  207. term.clear()
  208.  
  209. while true do
  210.   clearF()
  211.   event,param1,param2,param3 = os.pullEvent()
  212.   if event == "char" then
  213.     if string.len(written) < width-string.len(tostring(os.getComputerID()))-4 then
  214.       written = written .. param1
  215.     end
  216.   end
  217.   if event == "key" then
  218.     if param1 == 28 then
  219.       if string.len(written) ~= 0 then
  220.         if string.sub(written,0,1) == "/" then
  221.           --commands
  222.           command = string.sub(written,2)
  223.           if command == "logout" then
  224.             break
  225.           elseif command == "list" then
  226.             listUsers()
  227.           elseif command == "update" then
  228.             check4update()
  229.           elseif command == "help" or command == "commands" then
  230.             commandList()
  231.           else
  232.             term.setCursorPos(x,y)
  233.             printN(_,"Command \""..command.."\" does not exists!")
  234.           end
  235.         else
  236.           term.setCursorPos(x,y)
  237.           printN("<"..os.getComputerID().."> ",written)
  238.           table1 = {chat={isWritten=written}}
  239.           rednet.broadcast(textutils.serialize(table1))
  240.         end
  241.         written = ""
  242.       end
  243.     elseif param1 == 14 then
  244.       written = string.sub(written,1,-2)
  245.     end
  246.   elseif event == "rednet_message" then
  247.     term.setCursorPos(x,y)
  248.     table2 = textutils.unserialize(param2)
  249.     if type(table2) == "table" then
  250.       if table2.chat then
  251.         if table2.chat.isWritten then
  252.           if param1 ~= os.getComputerID() then
  253.             printN("<"..param1.."> ",table2.chat.isWritten)
  254.           end
  255.         elseif table2.chat.login == true or table2.chat.login == false then
  256.           if table2.chat.login == true then
  257.             printN("User #"..param1.." joined the chat!","")
  258.           elseif table2.chat.login == false then
  259.             printN("User #"..param1.." left the chat!","")
  260.           end
  261.         elseif table2.chat.listAll then
  262.           if table2.chat.listAll == true then
  263.             rednet.send(param1,textutils.serialize({chat={listMe=true}}))
  264.           end
  265.         end
  266.       end
  267.     end
  268.     table2 = {}
  269.   end
  270. end
  271.  
  272. rednet.broadcast(textutils.serialize({chat={login=false}}))
  273. rednet.close(modem)
  274. term.setCursorPos(1,height-1)
  275. printC(" Chat shut down ","-")
  276. term.setCursorPos(1,height)
  277. setColor(colors.white,colors.black)
  278. term.clearLine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement